Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding postreceive hooks, thanks to tekkub's gist

  • Loading branch information...
commit 18d8237e20a14893b0bce030a349f300f71bffd2 1 parent 4c63ec5
@qrush qrush authored
View
10 features/set_hooks.feature
@@ -0,0 +1,10 @@
+Feature: Set hooks
+
+ Scenario: Adding a single collaborator for a specific project
+ When I execute the following code
+ """
+ Enforcer "thoughtbot", "deadbeef" do
+ postreceive "shoulda", "http://ci.thoughtbot.com"
+ end
+ """
+ Then the GitHub API should have received a request to add "http://ci.thoughtbot.com" as a post-receive url for "shoulda"
View
5 features/step_definitions/enforcer_steps.rb
@@ -5,6 +5,7 @@
stub(Repository).new(anything, anything, anything) { @repo }
stub(@repo).add(anything)
stub(@repo).remove(anything)
+ stub(@repo).postreceive(anything)
stub(@repo).list { @existing_collaborators }
end
@@ -28,3 +29,7 @@
Then /^the GitHub API should have received a request to remove "([^\"]*)" as a collaborator for "([^\"]*)"$/ do |user, repo|
assert_received(@repo) { |subject| subject.remove(user) }
end
+
+Then /^the GitHub API should have received a request to add "([^\"]*)" as a post\-receive url for "([^\"]*)"$/ do |url, repo|
+ assert_received(@repo) { |subject| subject.postreceive(url) }
+end
View
2  features/support/env.rb
@@ -1,7 +1,7 @@
require 'fileutils'
require 'rr'
require 'test/unit'
-require 'enforcer'
+require 'lib/enforcer'
require 'fakeweb'
FakeWeb.allow_net_connect = false
View
6 lib/enforcer.rb
@@ -34,6 +34,12 @@ def project(project_name, *collaborators)
end
end
end
+
+ def postreceive(project_name, url)
+ STDOUT.puts "Enforcing post-receive url for #{project_name}"
+ repo = Repository.new(@account_name, @api_key, project_name)
+ repo.postreceive(url)
+ end
end
def Enforcer(account_name, api_key, &block)
View
14 lib/enforcer/repository.rb
@@ -38,4 +38,18 @@ def remove(collaborator)
STDOUT.puts ">> Removing #{collaborator}"
request(:post, "/collaborators/#{@project}/remove/#{collaborator}")
end
+
+ def postreceive(hook)
+ auth = {:login => @account, :token => @api_key}
+
+ url = URI.parse "https://github.com/#{@account}/#{@project}/edit/postreceive_urls"
+ req = Net::HTTP::Post.new(url.path)
+ req.set_form_data({"urls[]" => hook}.merge(auth), '&')
+
+ server = Net::HTTP.new url.host, url.port
+ server.use_ssl = url.scheme == 'https'
+ server.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ res = server.start {|http| http.request(req) }
+ res.body
+ end
end
View
21 test/enforcer_test.rb
@@ -10,6 +10,7 @@ class EnforcerTest < Test::Unit::TestCase
@api_key = "api key"
stub(@repo).add(anything)
+ stub(@repo).postreceive(anything)
stub(@repo).list { @existing_collaborators }
mock(Repository).new(@account, @api_key, @project) { @repo }
@@ -68,22 +69,14 @@ class EnforcerTest < Test::Unit::TestCase
subject.remove(@existing_user)
end
end
- end
- end
-
- context "setting up enforcer dsl" do
- setup do
- @enforcer = "enforcer"
- stub(@enforcer).project(anything, anything)
- @account = "thoughtbot"
- @api_key = "api key"
- mock(Enforcer).new(@account, @api_key) { @enforcer }
- end
+ should "add a postreceive url to the project" do
+ @enforcer.postreceive @project, 'http://example.com'
- should "be there and take a string and block" do
- Enforcer(@account, @api_key) { project("shoulda", "corey") {} }
- assert_received(@enforcer) { |subject| subject.project("shoulda", "corey") }
+ assert_received(@repo) do |subject|
+ subject.postreceive('http://example.com')
+ end
+ end
end
end
end
View
14 test/repository_test.rb
@@ -15,7 +15,7 @@ class RepositoryTest < Test::Unit::TestCase
end
context "github is down" do
- should "not fail" do
+ should "not fail on api methods" do
stub(Repository).get(anything, anything) { raise TimeoutError.new("Failwhale!") }
assert_nothing_raised do
@repo.list
@@ -23,6 +23,17 @@ class RepositoryTest < Test::Unit::TestCase
end
end
+ context "adding postreceive hook" do
+ setup do
+ @response = "Accepted!"
+ FakeWeb.register_uri(:post, "https://github.com/#{@account}/#{@project}/edit/postreceive_urls", :body => @response)
+ end
+
+ should "submit form data to github" do
+ assert_equal @response, @repo.postreceive('http://example.com')
+ end
+ end
+
context "listing collaborators" do
setup do
@collaborators = ["qrush", "coreyhaines"]
@@ -61,6 +72,7 @@ class RepositoryTest < Test::Unit::TestCase
assert_equal ["qrush", @user], @collaborators
end
end
+
context "removing a collaborator" do
setup do
@collaborators = ["qrush"]
View
3  test/test_helper.rb
@@ -2,11 +2,14 @@
require 'test/unit'
require 'shoulda'
require 'rr'
+require 'fakeweb'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'enforcer'
+FakeWeb.allow_net_connect = false
+
begin
require 'redgreen'
rescue LoadError
Please sign in to comment.
Something went wrong with that request. Please try again.