Use actual head pipeline on merge request serializer

This commit is contained in:
Felipe Artur 2017-12-01 11:26:51 -02:00
parent f586dc0735
commit 5cf3ff27c8
6 changed files with 33 additions and 21 deletions

View file

@ -283,15 +283,15 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
@merge_request.update(merge_error: nil)
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
.new(@project, current_user, merge_params)
.execute(@merge_request)
: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
# the tests finish at about the same time
@merge_request.merge_async(current_user.id, params)

View file

@ -148,7 +148,7 @@ class MergeRequest < ActiveRecord::Base
# 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.
# 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
end
@ -831,7 +831,7 @@ class MergeRequest < ActiveRecord::Base
return true unless project.only_allow_merge_if_pipeline_succeeds?
return true unless head_pipeline
current_head_pipeline&.success? || current_head_pipeline&.skipped?
actual_head_pipeline&.success? || actual_head_pipeline&.skipped?
end
def environments_for(current_user)
@ -1005,7 +1005,7 @@ class MergeRequest < ActiveRecord::Base
return true if autocomplete_precheck
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
true

View file

@ -163,7 +163,7 @@ class MergeRequestPresenter < Gitlab::View::Presenter::Delegated
end
def pipeline
@pipeline ||= head_pipeline
@pipeline ||= actual_head_pipeline
end
def issues_sentence(project, issues)

View file

@ -33,7 +33,7 @@ class MergeRequestEntity < IssuableEntity
end
expose :merge_commit_message
expose :head_pipeline, with: PipelineDetailsEntity, as: :pipeline
expose :actual_head_pipeline, with: PipelineDetailsEntity, as: :pipeline
# Booleans
expose :merge_ongoing?, as: :merge_ongoing

View file

@ -840,20 +840,20 @@ describe MergeRequest do
end
end
describe '#current_head_pipeline' do
describe '#actual_head_pipeline' do
it 'returns nil for MR with old pipeline' do
pipeline = create(:ci_empty_pipeline, sha: 'notlatestsha')
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
it 'returns the pipeline for MR with recent pipeline' do
pipeline = create(:ci_empty_pipeline, sha: 'lastsha')
subject.update_attribute(:head_pipeline_id, pipeline.id)
expect(subject.current_head_pipeline).to eq(subject.head_pipeline)
expect(subject.current_head_pipeline).to eq(pipeline)
expect(subject.actual_head_pipeline).to eq(subject.head_pipeline)
expect(subject.actual_head_pipeline).to eq(pipeline)
end
end
end

View file

@ -5,22 +5,34 @@ describe MergeRequestEntity do
let(:resource) { create(:merge_request, source_project: project, target_project: project) }
let(:user) { create(:user) }
let(:request) { double('request', current_user: user) }
let(:request) { double('request', current_user: user, project: project) }
subject do
described_class.new(resource, request: request).as_json
end
it 'includes pipeline' do
req = double('request', current_user: user)
pipeline = build_stubbed(:ci_pipeline)
allow(resource).to receive(:head_pipeline).and_return(pipeline)
describe 'pipeline' do
let(:pipeline) { create(:ci_empty_pipeline, project: project, ref: resource.source_branch, sha: resource.source_branch_sha, head_pipeline_of: resource) }
pipeline_payload = PipelineDetailsEntity
.represent(pipeline, request: req)
.as_json
context 'when is up to date' do
let(:req) { double('request', current_user: user, project: project) }
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
it 'includes issues_links' do