summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Artur <felipefac@gmail.com>2017-05-25 18:14:40 -0300
committerFelipe Artur <felipefac@gmail.com>2017-05-29 15:37:56 -0300
commitfe790c7543a3f3b618ca8b7238aeb73ff51cfde3 (patch)
tree24fa498c89026b17da723e98d74d7cb37fbb13c1
parent5b9e801e819b6daf1804874ed962bc2f1650c8da (diff)
downloadgitlab-ce-fe790c7543a3f3b618ca8b7238aeb73ff51cfde3.tar.gz
Set head pipeline when creating merge requestsissue_32225_2
-rw-r--r--app/services/ci/create_pipeline_service.rb21
-rw-r--r--app/services/merge_requests/create_service.rb18
-rw-r--r--changelogs/unreleased/issue_32225_2.yml2
-rw-r--r--spec/services/ci/create_pipeline_service_spec.rb2
-rw-r--r--spec/services/merge_requests/create_service_spec.rb31
5 files changed, 61 insertions, 13 deletions
diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb
index 801ff410fb5..a98b7167765 100644
--- a/app/services/ci/create_pipeline_service.rb
+++ b/app/services/ci/create_pipeline_service.rb
@@ -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,17 +128,6 @@ module Ci
origin_sha && origin_sha != Gitlab::Git::BLANK_SHA
end
- def update_merge_requests_head_pipeline
- merge_requests = MergeRequest.where(source_branch: @pipeline.ref, source_project: @pipeline.project)
-
- merge_requests_ids =
- merge_requests.select do |mr|
- mr.diff_head_sha == @pipeline.sha
- end.map(&:id)
-
- MergeRequest.where(id: merge_requests_ids).update_all(head_pipeline_id: @pipeline.id)
- end
-
def error(message, save: false)
pipeline.errors.add(:base, message)
pipeline.drop if save
diff --git a/app/services/merge_requests/create_service.rb b/app/services/merge_requests/create_service.rb
index b0ae2dfe4ce..fbf171f705e 100644
--- a/app/services/merge_requests/create_service.rb
+++ b/app/services/merge_requests/create_service.rb
@@ -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
diff --git a/changelogs/unreleased/issue_32225_2.yml b/changelogs/unreleased/issue_32225_2.yml
index 80d2bdcd63c..320b9fe00b8 100644
--- a/changelogs/unreleased/issue_32225_2.yml
+++ b/changelogs/unreleased/issue_32225_2.yml
@@ -1,4 +1,4 @@
---
-title: Sanity check for pipeline sha before saving merge request pipeline id
+title: Handle head pipeline when creating merge requests
merge_request:
author:
diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb
index c31b3d63676..030912b9f45 100644
--- a/spec/services/ci/create_pipeline_service_spec.rb
+++ b/spec/services/ci/create_pipeline_service_spec.rb
@@ -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)
diff --git a/spec/services/merge_requests/create_service_spec.rb b/spec/services/merge_requests/create_service_spec.rb
index b70e9d534a4..2963f62cc7d 100644
--- a/spec/services/merge_requests/create_service_spec.rb
+++ b/spec/services/merge_requests/create_service_spec.rb
@@ -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