debian-mirror-gitlab/app/services/ci/pipeline_trigger_service.rb

113 lines
3.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Ci
class PipelineTriggerService < BaseService
2018-03-17 18:26:18 +05:30
include Gitlab::Utils::StrongMemoize
2021-06-08 01:23:25 +05:30
include Services::ReturnServiceResponses
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
def execute
if trigger_from_token
2021-04-29 21:17:54 +05:30
set_application_context_from_trigger(trigger_from_token)
2017-09-10 17:25:29 +05:30
create_pipeline_from_trigger(trigger_from_token)
2019-03-02 22:35:43 +05:30
elsif job_from_token
2021-04-29 21:17:54 +05:30
set_application_context_from_job(job_from_token)
2019-03-02 22:35:43 +05:30
create_pipeline_from_job(job_from_token)
2017-09-10 17:25:29 +05:30
end
2020-09-03 11:15:55 +05:30
rescue Ci::AuthJobFinder::AuthError => e
error(e.message, 401)
2017-09-10 17:25:29 +05:30
end
private
2021-03-11 19:13:27 +05:30
PAYLOAD_VARIABLE_KEY = 'TRIGGER_PAYLOAD'
2021-06-08 01:23:25 +05:30
PAYLOAD_VARIABLE_HIDDEN_PARAMS = %i[token].freeze
2021-03-11 19:13:27 +05:30
2017-09-10 17:25:29 +05:30
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
2021-10-27 15:23:28 +05:30
response = Ci::CreatePipelineService
2021-03-08 18:12:59 +05:30
.new(project, trigger.owner, ref: params[:ref], variables_attributes: variables)
2017-09-10 17:25:29 +05:30
.execute(:trigger, ignore_skip_ci: true) do |pipeline|
2018-05-09 12:01:36 +05:30
pipeline.trigger_requests.build(trigger: trigger)
2017-09-10 17:25:29 +05:30
end
2021-10-27 15:23:28 +05:30
pipeline_service_response(response.payload)
2021-06-08 01:23:25 +05:30
end
def pipeline_service_response(pipeline)
if pipeline.created_successfully?
2017-09-10 17:25:29 +05:30
success(pipeline: pipeline)
2021-06-08 01:23:25 +05:30
elsif pipeline.persisted?
err = pipeline.errors.messages.presence || pipeline.failure_reason.presence || 'Could not create pipeline'
error(err, :unprocessable_entity)
2017-09-10 17:25:29 +05:30
else
2021-06-08 01:23:25 +05:30
error(pipeline.errors.messages, :bad_request)
2017-09-10 17:25:29 +05:30
end
end
def trigger_from_token
2018-03-17 18:26:18 +05:30
strong_memoize(:trigger) do
Ci::Trigger.find_by_token(params[:token].to_s)
end
2017-09-10 17:25:29 +05:30
end
2019-03-02 22:35:43 +05:30
def create_pipeline_from_job(job)
2019-12-21 20:55:43 +05:30
# 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-10-27 15:23:28 +05:30
response = Ci::CreatePipelineService
2021-03-08 18:12:59 +05:30
.new(project, job.user, ref: params[:ref], variables_attributes: variables)
2019-12-21 20:55:43 +05:30
.execute(:pipeline, ignore_skip_ci: true) do |pipeline|
source = job.sourced_pipelines.build(
source_pipeline: job.pipeline,
source_project: job.project,
pipeline: pipeline,
project: project)
pipeline.source_pipeline = source
end
2021-10-27 15:23:28 +05:30
pipeline_service_response(response.payload)
2019-03-02 22:35:43 +05:30
end
def job_from_token
2019-12-21 20:55:43 +05:30
strong_memoize(:job) do
2020-09-03 11:15:55 +05:30
Ci::AuthJobFinder.new(token: params[:token].to_s).execute!
2019-12-21 20:55:43 +05:30
end
2019-03-02 22:35:43 +05:30
end
2018-05-09 12:01:36 +05:30
def variables
2021-04-29 21:17:54 +05:30
param_variables + [payload_variable]
2021-03-11 19:13:27 +05:30
end
def param_variables
2018-05-09 12:01:36 +05:30
params[:variables].to_h.map do |key, value|
2017-09-10 17:25:29 +05:30
{ key: key, value: value }
end
end
2021-03-11 19:13:27 +05:30
def payload_variable
{ key: PAYLOAD_VARIABLE_KEY,
value: params.except(*PAYLOAD_VARIABLE_HIDDEN_PARAMS).to_json,
variable_type: :file }
end
2021-04-29 21:17:54 +05:30
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-09-10 17:25:29 +05:30
end
end