BigW Consortium Gitlab

system_hooks.rb 2.04 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
        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"
29
        optional :merge_requests_events, type: Boolean, desc: "Trigger hook on tag push events"
30
        optional :enable_ssl_verification, type: Boolean, desc: "Do SSL verification when triggering the hook"
31
      end
32
      post do
33
        hook = SystemHook.new(declared_params(include_missing: false))
34 35 36

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

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

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

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