BigW Consortium Gitlab

triggers.rb 4.6 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: { id: %r{[^/]+} } 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
        project = find_project(params[:id])
19 20 21 22 23
        trigger = Ci::Trigger.find_by_token(params[:token].to_s)
        not_found! unless project && trigger
        unauthorized! unless trigger.project == project

        # validate variables
24 25 26
        variables = params[:variables].to_h
        unless 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)
27 28 29 30 31
        end

        # create request and trigger builds
        trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref].to_s, variables)
        if trigger_request
Kamil Trzcinski committed
32
          present trigger_request.pipeline, with: Entities::Pipeline
33
        else
34
          errors = 'No pipeline created'
35 36 37
          render_api_error!(errors, 400)
        end
      end
38

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

        triggers = user_project.triggers.includes(:trigger_requests)

Robert Schilling committed
51
        present paginate(triggers), with: Entities::Trigger
52
      end
53

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

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

67
        present trigger, with: Entities::Trigger
68 69
      end

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

Kamil Trzcinski committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        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
97
      put ':id/triggers/:trigger_id' do
Kamil Trzcinski committed
98 99 100
        authenticate!
        authorize! :admin_build, user_project

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

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

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

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

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

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

142
        trigger = user_project.triggers.find(params.delete(:trigger_id))
143 144 145 146
        return not_found!('Trigger') unless trigger

        trigger.destroy
      end
147 148 149
    end
  end
end