Skip to content

Commit

Permalink
SSH Bitbucket support.
Browse files Browse the repository at this point in the history
  • Loading branch information
unsay committed Jan 29, 2011
1 parent 814f692 commit 9e8fea8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/controllers/hooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
82 changes: 82 additions & 0 deletions test/integration/bitbucket_autobuild_test.rb
Original file line number Diff line number Diff line change
@@ -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 9e8fea8

Please sign in to comment.