Commit 17c32ee8 authored by Rémy Coutable's avatar Rémy Coutable

Factorize duplicated code into a method in BambooService and update specs

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 46f3cd7c
...@@ -59,18 +59,7 @@ class BambooService < CiService ...@@ -59,18 +59,7 @@ class BambooService < CiService
end end
def build_info(sha) def build_info(sha)
url = URI.join("#{bamboo_url}/", "rest/api/latest/result?label=#{sha}").to_s @response = get_path("rest/api/latest/result?label=#{sha}")
if username.blank? && password.blank?
@response = HTTParty.get(url, verify: false)
else
url << '&os_authType=basic'
auth = {
username: username,
password: password
}
@response = HTTParty.get(url, verify: false, basic_auth: auth)
end
end end
def build_page(sha, ref) def build_page(sha, ref)
...@@ -110,19 +99,27 @@ class BambooService < CiService ...@@ -110,19 +99,27 @@ class BambooService < CiService
def execute(data) def execute(data)
return unless supported_events.include?(data[:object_kind]) return unless supported_events.include?(data[:object_kind])
# Bamboo requires a GET and does take authentification get_path("updateAndBuild.action?buildKey=#{build_key}")
url = URI.join("#{bamboo_url}/", "updateAndBuild.action?buildKey=#{build_key}").to_s end
private
def build_url(path)
URI.join("#{bamboo_url}/", path).to_s
end
def get_path(path)
url = build_url(path)
if username.blank? && password.blank? if username.blank? && password.blank?
HTTParty.get(url, verify: false) HTTParty.get(url, verify: false)
else else
url << '&os_authType=basic' url << '&os_authType=basic'
auth = { HTTParty.get(url, verify: false,
username: username, basic_auth: {
password: password username: username,
} password: password
HTTParty.get(url, verify: false, basic_auth: auth) })
end end
end end
end end
...@@ -126,25 +126,25 @@ describe BambooService, models: true do ...@@ -126,25 +126,25 @@ describe BambooService, models: true do
it 'returns a specific URL when status is 500' do it 'returns a specific URL when status is 500' do
stub_request(status: 500) stub_request(status: 500)
expect(service.build_page('123', 'unused')).to eq('http://gitlab.com/browse/foo') expect(service.build_page('123', 'unused')).to eq('http://gitlab.com/bamboo/browse/foo')
end end
it 'returns a specific URL when response has no results' do it 'returns a specific URL when response has no results' do
stub_request(body: %Q({"results":{"results":{"size":"0"}}})) stub_request(body: %Q({"results":{"results":{"size":"0"}}}))
expect(service.build_page('123', 'unused')).to eq('http://gitlab.com/browse/foo') expect(service.build_page('123', 'unused')).to eq('http://gitlab.com/bamboo/browse/foo')
end end
it 'returns a build URL when bamboo_url has no trailing slash' do it 'returns a build URL when bamboo_url has no trailing slash' do
stub_request(body: %Q({"results":{"results":{"result":{"planResultKey":{"key":"42"}}}}})) stub_request(body: %Q({"results":{"results":{"result":{"planResultKey":{"key":"42"}}}}}))
expect(service(bamboo_url: 'http://gitlab.com').build_page('123', 'unused')).to eq('http://gitlab.com/browse/42') expect(service(bamboo_url: 'http://gitlab.com/bamboo').build_page('123', 'unused')).to eq('http://gitlab.com/bamboo/browse/42')
end end
it 'returns a build URL when bamboo_url has a trailing slash' do it 'returns a build URL when bamboo_url has a trailing slash' do
stub_request(body: %Q({"results":{"results":{"result":{"planResultKey":{"key":"42"}}}}})) stub_request(body: %Q({"results":{"results":{"result":{"planResultKey":{"key":"42"}}}}}))
expect(service(bamboo_url: 'http://gitlab.com/').build_page('123', 'unused')).to eq('http://gitlab.com/browse/42') expect(service(bamboo_url: 'http://gitlab.com/bamboo/').build_page('123', 'unused')).to eq('http://gitlab.com/bamboo/browse/42')
end end
end end
...@@ -192,7 +192,7 @@ describe BambooService, models: true do ...@@ -192,7 +192,7 @@ describe BambooService, models: true do
end end
end end
def service(bamboo_url: 'http://gitlab.com') def service(bamboo_url: 'http://gitlab.com/bamboo')
described_class.create( described_class.create(
project: create(:empty_project), project: create(:empty_project),
properties: { properties: {
...@@ -205,7 +205,7 @@ describe BambooService, models: true do ...@@ -205,7 +205,7 @@ describe BambooService, models: true do
end end
def stub_request(status: 200, body: nil, build_state: 'success') def stub_request(status: 200, body: nil, build_state: 'success')
bamboo_full_url = 'http://mic:password@gitlab.com/rest/api/latest/result?label=123&os_authType=basic' bamboo_full_url = 'http://mic:password@gitlab.com/bamboo/rest/api/latest/result?label=123&os_authType=basic'
body ||= %Q({"results":{"results":{"result":{"buildState":"#{build_state}"}}}}) body ||= %Q({"results":{"results":{"result":{"buildState":"#{build_state}"}}}})
WebMock.stub_request(:get, bamboo_full_url).to_return( WebMock.stub_request(:get, bamboo_full_url).to_return(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment