Permalink
Browse files

Merge branch 'bitbucket' of https://github.com/unsay/bigtuna into uns…

…ay-bitbucket
  • Loading branch information...
2 parents 9241644 + 9e8fea8 commit 56847996be77371707773896371c5daae70bc51d Michal Bugno committed Feb 6, 2011
Showing with 104 additions and 0 deletions.
  1. +17 −0 app/controllers/hooks_controller.rb
  2. +4 −0 config/environment.rb
  3. +1 −0 config/routes.rb
  4. +82 −0 test/integration/bitbucket_autobuild_test.rb
View
17 app/controllers/hooks_controller.rb
@@ -30,6 +30,23 @@ def github
end
end
+ def bitbucket
+ payload = JSON.parse(params[:payload])
+ branch = payload["commits"][0]["branch"]
+ url = payload["repository"]["absolute_url"]
+ source = "ssh://hg@bitbucket.org#{url}"
+
+ project = Project.where(:vcs_source => source, :vcs_branch => branch).first
+
+ if BigTuna.bitbucket_secure.nil?
+ render :text => "bitbucket secure token is not set up", :status => 403
+ elsif project and params[:secure] == BigTuna.bitbucket_secure
+ trigger_and_respond(project)
+ else
+ render :text => "invalid secure token", :status => 404
+ end
+ end
+
def configure
@project = Project.find(params[:project_id])
@hook = Hook.where(:project_id => @project.id, :hook_name => params[:name]).first
View
4 config/environment.rb
@@ -25,6 +25,10 @@ def self.github_secure
config["github_secure"]
end
+ def self.bitbucket_secure
+ config["bitbucket_secure"]
+ end
+
def self.read_only?
env_force = ["true", "1", "yes", "y"].include?(ENV["BIGTUNA_READONLY"].to_s.downcase)
return true if env_force
View
1 config/routes.rb
@@ -19,5 +19,6 @@
match "/hooks/build/:hook_name", :to => "hooks#autobuild"
match "/hooks/build/github/:secure", :to => "hooks#github"
+ match "/hooks/build/bitbucket/:secure", :to => "hooks#bitbucket"
root :to => "projects#index"
end
View
82 test/integration/bitbucket_autobuild_test.rb
@@ -0,0 +1,82 @@
+require "integration_test_helper"
+
+class BitbucketAutobuildTest < ActionController::IntegrationTest
+ def setup
+ super
+ `cd test/files; mkdir repo; cd repo; hg init; echo "my file" > file; hg add file; hg commit -m "my file added"`
+ end
+
+ def teardown
+ FileUtils.rm_rf("test/files/repo")
+ FileUtils.rm_rf("builds/*")
+ super
+ end
+
+ test "if hook name is not found, 404 status is returned with appropriate description" do
+ post "/hooks/build/not_found_halp"
+ assert_status_code(404)
+ assert response.body.include?("hook name \"not_found_halp\" not found")
+ end
+
+ test "if bitbucket posts hook we look for specified branch to build" do
+ project1 = bitbucket_project(:name => "obywatelgc", :vcs_branch => "default")
+ project2 = bitbucket_project(:name => "obywatelgc2", :vcs_branch => "development")
+ old_token = BigTuna.config["bitbucket_secure"]
+ begin
+ BigTuna.config["bitbucket_secure"] = "mytoken"
+ token = BigTuna.bitbucket_secure
+ assert_difference("project1.builds.count", +1) do
+ assert_difference("project2.builds.count", 0) do
+ post "/hooks/build/bitbucket/#{token}", :payload => bitbucket_payload(project1)
+ assert_status_code(200)
+ assert response.body.include?("build for \"#{project1.name}\" triggered")
+ end
+ end
+ ensure
+ BigTuna.config["bitbucket_secure"] = old_token
+ end
+ end
+
+ test "bitbucket post with invalid token won't build anything" do
+ project1 = bitbucket_project(:name => "obywatelgc", :vcs_branch => "default")
+ project2 = bitbucket_project(:name => "obywatelgc2", :vcs_branch => "development")
+ old_token = BigTuna.config["bitbucket_secure"]
+ begin
+ BigTuna.config["bitbucket_secure"] = "mytoken"
+ token = BigTuna.bitbucket_secure
+ invalid_token = token + "a"
+ assert_difference("Build.count", 0) do
+ post "/hooks/build/bitbucket/#{invalid_token}", :payload => bitbucket_payload(project1)
+ assert_status_code(404)
+ assert response.body.include?("invalid secure token")
+ end
+ ensure
+ BigTuna.config["bitbucket_secure"] = old_token
+ end
+ end
+
+ test "bitbucket token has to be set up" do
+ project1 = bitbucket_project(:name => "obywatelgc", :vcs_branch => "default")
+ old_token = BigTuna.config["bitbucket_secure"]
+ begin
+ BigTuna.config["bitbucket_secure"] = nil
+ assert_equal nil, BigTuna.bitbucket_secure
+ post "/hooks/build/bitbucket/4ff", :payload => bitbucket_payload(project1)
+ assert_status_code(403)
+ assert response.body.include?("bitbucket secure token is not set up")
+ ensure
+ BigTuna.config["bitbucket_secure"] = old_token
+ end
+ end
+
+ private
+ def bitbucket_project(opts = {})
+ project = Project.make({:vcs_source => "ssh://hg@bitbucket.org/foo/bigtuna/", :vcs_branch => "default", :vcs_type => "hg", :max_builds => 2}.merge(opts))
+ step_list = StepList.make(:project => project, :steps => "ls")
+ project
+ end
+
+ def bitbucket_payload(project)
+ "{\"repository\": {\"owner\": \"foo\", \"website\": \"\", \"absolute_url\": \"/foo/bigtuna/\", \"slug\": \"bigtuna\", \"name\": \"bigtuna\"}, \"commits\": [{\"node\": \"94608d070caf\", \"files\": [{\"type\": \"modified\", \"file\": \"app/controllers/hooks_controller.rb\"}], \"author\": \"unsay\", \"timestamp\": \"2010-12-09 04:59:38\", \"raw_node\": \"94608d070caf01aeb60c39e099c528cebf62e9eb\", \"parents\": [\"e069646e9522\"], \"branch\": \"default\", \"message\": \"Ahh.\", \"size\": 57, \"revision\": 10}], \"user\": \"merp\"}"
+ end
+end

0 comments on commit 5684799

Please sign in to comment.