Commit ffe52f01 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'issue_32225_2' into 'master'

Set head pipeline when creating merge requests

See merge request !11669
parents f47e86fe fe790c75
......@@ -61,6 +61,16 @@ module Ci
private
def update_merge_requests_head_pipeline
merge_requests = MergeRequest.where(source_branch: @pipeline.ref, source_project: @pipeline.project)
merge_requests = merge_requests.select do |mr|
mr.diff_head_sha == @pipeline.sha
end
MergeRequest.where(id: merge_requests).update_all(head_pipeline_id: @pipeline.id)
end
def skip_ci?
return false unless pipeline.git_commit_message
pipeline.git_commit_message =~ /\[(ci[ _-]skip|skip[ _-]ci)\]/i
......@@ -118,11 +128,6 @@ module Ci
origin_sha && origin_sha != Gitlab::Git::BLANK_SHA
end
def update_merge_requests_head_pipeline
MergeRequest.where(source_branch: @pipeline.ref, source_project: @pipeline.project).
update_all(head_pipeline_id: @pipeline.id)
end
def error(message, save: false)
pipeline.errors.add(:base, message)
pipeline.drop if save
......
......@@ -11,7 +11,9 @@ module MergeRequests
merge_request = MergeRequest.new
merge_request.source_project = source_project
merge_request.source_branch = params[:source_branch]
merge_request.merge_params['force_remove_source_branch'] = params.delete(:force_remove_source_branch)
merge_request.head_pipeline = head_pipeline_for(merge_request)
create(merge_request)
end
......@@ -22,5 +24,21 @@ module MergeRequests
todo_service.new_merge_request(issuable, current_user)
issuable.cache_merge_request_closes_issues!(current_user)
end
private
def head_pipeline_for(merge_request)
return unless merge_request.source_project
sha = merge_request.source_branch_head&.id
return unless sha
pipelines =
Ci::Pipeline.where(ref: merge_request.source_branch, project_id: merge_request.source_project.id, sha: sha).
order(id: :desc)
pipelines.first
end
end
end
---
title: Handle head pipeline when creating merge requests
merge_request:
author:
......@@ -36,7 +36,7 @@ describe Ci::CreatePipelineService, services: true do
expect(pipeline.builds.first).to be_kind_of(Ci::Build)
end
context '#update_merge_requests_head_pipeline' do
context 'when merge requests already exist for this source branch' 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_2 = create(:merge_request, source_branch: 'master', target_branch: "branch_2", source_project: project)
......@@ -58,7 +58,7 @@ describe Ci::CreatePipelineService, services: true do
end
context 'when merge request target project is different from source project' do
let!(:target_project) { create(:empty_project) }
let!(:target_project) { create(:project) }
let!(:forked_project_link) { create(:forked_project_link, forked_to_project: project, forked_from_project: target_project) }
it 'updates head pipeline for merge request' do
......@@ -70,6 +70,17 @@ describe Ci::CreatePipelineService, services: true do
expect(merge_request.reload.head_pipeline).to eq(head_pipeline)
end
end
context 'when merge request head commit sha does not match pipeline sha' 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)
allow_any_instance_of(MergeRequestDiff).to receive(:head_commit).and_return(double(id: 1234))
pipeline
expect(merge_request.reload.head_pipeline).to be_nil
end
end
end
context 'auto-cancel enabled' do
......
......@@ -75,6 +75,37 @@ describe MergeRequests::CreateService, services: true do
expect(Todo.where(attributes).count).to eq 1
end
end
context 'when head pipelines already exist for merge request source branch' do
let(:sha) { project.commit(opts[:source_branch]).id }
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: sha) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: sha) }
let!(:pipeline_3) { create(:ci_pipeline, project: project, ref: "other_branch", project_id: project.id) }
before do
project.merge_requests.
where(source_branch: opts[:source_branch], target_branch: opts[:target_branch]).
destroy_all
end
it 'sets head pipeline' do
merge_request = service.execute
expect(merge_request.head_pipeline).to eq(pipeline_2)
expect(merge_request).to be_persisted
end
context 'when merge request head commit sha does not match pipeline sha' do
it 'sets the head pipeline correctly' do
pipeline_2.update(sha: 1234)
merge_request = service.execute
expect(merge_request.head_pipeline).to eq(pipeline_1)
expect(merge_request).to be_persisted
end
end
end
end
it_behaves_like 'new issuable record that supports slash commands' 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