Skip to content
Snippets Groups Projects
Commit 32f1eaaf authored by Sebastian Ziebell's avatar Sebastian Ziebell
Browse files

API: system hooks API functions and documentation updated

* updated system hooks documentation and code comments
* fixed access to system hooks if no user given resulting in a `500 Server Error`
* added tests
parent ecf53bb9
No related branches found
No related tags found
No related merge requests found
Loading
@@ -8,7 +8,10 @@ Get list of system hooks
Loading
@@ -8,7 +8,10 @@ Get list of system hooks
GET /hooks GET /hooks
``` ```
Will return hooks with status `200 OK` on success, or `404 Not found` on fail. Parameters:
+ **none**
## Add new system hook hook ## Add new system hook hook
Loading
@@ -20,7 +23,6 @@ Parameters:
Loading
@@ -20,7 +23,6 @@ Parameters:
+ `url` (required) - The hook URL + `url` (required) - The hook URL
Will return status `201 Created` on success, or `404 Not found` on fail.
## Test system hook ## Test system hook
Loading
@@ -32,10 +34,12 @@ Parameters:
Loading
@@ -32,10 +34,12 @@ Parameters:
+ `id` (required) - The ID of hook + `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 system hook
Deletes a system hook. This is an idempotent API function and returns `200 Ok` even if the hook
is not available. If the hook is deleted it is also returned as JSON.
``` ```
DELETE /hooks/:id DELETE /hooks/:id
``` ```
Loading
@@ -43,5 +47,3 @@ DELETE /hooks/:id
Loading
@@ -43,5 +47,3 @@ DELETE /hooks/:id
Parameters: Parameters:
+ `id` (required) - The ID of hook + `id` (required) - The ID of hook
Will return status `200 OK` on success, or `404 Not found` on fail.
\ No newline at end of file
module Gitlab module Gitlab
# Hooks API # Hooks API
class SystemHooks < Grape::API class SystemHooks < Grape::API
before { authenticated_as_admin! } before {
authenticate!
authenticated_as_admin!
}
resource :hooks do resource :hooks do
# Get the list of system hooks # Get the list of system hooks
Loading
@@ -21,6 +24,7 @@ module Gitlab
Loading
@@ -21,6 +24,7 @@ module Gitlab
# POST /hooks # POST /hooks
post do post do
attrs = attributes_for_keys [:url] attrs = attributes_for_keys [:url]
required_attributes! [:url]
@hook = SystemHook.new attrs @hook = SystemHook.new attrs
if @hook.save if @hook.save
present @hook, with: Entities::Hook present @hook, with: Entities::Hook
Loading
@@ -47,13 +51,19 @@ module Gitlab
Loading
@@ -47,13 +51,19 @@ module Gitlab
data data
end end
# Delete a hook # Delete a hook. This is an idempotent function.
# #
# Parameters:
# id (required) - ID of the hook
# Example Request: # Example Request:
# DELETE /hooks/:id # DELETE /hooks/:id
delete ":id" do delete ":id" do
begin
@hook = SystemHook.find(params[:id]) @hook = SystemHook.find(params[:id])
@hook.destroy @hook.destroy
rescue
# SystemHook raises an Error if no hook with id found
end
end end
end end
end end
Loading
Loading
Loading
@@ -10,6 +10,13 @@ describe Gitlab::API do
Loading
@@ -10,6 +10,13 @@ describe Gitlab::API do
before { stub_request(:post, hook.url) } before { stub_request(:post, hook.url) }
describe "GET /hooks" do describe "GET /hooks" do
context "when no user" do
it "should return authentication error" do
get api("/hooks")
response.status.should == 401
end
end
context "when not an admin" do context "when not an admin" do
it "should return forbidden error" do it "should return forbidden error" do
get api("/hooks", user) get api("/hooks", user)
Loading
@@ -34,9 +41,9 @@ describe Gitlab::API do
Loading
@@ -34,9 +41,9 @@ describe Gitlab::API do
}.to change { SystemHook.count }.by(1) }.to change { SystemHook.count }.by(1)
end end
it "should respond with 404 on failure" do it "should respond with 400 if url not given" do
post api("/hooks", admin) post api("/hooks", admin)
response.status.should == 404 response.status.should == 400
end end
it "should not create new hook without url" do it "should not create new hook without url" do
Loading
@@ -65,5 +72,10 @@ describe Gitlab::API do
Loading
@@ -65,5 +72,10 @@ describe Gitlab::API do
delete api("/hooks/#{hook.id}", admin) delete api("/hooks/#{hook.id}", admin)
}.to change { SystemHook.count }.by(-1) }.to change { SystemHook.count }.by(-1)
end end
it "should return success if hook id not found" do
delete api("/hooks/12345", admin)
response.status.should == 200
end
end end
end end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment