提交 94be44c5 编写于 作者: D Douwe Maan

Fix race condition between pipeline creation and MR diff_head_sha update

上级 42c13b26
...@@ -63,13 +63,10 @@ module Ci ...@@ -63,13 +63,10 @@ module Ci
private private
def update_merge_requests_head_pipeline def update_merge_requests_head_pipeline
merge_requests = MergeRequest.where(source_branch: @pipeline.ref, source_project: @pipeline.project) return unless pipeline.latest?
merge_requests = merge_requests.select do |mr| MergeRequest.where(source_project: @pipeline.project, source_branch: @pipeline.ref).
mr.diff_head_sha == @pipeline.sha update_all(head_pipeline_id: @pipeline.id)
end
MergeRequest.where(id: merge_requests).update_all(head_pipeline_id: @pipeline.id)
end end
def skip_ci? def skip_ci?
......
...@@ -30,15 +30,12 @@ module MergeRequests ...@@ -30,15 +30,12 @@ module MergeRequests
def head_pipeline_for(merge_request) def head_pipeline_for(merge_request)
return unless merge_request.source_project return unless merge_request.source_project
sha = merge_request.source_branch_head&.id sha = merge_request.source_branch_sha
return unless sha return unless sha
pipelines = pipelines = merge_request.source_project.pipelines.where(ref: merge_request.source_branch, sha: sha)
Ci::Pipeline.where(ref: merge_request.source_branch, project_id: merge_request.source_project.id, sha: sha).
order(id: :desc)
pipelines.first pipelines.order(id: :desc).first
end end
end end
end end
...@@ -72,10 +72,11 @@ describe Ci::CreatePipelineService, services: true do ...@@ -72,10 +72,11 @@ describe Ci::CreatePipelineService, services: true do
end end
end end
context 'when merge request head commit sha does not match pipeline sha' 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(MergeRequestDiff).to receive(:head_commit).and_return(double(id: 1234))
allow_any_instance_of(Ci::Pipeline).to receive(:latest?).and_return(false)
pipeline pipeline
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册