2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-26 05:31:09 -04:00
|
|
|
module Ci
|
|
|
|
class PipelineTriggerService < BaseService
|
2017-11-13 10:27:30 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2021-05-01 17:09:45 -04:00
|
|
|
include Services::ReturnServiceResponses
|
2022-04-21 08:10:16 -04:00
|
|
|
include Ci::DownstreamPipelineHelpers
|
2017-11-13 10:27:30 -05:00
|
|
|
|
2017-07-26 05:31:09 -04:00
|
|
|
def execute
|
|
|
|
if trigger_from_token
|
2021-03-29 11:09:30 -04:00
|
|
|
set_application_context_from_trigger(trigger_from_token)
|
2017-07-26 05:31:09 -04:00
|
|
|
create_pipeline_from_trigger(trigger_from_token)
|
2019-02-01 08:58:30 -05:00
|
|
|
elsif job_from_token
|
2021-03-29 11:09:30 -04:00
|
|
|
set_application_context_from_job(job_from_token)
|
2019-02-01 08:58:30 -05:00
|
|
|
create_pipeline_from_job(job_from_token)
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
2020-09-02 11:10:54 -04:00
|
|
|
|
|
|
|
rescue Ci::AuthJobFinder::AuthError => e
|
|
|
|
error(e.message, 401)
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-16 07:09:03 -05:00
|
|
|
PAYLOAD_VARIABLE_KEY = 'TRIGGER_PAYLOAD'
|
2021-05-01 17:09:45 -04:00
|
|
|
PAYLOAD_VARIABLE_HIDDEN_PARAMS = %i[token].freeze
|
2021-02-16 07:09:03 -05:00
|
|
|
|
2017-07-26 05:31:09 -04:00
|
|
|
def create_pipeline_from_trigger(trigger)
|
|
|
|
# this check is to not leak the presence of the project if user cannot read it
|
|
|
|
return unless trigger.project == project
|
2022-06-01 17:08:14 -04:00
|
|
|
return unless can?(trigger.owner, :read_project, project)
|
2017-07-26 05:31:09 -04:00
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
response = Ci::CreatePipelineService
|
2021-01-14 07:10:54 -05:00
|
|
|
.new(project, trigger.owner, ref: params[:ref], variables_attributes: variables)
|
2017-07-26 05:31:09 -04:00
|
|
|
.execute(:trigger, ignore_skip_ci: true) do |pipeline|
|
2018-03-21 05:59:11 -04:00
|
|
|
pipeline.trigger_requests.build(trigger: trigger)
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
pipeline_service_response(response.payload)
|
2021-05-01 17:09:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_service_response(pipeline)
|
|
|
|
if pipeline.created_successfully?
|
2017-07-26 05:31:09 -04:00
|
|
|
success(pipeline: pipeline)
|
2021-05-01 17:09:45 -04:00
|
|
|
elsif pipeline.persisted?
|
|
|
|
err = pipeline.errors.messages.presence || pipeline.failure_reason.presence || 'Could not create pipeline'
|
|
|
|
error(err, :unprocessable_entity)
|
2017-07-26 05:31:09 -04:00
|
|
|
else
|
2021-05-01 17:09:45 -04:00
|
|
|
error(pipeline.errors.messages, :bad_request)
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def trigger_from_token
|
2017-11-13 10:27:30 -05:00
|
|
|
strong_memoize(:trigger) do
|
|
|
|
Ci::Trigger.find_by_token(params[:token].to_s)
|
|
|
|
end
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
|
|
|
|
2019-02-01 08:58:30 -05:00
|
|
|
def create_pipeline_from_job(job)
|
2019-10-16 05:07:51 -04:00
|
|
|
# this check is to not leak the presence of the project if user cannot read it
|
|
|
|
return unless can?(job.user, :read_project, project)
|
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
response = Ci::CreatePipelineService
|
2021-01-14 07:10:54 -05:00
|
|
|
.new(project, job.user, ref: params[:ref], variables_attributes: variables)
|
2020-01-08 07:07:59 -05:00
|
|
|
.execute(:pipeline, ignore_skip_ci: true) do |pipeline|
|
2019-10-16 05:07:51 -04:00
|
|
|
source = job.sourced_pipelines.build(
|
|
|
|
source_pipeline: job.pipeline,
|
|
|
|
source_project: job.project,
|
|
|
|
pipeline: pipeline,
|
|
|
|
project: project)
|
|
|
|
|
|
|
|
pipeline.source_pipeline = source
|
|
|
|
end
|
|
|
|
|
2022-04-21 08:10:16 -04:00
|
|
|
log_downstream_pipeline_creation(response.payload)
|
2021-07-20 08:10:29 -04:00
|
|
|
pipeline_service_response(response.payload)
|
2019-02-01 08:58:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def job_from_token
|
2019-10-16 05:07:51 -04:00
|
|
|
strong_memoize(:job) do
|
2020-09-02 11:10:54 -04:00
|
|
|
Ci::AuthJobFinder.new(token: params[:token].to_s).execute!
|
2019-10-16 05:07:51 -04:00
|
|
|
end
|
2019-02-01 08:58:30 -05:00
|
|
|
end
|
|
|
|
|
2018-03-21 05:59:11 -04:00
|
|
|
def variables
|
2021-03-23 20:09:26 -04:00
|
|
|
param_variables + [payload_variable]
|
2021-02-16 07:09:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def param_variables
|
2018-03-21 05:59:11 -04:00
|
|
|
params[:variables].to_h.map do |key, value|
|
2017-07-26 05:31:09 -04:00
|
|
|
{ key: key, value: value }
|
|
|
|
end
|
|
|
|
end
|
2021-02-16 07:09:03 -05:00
|
|
|
|
|
|
|
def payload_variable
|
|
|
|
{ key: PAYLOAD_VARIABLE_KEY,
|
|
|
|
value: params.except(*PAYLOAD_VARIABLE_HIDDEN_PARAMS).to_json,
|
|
|
|
variable_type: :file }
|
|
|
|
end
|
2021-03-29 11:09:30 -04:00
|
|
|
|
|
|
|
def set_application_context_from_trigger(trigger)
|
|
|
|
Gitlab::ApplicationContext.push(
|
|
|
|
user: trigger.owner,
|
|
|
|
project: trigger.project
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_application_context_from_job(job)
|
|
|
|
Gitlab::ApplicationContext.push(
|
|
|
|
user: job.user,
|
|
|
|
project: job.project,
|
|
|
|
runner: job.runner
|
|
|
|
)
|
|
|
|
end
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
|
|
|
end
|