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