Skip to content

Commit

Permalink
Project hooks: set events to trigger on
Browse files Browse the repository at this point in the history
  • Loading branch information
Semenyuk Dmitriy committed Apr 25, 2014
1 parent af49245 commit b347574
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
9 changes: 7 additions & 2 deletions lib/gitlab/client/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,14 @@ def project_hook(project, id)
#
# @param [Integer, String] project The ID or name of a project.
# @param [String] url The hook URL.
# @param [Hash] options Events list (`{push_events: true, merge_requests_events: false}`).
# @return [Gitlab::ObjectifiedHash] Information about added hook.
def add_project_hook(project, url)
post("/projects/#{project}/hooks", :body => {:url => url})
def add_project_hook(project, url, options = {})
available_events = [:push_events, :merge_requests_events, :issues_events]
passed_events = available_events.select { |event| options[event] }
events = Hash[passed_events.map { |event| [event, options[event]] }]

post("/projects/#{project}/hooks", :body => {:url => url}.merge(events))
end

# Updates a project hook URL.
Expand Down
42 changes: 30 additions & 12 deletions spec/gitlab/client/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,36 @@
end

describe ".add_project_hook" do
before do
stub_post("/projects/1/hooks", "project_hook")
@hook = Gitlab.add_project_hook(1, "https://api.example.net/v1/webhooks/ci")
end

it "should get the correct resource" do
body = {:url => "https://api.example.net/v1/webhooks/ci"}
a_post("/projects/1/hooks").with(:body => body).should have_been_made
end

it "should return information about an added hook" do
@hook.url.should == "https://api.example.net/v1/webhooks/ci"
context "without specified events" do
before do
stub_post("/projects/1/hooks", "project_hook")
@hook = Gitlab.add_project_hook(1, "https://api.example.net/v1/webhooks/ci")
end

it "should get the correct resource" do
body = {:url => "https://api.example.net/v1/webhooks/ci"}
a_post("/projects/1/hooks").with(:body => body).should have_been_made
end

it "should return information about an added hook" do
@hook.url.should == "https://api.example.net/v1/webhooks/ci"
end
end

context "with specified events" do
before do
stub_post("/projects/1/hooks", "project_hook")
@hook = Gitlab.add_project_hook(1, "https://api.example.net/v1/webhooks/ci", push_events: true, merge_requests_events: true)
end

it "should get the correct resource" do
body = {:url => "https://api.example.net/v1/webhooks/ci", push_events: "true", merge_requests_events: "true"}
a_post("/projects/1/hooks").with(:body => body).should have_been_made
end

it "should return information about an added hook" do
@hook.url.should == "https://api.example.net/v1/webhooks/ci"
end
end
end

Expand Down

0 comments on commit b347574

Please sign in to comment.