Skip to content

Commit

Permalink
Enable auto merge for PRs when when the first merge attampt fails
Browse files Browse the repository at this point in the history
- This will ensure that we try to merge PRs for repositories which do not have the
auto merge feature available
  • Loading branch information
nid90 committed Jan 8, 2025
1 parent 0b694c9 commit 196ad82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
17 changes: 10 additions & 7 deletions app/libs/triggers/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ def create_and_merge!
pr_in_work.reload
return GitHub::Result.new { pr_in_work } if pr_in_work.closed?

# enable auto-merge if possible and avoid merging manually
if enable_auto_merge?
repo_integration.enable_auto_merge!(pr_in_work.number)
return GitHub::Result.new { pr_in_work }
end

# try and merge, when:
# - create PR is successful
# - or PR already exists and is _not_ already closed
merge!(pr_in_work).then { GitHub::Result.new { pr_in_work.close! } }
merge_result = merge!(pr_in_work)
if merge_result.ok?
pr_in_work.close!
elsif enable_auto_merge? # enable auto-merge if possible
repo_integration.enable_auto_merge!(pr_in_work.number)
else
return merge_result
end

GitHub::Result.new { pr_in_work }
end

private
Expand Down
28 changes: 21 additions & 7 deletions spec/libs/triggers/pull_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
let(:merge_payload) { JSON.parse(File.read("spec/fixtures/github/merge_pull_request.json")).with_indifferent_access }

it "creates a PR for the release" do
allow(repo_integration).to receive_messages(create_pr!: create_payload, enable_auto_merge: true)
allow(repo_integration).to receive_messages(create_pr!: create_payload, merge_pr!: merge_payload, enable_auto_merge: true)

result = described_class.create_and_merge!(
release: release,
Expand All @@ -44,7 +44,7 @@
end

it "creates a patch PR for the commit" do
allow(repo_integration).to receive_messages(cherry_pick_pr: create_payload, enable_auto_merge: true)
allow(repo_integration).to receive_messages(cherry_pick_pr: create_payload, merge_pr!: merge_payload, enable_auto_merge: true)
commit = create(:commit, release:)
patch_branch = "patch-main-#{commit.commit_hash}"

Expand All @@ -64,8 +64,25 @@
expect(commit.reload.pull_request).to be_present
end

it "does not merge the PR if auto merge is enabled" do
it "merges the PR" do
allow(repo_integration).to receive_messages(create_pr!: create_payload, merge_pr!: merge_payload, enable_auto_merge: true)

result = described_class.create_and_merge!(
release: release,
new_pull_request: release.pull_requests.post_release.open.build,
to_branch_ref: working_branch,
from_branch_ref: release_branch,
title: pr_title,
description: pr_description
)
expect(repo_integration).to have_received(:merge_pr!).with(repo_name, result.value!.number)
expect(result.ok?).to be(true)
expect(release.reload.pull_requests.size).to eq(1)
end

it "enables auto merge if merge fails" do

Check failure on line 83 in spec/libs/triggers/pull_request_spec.rb

View workflow job for this annotation

GitHub Actions / lint

RSpec/MultipleExpectations: Example has too many expectations [6/5].
allow(repo_integration).to receive_messages(cherry_pick_pr: create_payload, enable_auto_merge: true)
allow(repo_integration).to receive(:merge_pr!).and_raise(Installations::Error.new("Failed to merge the Pull Request", reason: :pull_request_not_mergeable))
commit = create(:commit, release:)
patch_branch = "patch-main-#{commit.commit_hash}"

Expand All @@ -83,6 +100,7 @@
created_pr = commit.reload.pull_request

expect(repo_integration).to have_received(:cherry_pick_pr).with(repo_name, working_branch, commit.commit_hash, patch_branch, pr_title, pr_description, GithubIntegration::PR_TRANSFORMATIONS)
expect(repo_integration).to have_received(:merge_pr!).with(repo_name, created_pr.number)
expect(repo_integration).to have_received(:enable_auto_merge).with(app.config.code_repo_namespace, app.config.code_repo_name_only, created_pr.number)
expect(result.ok?).to be(true)
expect(created_pr).to be_present
Expand All @@ -91,7 +109,6 @@

it "does not create PR if the PR does not have a diff to create" do
allow(repo_integration).to receive(:create_pr!).and_raise(no_diff_error)
allow(repo_integration).to receive(:enable_auto_merge)

result = described_class.create_and_merge!(
release: release,
Expand All @@ -105,14 +122,12 @@
namespaced_release_branch = "#{release.train.app.config.code_repo_namespace}:#{release_branch}"

expect(repo_integration).to have_received(:create_pr!).with(repo_name, working_branch, namespaced_release_branch, pr_title, pr_description, GithubIntegration::PR_TRANSFORMATIONS)
expect(repo_integration).not_to have_received(:enable_auto_merge)
expect(result.ok?).to be(true)
expect(release.reload.pull_requests.size).to eq(0)
end

it "returns an unsuccessful result if the PR does not have a diff to create and allow without diff is false" do
allow(repo_integration).to receive(:create_pr!).and_raise(no_diff_error)
allow(repo_integration).to receive(:enable_auto_merge)

result = described_class.create_and_merge!(
release: release,
Expand All @@ -126,7 +141,6 @@
namespaced_release_branch = "#{release.train.app.config.code_repo_namespace}:#{release_branch}"

expect(repo_integration).to have_received(:create_pr!).with(repo_name, working_branch, namespaced_release_branch, pr_title, pr_description, GithubIntegration::PR_TRANSFORMATIONS)
expect(repo_integration).not_to have_received(:enable_auto_merge)
expect(result.ok?).to be(false)
expect(release.reload.pull_requests.size).to eq(0)
end
Expand Down

0 comments on commit 196ad82

Please sign in to comment.