Skip to content

Commit

Permalink
Merge branch 'sc-fix-bubble-merge'
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Coffey committed Mar 21, 2023
2 parents 0b469e9 + 507ca4c commit 9a74f70
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 158 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ gem "octokit"
group :development do
gem "pry-byebug"
gem "rspec"
gem "hashie"
end
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GEM
diff-lcs (1.4.4)
faraday (1.0.1)
multipart-post (>= 1.2, < 3)
hashie (5.0.0)
method_source (1.0.0)
multipart-post (2.1.1)
octokit (4.18.0)
Expand Down Expand Up @@ -41,6 +42,7 @@ PLATFORMS
ruby

DEPENDENCIES
hashie
octokit
pry-byebug
rspec
Expand Down
85 changes: 44 additions & 41 deletions actions/bubble_merge/git_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Squiddy
class GitClient
attr_reader :client, :event, :pr, :repo
attr_reader :client, :event, :pr

CHECK_STATUSES = %w[queued in_progress completed].freeze
CHECK_CONCLUSIONS = %w[success failure neutral cancelled skipped timed_out action_required stale].freeze
Expand All @@ -16,25 +16,56 @@ def initialize
@client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
@event = git_event
@pr = pull_request
@repo = repository
end

def bubble_merge
if already_merged?
client.add_comment(repo_name, pr_number, 'This PR is already merged.')
else
merge_and_close_pr
wait_for_pr_to_match_branch do |merge_sha|
merge_and_close_pr(merge_sha)
end
end
end

private

def wait_for_pr_to_match_branch
retry_sleeps = [1, 2, 3, 5, 8]

loop do
expected = branch_head_sha
actual = pull_request_head_sha

if actual == expected
puts "PR HEAD #{actual[0..10]} matches #{branch} HEAD #{expected[0..10]}; merging..."
yield expected
return
elsif s = retry_sleeps.shift
puts "PR HEAD #{actual[0..10]} did not match #{branch} HEAD #{expected[0..10]}; waiting for #{s}s..."
sleep s
next
else
raise "PR HEAD #{actual[0..10]} did not match #{branch} HEAD #{expected[0..10]}; exiting"
end
end
end

def already_merged?
client.pull_merged?(repo_name, pr_number)
end

def merge_and_close_pr
merge
def merge_and_close_pr(merge_sha)
puts "PR ##{pr_number}: merging #{branch}@#{merge_sha} into #{base_branch}"
client.merge_pull_request(
repo_name,
pr_number,
commit_message,
{
merge_method: 'merge',
sha: merge_sha
}
)
delete_branch
rescue StandardError => e
message = <<~MESSAGE
Expand All @@ -46,59 +77,31 @@ def merge_and_close_pr
client.add_comment(repo_name, pr_number, message)
end

def merge
client.merge(
repo_name,
base_branch,
branch,
{
merge_method: 'rebase',
commit_message: commit_message,
sha: main_sha
}
)
end

def delete_branch
return if repo.delete_branch_on_merge
return if repository.delete_branch_on_merge

puts "PR ##{pr_number}: deleting #{branch}"
client.delete_branch(repo_name, branch)
end

def main_sha
client.list_commits(repo_name).last.sha
end

def branch
pr[:head][:ref]
pr.head.ref
end

def base_branch
pr[:base][:ref]
pr.base.ref
end

def pr_number
event.dig('issue', 'number')
end

def pr_status
checks = head_commit_checks[:check_suites]

if checks.any? { |check| check[:status] != 'completed' }
'pending'
elsif checks.any? { |check| check[:conclusion] != 'success' }
'failure'
else
'success'
end
end

def head_commit_checks
client.check_suites_for_ref(repo_name, head_commit_sha)
def branch_head_sha
client.branch(repo_name, branch).commit.sha
end

def head_commit_sha
client.pull_request_commits(repo_name, pr_number).last.sha
def pull_request_head_sha
client.pull_request(repo_name, pr_number).head.sha
end

def comment_author
Expand Down
91 changes: 0 additions & 91 deletions lib/squiddy.rb

This file was deleted.

Loading

0 comments on commit 9a74f70

Please sign in to comment.