Commit 921c2be4 authored by Felipe Artur's avatar Felipe Artur

Add transient head_pipeline_of to pipeline factories

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