BigW Consortium Gitlab

system_hooks.rb 1.95 KB
Newer Older
1
module API
2
  class SystemHooks < Grape::API
3 4
    include PaginationParams

5
    before do
6 7
      authenticate!
      authenticated_as_admin!
8
    end
9 10

    resource :hooks do
11 12 13
      desc 'Get the list of system hooks' do
        success Entities::Hook
      end
14 15 16
      params do
        use :pagination
      end
17
      get do
18
        present paginate(SystemHook.all), with: Entities::Hook
19 20
      end

21 22 23 24
      desc 'Create a new system hook' do
        success Entities::Hook
      end
      params do
25 26 27 28 29
        requires :url, type: String, desc: "The URL to send the request to"
        optional :token, type: String, desc: 'The token used to validate payloads'
        optional :push_events, type: Boolean, desc: "Trigger hook on push events"
        optional :tag_push_events, type: Boolean, desc: "Trigger hook on tag push events"
        optional :enable_ssl_verification, type: Boolean, desc: "Do SSL verification when triggering the hook"
30
      end
31
      post do
32
        hook = SystemHook.new(declared_params(include_missing: false))
33 34 35

        if hook.save
          present hook, with: Entities::Hook
36
        else
37
          render_validation_error!(hook)
38 39 40
        end
      end

41 42 43 44
      desc 'Test a hook'
      params do
        requires :id, type: Integer, desc: 'The ID of the system hook'
      end
45
      get ":id" do
46
        hook = SystemHook.find(params[:id])
47 48 49 50 51 52 53 54
        data = {
          event_name: "project_create",
          name: "Ruby",
          path: "ruby",
          project_id: 1,
          owner_name: "Someone",
          owner_email: "example@gitlabhq.com"
        }
55
        hook.execute(data, 'system_hooks')
56 57 58
        data
      end

59 60 61 62 63 64
      desc 'Delete a hook' do
        success Entities::Hook
      end
      params do
        requires :id, type: Integer, desc: 'The ID of the system hook'
      end
65
      delete ":id" do
66 67 68
        hook = SystemHook.find_by(id: params[:id])
        not_found!('System hook') unless hook

69
        destroy_conditionally!(hook)
70 71 72 73
      end
    end
  end
end