debian-mirror-gitlab/spec/services/merge_requests/create_pipeline_service_spec.rb

198 lines
5.8 KiB
Ruby
Raw Normal View History

2019-09-04 21:01:54 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe MergeRequests::CreatePipelineService do
include ProjectForksHelper
2020-11-24 15:15:51 +05:30
let_it_be(:project, reload: true) { create(:project, :repository) }
2020-03-13 15:44:24 +05:30
let_it_be(:user) { create(:user) }
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
let(:service) { described_class.new(project: project, current_user: actor, params: params) }
2020-07-28 23:09:34 +05:30
let(:actor) { user }
2019-09-04 21:01:54 +05:30
let(:params) { {} }
before do
2020-11-24 15:15:51 +05:30
stub_feature_flags(ci_disallow_to_create_merge_request_pipelines_in_target_project: false)
2019-09-04 21:01:54 +05:30
project.add_developer(user)
end
describe '#execute' do
subject { service.execute(merge_request) }
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-28 23:09:34 +05:30
source_project: source_project,
2019-09-04 21:01:54 +05:30
target_branch: 'master',
target_project: project)
end
2020-07-28 23:09:34 +05:30
let(:source_project) { project }
2019-09-04 21:01:54 +05:30
it 'creates a detached merge request pipeline' do
expect { subject }.to change { Ci::Pipeline.count }.by(1)
expect(subject).to be_persisted
expect(subject).to be_detached_merge_request_pipeline
end
2019-12-04 20:38:33 +05:30
it 'defaults to merge_request_event' do
expect(subject.source).to eq('merge_request_event')
end
2020-07-28 23:09:34 +05:30
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-29 21:17:54 +05:30
2020-07-28 23:09:34 +05:30
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
expect(subject.project).to eq(project)
end
2020-11-24 15:15:51 +05:30
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
expect(subject.project).to eq(source_project)
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
expect(subject.project).to eq(project)
end
end
end
context 'when ci_disallow_to_create_merge_request_pipelines_in_target_project feature flag is enabled' do
2020-07-28 23:09:34 +05:30
before do
2020-11-24 15:15:51 +05:30
stub_feature_flags(ci_disallow_to_create_merge_request_pipelines_in_target_project: true)
2020-07-28 23:09:34 +05:30
end
it 'creates a pipeline in the source project' do
expect(subject.project).to eq(source_project)
end
end
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
expect(subject.project).to eq(source_project)
end
end
context 'when actor does not have permission to create pipelines' do
let(:actor) { create(:user) }
it 'returns nothing' do
expect(subject.full_error_messages).to include('Insufficient permissions to create a new pipeline')
end
end
end
2019-09-04 21:01:54 +05:30
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
2020-01-01 13:55:28 +05:30
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
expect { subject }.not_to change { Ci::Pipeline.count }
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
expect { subject }.not_to change { Ci::Pipeline.count }
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
it 'creates a detached merge request pipeline' do
expect { subject }.to change { Ci::Pipeline.count }.by(1)
expect(subject).to be_persisted
expect(subject).to be_detached_merge_request_pipeline
end
2019-09-04 21:01:54 +05:30
end
2020-01-01 13:55:28 +05:30
context 'with rules do specify creation on a tag' do
let(:config) do
{
workflow: {
rules: [{ if: '$CI_COMMIT_TAG' }]
},
rspec: { script: 'echo' }
}
end
it 'does not create a pipeline' do
expect { subject }.not_to change { Ci::Pipeline.count }
end
2019-09-04 21:01:54 +05:30
end
end
end
end