diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-22 10:33:34 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-22 10:34:09 +0100 |
commit | 7c288c07f12bad7047cc2c7c0d4eee8c1c8f2733 (patch) | |
tree | 514a6f831bfb1b060785c965397685affc497eab | |
parent | 04d1b412587028260ac219a32bdf3b03ac022308 (diff) | |
download | gitlab-ce-improve-merge-when-succeeded.tar.gz |
Check the HEAD of branch when doing Merge When Succeededimprove-merge-when-succeeded
-rw-r--r-- | app/services/merge_requests/merge_when_build_succeeds_service.rb | 12 | ||||
-rw-r--r-- | spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb | 16 |
2 files changed, 25 insertions, 3 deletions
diff --git a/app/services/merge_requests/merge_when_build_succeeds_service.rb b/app/services/merge_requests/merge_when_build_succeeds_service.rb index 531bbc9b067..d6af12f9739 100644 --- a/app/services/merge_requests/merge_when_build_succeeds_service.rb +++ b/app/services/merge_requests/merge_when_build_succeeds_service.rb @@ -24,10 +24,14 @@ module MergeRequests merge_requests.each do |merge_request| next unless merge_request.merge_when_build_succeeds? + next unless merge_request.mergeable? - if merge_request.ci_commit && merge_request.ci_commit.success? && merge_request.mergeable? - MergeWorker.perform_async(merge_request.id, merge_request.merge_user_id, merge_request.merge_params) - end + ci_commit = merge_request.ci_commit + next unless ci_commit + next unless ci_commit.sha == commit_status.sha + next unless ci_commit.success? + + MergeWorker.perform_async(merge_request.id, merge_request.merge_user_id, merge_request.merge_params) end end @@ -51,6 +55,8 @@ module MergeRequests # This is for ref-less builds branches ||= @project.repository.branch_names_contains(commit_status.sha) + return [] if branches.blank? + merge_requests = @project.origin_merge_requests.opened.where(source_branch: branches).to_a merge_requests += @project.fork_merge_requests.opened.where(source_branch: branches).to_a diff --git a/spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb b/spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb index f285517cdac..52a302e0e1a 100644 --- a/spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb +++ b/spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb @@ -66,9 +66,25 @@ describe MergeRequests::MergeWhenBuildSucceedsService do end end + context 'triggered by an old build' do + let(:old_build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch, status: "success") } + let(:build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch, status: "success") } + + it "merges all merge requests with merge when build succeeds enabled" do + allow_any_instance_of(MergeRequest).to receive(:ci_commit).and_return(ci_commit) + allow(ci_commit).to receive(:success?).and_return(true) + allow(old_build).to receive(:sha).and_return('1234abcdef') + + expect(MergeWorker).to_not receive(:perform_async) + service.trigger(old_build) + end + end + context 'commit status without ref' do let(:commit_status) { create(:generic_commit_status, status: 'success') } + before { mr_merge_if_green_enabled } + it "doesn't merge a requests for status on other branch" do allow(project.repository).to receive(:branch_names_contains).with(commit_status.sha).and_return([]) |