pipeline_trigger_service.rb 1.09 KB
Newer Older
1 2
# frozen_string_literal: true

Shinya Maeda's avatar
init  
Shinya Maeda committed
3 4
module Ci
  class PipelineTriggerService < BaseService
5 6
    include Gitlab::Utils::StrongMemoize

Shinya Maeda's avatar
init  
Shinya Maeda committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
    def execute
      if trigger_from_token
        create_pipeline_from_trigger(trigger_from_token)
      end
    end

    private

    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

      pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: params[:ref])
        .execute(:trigger, ignore_skip_ci: true) do |pipeline|
21 22
          pipeline.trigger_requests.build(trigger: trigger)
          pipeline.variables.build(variables)
Shinya Maeda's avatar
init  
Shinya Maeda committed
23 24 25 26 27 28 29 30 31 32
        end

      if pipeline.persisted?
        success(pipeline: pipeline)
      else
        error(pipeline.errors.messages, 400)
      end
    end

    def trigger_from_token
33 34 35
      strong_memoize(:trigger) do
        Ci::Trigger.find_by_token(params[:token].to_s)
      end
Shinya Maeda's avatar
init  
Shinya Maeda committed
36 37
    end

38 39
    def variables
      params[:variables].to_h.map do |key, value|
Shinya Maeda's avatar
init  
Shinya Maeda committed
40 41 42 43 44
        { key: key, value: value }
      end
    end
  end
end