2019-05-27 08:26:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe MergeRequests::CreatePipelineService do
|
2020-07-15 17:09:26 -04:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2020-09-04 05:08:38 -04:00
|
|
|
let_it_be(:project, reload: true) { create(:project, :repository) }
|
2020-01-10 04:07:49 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
2021-04-12 05:09:09 -04:00
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
let(:service) { described_class.new(project: project, current_user: actor, params: params) }
|
2020-07-15 17:09:26 -04:00
|
|
|
let(:actor) { user }
|
2019-05-27 08:26:39 -04:00
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2021-07-20 08:10:29 -04:00
|
|
|
subject(:response) { service.execute(merge_request) }
|
2019-05-27 08:26:39 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_ci_pipeline_yaml_file(YAML.dump(config))
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:config) do
|
|
|
|
{ rspec: { script: 'echo', only: ['merge_requests'] } }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request,
|
|
|
|
source_branch: 'feature',
|
2020-07-15 17:09:26 -04:00
|
|
|
source_project: source_project,
|
2019-05-27 08:26:39 -04:00
|
|
|
target_branch: 'master',
|
|
|
|
target_project: project)
|
|
|
|
end
|
|
|
|
|
2020-07-15 17:09:26 -04:00
|
|
|
let(:source_project) { project }
|
|
|
|
|
2019-05-27 08:26:39 -04:00
|
|
|
it 'creates a detached merge request pipeline' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect { response }.to change { Ci::Pipeline.count }.by(1)
|
2019-05-27 08:26:39 -04:00
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.payload).to be_persisted
|
|
|
|
expect(response.payload).to be_detached_merge_request_pipeline
|
2019-05-27 08:26:39 -04:00
|
|
|
end
|
|
|
|
|
2019-09-11 05:06:33 -04:00
|
|
|
it 'defaults to merge_request_event' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect(response.payload.source).to eq('merge_request_event')
|
2019-09-11 05:06:33 -04:00
|
|
|
end
|
|
|
|
|
2022-05-23 14:08:14 -04:00
|
|
|
context 'when push options contain ci.skip' do
|
|
|
|
let(:params) { { push_options: { ci: { skip: true } } } }
|
|
|
|
|
|
|
|
it 'creates a skipped pipeline' do
|
|
|
|
expect { response }.to change { Ci::Pipeline.count }.by(1)
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.payload).to be_persisted
|
|
|
|
expect(response.payload.builds).to be_empty
|
|
|
|
expect(response.payload).to be_skipped
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-15 17:09:26 -04:00
|
|
|
context 'with fork merge request' do
|
|
|
|
let_it_be(:forked_project) { fork_project(project, nil, repository: true, target_project: create(:project, :private, :repository)) }
|
2021-04-12 05:09:09 -04:00
|
|
|
|
2020-07-15 17:09:26 -04:00
|
|
|
let(:source_project) { forked_project }
|
|
|
|
|
|
|
|
context 'when actor has permission to create pipelines in target project' do
|
|
|
|
let(:actor) { user }
|
|
|
|
|
|
|
|
it 'creates a pipeline in the target project' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect(response.payload.project).to eq(project)
|
2020-07-15 17:09:26 -04:00
|
|
|
end
|
|
|
|
|
2022-08-10 11:11:53 -04:00
|
|
|
context 'when the feature is disabled in CI/CD settings' do
|
|
|
|
before do
|
|
|
|
project.update!(ci_allow_fork_pipelines_to_run_in_parent_project: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a pipeline in the source project' do
|
|
|
|
expect(response.payload.project).to eq(source_project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-04 05:08:38 -04:00
|
|
|
context 'when source branch is protected' do
|
|
|
|
context 'when actor does not have permission to update the protected branch in target project' do
|
|
|
|
let!(:protected_branch) { create(:protected_branch, name: '*', project: project) }
|
|
|
|
|
|
|
|
it 'creates a pipeline in the source project' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect(response.payload.project).to eq(source_project)
|
2020-09-04 05:08:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when actor has permission to update the protected branch in target project' do
|
|
|
|
let!(:protected_branch) { create(:protected_branch, :developers_can_merge, name: '*', project: project) }
|
|
|
|
|
|
|
|
it 'creates a pipeline in the target project' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect(response.payload.project).to eq(project)
|
2020-09-04 05:08:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-15 17:09:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when actor has permission to create pipelines in forked project' do
|
|
|
|
let(:actor) { fork_user }
|
|
|
|
let(:fork_user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
source_project.add_developer(fork_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a pipeline in the source project' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect(response.payload.project).to eq(source_project)
|
2020-07-15 17:09:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when actor does not have permission to create pipelines' do
|
|
|
|
let(:actor) { create(:user) }
|
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
it 'responds with error' do
|
|
|
|
expect(response).to be_error
|
|
|
|
expect(response.message).to include('Insufficient permissions to create a new pipeline')
|
2020-07-15 17:09:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-27 08:26:39 -04:00
|
|
|
context 'when service is called multiple times' do
|
|
|
|
it 'creates a pipeline once' do
|
|
|
|
expect do
|
|
|
|
service.execute(merge_request)
|
|
|
|
service.execute(merge_request)
|
|
|
|
end.to change { Ci::Pipeline.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when allow_duplicate option is true' do
|
|
|
|
let(:params) { { allow_duplicate: true } }
|
|
|
|
|
|
|
|
it 'creates pipelines multiple times' do
|
|
|
|
expect do
|
|
|
|
service.execute(merge_request)
|
|
|
|
service.execute(merge_request)
|
|
|
|
end.to change { Ci::Pipeline.count }.by(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
context 'when .gitlab-ci.yml does not use workflow:rules' do
|
|
|
|
context 'without only: [merge_requests] keyword' do
|
|
|
|
let(:config) do
|
|
|
|
{ rspec: { script: 'echo' } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a pipeline' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect { response }.not_to change { Ci::Pipeline.count }
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with rules that specify creation on a tag' do
|
|
|
|
let(:config) do
|
|
|
|
{
|
|
|
|
rspec: {
|
|
|
|
script: 'echo',
|
|
|
|
rules: [{ if: '$CI_COMMIT_TAG' }]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a pipeline' do
|
2021-07-20 08:10:29 -04:00
|
|
|
expect { response }.not_to change { Ci::Pipeline.count }
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when workflow:rules are specified' do
|
|
|
|
context 'when rules request creation on merge request' do
|
|
|
|
let(:config) do
|
|
|
|
{
|
|
|
|
workflow: {
|
|
|
|
rules: [{ if: '$CI_MERGE_REQUEST_ID' }]
|
|
|
|
},
|
|
|
|
rspec: { script: 'echo' }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
it 'creates a detached merge request pipeline', :aggregate_failures do
|
|
|
|
expect { response }.to change { Ci::Pipeline.count }.by(1)
|
2019-12-16 07:07:43 -05:00
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.payload).to be_persisted
|
|
|
|
expect(response.payload).to be_detached_merge_request_pipeline
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
2019-05-27 08:26:39 -04:00
|
|
|
end
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
context 'with rules do specify creation on a tag' do
|
|
|
|
let(:config) do
|
|
|
|
{
|
|
|
|
workflow: {
|
|
|
|
rules: [{ if: '$CI_COMMIT_TAG' }]
|
|
|
|
},
|
|
|
|
rspec: { script: 'echo' }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
it 'does not create a pipeline', :aggregate_failures do
|
|
|
|
expect { response }.not_to change { Ci::Pipeline.count }
|
|
|
|
expect(response).to be_error
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
2019-05-27 08:26:39 -04:00
|
|
|
end
|
|
|
|
end
|
2021-07-20 08:10:29 -04:00
|
|
|
|
|
|
|
context 'when merge request has no commits' do
|
|
|
|
before do
|
|
|
|
allow(merge_request).to receive(:has_no_commits?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a pipeline', :aggregate_failures do
|
|
|
|
expect { response }.not_to change { Ci::Pipeline.count }
|
|
|
|
|
|
|
|
expect(response).to be_error
|
|
|
|
expect(response.message).to eq('Cannot create a pipeline for this merge request.')
|
|
|
|
expect(response.payload).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2019-05-27 08:26:39 -04:00
|
|
|
end
|
|
|
|
end
|