Commit 5cf3ff27 authored by Felipe Artur's avatar Felipe Artur

Use actual head pipeline on merge request serializer

parent f586dc07
...@@ -283,15 +283,15 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo ...@@ -283,15 +283,15 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
@merge_request.update(merge_error: nil) @merge_request.update(merge_error: nil)
if params[:merge_when_pipeline_succeeds].present? if params[:merge_when_pipeline_succeeds].present?
return :failed unless @merge_request.current_head_pipeline return :failed unless @merge_request.actual_head_pipeline
if @merge_request.current_head_pipeline.active? if @merge_request.actual_head_pipeline.active?
::MergeRequests::MergeWhenPipelineSucceedsService ::MergeRequests::MergeWhenPipelineSucceedsService
.new(@project, current_user, merge_params) .new(@project, current_user, merge_params)
.execute(@merge_request) .execute(@merge_request)
:merge_when_pipeline_succeeds :merge_when_pipeline_succeeds
elsif @merge_request.current_head_pipeline.success? elsif @merge_request.actual_head_pipeline.success?
# This can be triggered when a user clicks the auto merge button while # This can be triggered when a user clicks the auto merge button while
# the tests finish at about the same time # the tests finish at about the same time
@merge_request.merge_async(current_user.id, params) @merge_request.merge_async(current_user.id, params)
......
...@@ -148,7 +148,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -148,7 +148,7 @@ class MergeRequest < ActiveRecord::Base
# Use this method whenever you need to make sure the head_pipeline is synced with the # Use this method whenever you need to make sure the head_pipeline is synced with the
# branch head commit, for example checking if a merge request can be merged. # branch head commit, for example checking if a merge request can be merged.
# For more information check: https://gitlab.com/gitlab-org/gitlab-ce/issues/40004 # For more information check: https://gitlab.com/gitlab-org/gitlab-ce/issues/40004
def current_head_pipeline def actual_head_pipeline
head_pipeline&.sha == diff_head_sha ? head_pipeline : nil head_pipeline&.sha == diff_head_sha ? head_pipeline : nil
end end
...@@ -831,7 +831,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -831,7 +831,7 @@ class MergeRequest < ActiveRecord::Base
return true unless project.only_allow_merge_if_pipeline_succeeds? return true unless project.only_allow_merge_if_pipeline_succeeds?
return true unless head_pipeline return true unless head_pipeline
current_head_pipeline&.success? || current_head_pipeline&.skipped? actual_head_pipeline&.success? || actual_head_pipeline&.skipped?
end end
def environments_for(current_user) def environments_for(current_user)
...@@ -1005,7 +1005,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -1005,7 +1005,7 @@ class MergeRequest < ActiveRecord::Base
return true if autocomplete_precheck return true if autocomplete_precheck
return false unless mergeable?(skip_ci_check: true) return false unless mergeable?(skip_ci_check: true)
return false if current_head_pipeline && !(current_head_pipeline.success? || current_head_pipeline.active?) return false if actual_head_pipeline && !(actual_head_pipeline.success? || actual_head_pipeline.active?)
return false if last_diff_sha != diff_head_sha return false if last_diff_sha != diff_head_sha
true true
......
...@@ -163,7 +163,7 @@ class MergeRequestPresenter < Gitlab::View::Presenter::Delegated ...@@ -163,7 +163,7 @@ class MergeRequestPresenter < Gitlab::View::Presenter::Delegated
end end
def pipeline def pipeline
@pipeline ||= head_pipeline @pipeline ||= actual_head_pipeline
end end
def issues_sentence(project, issues) def issues_sentence(project, issues)
......
...@@ -33,7 +33,7 @@ class MergeRequestEntity < IssuableEntity ...@@ -33,7 +33,7 @@ class MergeRequestEntity < IssuableEntity
end end
expose :merge_commit_message expose :merge_commit_message
expose :head_pipeline, with: PipelineDetailsEntity, as: :pipeline expose :actual_head_pipeline, with: PipelineDetailsEntity, as: :pipeline
# Booleans # Booleans
expose :merge_ongoing?, as: :merge_ongoing expose :merge_ongoing?, as: :merge_ongoing
......
...@@ -840,20 +840,20 @@ describe MergeRequest do ...@@ -840,20 +840,20 @@ describe MergeRequest do
end end
end end
describe '#current_head_pipeline' do describe '#actual_head_pipeline' do
it 'returns nil for MR with old pipeline' do it 'returns nil for MR with old pipeline' do
pipeline = create(:ci_empty_pipeline, sha: 'notlatestsha') pipeline = create(:ci_empty_pipeline, sha: 'notlatestsha')
subject.update_attribute(:head_pipeline_id, pipeline.id) subject.update_attribute(:head_pipeline_id, pipeline.id)
expect(subject.current_head_pipeline).to be_nil expect(subject.actual_head_pipeline).to be_nil
end end
it 'returns the pipeline for MR with recent pipeline' do it 'returns the pipeline for MR with recent pipeline' do
pipeline = create(:ci_empty_pipeline, sha: 'lastsha') pipeline = create(:ci_empty_pipeline, sha: 'lastsha')
subject.update_attribute(:head_pipeline_id, pipeline.id) subject.update_attribute(:head_pipeline_id, pipeline.id)
expect(subject.current_head_pipeline).to eq(subject.head_pipeline) expect(subject.actual_head_pipeline).to eq(subject.head_pipeline)
expect(subject.current_head_pipeline).to eq(pipeline) expect(subject.actual_head_pipeline).to eq(pipeline)
end end
end end
end end
......
...@@ -5,22 +5,34 @@ describe MergeRequestEntity do ...@@ -5,22 +5,34 @@ describe MergeRequestEntity do
let(:resource) { create(:merge_request, source_project: project, target_project: project) } let(:resource) { create(:merge_request, source_project: project, target_project: project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:request) { double('request', current_user: user) } let(:request) { double('request', current_user: user, project: project) }
subject do subject do
described_class.new(resource, request: request).as_json described_class.new(resource, request: request).as_json
end end
it 'includes pipeline' do describe 'pipeline' do
req = double('request', current_user: user) let(:pipeline) { create(:ci_empty_pipeline, project: project, ref: resource.source_branch, sha: resource.source_branch_sha, head_pipeline_of: resource) }
pipeline = build_stubbed(:ci_pipeline)
allow(resource).to receive(:head_pipeline).and_return(pipeline)
pipeline_payload = PipelineDetailsEntity context 'when is up to date' do
.represent(pipeline, request: req) let(:req) { double('request', current_user: user, project: project) }
.as_json
expect(subject[:pipeline]).to eq(pipeline_payload) it 'returns pipeline' do
pipeline_payload = PipelineDetailsEntity
.represent(pipeline, request: req)
.as_json
expect(subject[:pipeline]).to eq(pipeline_payload)
end
end
context 'when is not up to date' do
it 'returns nil' do
pipeline.update(sha: "not up to date")
expect(subject[:pipeline]).to be_nil
end
end
end end
it 'includes issues_links' do it 'includes issues_links' 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