lib/tutter/action/sppuppet.rb in tutter-sppuppet-1.1.5 vs lib/tutter/action/sppuppet.rb in tutter-sppuppet-1.2.0
- old
+ new
@@ -1,14 +1,19 @@
require 'fileutils'
require 'json'
class Sppuppet
+ # Match regexps
+ MERGE_COMMENT = /(:shipit:|:ship:|!merge)/
+ PLUS_VOTE = /(:+1:|^\+1|^LGTM)/
+ MINUS_VOTE = /(:-1:|^-1)/
+ BLOCK_VOTE = /^(:poop:|:hankey:|-2)/ # Blocks merge
+ INCIDENT = /jira.*INCIDENT/
def initialize(settings, client, project, data, event)
@settings = settings
@settings['plus_ones_required'] ||= 1
- @settings['reports_dir'] ||= '/var/lib/tutter/reports'
@client = client
@project = project
@data = data
@event = event
end
@@ -21,16 +26,15 @@
return 200, 'not a new comment, skipping'
end
pull_request_id = @data['issue']['number']
- merge_command = (@data['comment']['body'] == '!merge' ||
- @data['comment']['body'].start_with?(':shipit:'))
+ merge_command = MERGE_COMMENT.match @data['comment']['body']
return 200, 'Not a merge comment' unless merge_command
- return maybe_merge(pull_request_id, true)
+ return maybe_merge(pull_request_id, true, @data['sender']['login'])
when 'status'
return 200, 'Merge state not clean' unless @data['state'] == 'success'
commit_sha = @data['commit']['sha']
@client.pull_requests(@project).each do |pr|
@@ -50,13 +54,12 @@
else
return 200, "Unhandled event type #{@event}"
end
end
- def maybe_merge(pull_request_id, merge_command)
+ def maybe_merge(pull_request_id, merge_command, merger = nil)
votes = {}
- merger = nil
incident_merge_override = false
pr = @client.pull_request @project, pull_request_id
# We fetch the latest commit and it's date.
last_commit = @client.pull_request_commits(@project, pull_request_id).last
@@ -68,34 +71,32 @@
comments.each do |i|
# Comment is older than last commit.
# We only want to check newer comments
next if last_commit_date > i.created_at
- if i.body == '!merge' || i.body.start_with?(':shipit:') || i.body.start_with?(':ship:')
+ if MERGE_COMMENT.match i.body
merger ||= i.attrs[:user].attrs[:login]
# Count as a +1 if it is not the author
unless pr.user.login == i.attrs[:user].attrs[:login]
votes[i.attrs[:user].attrs[:login]] = 1
end
end
- match = /^(:?([+-])1:?|LGTM)/.match(i.body)
- if match
- score = match[2] == '-' ? -1 : 1
- # pull request submitter cant +1
- unless pr.user.login == i.attrs[:user].attrs[:login]
- votes[i.attrs[:user].attrs[:login]] = score
- end
+ if PLUS_VOTE.match i.body && pr.user.login != i.attrs[:user].attrs[:login]
+ votes[i.attrs[:user].attrs[:login]] = 1
end
- match = /^(:poop:|:hankey:|-2)/.match(i.body)
- if match
- msg = "Commit cannot be merged so long as a -2 comment appears in the PR."
+ if MINUS_VOTE.match i.body && pr.user.login != i.attrs[:user].attrs[:login]
+ votes[i.attrs[:user].attrs[:login]] = -1
+ end
+
+ if BLOCK_VOTE.match i.body
+ msg = 'Commit cannot be merged so long as a -2 comment appears in the PR.'
return post_comment(pull_request_id, msg)
end
- if /jira.*INCIDENT/.match(i.body)
+ if INCIDENT.match(i.body)
incident_merge_override = true
end
end
if pr.mergeable_state != 'clean' && !incident_merge_override
@@ -116,19 +117,10 @@
if num_votes < @settings['plus_ones_required'] && !incident_merge_override
msg = "Not enough plus ones. #{@settings['plus_ones_required']} required, and only have #{num_votes}"
return post_comment(pull_request_id, msg)
end
- json = { url: pr.url,
- title: pr.title,
- opened_by: pr.user.login,
- description: pr.body,
- commits: @client.pull_request_commits(@project, pr.number).map { |c| { author: c.author, message: c.commit.message, sha: c.commit.tree.sha } },
- head_sha: pr.head.sha,
- tests: @client.combined_status(@project, pr.head.sha).statuses.map { |s| {state: s.state, url: s.target_url, description: s.description } },
- reviewers: votes.keys,
- deployer: merger }
# TODO: Word wrap description
merge_msg = <<MERGE_MSG
Title: #{pr.title}
Opened by: #{pr.user.login}
Reviewers: #{votes.keys.join ', '}
@@ -143,17 +135,9 @@
end
begin
merge_commit = @client.merge_pull_request(@project, pull_request_id, merge_msg)
rescue Octokit::MethodNotAllowed => e
return post_comment(pull_request_id, "Pull request not mergeable: #{e.message}")
- end
- puts merge_commit.inspect
- json[:merge_sha] = merge_commit.sha
- report_directory = "#{@settings['reports_dir']}/#{merge_commit.sha[0..1]}/#{merge_commit.sha[2..3]}"
- report_path = "#{report_directory}/#{merge_commit.sha}.json"
- if @settings['generate_reports']
- FileUtils.mkdir_p report_directory
- File.open(report_path, 'w') { |f| f.write(JSON.pretty_generate(json)) }
end
return 200, "merging #{pull_request_id} #{@project}"
end
def post_comment(issue, comment)