2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Ci::PipelineTriggerService, feature_category: :continuous_integration do
|
2021-03-08 18:12:59 +05:30
|
|
|
include AfterNextHelpers
|
|
|
|
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
stub_ci_pipeline_to_return_yaml_file
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2021-03-08 18:12:59 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:result) { described_class.new(project, user, params).execute }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
shared_examples 'detecting an unprocessable pipeline trigger' do
|
|
|
|
context 'when the pipeline was not created successfully' do
|
|
|
|
let(:fail_pipeline) do
|
|
|
|
receive(:execute).and_wrap_original do |original, *args|
|
2021-10-27 15:23:28 +05:30
|
|
|
response = original.call(*args)
|
|
|
|
pipeline = response.payload
|
2021-06-08 01:23:25 +05:30
|
|
|
pipeline.update!(failure_reason: 'unknown_failure')
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
response
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_next(Ci::CreatePipelineService).to fail_pipeline
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the correct status code' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }
|
|
|
|
expect(result).to be_error
|
|
|
|
expect(result.http_status).to eq(:unprocessable_entity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'with a trigger token' do
|
|
|
|
let(:trigger) { create(:ci_trigger, project: project, owner: user) }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when trigger belongs to a different project' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: nil } }
|
2019-12-21 20:55:43 +05:30
|
|
|
let(:trigger) { create(:ci_trigger, project: create(:project), owner: user) }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
it 'does nothing' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2022-06-02 21:05:25 +05:30
|
|
|
context 'when trigger owner does not have a permission to read a project' do
|
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: nil } }
|
|
|
|
let(:trigger) { create(:ci_trigger, project: project, owner: create(:user)) }
|
|
|
|
|
|
|
|
it 'does nothing' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
context 'when params have an existing trigger token' do
|
|
|
|
context 'when params have an existing ref' do
|
2019-12-21 20:55:43 +05:30
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: nil } }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
it 'triggers a pipeline' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect { result }.to change { Ci::Pipeline.count }.by(1)
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(result[:pipeline].ref).to eq('master')
|
|
|
|
expect(result[:pipeline].project).to eq(project)
|
|
|
|
expect(result[:pipeline].user).to eq(trigger.owner)
|
|
|
|
expect(result[:pipeline].trigger_requests.to_a)
|
|
|
|
.to eq(result[:pipeline].builds.map(&:trigger_request).uniq)
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'stores the payload as a variable' do
|
|
|
|
expect { result }.to change { Ci::PipelineVariable.count }.by(1)
|
|
|
|
|
|
|
|
var = result[:pipeline].variables.first
|
|
|
|
|
|
|
|
expect(var.key).to eq('TRIGGER_PAYLOAD')
|
|
|
|
expect(var.value).to eq('{"ref":"master","variables":null}')
|
|
|
|
expect(var.variable_type).to eq('file')
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when commit message has [ci skip]' do
|
|
|
|
before do
|
2021-03-08 18:12:59 +05:30
|
|
|
allow_next(Ci::Pipeline).to receive(:git_commit_message) { '[ci skip]' }
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores [ci skip] and create as general' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }.by(1)
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(result).to be_success
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params have a variable' do
|
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: variables } }
|
|
|
|
let(:variables) { { 'AAA' => 'AAA123' } }
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'has variables' do
|
|
|
|
expect { result }.to change { Ci::PipelineVariable.count }.by(2)
|
2019-12-21 20:55:43 +05:30
|
|
|
.and change { Ci::TriggerRequest.count }.by(1)
|
|
|
|
expect(result[:pipeline].variables.map { |v| { v.key => v.value } }.first).to eq(variables)
|
|
|
|
expect(result[:pipeline].trigger_requests.last.variables).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when params have duplicate variables' do
|
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: variables } }
|
|
|
|
let(:variables) { { 'TRIGGER_PAYLOAD' => 'duplicate value' } }
|
|
|
|
|
|
|
|
it 'creates a failed pipeline without variables' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }
|
|
|
|
expect(result).to be_error
|
|
|
|
expect(result.message[:base]).to eq(['Duplicate variable name: TRIGGER_PAYLOAD'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'detecting an unprocessable pipeline trigger'
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when params have a non-existant ref' do
|
2019-12-21 20:55:43 +05:30
|
|
|
let(:params) { { token: trigger.token, ref: 'invalid-ref', variables: nil } }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
it 'does not trigger a pipeline' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(result).to be_error
|
|
|
|
expect(result.http_status).to eq(:bad_request)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when params have a non-existant trigger token' do
|
2019-12-21 20:55:43 +05:30
|
|
|
let(:params) { { token: 'invalid-token', ref: nil, variables: nil } }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'does not trigger a pipeline' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(result).to be_nil
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'with a pipeline job token' do
|
|
|
|
let!(:pipeline) { create(:ci_empty_pipeline, project: project) }
|
|
|
|
let(:job) { create(:ci_build, :running, pipeline: pipeline, user: user) }
|
|
|
|
|
|
|
|
context 'when job user does not have a permission to read a project' do
|
|
|
|
let(:params) { { token: job.token, ref: 'master', variables: nil } }
|
|
|
|
let(:job) { create(:ci_build, pipeline: pipeline, user: create(:user)) }
|
|
|
|
|
|
|
|
it 'does nothing' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when job is not running' do
|
|
|
|
let(:params) { { token: job.token, ref: 'master', variables: nil } }
|
|
|
|
let(:job) { create(:ci_build, :success, pipeline: pipeline, user: user) }
|
|
|
|
|
2020-09-03 11:15:55 +05:30
|
|
|
it 'does nothing', :aggregate_failures do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
|
|
|
expect(result[:message]).to eq('Job is not running')
|
|
|
|
expect(result[:http_status]).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when job does not have a project' do
|
|
|
|
let(:params) { { token: job.token, ref: 'master', variables: nil } }
|
|
|
|
let(:job) { create(:ci_build, status: :running, pipeline: pipeline, user: user) }
|
|
|
|
|
|
|
|
it 'does nothing', :aggregate_failures do
|
|
|
|
job.update!(project: nil)
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
2020-09-03 11:15:55 +05:30
|
|
|
expect(result[:message]).to eq('Project has been deleted!')
|
|
|
|
expect(result[:http_status]).to eq(401)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when params have an existsed job token' do
|
|
|
|
context 'when params have an existsed ref' do
|
|
|
|
let(:params) { { token: job.token, ref: 'master', variables: nil } }
|
|
|
|
|
|
|
|
it 'triggers a pipeline' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }.by(1)
|
|
|
|
expect(result[:pipeline].ref).to eq('master')
|
|
|
|
expect(result[:pipeline].project).to eq(project)
|
|
|
|
expect(result[:pipeline].user).to eq(job.user)
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
it_behaves_like 'logs downstream pipeline creation' do
|
2023-03-04 22:38:38 +05:30
|
|
|
let(:downstream_pipeline) { result[:pipeline] }
|
2022-07-16 23:28:13 +05:30
|
|
|
let(:expected_root_pipeline) { pipeline }
|
|
|
|
let(:expected_hierarchy_size) { 2 }
|
|
|
|
let(:expected_downstream_relationship) { :multi_project }
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when commit message has [ci skip]' do
|
|
|
|
before do
|
2020-01-01 13:55:28 +05:30
|
|
|
allow_next_instance_of(Ci::Pipeline) do |instance|
|
|
|
|
allow(instance).to receive(:git_commit_message) { '[ci skip]' }
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores [ci skip] and create as general' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }.by(1)
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params have a variable' do
|
|
|
|
let(:params) { { token: job.token, ref: 'master', variables: variables } }
|
|
|
|
let(:variables) { { 'AAA' => 'AAA123' } }
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'has variables' do
|
|
|
|
expect { result }.to change { Ci::PipelineVariable.count }.by(2)
|
2019-12-21 20:55:43 +05:30
|
|
|
.and change { Ci::Sources::Pipeline.count }.by(1)
|
|
|
|
expect(result[:pipeline].variables.map { |v| { v.key => v.value } }.first).to eq(variables)
|
|
|
|
expect(job.sourced_pipelines.last.pipeline_id).to eq(result[:pipeline].id)
|
|
|
|
end
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when params have duplicate variables' do
|
|
|
|
let(:params) { { token: job.token, ref: 'master', variables: variables } }
|
|
|
|
let(:variables) { { 'TRIGGER_PAYLOAD' => 'duplicate value' } }
|
|
|
|
|
|
|
|
it 'creates a failed pipeline without variables' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }
|
|
|
|
expect(result).to be_error
|
|
|
|
expect(result.message[:base]).to eq(['Duplicate variable name: TRIGGER_PAYLOAD'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'detecting an unprocessable pipeline trigger'
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when params have a non-existant ref' do
|
2019-12-21 20:55:43 +05:30
|
|
|
let(:params) { { token: job.token, ref: 'invalid-ref', variables: nil } }
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'does not trigger a job in the pipeline' do
|
2019-12-21 20:55:43 +05:30
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(result).to be_error
|
|
|
|
expect(result.http_status).to eq(:bad_request)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params have a non-existsed trigger token' do
|
|
|
|
let(:params) { { token: 'invalid-token', ref: nil, variables: nil } }
|
|
|
|
|
|
|
|
it 'does not trigger a pipeline' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|