triggers.rb 3.07 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1 2
module API
  class Triggers < Grape::API
Robert Schilling's avatar
Robert Schilling committed
3 4 5
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
6
    resource :projects do
Robert Schilling's avatar
Robert Schilling committed
7 8 9 10 11 12 13 14
      desc 'Trigger a GitLab project build' do
        success 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
15
      post ":id/(ref/:ref/)trigger/builds" do
16
        project = find_project(params[:id])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        trigger = Ci::Trigger.find_by_token(params[:token].to_s)
        not_found! unless project && trigger
        unauthorized! unless trigger.project == project

        # validate variables
        variables = params[:variables]
        if variables
          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)
          end

          # convert variables from Mash to Hash
          variables = variables.to_h
        end

        # create request and trigger builds
        trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref].to_s, variables)
        if trigger_request
          present trigger_request, with: Entities::TriggerRequest
        else
          errors = 'No builds created'
          render_api_error!(errors, 400)
        end
      end
41

Robert Schilling's avatar
Robert Schilling committed
42 43 44
      desc 'Get triggers list' do
        success Entities::Trigger
      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's avatar
Robert Schilling committed
51
        present paginate(triggers), with: Entities::Trigger
52
      end
53

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

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

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

Robert Schilling's avatar
Robert Schilling committed
70 71 72
      desc 'Create a trigger' do
        success Entities::Trigger
      end
73 74
      post ':id/triggers' do
        authenticate!
75
        authorize! :admin_build, user_project
76

77
        trigger = user_project.triggers.create
78 79 80 81

        present trigger, with: Entities::Trigger
      end

Robert Schilling's avatar
Robert Schilling committed
82 83 84 85 86 87
      desc 'Delete a trigger' do
        success Entities::Trigger
      end
      params do
        requires :token, type: String, desc: 'The unique token of trigger'
      end
88
      delete ':id/triggers/:token' do
89
        authenticate!
90
        authorize! :admin_build, user_project
91

92
        trigger = user_project.triggers.find_by(token: params[:token].to_s)
93 94 95 96 97 98
        return not_found!('Trigger') unless trigger

        trigger.destroy

        present trigger, with: Entities::Trigger
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
99 100 101
    end
  end
end