diff options
-rw-r--r-- | app/services/merge_requests/base_service.rb | 20 | ||||
-rw-r--r-- | spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb | 13 |
2 files changed, 11 insertions, 22 deletions
diff --git a/app/services/merge_requests/base_service.rb b/app/services/merge_requests/base_service.rb index 4f4c1c77ec3..58f69a41e14 100644 --- a/app/services/merge_requests/base_service.rb +++ b/app/services/merge_requests/base_service.rb @@ -42,20 +42,19 @@ module MergeRequests super(:merge_request) end - def merge_requests_for(branches, sha) - # This is for ref-less builds - branches ||= @project.repository.branch_names_contains(sha) + def merge_requests_for(branch) + origin_merge_requests = @project.origin_merge_requests + .opened.where(source_branch: branch).to_a - return [] if branches.blank? + fork_merge_requests = @project.fork_merge_requests + .opened.where(source_branch: branch).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.uniq.select(&:source_project) + (origin_merge_requests + fork_merge_requests) + .uniq.select(&:source_project) end def pipeline_merge_requests(pipeline) - merge_requests_for(pipeline.ref, pipeline.sha).each do |merge_request| + merge_requests_for(pipeline.ref).each do |merge_request| next unless pipeline == merge_request.pipeline yield merge_request @@ -63,8 +62,9 @@ module MergeRequests end 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).each do |merge_request| pipeline = merge_request.pipeline + next unless pipeline next unless pipeline.sha == commit_status.sha 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 df8f8b61df5..03345ed9c15 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 @@ -97,20 +97,9 @@ describe MergeRequests::MergeWhenBuildSucceedsService do expect(MergeWorker).not_to receive(:perform_async) service.trigger(unrelated_pipeline) end - - it 'discovers branches and merges all merge requests when status is success' do - allow(project.repository).to receive(:branch_names_contains). - with(commit_status.sha).and_return([mr_merge_if_green_enabled.source_branch]) - allow(pipeline).to receive(:success?).and_return(true) - allow_any_instance_of(MergeRequest).to receive(:pipeline).and_return(pipeline) - allow(pipeline).to receive(:success?).and_return(true) - - expect(MergeWorker).to receive(:perform_async) - service.trigger(commit_status) - end end - context 'properly handles multiple stages' do + context 'when there are multiple stages in the pipeline' do let(:ref) { mr_merge_if_green_enabled.source_branch } let!(:build) { create(:ci_build, :created, pipeline: pipeline, ref: ref, name: 'build', stage: 'build') } let!(:test) { create(:ci_build, :created, pipeline: pipeline, ref: ref, name: 'test', stage: 'test') } |