2015-12-23 02:04:40 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe MergeRequests::MergeWhenPipelineSucceedsService do
|
2016-06-16 23:09:34 +05:30
|
|
|
let(:user) { create(:user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
let(:mr_merge_if_green_enabled) do
|
2017-08-17 22:00:37 +05:30
|
|
|
create(:merge_request, merge_when_pipeline_succeeds: true, merge_user: user,
|
2016-01-29 22:53:50 +05:30
|
|
|
source_branch: "master", target_branch: 'feature',
|
2015-12-23 02:04:40 +05:30
|
|
|
source_project: project, target_project: project, state: "opened")
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline_with_one_job, ref: mr_merge_if_green_enabled.source_branch,
|
|
|
|
project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:service) do
|
|
|
|
described_class.new(project, user, commit_message: 'Awesome message')
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
describe "#execute" do
|
2016-06-16 23:09:34 +05:30
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, target_project: project, source_project: project,
|
|
|
|
source_branch: "feature", target_branch: 'master')
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
context 'first time enabling' do
|
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
allow(merge_request)
|
|
|
|
.to receive(:head_pipeline)
|
|
|
|
.and_return(pipeline)
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the params, merge_user, and flag' do
|
|
|
|
expect(merge_request).to be_valid
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(merge_request.merge_when_pipeline_succeeds).to be_truthy
|
2015-12-23 02:04:40 +05:30
|
|
|
expect(merge_request.merge_params).to eq commit_message: 'Awesome message'
|
|
|
|
expect(merge_request.merge_user).to be user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a system note' do
|
|
|
|
note = merge_request.notes.last
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(note.note).to match %r{enabled an automatic merge when the pipeline for (\w+/\w+@)?\h{8}}
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'already approved' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:service) { described_class.new(project, user, new_key: true) }
|
2015-12-23 02:04:40 +05:30
|
|
|
let(:build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch) }
|
|
|
|
|
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
allow(mr_merge_if_green_enabled).to receive(:head_pipeline)
|
|
|
|
.and_return(pipeline)
|
|
|
|
|
|
|
|
allow(mr_merge_if_green_enabled).to receive(:mergeable?)
|
|
|
|
.and_return(true)
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
allow(pipeline).to receive(:success?).and_return(true)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the merge params' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(SystemNoteService).not_to receive(:merge_when_pipeline_succeeds)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
service.execute(mr_merge_if_green_enabled)
|
|
|
|
expect(mr_merge_if_green_enabled.merge_params).to have_key(:new_key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#trigger" do
|
2016-11-03 12:29:30 +05:30
|
|
|
let(:merge_request_ref) { mr_merge_if_green_enabled.source_branch }
|
|
|
|
let(:merge_request_head) do
|
|
|
|
project.commit(mr_merge_if_green_enabled.source_branch).id
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
context 'when triggered by pipeline with valid ref and sha' do
|
|
|
|
let(:triggering_pipeline) do
|
|
|
|
create(:ci_pipeline, project: project, ref: merge_request_ref,
|
2017-09-10 17:25:29 +05:30
|
|
|
sha: merge_request_head, status: 'success',
|
|
|
|
head_pipeline_of: mr_merge_if_green_enabled)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it "merges all merge requests with merge when the pipeline succeeds enabled" do
|
2016-04-02 18:10:28 +05:30
|
|
|
expect(MergeWorker).to receive(:perform_async)
|
2016-11-03 12:29:30 +05:30
|
|
|
service.trigger(triggering_pipeline)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
context 'when triggered by an old pipeline' do
|
|
|
|
let(:old_pipeline) do
|
|
|
|
create(:ci_pipeline, project: project, ref: merge_request_ref,
|
|
|
|
sha: '1234abcdef', status: 'success')
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
it 'it does not merge merge request' do
|
2016-06-16 23:09:34 +05:30
|
|
|
expect(MergeWorker).not_to receive(:perform_async)
|
2016-11-03 12:29:30 +05:30
|
|
|
service.trigger(old_pipeline)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
context 'when triggered by pipeline from a different branch' do
|
|
|
|
let(:unrelated_pipeline) do
|
|
|
|
create(:ci_pipeline, project: project, ref: 'feature',
|
|
|
|
sha: merge_request_head, status: 'success')
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
it 'does not merge request' do
|
2016-06-16 23:09:34 +05:30
|
|
|
expect(MergeWorker).not_to receive(:perform_async)
|
2016-11-03 12:29:30 +05:30
|
|
|
service.trigger(unrelated_pipeline)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
context 'when the merge request is not mergeable' do
|
|
|
|
let(:mr_conflict) do
|
|
|
|
create(:merge_request, merge_when_pipeline_succeeds: true, merge_user: user,
|
|
|
|
source_branch: 'master', target_branch: 'feature-conflict',
|
|
|
|
source_project: project, target_project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:conflict_pipeline) do
|
|
|
|
create(:ci_pipeline, project: project, ref: mr_conflict.source_branch,
|
2017-09-10 17:25:29 +05:30
|
|
|
sha: mr_conflict.diff_head_sha, status: 'success',
|
|
|
|
head_pipeline_of: mr_conflict)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not merge the merge request' do
|
|
|
|
expect(MergeWorker).not_to receive(:perform_async)
|
|
|
|
|
|
|
|
service.trigger(conflict_pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates todos for unmergeability' do
|
|
|
|
expect_any_instance_of(TodoService).to receive(:merge_request_became_unmergeable).with(mr_conflict)
|
|
|
|
|
|
|
|
service.trigger(conflict_pipeline)
|
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
describe "#cancel" do
|
|
|
|
before do
|
|
|
|
service.cancel(mr_merge_if_green_enabled)
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it "resets all the pipeline succeeds params" do
|
|
|
|
expect(mr_merge_if_green_enabled.merge_when_pipeline_succeeds).to be_falsey
|
2016-11-03 12:29:30 +05:30
|
|
|
expect(mr_merge_if_green_enabled.merge_params).to eq({})
|
|
|
|
expect(mr_merge_if_green_enabled.merge_user).to be nil
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
it 'Posts a system note' do
|
|
|
|
note = mr_merge_if_green_enabled.notes.last
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(note.note).to include 'canceled the automatic merge'
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'pipeline integration' do
|
|
|
|
context 'when there are multiple stages in the pipeline' do
|
2016-04-02 18:10:28 +05:30
|
|
|
let(:ref) { mr_merge_if_green_enabled.source_branch }
|
2016-11-03 12:29:30 +05:30
|
|
|
let(:sha) { project.commit(ref).id }
|
|
|
|
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_empty_pipeline, ref: ref, sha: sha, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:build) do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, ref: ref,
|
|
|
|
name: 'build', stage: 'build')
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:test) do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, ref: ref,
|
|
|
|
name: 'test', stage: 'test')
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
# This behavior of MergeRequest: we instantiate a new object
|
2017-08-17 22:00:37 +05:30
|
|
|
#
|
|
|
|
allow_any_instance_of(MergeRequest)
|
|
|
|
.to receive(:head_pipeline)
|
|
|
|
.and_wrap_original do
|
|
|
|
Ci::Pipeline.find(pipeline.id)
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
it "doesn't merge if any of stages failed" do
|
2016-06-16 23:09:34 +05:30
|
|
|
expect(MergeWorker).not_to receive(:perform_async)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
build.success
|
2017-08-17 22:00:37 +05:30
|
|
|
test.reload
|
2016-04-02 18:10:28 +05:30
|
|
|
test.drop
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
it 'merges when all stages succeeded' do
|
2016-04-02 18:10:28 +05:30
|
|
|
expect(MergeWorker).to receive(:perform_async)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
build.success
|
2017-08-17 22:00:37 +05:30
|
|
|
test.reload
|
2016-04-02 18:10:28 +05:30
|
|
|
test.success
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|