Skip to content

Commit

Permalink
System Hook API
Browse files Browse the repository at this point in the history
  • Loading branch information
nanofi committed Apr 30, 2014
1 parent f48c7a9 commit 0d6ca05
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Gitlab
class Client < API
Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}

include SystemHook
include Users
include Issues
include Notes
Expand Down
55 changes: 55 additions & 0 deletions lib/gitlab/client/system_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Gitlab::Client
# Defines methods related to system hooks.
module SystemHook
# Gets a list of system hooks.
#
# @example
# Gitlab.hooks
# Gitlab.system_hooks
#
# @return [Array<Gitlab::ObjectifiedHash>]
def hooks
get("/hooks")
end
alias_method :system_hooks, :hooks

# Adds a new system hook.
#
# @example
# Gitlab.add_hook('http://example.com/hook')
# Gitlab.add_system_hook('https://api.example.net/v1/hook')
#
# @param [String] url The hook URL.
# @return [Gitlab::ObjectifiedHash]
def add_hook(url)
post("/hooks", :body => {:url => url})
end
alias_method :add_system_hook, :add_hook

# Tests a system hook.
#
# @example
# Gitlab.hook(3)
# Gitlab.system_hook(12)
#
# @param [Integer] id The ID of a system hook.
# @return [Array<Gitlab::ObjectifiedHash>]
def hook(id)
get("/hooks/#{id}")
end
alias_method :system_hook, :hook

# Deletes a new system hook.
#
# @example
# Gitlab.delete_hook(3)
# Gitlab.delete_system_hook(12)
#
# @param [Integer] id The ID of a system hook.
# @return [Gitlab::ObjectifiedHash]
def delete_hook(id)
delete("/hooks/#{id}")
end
alias_method :delete_system_hook, :delete_hook
end
end
1 change: 1 addition & 0 deletions spec/fixtures/system_hook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id": 3, "url": "http://example.com/hook", "created_at": "2013-10-02T10:15:31Z"}
1 change: 1 addition & 0 deletions spec/fixtures/system_hook_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "event_name": "project_create", "name": "Ruby", "path": "ruby", "project_id": 1, "owner_name": "Someone", "owner_email": "example@gitlabhq.com" }
1 change: 1 addition & 0 deletions spec/fixtures/system_hooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id": 3, "url": "http://example.com/hook", "created_at": "2013-10-02T10:15:31Z"}]
69 changes: 69 additions & 0 deletions spec/gitlab/client/system_hook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'spec_helper'

describe Gitlab::Client do
it { should respond_to :system_hooks }
it { should respond_to :add_system_hook }
it { should respond_to :system_hook }
it { should respond_to :delete_system_hook }

describe ".hooks" do
before do
stub_get("/hooks", "system_hooks")
@hooks = Gitlab.hooks
end

it "should get the correct resource" do
a_get("/hooks").should have_been_made
end

it "should return an array of system hooks" do
@hooks.should be_an Array
@hooks.first.url.should == "http://example.com/hook"
end
end

describe ".add_hook" do
before do
stub_post("/hooks", "system_hook")
@hook = Gitlab.add_hook("http://example.com/hook")
end

it "should get the correct resource" do
a_post("/hooks").should have_been_made
end

it "should return information about a added system hook" do
@hook.url.should == "http://example.com/hook"
end
end

describe ".hook" do
before do
stub_get("/hooks/3", "system_hook_test")
@hook = Gitlab.hook(3)
end
it "should get the correct resource" do
a_get("/hooks/3").should have_been_made
end

it "should return information about a added system hook" do
@hook.event_name.should == "project_create"
@hook.project_id.should == 1
end
end

describe ".delete_hook" do
before do
stub_delete("/hooks/3", "system_hook")
@hook = Gitlab.delete_hook(3)
end

it "should get the correct resource" do
a_delete("/hooks/3").should have_been_made
end

it "should return information about a deleted system hook" do
@hook.url.should == "http://example.com/hook"
end
end
end

0 comments on commit 0d6ca05

Please sign in to comment.