diff options
Diffstat (limited to 'spec/finders/ci/pipelines_for_merge_request_finder_spec.rb')
-rw-r--r-- | spec/finders/ci/pipelines_for_merge_request_finder_spec.rb | 176 |
1 files changed, 95 insertions, 81 deletions
diff --git a/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb b/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb index 65f6dc0ba74..8a802e9660b 100644 --- a/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb +++ b/spec/finders/ci/pipelines_for_merge_request_finder_spec.rb @@ -44,7 +44,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do let(:actor) { developer_in_both } it 'returns all pipelines' do - is_expected.to eq([pipeline_in_fork, pipeline_in_parent]) + is_expected.to match_array([pipeline_in_fork, pipeline_in_parent]) end end @@ -52,7 +52,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do let(:actor) { reporter_in_parent_and_developer_in_fork } it 'returns all pipelines' do - is_expected.to eq([pipeline_in_fork, pipeline_in_parent]) + is_expected.to match_array([pipeline_in_fork, pipeline_in_parent]) end end @@ -60,7 +60,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do let(:actor) { developer_in_parent } it 'returns pipelines in parent' do - is_expected.to eq([pipeline_in_parent]) + is_expected.to match_array([pipeline_in_parent]) end end @@ -68,7 +68,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do let(:actor) { developer_in_fork } it 'returns pipelines in fork' do - is_expected.to eq([pipeline_in_fork]) + is_expected.to match_array([pipeline_in_fork]) end end @@ -97,7 +97,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do shared_examples 'returning pipelines with proper ordering' do let!(:all_pipelines) do - merge_request.all_commit_shas.map do |sha| + merge_request.recent_diff_head_shas.map do |sha| create(:ci_empty_pipeline, project: project, sha: sha, ref: merge_request.source_branch) end @@ -135,12 +135,92 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do end context 'when pipelines exist for the branch and merge request' do + shared_examples 'returns all pipelines for merge request' do + it 'returns merge request pipeline first' do + expect(subject.all).to eq([detached_merge_request_pipeline, branch_pipeline]) + end + + context 'when there are a branch pipeline and a merge request pipeline' do + let!(:branch_pipeline_2) do + create(:ci_pipeline, source: :push, project: project, + ref: source_ref, sha: shas.first) + end + + let!(:detached_merge_request_pipeline_2) do + create(:ci_pipeline, source: :merge_request_event, project: project, + ref: source_ref, sha: shas.first, merge_request: merge_request) + end + + it 'returns merge request pipelines first' do + expect(subject.all) + .to eq([detached_merge_request_pipeline_2, + detached_merge_request_pipeline, + branch_pipeline_2, + branch_pipeline]) + end + end + + context 'when there are multiple merge request pipelines from the same branch' do + let!(:branch_pipeline_2) do + create(:ci_pipeline, source: :push, project: project, + ref: source_ref, sha: shas.first) + end + + let!(:branch_pipeline_with_sha_not_belonging_to_merge_request) do + create(:ci_pipeline, source: :push, project: project, ref: source_ref) + end + + let!(:detached_merge_request_pipeline_2) do + create(:ci_pipeline, source: :merge_request_event, project: project, + ref: source_ref, sha: shas.first, merge_request: merge_request_2) + end + + let(:merge_request_2) do + create(:merge_request, source_project: project, source_branch: source_ref, + target_project: project, target_branch: 'stable') + end + + before do + shas.each.with_index do |sha, index| + create(:merge_request_diff_commit, + merge_request_diff: merge_request_2.merge_request_diff, + sha: sha, relative_order: index) + end + end + + it 'returns only related merge request pipelines' do + expect(subject.all) + .to eq([detached_merge_request_pipeline, + branch_pipeline_2, + branch_pipeline]) + + expect(described_class.new(merge_request_2, nil).all) + .to match_array([detached_merge_request_pipeline_2, branch_pipeline_2, branch_pipeline]) + end + end + + context 'when detached merge request pipeline is run on head ref of the merge request' do + let!(:detached_merge_request_pipeline) do + create(:ci_pipeline, source: :merge_request_event, project: project, + ref: merge_request.ref_path, sha: shas.second, merge_request: merge_request) + end + + it 'sets the head ref of the merge request to the pipeline ref' do + expect(detached_merge_request_pipeline.ref).to match(%r{refs/merge-requests/\d+/head}) + end + + it 'includes the detached merge request pipeline even though the ref is custom path' do + expect(merge_request.all_pipelines).to include(detached_merge_request_pipeline) + end + end + end + let(:source_ref) { 'feature' } let(:target_ref) { 'master' } let!(:branch_pipeline) do create(:ci_pipeline, source: :push, project: project, - ref: source_ref, sha: shas.second) + ref: source_ref, sha: merge_request.merge_request_diff.head_commit_sha) end let!(:tag_pipeline) do @@ -149,97 +229,31 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do let!(:detached_merge_request_pipeline) do create(:ci_pipeline, source: :merge_request_event, project: project, - ref: source_ref, sha: shas.second, merge_request: merge_request) + ref: source_ref, sha: shas.second, merge_request: merge_request) end let(:merge_request) do create(:merge_request, source_project: project, source_branch: source_ref, - target_project: project, target_branch: target_ref) + target_project: project, target_branch: target_ref) end let(:project) { create(:project, :repository) } let(:shas) { project.repository.commits(source_ref, limit: 2).map(&:id) } - before do - create(:merge_request_diff_commit, - merge_request_diff: merge_request.merge_request_diff, - sha: shas.second, relative_order: 1) - end - - it 'returns merge request pipeline first' do - expect(subject.all).to eq([detached_merge_request_pipeline, branch_pipeline]) - end - - context 'when there are a branch pipeline and a merge request pipeline' do - let!(:branch_pipeline_2) do - create(:ci_pipeline, source: :push, project: project, - ref: source_ref, sha: shas.first) - end - - let!(:detached_merge_request_pipeline_2) do - create(:ci_pipeline, source: :merge_request_event, project: project, - ref: source_ref, sha: shas.first, merge_request: merge_request) - end - - it 'returns merge request pipelines first' do - expect(subject.all) - .to eq([detached_merge_request_pipeline_2, - detached_merge_request_pipeline, - branch_pipeline_2, - branch_pipeline]) - end - end - - context 'when there are multiple merge request pipelines from the same branch' do - let!(:branch_pipeline_2) do - create(:ci_pipeline, source: :push, project: project, - ref: source_ref, sha: shas.first) - end - - let!(:detached_merge_request_pipeline_2) do - create(:ci_pipeline, source: :merge_request_event, project: project, - ref: source_ref, sha: shas.first, merge_request: merge_request_2) - end - - let(:merge_request_2) do - create(:merge_request, source_project: project, source_branch: source_ref, - target_project: project, target_branch: 'stable') - end - + context 'when `decomposed_ci_query_in_pipelines_for_merge_request_finder` feature flag enabled' do before do - shas.each.with_index do |sha, index| - create(:merge_request_diff_commit, - merge_request_diff: merge_request_2.merge_request_diff, - sha: sha, relative_order: index) - end + stub_feature_flags(decomposed_ci_query_in_pipelines_for_merge_request_finder: merge_request.target_project) end - it 'returns only related merge request pipelines' do - expect(subject.all) - .to eq([detached_merge_request_pipeline, - branch_pipeline_2, - branch_pipeline]) - - expect(described_class.new(merge_request_2, nil).all) - .to eq([detached_merge_request_pipeline_2, - branch_pipeline_2, - branch_pipeline]) - end + it_behaves_like 'returns all pipelines for merge request' end - context 'when detached merge request pipeline is run on head ref of the merge request' do - let!(:detached_merge_request_pipeline) do - create(:ci_pipeline, source: :merge_request_event, project: project, - ref: merge_request.ref_path, sha: shas.second, merge_request: merge_request) - end - - it 'sets the head ref of the merge request to the pipeline ref' do - expect(detached_merge_request_pipeline.ref).to match(%r{refs/merge-requests/\d+/head}) + context 'when `decomposed_ci_query_in_pipelines_for_merge_request_finder` feature flag disabled' do + before do + stub_feature_flags(decomposed_ci_query_in_pipelines_for_merge_request_finder: false) end - it 'includes the detached merge request pipeline even though the ref is custom path' do - expect(merge_request.all_pipelines).to include(detached_merge_request_pipeline) - end + it_behaves_like 'returns all pipelines for merge request' end end end |