Permalink
Browse files

merge

  • Loading branch information...
2 parents 10c309e + e9351d9 commit 44a7be2c9a2c43f3b4e4e1b937d1824997b5322b @technoweenie technoweenie committed Apr 27, 2012
Showing with 41 additions and 85 deletions.
  1. +17 −24 services/bamboo.rb
  2. +24 −61 test/bamboo_test.rb
View
@@ -6,7 +6,7 @@ class Service::Bamboo < Service
def receive_push
verify_config
branch = payload['ref']
- authenticated { |token| trigger_build(token, branch) }
+ trigger_build(branch)
rescue SocketError => e
if e.to_s =~ /getaddrinfo: Name or service not known/
raise_config_error("Invalid Bamboo host name")
@@ -15,11 +15,14 @@ def receive_push
end
end
- def trigger_build(token, ref)
+ def trigger_build(ref)
+ # Post body is empty but Bamboo REST expects this to be set (in 3.x)
+ http.headers['Content-Type'] = 'application/xml'
+
commit_branch = ref.split('/').last
build_key.split(',').each do |branch_key|
- #See if the split result is just a key or a branch:key
+ #See if the split result is just a key or a branch:key
parts = branch_key.split(':')
key = parts[0]
if parts.length == 2
@@ -30,33 +33,22 @@ def trigger_build(token, ref)
next unless branch == commit_branch
end
- res = http_post "api/rest/executeBuild.action",
- :auth => token, :buildKey => key
- msg = XmlSimple.xml_in(res.body)
- raise_config_error msg["error"] if msg["error"]
@technoweenie

technoweenie Apr 27, 2012

Member

@juretta: I had a conflict here. This diff looks fine. Be sure to update your fork's master against the source's master if you plan to send more pulls.

+ res = http_post "rest/api/latest/queue/#{key}"
+ handle_response(res)
end
end
- def authenticated
- token = login
- yield token
- ensure
- logout(token)
- end
-
- def login
- res = http_post "api/rest/login.action", :username => username, :password => password
- case res.status
+ def handle_response(response)
+ case response.status
when 200..204
- XmlSimple.xml_in(res.body)['auth'].first
+ "Ok"
when 403, 401, 422 then raise_config_error("Invalid credentials")
when 404, 301 then raise_config_error("Invalid Bamboo project URL")
- end
- end
-
- def logout(token)
- return unless token
- http_post "api/rest/logout.action", "auth=#{CGI.escape(token)}"
+ else
+ maybe_xml = response.body
+ msg = (XmlSimple.xml_in(maybe_xml) if maybe_xml =~ /<?xml/) || {}
+ raise_config_error msg["message"] if msg["message"]
+ end
end
def verify_config
@@ -65,6 +57,7 @@ def verify_config
end
http.ssl[:verify] = false
http.url_prefix = base_url
+ http.basic_auth(username, password)
end
def base_url
View
@@ -13,17 +13,8 @@ def setup
end
def test_triggers_build
- @stubs.post "/api/rest/login.action" do |env|
- assert_params env[:body], :username => 'admin', :password => 'pwd'
- [200, {}, '<response><auth>TOKEN123</auth></response>']
- end
- @stubs.post "/api/rest/executeBuild.action" do |env|
- assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
- [200, {}, '<response></response>']
- end
- @stubs.post "/api/rest/logout.action" do |env|
- assert_equal "auth=TOKEN123", env[:body]
- [200, {}, '']
+ @stubs.post "/rest/api/latest/queue/ABC" do |env|
+ valid_response("ABC")
end
svc = service :push, data, payload
@@ -33,21 +24,10 @@ def test_triggers_build
end
def test_triggers_compound_build
- @stubs.post "/api/rest/login.action" do |env|
- assert_params env[:body], :username => 'admin', :password => 'pwd'
- [200, {}, '<response><auth>TOKEN123</auth></response>']
- end
- @stubs.post "/api/rest/executeBuild.action" do |env|
- assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
- [200, {}, '<response></response>']
- end
- @stubs.post "/api/rest/executeBuild.action" do |env|
- assert_params env[:body], :auth => "TOKEN123", :buildKey => "A"
- [200, {}, '<response></response>']
- end
- @stubs.post "/api/rest/logout.action" do |env|
- assert_equal "auth=TOKEN123", env[:body]
- [200, {}, '']
+ ["ABC", "A"].each do |key|
+ @stubs.post "/rest/api/latest/queue/#{key}" do |env|
+ valid_response(key)
+ end
end
svc = service :push, compound_data1, payload
@@ -57,17 +37,8 @@ def test_triggers_compound_build
end
def test_triggers_build_with_context_path
- @stubs.post "/context/api/rest/login.action" do |env|
- assert_params env[:body], :username => 'admin', :password => 'pwd'
- [200, {}, '<response><auth>TOKEN123</auth></response>']
- end
- @stubs.post "/context/api/rest/executeBuild.action" do |env|
- assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
- [200, {}, '<response></response>']
- end
- @stubs.post "/context/api/rest/logout.action" do |env|
- assert_equal "auth=TOKEN123", env[:body]
- [200, {}, '']
+ @stubs.post "/context/rest/api/latest/queue/ABC" do |env|
+ valid_response("ABC")
end
data = self.data.update('base_url' => "https://secure.bamboo.com/context")
@@ -78,17 +49,8 @@ def test_triggers_build_with_context_path
end
def test_passes_build_error
- @stubs.post "/api/rest/login.action" do |env|
- assert_params env[:body], :username => 'admin', :password => 'pwd'
- [200, {}, '<response><auth>TOKEN123</auth></response>']
- end
- @stubs.post "/api/rest/executeBuild.action" do |env|
- assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
- [200, {}, '<response><error>oh hai</error></response>']
- end
- @stubs.post "/api/rest/logout.action" do |env|
- assert_equal "auth=TOKEN123", env[:body]
- [200, {}, '']
+ @stubs.post "/rest/api/latest/queue/ABC" do |env|
+ error_response(404, "Plan ABC not found")
end
svc = service :push, data, payload
@@ -99,18 +61,6 @@ def test_passes_build_error
@stubs.verify_stubbed_calls
end
- def test_requires_valid_login
- @stubs.post "/api/rest/login.action" do
- [401, {}, '']
- end
-
- svc = service :push, data, payload
-
- assert_raise Service::ConfigurationError do
- svc.receive
- end
- end
-
def test_requires_base_url
data = self.data.update('base_url' => '')
svc = service :push, data, payload
@@ -159,7 +109,7 @@ def svc.http_post(*args)
end
def test_invalid_bamboo_url
- @stubs.post "/api/rest/login.action" do
+ @stubs.post "/rest/api/latest/queue/ABC" do
[404, {}, '']
end
@@ -208,5 +158,18 @@ def assert_params(body, expected)
def service(*args)
super Service::Bamboo, *args
end
+
+ private
+ def valid_response(key)
+ xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
+ "<restQueuedBuild buildResultKey=\"#{key}-7\" buildNumber=\"7\" planKey=\"#{key}\">" +
+ "<triggerReason>Manual build</triggerReason><link rel=\"self\" href=\"http://bamboo.example.com/rest/api/latest/result/#{key}-7\"/>" +
+ "</restQueuedBuild>"
+ [200, {}, xml]
+ end
+ def error_response(status, msg)
+ xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><status><status-code>#{status}</status-code><message>#{msg}</message></status>"
+ [status, {}, xml]
+ end
end

0 comments on commit 44a7be2

Please sign in to comment.