Commit 6017e3b9 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'fix/gb/fix-head-pipeline-when-pipeline-has-errors' into 'master'

Fix merge request head pipeline when pipeline has errors

Closes #36657

See merge request !13664
parents e00e62c2 13b616e7
...@@ -176,9 +176,14 @@ module Ci ...@@ -176,9 +176,14 @@ module Ci
end end
def error(message, save: false) def error(message, save: false)
pipeline.errors.add(:base, message) pipeline.tap do
pipeline.drop if save pipeline.errors.add(:base, message)
pipeline
if save
pipeline.drop
update_merge_requests_head_pipeline
end
end
end end
def pipeline_created_counter def pipeline_created_counter
......
---
title: Fix merge request pipeline status when pipeline has errors
merge_request: 13664
author:
type: fixed
...@@ -55,10 +55,15 @@ describe Ci::CreatePipelineService do ...@@ -55,10 +55,15 @@ describe Ci::CreatePipelineService do
context 'when merge requests already exist for this source branch' do context 'when merge requests already exist for this source branch' do
it 'updates head pipeline of each merge request' do it 'updates head pipeline of each merge request' do
merge_request_1 = create(:merge_request, source_branch: 'master', target_branch: "branch_1", source_project: project) merge_request_1 = create(:merge_request, source_branch: 'master',
merge_request_2 = create(:merge_request, source_branch: 'master', target_branch: "branch_2", source_project: project) target_branch: "branch_1",
source_project: project)
head_pipeline = pipeline merge_request_2 = create(:merge_request, source_branch: 'master',
target_branch: "branch_2",
source_project: project)
head_pipeline = execute_service
expect(merge_request_1.reload.head_pipeline).to eq(head_pipeline) expect(merge_request_1.reload.head_pipeline).to eq(head_pipeline)
expect(merge_request_2.reload.head_pipeline).to eq(head_pipeline) expect(merge_request_2.reload.head_pipeline).to eq(head_pipeline)
...@@ -66,9 +71,11 @@ describe Ci::CreatePipelineService do ...@@ -66,9 +71,11 @@ describe Ci::CreatePipelineService do
context 'when there is no pipeline for source branch' do context 'when there is no pipeline for source branch' do
it "does not update merge request head pipeline" do it "does not update merge request head pipeline" do
merge_request = create(:merge_request, source_branch: 'feature', target_branch: "branch_1", source_project: project) merge_request = create(:merge_request, source_branch: 'feature',
target_branch: "branch_1",
source_project: project)
head_pipeline = pipeline head_pipeline = execute_service
expect(merge_request.reload.head_pipeline).not_to eq(head_pipeline) expect(merge_request.reload.head_pipeline).not_to eq(head_pipeline)
end end
...@@ -76,13 +83,19 @@ describe Ci::CreatePipelineService do ...@@ -76,13 +83,19 @@ describe Ci::CreatePipelineService do
context 'when merge request target project is different from source project' do context 'when merge request target project is different from source project' do
let!(:target_project) { create(:project, :repository) } let!(:target_project) { create(:project, :repository) }
let!(:forked_project_link) { create(:forked_project_link, forked_to_project: project, forked_from_project: target_project) }
let!(:forked_project_link) do
create(:forked_project_link, forked_to_project: project,
forked_from_project: target_project)
end
it 'updates head pipeline for merge request' do it 'updates head pipeline for merge request' do
merge_request = merge_request = create(:merge_request, source_branch: 'master',
create(:merge_request, source_branch: 'master', target_branch: "branch_1", source_project: project, target_project: target_project) target_branch: "branch_1",
source_project: project,
target_project: target_project)
head_pipeline = pipeline head_pipeline = execute_service
expect(merge_request.reload.head_pipeline).to eq(head_pipeline) expect(merge_request.reload.head_pipeline).to eq(head_pipeline)
end end
...@@ -90,15 +103,36 @@ describe Ci::CreatePipelineService do ...@@ -90,15 +103,36 @@ describe Ci::CreatePipelineService do
context 'when the pipeline is not the latest for the branch' do context 'when the pipeline is not the latest for the branch' do
it 'does not update merge request head pipeline' do it 'does not update merge request head pipeline' do
merge_request = create(:merge_request, source_branch: 'master', target_branch: "branch_1", source_project: project) merge_request = create(:merge_request, source_branch: 'master',
target_branch: "branch_1",
source_project: project)
allow_any_instance_of(Ci::Pipeline).to receive(:latest?).and_return(false) allow_any_instance_of(Ci::Pipeline)
.to receive(:latest?).and_return(false)
pipeline execute_service
expect(merge_request.reload.head_pipeline).to be_nil expect(merge_request.reload.head_pipeline).to be_nil
end end
end end
context 'when pipeline has errors' do
before do
stub_ci_pipeline_yaml_file('some invalid syntax')
end
it 'updates merge request head pipeline reference' do
merge_request = create(:merge_request, source_branch: 'master',
target_branch: 'feature',
source_project: project)
head_pipeline = execute_service
expect(head_pipeline).to be_persisted
expect(head_pipeline.yaml_errors).to be_present
expect(merge_request.reload.head_pipeline).to eq head_pipeline
end
end
end end
context 'auto-cancel enabled' do context 'auto-cancel enabled' 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