Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,45 @@ require 'pry'
require_relative 'github_wrapper'
require_relative 'slack_wrapper'

STDOUT.sync = true

class App < Rack::App
REVIEWERS_COUNT = ENV.key?('REVIEWERS_COUNT') ? ENV['REVIEWERS_COUNT'].to_i : 2

apply_extensions :logger

desc 'assign_reviewers'
post '/assign_reviewers' do
params = JSON.parse(payload)
pull = params['pull_request']

if params["action"] == 'labeled' && params['label']['name'] == 'Code Review'
requested_reviewers_count = REVIEWERS_COUNT - pull['requested_reviewers'].size
action = params['action']
labeled = (action == 'labeled')
logger.info("action: #{action.inspect}")
return unless labeled

return if requested_reviewers_count <= 0
label_name = params['label']['name']
logger.info("label: #{label_name.inspect}")
return if label_name != 'Code Review'

creator = pull.dig(*%w(user login))
requested_reviewers_count = REVIEWERS_COUNT - pull['requested_reviewers'].size

logins = GithubWrapper.team_members.map { |m| m['login'] }
logins.delete(creator)
reviewers = logins.sample(requested_reviewers_count)
return if requested_reviewers_count <= 0

GithubWrapper.request_review(pull['url'], reviewers)
creator = pull.dig(*%w(user login))

pull_url = pull['url'].gsub(/(api.|repos\/)/, '').gsub('pulls', 'issues')
SlackWrapper.notify_reviewers(pull_url, reviewers)
logins = GithubWrapper.team_members.map { |m| m['login'] }
logins.delete(creator)
reviewers = logins.sample(requested_reviewers_count)

github_response = GithubWrapper.request_review(pull['url'], reviewers)
unless github_response.success?
response.status = github_response.code
return github_response.body
end

pull_url = pull['url'].gsub(/(api.|repos\/)/, '').gsub('pulls', 'issues')
SlackWrapper.notify_reviewers(pull_url, reviewers)
end

end
Expand Down
2 changes: 1 addition & 1 deletion github_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def request_review(url, requested_reviewers)
}
)

request.success?
request
end

def team_members
Expand Down