Commit 1dd712ddc238d2e6c30be09cb071c8e9b60cfcac

Authored by Matt Humphrey
1 parent d03af842

System hooks API.

doc/api/system_hooks.md 0 → 100644
... ... @@ -0,0 +1,47 @@
  1 +All methods require admin authorization.
  2 +
  3 +## List system hooks
  4 +
  5 +Get list of system hooks
  6 +
  7 +```
  8 +GET /hooks
  9 +```
  10 +
  11 +Will return hooks with status `200 OK` on success, or `404 Not found` on fail.
  12 +
  13 +## Add new system hook hook
  14 +
  15 +```
  16 +POST /hooks
  17 +```
  18 +
  19 +Parameters:
  20 +
  21 ++ `url` (required) - The hook URL
  22 +
  23 +Will return status `201 Created` on success, or `404 Not found` on fail.
  24 +
  25 +## Test system hook
  26 +
  27 +```
  28 +GET /hooks/:id
  29 +```
  30 +
  31 +Parameters:
  32 +
  33 ++ `id` (required) - The ID of hook
  34 +
  35 +Will return hook with status `200 OK` on success, or `404 Not found` on fail.
  36 +
  37 +## Delete system hook
  38 +
  39 +```
  40 +DELETE /hooks/:id
  41 +```
  42 +
  43 +Parameters:
  44 +
  45 ++ `id` (required) - The ID of hook
  46 +
  47 +Will return status `200 OK` on success, or `404 Not found` on fail.
0 48 \ No newline at end of file
... ...
lib/api.rb
... ... @@ -20,5 +20,6 @@ module Gitlab
20 20 mount MergeRequests
21 21 mount Notes
22 22 mount Internal
  23 + mount SystemHooks
23 24 end
24 25 end
... ...
lib/api/system_hooks.rb 0 → 100644
... ... @@ -0,0 +1,60 @@
  1 +module Gitlab
  2 + # Hooks API
  3 + class SystemHooks < Grape::API
  4 + before { authenticated_as_admin! }
  5 +
  6 + resource :hooks do
  7 + # Get the list of system hooks
  8 + #
  9 + # Example Request:
  10 + # GET /hooks
  11 + get do
  12 + @hooks = SystemHook.all
  13 + present @hooks, with: Entities::Hook
  14 + end
  15 +
  16 + # Create new system hook
  17 + #
  18 + # Parameters:
  19 + # url (required) - url for system hook
  20 + # Example Request
  21 + # POST /hooks
  22 + post do
  23 + attrs = attributes_for_keys [:url]
  24 + @hook = SystemHook.new attrs
  25 + if @hook.save
  26 + present @hook, with: Entities::Hook
  27 + else
  28 + not_found!
  29 + end
  30 + end
  31 +
  32 + # Test a hook
  33 + #
  34 + # Example Request
  35 + # GET /hooks/:id
  36 + get ":id" do
  37 + @hook = SystemHook.find(params[:id])
  38 + data = {
  39 + event_name: "project_create",
  40 + name: "Ruby",
  41 + path: "ruby",
  42 + project_id: 1,
  43 + owner_name: "Someone",
  44 + owner_email: "example@gitlabhq.com"
  45 + }
  46 + @hook.execute(data)
  47 + data
  48 + end
  49 +
  50 + # Delete a hook
  51 + #
  52 + # Example Request:
  53 + # DELETE /hooks/:id
  54 + delete ":id" do
  55 + @hook = SystemHook.find(params[:id])
  56 + @hook.destroy
  57 + end
  58 + end
  59 + end
  60 +end
0 61 \ No newline at end of file
... ...
spec/requests/api/system_hooks_spec.rb 0 → 100644
... ... @@ -0,0 +1,69 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Gitlab::API do
  4 + include ApiHelpers
  5 +
  6 + let(:user) { create(:user) }
  7 + let(:admin) { create(:admin) }
  8 + let!(:hook) { create(:system_hook, url: "http://example.com") }
  9 +
  10 + before { stub_request(:post, hook.url) }
  11 +
  12 + describe "GET /hooks" do
  13 + context "when not an admin" do
  14 + it "should return forbidden error" do
  15 + get api("/hooks", user)
  16 + response.status.should == 403
  17 + end
  18 + end
  19 +
  20 + context "when authenticated as admin" do
  21 + it "should return an array of hooks" do
  22 + get api("/hooks", admin)
  23 + response.status.should == 200
  24 + json_response.should be_an Array
  25 + json_response.first['url'].should == hook.url
  26 + end
  27 + end
  28 + end
  29 +
  30 + describe "POST /hooks" do
  31 + it "should create new hook" do
  32 + expect {
  33 + post api("/hooks", admin), url: 'http://example.com'
  34 + }.to change { SystemHook.count }.by(1)
  35 + end
  36 +
  37 + it "should respond with 404 on failure" do
  38 + post api("/hooks", admin)
  39 + response.status.should == 404
  40 + end
  41 +
  42 + it "should not create new hook without url" do
  43 + expect {
  44 + post api("/hooks", admin)
  45 + }.to_not change { SystemHook.count }
  46 + end
  47 + end
  48 +
  49 + describe "GET /hooks/:id" do
  50 + it "should return hook by id" do
  51 + get api("/hooks/#{hook.id}", admin)
  52 + response.status.should == 200
  53 + json_response['event_name'].should == 'project_create'
  54 + end
  55 +
  56 + it "should return 404 on failure" do
  57 + get api("/hooks/404", admin)
  58 + response.status.should == 404
  59 + end
  60 + end
  61 +
  62 + describe "DELETE /hooks/:id" do
  63 + it "should delete a hook" do
  64 + expect {
  65 + delete api("/hooks/#{hook.id}", admin)
  66 + }.to change { SystemHook.count }.by(-1)
  67 + end
  68 + end
  69 +end
0 70 \ No newline at end of file
... ...