Commit 9824a69c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Fix tests and code for MWBS event move to pipeline

parent 21724407
...@@ -56,12 +56,18 @@ module MergeRequests ...@@ -56,12 +56,18 @@ module MergeRequests
def pipeline_merge_requests(pipeline) def pipeline_merge_requests(pipeline)
merge_requests_for(pipeline.ref, pipeline.sha).each do |merge_request| merge_requests_for(pipeline.ref, pipeline.sha).each do |merge_request|
next unless pipeline == merge_request.pipeline
yield merge_request yield merge_request
end end
end end
def commit_status_merge_requests(commit_status) def commit_status_merge_requests(commit_status)
merge_requests_for(commit_status.ref, commit_status.sha).each do |merge_request| merge_requests_for(commit_status.ref, commit_status.sha).each do |merge_request|
pipeline = merge_request.pipeline
next unless pipeline
next unless pipeline.sha == commit_status.sha
yield merge_request yield merge_request
end end
end end
......
...@@ -58,42 +58,44 @@ describe MergeRequests::MergeWhenBuildSucceedsService do ...@@ -58,42 +58,44 @@ describe MergeRequests::MergeWhenBuildSucceedsService do
end end
describe "#trigger" do describe "#trigger" do
context 'build with ref' do let(:merge_request_ref) { mr_merge_if_green_enabled.source_branch }
let(:build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch, status: "success") } let(:merge_request_head) do
project.commit(mr_merge_if_green_enabled.source_branch).id
end
it "merges all merge requests with merge when build succeeds enabled" do context 'when triggered by pipeline with valid ref and sha' do
allow_any_instance_of(MergeRequest).to receive(:pipeline).and_return(pipeline) let(:triggering_pipeline) do
allow(pipeline).to receive(:success?).and_return(true) create(:ci_pipeline, project: project, ref: merge_request_ref,
sha: merge_request_head, status: 'success')
end
it "merges all merge requests with merge when build succeeds enabled" do
expect(MergeWorker).to receive(:perform_async) expect(MergeWorker).to receive(:perform_async)
service.trigger(build) service.trigger(triggering_pipeline)
end end
end end
context 'triggered by an old build' do context 'when triggered by an old pipeline' do
let(:old_build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch, status: "success") } let(:old_pipeline) do
let(:build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch, status: "success") } create(:ci_pipeline, project: project, ref: merge_request_ref,
sha: '1234abcdef', status: 'success')
it "merges all merge requests with merge when build succeeds enabled" do end
allow_any_instance_of(MergeRequest).to receive(:pipeline).and_return(pipeline)
allow(pipeline).to receive(:success?).and_return(true)
allow(old_build).to receive(:sha).and_return('1234abcdef')
it 'it does not merge merge request' do
expect(MergeWorker).not_to receive(:perform_async) expect(MergeWorker).not_to receive(:perform_async)
service.trigger(old_build) service.trigger(old_pipeline)
end end
end end
context 'commit status without ref' do context 'when triggered by pipeline from a different branch' do
let(:commit_status) { create(:generic_commit_status, status: 'success') } let(:unrelated_pipeline) do
create(:ci_pipeline, project: project, ref: 'feature',
before { mr_merge_if_green_enabled } sha: merge_request_head, status: 'success')
end
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([])
it 'does not merge request' do
expect(MergeWorker).not_to receive(:perform_async) expect(MergeWorker).not_to receive(:perform_async)
service.trigger(commit_status) service.trigger(unrelated_pipeline)
end end
it 'discovers branches and merges all merge requests when status is success' do it 'discovers branches and merges all merge requests when status is success' do
......
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