Commit 6f4e9770 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'issue_32225' into 'master'

Add transient head_pipeline_of to pipeline factories

See merge request !11566
parents 2d67845d 921c2be4
...@@ -556,8 +556,14 @@ class Spinach::Features::ProjectMergeRequests < Spinach::FeatureSteps ...@@ -556,8 +556,14 @@ class Spinach::Features::ProjectMergeRequests < Spinach::FeatureSteps
step '"Bug NS-05" has CI status' do step '"Bug NS-05" has CI status' do
project = merge_request.source_project project = merge_request.source_project
project.enable_ci project.enable_ci
pipeline = create :ci_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch
merge_request.update(head_pipeline: pipeline) pipeline =
create(:ci_pipeline,
project: project,
sha: merge_request.diff_head_sha,
ref: merge_request.source_branch,
head_pipeline_of: merge_request)
create :ci_build, pipeline: pipeline create :ci_build, pipeline: pipeline
end end
......
...@@ -357,8 +357,7 @@ describe Projects::MergeRequestsController do ...@@ -357,8 +357,7 @@ describe Projects::MergeRequestsController do
end end
before do before do
pipeline = create(:ci_empty_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch) create(:ci_empty_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch, head_pipeline_of: merge_request)
merge_request.update(head_pipeline: pipeline)
end end
it 'returns :merge_when_pipeline_succeeds' do it 'returns :merge_when_pipeline_succeeds' do
...@@ -1173,13 +1172,13 @@ describe Projects::MergeRequestsController do ...@@ -1173,13 +1172,13 @@ describe Projects::MergeRequestsController do
let!(:pipeline) do let!(:pipeline) do
create(:ci_pipeline, project: merge_request.source_project, create(:ci_pipeline, project: merge_request.source_project,
ref: merge_request.source_branch, ref: merge_request.source_branch,
sha: merge_request.diff_head_sha) sha: merge_request.diff_head_sha,
head_pipeline_of: merge_request)
end end
let(:status) { pipeline.detailed_status(double('user')) } let(:status) { pipeline.detailed_status(double('user')) }
before do before do
merge_request.update(head_pipeline: pipeline)
get_pipeline_status get_pipeline_status
end end
......
...@@ -20,6 +20,15 @@ FactoryGirl.define do ...@@ -20,6 +20,15 @@ FactoryGirl.define do
end end
end end
# Persist merge request head_pipeline_id
# on pipeline factories to avoid circular references
transient { head_pipeline_of nil }
after(:create) do |pipeline, evaluator|
merge_request = evaluator.head_pipeline_of
merge_request&.update(head_pipeline: pipeline)
end
factory :ci_pipeline do factory :ci_pipeline do
transient { config nil } transient { config nil }
......
...@@ -7,7 +7,7 @@ feature 'Cycle Analytics', feature: true, js: true do ...@@ -7,7 +7,7 @@ feature 'Cycle Analytics', feature: true, js: true do
let(:issue) { create(:issue, project: project, created_at: 2.days.ago) } let(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
let(:milestone) { create(:milestone, project: project) } let(:milestone) { create(:milestone, project: project) }
let(:mr) { create_merge_request_closing_issue(issue, commit_message: "References #{issue.to_reference}") } let(:mr) { create_merge_request_closing_issue(issue, commit_message: "References #{issue.to_reference}") }
let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha) } let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha, head_pipeline_of: mr) }
context 'as an allowed user' do context 'as an allowed user' do
context 'when project is new' do context 'when project is new' do
...@@ -33,7 +33,6 @@ feature 'Cycle Analytics', feature: true, js: true do ...@@ -33,7 +33,6 @@ feature 'Cycle Analytics', feature: true, js: true do
context "when there's cycle analytics data" do context "when there's cycle analytics data" do
before do before do
allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue]) allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
mr.update(head_pipeline: pipeline)
project.add_master(user) project.add_master(user)
create_cycle create_cycle
......
...@@ -13,12 +13,12 @@ feature 'Merge When Pipeline Succeeds', :feature, :js do ...@@ -13,12 +13,12 @@ feature 'Merge When Pipeline Succeeds', :feature, :js do
let(:pipeline) do let(:pipeline) do
create(:ci_pipeline, project: project, create(:ci_pipeline, project: project,
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
ref: merge_request.source_branch) ref: merge_request.source_branch,
head_pipeline_of: merge_request)
end end
before do before do
project.add_master(user) project.add_master(user)
merge_request.update(head_pipeline_id: pipeline.id)
end end
context 'when there is active pipeline for merge request' do context 'when there is active pipeline for merge request' do
......
...@@ -28,11 +28,9 @@ feature 'Only allow merge requests to be merged if the pipeline succeeds', featu ...@@ -28,11 +28,9 @@ feature 'Only allow merge requests to be merged if the pipeline succeeds', featu
project: project, project: project,
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
ref: merge_request.source_branch, ref: merge_request.source_branch,
status: status) status: status, head_pipeline_of: merge_request)
end end
before { merge_request.update(head_pipeline: pipeline) }
context 'when merge requests can only be merged if the pipeline succeeds' do context 'when merge requests can only be merged if the pipeline succeeds' do
before do before do
project.update_attribute(:only_allow_merge_if_pipeline_succeeds, true) project.update_attribute(:only_allow_merge_if_pipeline_succeeds, true)
......
...@@ -88,11 +88,10 @@ describe 'Merge request', :feature, :js do ...@@ -88,11 +88,10 @@ describe 'Merge request', :feature, :js do
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
ref: merge_request.source_branch, ref: merge_request.source_branch,
status: 'failed', status: 'failed',
statuses: [commit_status]) statuses: [commit_status],
head_pipeline_of: merge_request)
create(:ci_build, :pending, pipeline: pipeline) create(:ci_build, :pending, pipeline: pipeline)
merge_request.update(head_pipeline: pipeline)
visit namespace_project_merge_request_path(project.namespace, project, merge_request) visit namespace_project_merge_request_path(project.namespace, project, merge_request)
end end
...@@ -105,15 +104,13 @@ describe 'Merge request', :feature, :js do ...@@ -105,15 +104,13 @@ describe 'Merge request', :feature, :js do
context 'when merge request is in the blocked pipeline state' do context 'when merge request is in the blocked pipeline state' do
before do before do
pipeline = create( create(
:ci_pipeline, :ci_pipeline,
project: project, project: project,
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
ref: merge_request.source_branch, ref: merge_request.source_branch,
status: :manual status: :manual,
) head_pipeline_of: merge_request)
merge_request.update(head_pipeline: pipeline)
visit namespace_project_merge_request_path(project.namespace, visit namespace_project_merge_request_path(project.namespace,
project, project,
...@@ -135,11 +132,10 @@ describe 'Merge request', :feature, :js do ...@@ -135,11 +132,10 @@ describe 'Merge request', :feature, :js do
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
ref: merge_request.source_branch, ref: merge_request.source_branch,
status: 'pending', status: 'pending',
statuses: [commit_status]) statuses: [commit_status],
head_pipeline_of: merge_request)
create(:ci_build, :pending, pipeline: pipeline) create(:ci_build, :pending, pipeline: pipeline)
merge_request.update(head_pipeline: pipeline)
visit namespace_project_merge_request_path(project.namespace, project, merge_request) visit namespace_project_merge_request_path(project.namespace, project, merge_request)
end end
......
...@@ -126,12 +126,11 @@ describe 'cycle analytics events' do ...@@ -126,12 +126,11 @@ describe 'cycle analytics events' do
create(:ci_pipeline, create(:ci_pipeline,
ref: merge_request.source_branch, ref: merge_request.source_branch,
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
project: context.project) project: context.project,
head_pipeline_of: merge_request)
end end
before do before do
merge_request.update(head_pipeline: pipeline)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
...@@ -224,12 +223,11 @@ describe 'cycle analytics events' do ...@@ -224,12 +223,11 @@ describe 'cycle analytics events' do
create(:ci_pipeline, create(:ci_pipeline,
ref: merge_request.source_branch, ref: merge_request.source_branch,
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
project: context.project) project: context.project,
head_pipeline_of: merge_request)
end end
before do before do
merge_request.update(head_pipeline: pipeline)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
......
...@@ -13,8 +13,7 @@ describe 'CycleAnalytics#test', feature: true do ...@@ -13,8 +13,7 @@ describe 'CycleAnalytics#test', feature: true do
data_fn: lambda do |context| data_fn: lambda do |context|
issue = context.create(:issue, project: context.project) issue = context.create(:issue, project: context.project)
merge_request = context.create_merge_request_closing_issue(issue) merge_request = context.create_merge_request_closing_issue(issue)
pipeline = context.create(:ci_pipeline, ref: merge_request.source_branch, sha: merge_request.diff_head_sha, project: context.project) pipeline = context.create(:ci_pipeline, ref: merge_request.source_branch, sha: merge_request.diff_head_sha, project: context.project, head_pipeline_of: merge_request)
merge_request.update(head_pipeline: pipeline)
{ pipeline: pipeline, issue: issue } { pipeline: pipeline, issue: issue }
end, end,
start_time_conditions: [["pipeline is started", -> (context, data) { data[:pipeline].run! }]], start_time_conditions: [["pipeline is started", -> (context, data) { data[:pipeline].run! }]],
......
...@@ -718,8 +718,7 @@ describe MergeRequest, models: true do ...@@ -718,8 +718,7 @@ describe MergeRequest, models: true do
describe '#head_pipeline' do describe '#head_pipeline' do
describe 'when the source project exists' do describe 'when the source project exists' do
it 'returns the latest pipeline' do it 'returns the latest pipeline' do
pipeline = create(:ci_empty_pipeline, project: subject.source_project, ref: 'master', status: 'running', sha: "123abc") pipeline = create(:ci_empty_pipeline, project: subject.source_project, ref: 'master', status: 'running', sha: "123abc", head_pipeline_of: subject)
subject.update(head_pipeline: pipeline)
expect(subject.head_pipeline).to eq(pipeline) expect(subject.head_pipeline).to eq(pipeline)
end end
...@@ -1396,9 +1395,8 @@ describe MergeRequest, models: true do ...@@ -1396,9 +1395,8 @@ describe MergeRequest, models: true do
project: project, project: project,
ref: merge_request.source_branch, ref: merge_request.source_branch,
sha: merge_request.diff_head_sha, sha: merge_request.diff_head_sha,
status: status) status: status,
head_pipeline_of: merge_request)
merge_request.update(head_pipeline: pipeline)
pipeline pipeline
end end
......
...@@ -121,8 +121,7 @@ describe 'cycle analytics events', api: true do ...@@ -121,8 +121,7 @@ describe 'cycle analytics events', api: true do
issue.update(milestone: milestone) issue.update(milestone: milestone)
mr = create_merge_request_closing_issue(issue, commit_message: "References #{issue.to_reference}") mr = create_merge_request_closing_issue(issue, commit_message: "References #{issue.to_reference}")
pipeline = create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha) pipeline = create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha, head_pipeline_of: mr)
mr.update(head_pipeline_id: pipeline.id)
pipeline.run pipeline.run
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
......
...@@ -79,11 +79,8 @@ describe MergeRequests::MergeWhenPipelineSucceedsService do ...@@ -79,11 +79,8 @@ describe MergeRequests::MergeWhenPipelineSucceedsService do
context 'when triggered by pipeline with valid ref and sha' do context 'when triggered by pipeline with valid ref and sha' do
let(:triggering_pipeline) do let(:triggering_pipeline) do
create(:ci_pipeline, project: project, ref: merge_request_ref, create(:ci_pipeline, project: project, ref: merge_request_ref,
sha: merge_request_head, status: 'success') sha: merge_request_head, status: 'success',
end head_pipeline_of: mr_merge_if_green_enabled)
before do
mr_merge_if_green_enabled.update(head_pipeline: triggering_pipeline)
end end
it "merges all merge requests with merge when the pipeline succeeds enabled" do it "merges all merge requests with merge when the pipeline succeeds enabled" do
...@@ -125,11 +122,10 @@ describe MergeRequests::MergeWhenPipelineSucceedsService do ...@@ -125,11 +122,10 @@ describe MergeRequests::MergeWhenPipelineSucceedsService do
let(:conflict_pipeline) do let(:conflict_pipeline) do
create(:ci_pipeline, project: project, ref: mr_conflict.source_branch, create(:ci_pipeline, project: project, ref: mr_conflict.source_branch,
sha: mr_conflict.diff_head_sha, status: 'success') sha: mr_conflict.diff_head_sha, status: 'success',
head_pipeline_of: mr_conflict)
end end
before { mr_conflict.update(head_pipeline: conflict_pipeline) }
it 'does not merge the merge request' do it 'does not merge the merge request' do
expect(MergeWorker).not_to receive(:perform_async) expect(MergeWorker).not_to receive(:perform_async)
......
...@@ -180,12 +180,13 @@ describe MergeRequests::UpdateService, services: true do ...@@ -180,12 +180,13 @@ describe MergeRequests::UpdateService, services: true do
context 'with active pipeline' do context 'with active pipeline' do
before do before do
service_mock = double service_mock = double
pipeline = create(:ci_pipeline_with_one_job, create(
:ci_pipeline_with_one_job,
project: project, project: project,
ref: merge_request.source_branch, ref: merge_request.source_branch,
sha: merge_request.diff_head_sha) sha: merge_request.diff_head_sha,
head_pipeline_of: merge_request
merge_request.update(head_pipeline: pipeline) )
expect(MergeRequests::MergeWhenPipelineSucceedsService).to receive(:new).with(project, user). expect(MergeRequests::MergeWhenPipelineSucceedsService).to receive(:new).with(project, user).
and_return(service_mock) and_return(service_mock)
......
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