Commit df1cdefe authored by Douwe Maan's avatar Douwe Maan

Merge branch 'improve-merge-when-succeeded' into 'master'

Check the HEAD of branch when doing Merge When Succeeded

Implements https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/2894#note_3845158

cc @DouweM @grzesiek 


See merge request !2907
parents 8c4cfb1f 7c288c07
...@@ -24,12 +24,16 @@ module MergeRequests ...@@ -24,12 +24,16 @@ module MergeRequests
merge_requests.each do |merge_request| merge_requests.each do |merge_request|
next unless merge_request.merge_when_build_succeeds? next unless merge_request.merge_when_build_succeeds?
next unless merge_request.mergeable?
ci_commit = merge_request.ci_commit
next unless ci_commit
next unless ci_commit.sha == commit_status.sha
next unless ci_commit.success?
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) MergeWorker.perform_async(merge_request.id, merge_request.merge_user_id, merge_request.merge_params)
end end
end end
end
# Cancels the automatic merge # Cancels the automatic merge
def cancel(merge_request) def cancel(merge_request)
...@@ -51,6 +55,8 @@ module MergeRequests ...@@ -51,6 +55,8 @@ module MergeRequests
# This is for ref-less builds # This is for ref-less builds
branches ||= @project.repository.branch_names_contains(commit_status.sha) 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.origin_merge_requests.opened.where(source_branch: branches).to_a
merge_requests += @project.fork_merge_requests.opened.where(source_branch: branches).to_a merge_requests += @project.fork_merge_requests.opened.where(source_branch: branches).to_a
......
...@@ -66,9 +66,25 @@ describe MergeRequests::MergeWhenBuildSucceedsService do ...@@ -66,9 +66,25 @@ describe MergeRequests::MergeWhenBuildSucceedsService do
end end
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 context 'commit status without ref' do
let(:commit_status) { create(:generic_commit_status, status: 'success') } 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 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([]) allow(project.repository).to receive(:branch_names_contains).with(commit_status.sha).and_return([])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment