System hooks API.
This commit is contained in:
parent
d03af842c5
commit
1dd712ddc2
4 changed files with 177 additions and 0 deletions
47
doc/api/system_hooks.md
Normal file
47
doc/api/system_hooks.md
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
All methods require admin authorization.
|
||||||
|
|
||||||
|
## List system hooks
|
||||||
|
|
||||||
|
Get list of system hooks
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /hooks
|
||||||
|
```
|
||||||
|
|
||||||
|
Will return hooks with status `200 OK` on success, or `404 Not found` on fail.
|
||||||
|
|
||||||
|
## Add new system hook hook
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /hooks
|
||||||
|
```
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
+ `url` (required) - The hook URL
|
||||||
|
|
||||||
|
Will return status `201 Created` on success, or `404 Not found` on fail.
|
||||||
|
|
||||||
|
## Test system hook
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /hooks/:id
|
||||||
|
```
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
+ `id` (required) - The ID of hook
|
||||||
|
|
||||||
|
Will return hook with status `200 OK` on success, or `404 Not found` on fail.
|
||||||
|
|
||||||
|
## Delete system hook
|
||||||
|
|
||||||
|
```
|
||||||
|
DELETE /hooks/:id
|
||||||
|
```
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
+ `id` (required) - The ID of hook
|
||||||
|
|
||||||
|
Will return status `200 OK` on success, or `404 Not found` on fail.
|
|
@ -20,5 +20,6 @@ module Gitlab
|
||||||
mount MergeRequests
|
mount MergeRequests
|
||||||
mount Notes
|
mount Notes
|
||||||
mount Internal
|
mount Internal
|
||||||
|
mount SystemHooks
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
60
lib/api/system_hooks.rb
Normal file
60
lib/api/system_hooks.rb
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
module Gitlab
|
||||||
|
# Hooks API
|
||||||
|
class SystemHooks < Grape::API
|
||||||
|
before { authenticated_as_admin! }
|
||||||
|
|
||||||
|
resource :hooks do
|
||||||
|
# Get the list of system hooks
|
||||||
|
#
|
||||||
|
# Example Request:
|
||||||
|
# GET /hooks
|
||||||
|
get do
|
||||||
|
@hooks = SystemHook.all
|
||||||
|
present @hooks, with: Entities::Hook
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create new system hook
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# url (required) - url for system hook
|
||||||
|
# Example Request
|
||||||
|
# POST /hooks
|
||||||
|
post do
|
||||||
|
attrs = attributes_for_keys [:url]
|
||||||
|
@hook = SystemHook.new attrs
|
||||||
|
if @hook.save
|
||||||
|
present @hook, with: Entities::Hook
|
||||||
|
else
|
||||||
|
not_found!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Test a hook
|
||||||
|
#
|
||||||
|
# Example Request
|
||||||
|
# GET /hooks/:id
|
||||||
|
get ":id" do
|
||||||
|
@hook = SystemHook.find(params[:id])
|
||||||
|
data = {
|
||||||
|
event_name: "project_create",
|
||||||
|
name: "Ruby",
|
||||||
|
path: "ruby",
|
||||||
|
project_id: 1,
|
||||||
|
owner_name: "Someone",
|
||||||
|
owner_email: "example@gitlabhq.com"
|
||||||
|
}
|
||||||
|
@hook.execute(data)
|
||||||
|
data
|
||||||
|
end
|
||||||
|
|
||||||
|
# Delete a hook
|
||||||
|
#
|
||||||
|
# Example Request:
|
||||||
|
# DELETE /hooks/:id
|
||||||
|
delete ":id" do
|
||||||
|
@hook = SystemHook.find(params[:id])
|
||||||
|
@hook.destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
69
spec/requests/api/system_hooks_spec.rb
Normal file
69
spec/requests/api/system_hooks_spec.rb
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Gitlab::API do
|
||||||
|
include ApiHelpers
|
||||||
|
|
||||||
|
let(:user) { create(:user) }
|
||||||
|
let(:admin) { create(:admin) }
|
||||||
|
let!(:hook) { create(:system_hook, url: "http://example.com") }
|
||||||
|
|
||||||
|
before { stub_request(:post, hook.url) }
|
||||||
|
|
||||||
|
describe "GET /hooks" do
|
||||||
|
context "when not an admin" do
|
||||||
|
it "should return forbidden error" do
|
||||||
|
get api("/hooks", user)
|
||||||
|
response.status.should == 403
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when authenticated as admin" do
|
||||||
|
it "should return an array of hooks" do
|
||||||
|
get api("/hooks", admin)
|
||||||
|
response.status.should == 200
|
||||||
|
json_response.should be_an Array
|
||||||
|
json_response.first['url'].should == hook.url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "POST /hooks" do
|
||||||
|
it "should create new hook" do
|
||||||
|
expect {
|
||||||
|
post api("/hooks", admin), url: 'http://example.com'
|
||||||
|
}.to change { SystemHook.count }.by(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should respond with 404 on failure" do
|
||||||
|
post api("/hooks", admin)
|
||||||
|
response.status.should == 404
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not create new hook without url" do
|
||||||
|
expect {
|
||||||
|
post api("/hooks", admin)
|
||||||
|
}.to_not change { SystemHook.count }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "GET /hooks/:id" do
|
||||||
|
it "should return hook by id" do
|
||||||
|
get api("/hooks/#{hook.id}", admin)
|
||||||
|
response.status.should == 200
|
||||||
|
json_response['event_name'].should == 'project_create'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return 404 on failure" do
|
||||||
|
get api("/hooks/404", admin)
|
||||||
|
response.status.should == 404
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "DELETE /hooks/:id" do
|
||||||
|
it "should delete a hook" do
|
||||||
|
expect {
|
||||||
|
delete api("/hooks/#{hook.id}", admin)
|
||||||
|
}.to change { SystemHook.count }.by(-1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue