BigW Consortium Gitlab

triggers.rb 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
module API
  # Triggers API
  class Triggers < Grape::API
    resource :projects do
      # Trigger a GitLab project build
      #
      # Parameters:
      #   id (required) - The ID of a CI project
      #   ref (required) - The name of project's branch or tag
      #   token (required) - The uniq token of trigger
      #   variables (optional) - The list of variables to be injected into build
      # Example Request:
      #   POST /projects/:id/trigger/builds
      post ":id/trigger/builds" do
        required_attributes! [:ref, :token]

Kamil Trzcinski committed
17
        project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
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
        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.is_a?(Hash)
            render_api_error!('variables needs to be a hash', 400)
          end

          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
46 47 48 49 50 51 52 53 54 55 56

      # Get triggers list
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   page (optional) - The page number for pagination
      #   per_page (optional) - The value of items per page to show
      # Example Request:
      #   GET /projects/:id/triggers
      get ':id/triggers' do
        authenticate!
57
        authorize! :admin_build, user_project
58 59 60 61 62 63

        triggers = user_project.triggers.includes(:trigger_requests)
        triggers = paginate(triggers)

        present triggers, with: Entities::Trigger
      end
64

65 66 67 68
      # Get specific trigger of a project
      #
      # Parameters:
      #   id (required) - The ID of a project
69
      #   token (required) - The `token` of a trigger
70
      # Example Request:
71 72
      #   GET /projects/:id/triggers/:token
      get ':id/triggers/:token' do
73
        authenticate!
74
        authorize! :admin_build, user_project
75

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

79
        present trigger, with: Entities::Trigger
80 81
      end

82 83 84 85 86 87 88 89
      # Create trigger
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   POST /projects/:id/triggers
      post ':id/triggers' do
        authenticate!
90
        authorize! :admin_build, user_project
91

92
        trigger = user_project.triggers.create
93 94 95 96

        present trigger, with: Entities::Trigger
      end

97 98 99 100
      # Delete trigger
      #
      # Parameters:
      #   id (required) - The ID of a project
101
      #   token (required) - The `token` of a trigger
102
      # Example Request:
103 104
      #   DELETE /projects/:id/triggers/:token
      delete ':id/triggers/:token' do
105
        authenticate!
106
        authorize! :admin_build, user_project
107

108
        trigger = user_project.triggers.find_by(token: params[:token].to_s)
109 110 111 112 113 114
        return not_found!('Trigger') unless trigger

        trigger.destroy

        present trigger, with: Entities::Trigger
      end
115 116 117
    end
  end
end