project_hooks.rb 3.3 KB
Newer Older
1 2 3 4
module API
  # Projects API
  class ProjectHooks < Grape::API
    before { authenticate! }
5
    before { authorize_admin_project }
6 7 8 9 10 11 12 13 14 15

    resource :projects do
      # Get project hooks
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   GET /projects/:id/hooks
      get ":id/hooks" do
        @hooks = paginate user_project.hooks
16
        present @hooks, with: Entities::ProjectHook
17 18 19 20 21 22 23 24 25 26 27
      end

      # Get a project hook
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   hook_id (required) - The ID of a project hook
      # Example Request:
      #   GET /projects/:id/hooks/:hook_id
      get ":id/hooks/:hook_id" do
        @hook = user_project.hooks.find(params[:hook_id])
28
        present @hook, with: Entities::ProjectHook
29 30 31 32 33 34 35 36 37 38 39
      end

      # Add hook to project
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   url (required) - The hook URL
      # Example Request:
      #   POST /projects/:id/hooks
      post ":id/hooks" do
        required_attributes! [:url]
40 41 42 43 44
        attrs = attributes_for_keys [
          :url,
          :push_events,
          :issues_events,
          :merge_requests_events,
S
Stan Hu 已提交
45
          :tag_push_events,
46
          :note_events,
47
          :build_events,
48
          :pipeline_events,
49
          :wiki_page_events,
50 51
          :enable_ssl_verification,
          :token
52
        ]
53
        @hook = user_project.hooks.new(attrs)
54 55

        if @hook.save
56
          present @hook, with: Entities::ProjectHook
57 58 59 60
        else
          if @hook.errors[:url].present?
            error!("Invalid url given", 422)
          end
61
          not_found!("Project hook #{@hook.errors.messages}")
62 63 64 65 66 67 68 69 70 71 72 73 74 75
        end
      end

      # Update an existing project hook
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   hook_id (required) - The ID of a project hook
      #   url (required) - The hook URL
      # Example Request:
      #   PUT /projects/:id/hooks/:hook_id
      put ":id/hooks/:hook_id" do
        @hook = user_project.hooks.find(params[:hook_id])
        required_attributes! [:url]
76 77 78 79 80
        attrs = attributes_for_keys [
          :url,
          :push_events,
          :issues_events,
          :merge_requests_events,
S
Stan Hu 已提交
81
          :tag_push_events,
82
          :note_events,
83
          :build_events,
84
          :pipeline_events,
85
          :wiki_page_events,
86 87
          :enable_ssl_verification,
          :token
88
        ]
89 90

        if @hook.update_attributes attrs
91
          present @hook, with: Entities::ProjectHook
92 93 94 95
        else
          if @hook.errors[:url].present?
            error!("Invalid url given", 422)
          end
96
          not_found!("Project hook #{@hook.errors.messages}")
97 98 99 100 101 102 103 104 105 106 107 108 109 110
        end
      end

      # Deletes project hook. This is an idempotent function.
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   hook_id (required) - The ID of hook to delete
      # Example Request:
      #   DELETE /projects/:id/hooks/:hook_id
      delete ":id/hooks/:hook_id" do
        required_attributes! [:hook_id]

        begin
111
          @hook = user_project.hooks.destroy(params[:hook_id])
112 113
        rescue
          # ProjectHook can raise Error if hook_id not found
114
          not_found!("Error deleting hook #{params[:hook_id]}")
115 116 117 118 119
        end
      end
    end
  end
end