summaryrefslogtreecommitdiff
path: root/lib/api/v3/triggers.rb
blob: 534911fde5c8cdbb833acbc6852e3d709717a38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module API
  module V3
    class Triggers < Grape::API
      include PaginationParams

      params do
        requires :id, type: String, desc: 'The ID of a project'
      end
      resource :projects, requirements: { id: %r{[^/]+} } do
        desc 'Trigger a GitLab project build' do
          success ::API::V3::Entities::TriggerRequest
        end
        params do
          requires :ref, type: String, desc: 'The commit sha or name of a branch or tag'
          requires :token, type: String, desc: 'The unique token of trigger'
          optional :variables, type: Hash, desc: 'The list of variables to be injected into build'
        end
        post ":id/(ref/:ref/)trigger/builds", requirements: { ref: /.+/ } do
          # validate variables
          params[:variables] = params[:variables].to_h
          unless params[:variables].all? { |key, value| key.is_a?(String) && value.is_a?(String) }
            render_api_error!('variables needs to be a map of key-valued strings', 400)
          end

          project = find_project(params[:id])
          not_found! unless project

          result = Ci::PipelineTriggerService.new(project, nil, params).execute
          not_found! unless result

          if result[:http_status]
            render_api_error!(result[:message], result[:http_status])
          else
            pipeline = result[:pipeline]

            # We switched to Ci::PipelineVariable from Ci::TriggerRequest.variables.
            # Ci::TriggerRequest doesn't save variables anymore.
            # Here is copying Ci::PipelineVariable to Ci::TriggerRequest.variables for presenting the variables.
            # The same endpoint in v4 API pressents Pipeline instead of TriggerRequest, so it doesn't need such a process.
            trigger_request = pipeline.trigger_requests.last
            trigger_request.variables = params[:variables]

            present trigger_request, with: ::API::V3::Entities::TriggerRequest
          end
        end

        desc 'Get triggers list' do
          success ::API::V3::Entities::Trigger
        end
        params do
          use :pagination
        end
        get ':id/triggers' do
          authenticate!
          authorize! :admin_build, user_project

          triggers = user_project.triggers.includes(:trigger_requests)

          present paginate(triggers), with: ::API::V3::Entities::Trigger
        end

        desc 'Get specific trigger of a project' do
          success ::API::V3::Entities::Trigger
        end
        params do
          requires :token, type: String, desc: 'The unique token of trigger'
        end
        get ':id/triggers/:token' do
          authenticate!
          authorize! :admin_build, user_project

          trigger = user_project.triggers.find_by(token: params[:token].to_s)
          return not_found!('Trigger') unless trigger

          present trigger, with: ::API::V3::Entities::Trigger
        end

        desc 'Create a trigger' do
          success ::API::V3::Entities::Trigger
        end
        post ':id/triggers' do
          authenticate!
          authorize! :admin_build, user_project

          trigger = user_project.triggers.create

          present trigger, with: ::API::V3::Entities::Trigger
        end

        desc 'Delete a trigger' do
          success ::API::V3::Entities::Trigger
        end
        params do
          requires :token, type: String, desc: 'The unique token of trigger'
        end
        delete ':id/triggers/:token' do
          authenticate!
          authorize! :admin_build, user_project

          trigger = user_project.triggers.find_by(token: params[:token].to_s)
          return not_found!('Trigger') unless trigger

          trigger.destroy

          present trigger, with: ::API::V3::Entities::Trigger
        end
      end
    end
  end
end