BigW Consortium Gitlab

triggers.rb 4.45 KB
Newer Older
1 2
module API
  class Triggers < Grape::API
3 4
    include PaginationParams

Robert Schilling committed
5 6 7
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
8
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS  do
Kamil Trzcinski committed
9
      desc 'Trigger a GitLab project pipeline' do
10
        success Entities::Pipeline
Robert Schilling committed
11 12 13 14 15 16
      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
17
      post ":id/(ref/:ref/)trigger/pipeline", requirements: { ref: /.+/ } do
18
        # validate variables
Shinya Maeda committed
19 20
        params[:variables] = params[:variables].to_h
        unless params[:variables].all? { |key, value| key.is_a?(String) && value.is_a?(String) }
21
          render_api_error!('variables needs to be a map of key-valued strings', 400)
22 23
        end

Shinya Maeda committed
24 25 26 27 28
        project = find_project(params[:id])
        not_found! unless project

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

Shinya Maeda committed
30 31
        if result[:http_status]
          render_api_error!(result[:message], result[:http_status])
32
        else
Shinya Maeda committed
33
          present result[:pipeline], with: Entities::Pipeline
34 35
        end
      end
36

Robert Schilling committed
37 38 39
      desc 'Get triggers list' do
        success Entities::Trigger
      end
40 41 42
      params do
        use :pagination
      end
43 44
      get ':id/triggers' do
        authenticate!
45
        authorize! :admin_build, user_project
46 47 48

        triggers = user_project.triggers.includes(:trigger_requests)

Robert Schilling committed
49
        present paginate(triggers), with: Entities::Trigger
50
      end
51

Robert Schilling committed
52 53 54 55
      desc 'Get specific trigger of a project' do
        success Entities::Trigger
      end
      params do
Kamil Trzcinski committed
56
        requires :trigger_id, type: Integer,  desc: 'The trigger ID'
Robert Schilling committed
57
      end
Kamil Trzcinski committed
58
      get ':id/triggers/:trigger_id' do
59
        authenticate!
60
        authorize! :admin_build, user_project
61

62
        trigger = user_project.triggers.find(params.delete(:trigger_id))
63
        return not_found!('Trigger') unless trigger
64

65
        present trigger, with: Entities::Trigger
66 67
      end

Robert Schilling committed
68 69 70
      desc 'Create a trigger' do
        success Entities::Trigger
      end
Kamil Trzcinski committed
71 72 73
      params do
        requires :description, type: String,  desc: 'The trigger description'
      end
74 75
      post ':id/triggers' do
        authenticate!
76
        authorize! :admin_build, user_project
77

Kamil Trzcinski committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        trigger = user_project.triggers.create(
          declared_params(include_missing: false).merge(owner: current_user))

        if trigger.valid?
          present trigger, with: Entities::Trigger
        else
          render_validation_error!(trigger)
        end
      end

      desc 'Update a trigger' do
        success Entities::Trigger
      end
      params do
        requires :trigger_id, type: Integer,  desc: 'The trigger ID'
        optional :description, type: String,  desc: 'The trigger description'
      end
95
      put ':id/triggers/:trigger_id' do
Kamil Trzcinski committed
96 97 98
        authenticate!
        authorize! :admin_build, user_project

99
        trigger = user_project.triggers.find(params.delete(:trigger_id))
Kamil Trzcinski committed
100
        return not_found!('Trigger') unless trigger
101

102 103 104 105 106
        if trigger.update(declared_params(include_missing: false))
          present trigger, with: Entities::Trigger
        else
          render_validation_error!(trigger)
        end
107 108
      end

Kamil Trzcinski committed
109 110 111 112 113 114
      desc 'Take ownership of trigger' do
        success Entities::Trigger
      end
      params do
        requires :trigger_id, type: Integer,  desc: 'The trigger ID'
      end
115
      post ':id/triggers/:trigger_id/take_ownership' do
Kamil Trzcinski committed
116 117 118
        authenticate!
        authorize! :admin_build, user_project

119
        trigger = user_project.triggers.find(params.delete(:trigger_id))
Kamil Trzcinski committed
120 121 122
        return not_found!('Trigger') unless trigger

        if trigger.update(owner: current_user)
123
          status :ok
Kamil Trzcinski committed
124 125 126 127 128 129
          present trigger, with: Entities::Trigger
        else
          render_validation_error!(trigger)
        end
      end

Robert Schilling committed
130 131 132 133
      desc 'Delete a trigger' do
        success Entities::Trigger
      end
      params do
Kamil Trzcinski committed
134
        requires :trigger_id, type: Integer,  desc: 'The trigger ID'
Robert Schilling committed
135
      end
Kamil Trzcinski committed
136
      delete ':id/triggers/:trigger_id' do
137
        authenticate!
138
        authorize! :admin_build, user_project
139

140
        trigger = user_project.triggers.find(params.delete(:trigger_id))
141 142
        return not_found!('Trigger') unless trigger

143
        destroy_conditionally!(trigger)
144
      end
145 146 147
    end
  end
end