Commit 4ab2dd44 authored by Marius Bobin's avatar Marius Bobin

Use Gitlab::HTTP.try_get to find commit_status from Bamboo CI

parent dc178732
...@@ -73,7 +73,7 @@ class BambooService < CiService ...@@ -73,7 +73,7 @@ class BambooService < CiService
end end
def calculate_reactive_cache(sha, ref) def calculate_reactive_cache(sha, ref)
response = get_path("rest/api/latest/result/byChangeset/#{sha}") response = try_get_path("rest/api/latest/result/byChangeset/#{sha}")
{ build_page: read_build_page(response), commit_status: read_commit_status(response) } { build_page: read_build_page(response), commit_status: read_commit_status(response) }
end end
...@@ -81,7 +81,7 @@ class BambooService < CiService ...@@ -81,7 +81,7 @@ class BambooService < CiService
private private
def get_build_result(response) def get_build_result(response)
return if response.code != 200 return if response&.code != 200
# May be nil if no result, a single result hash, or an array if multiple results for a given changeset. # May be nil if no result, a single result hash, or an array if multiple results for a given changeset.
result = response.dig('results', 'results', 'result') result = response.dig('results', 'results', 'result')
...@@ -107,7 +107,7 @@ class BambooService < CiService ...@@ -107,7 +107,7 @@ class BambooService < CiService
end end
def read_commit_status(response) def read_commit_status(response)
return :error unless response.code == 200 || response.code == 404 return :error unless response && (response.code == 200 || response.code == 404)
result = get_build_result(response) result = get_build_result(response)
status = status =
...@@ -130,24 +130,31 @@ class BambooService < CiService ...@@ -130,24 +130,31 @@ class BambooService < CiService
end end
end end
def try_get_path(path, query_params = {})
params = build_get_params(query_params)
params[:extra_log_info] = { project_id: project_id }
Gitlab::HTTP.try_get(build_url(path), params)
end
def get_path(path, query_params = {})
Gitlab::HTTP.get(build_url(path), build_get_params(query_params))
end
def build_url(path) def build_url(path)
Gitlab::Utils.append_path(bamboo_url, path) Gitlab::Utils.append_path(bamboo_url, path)
end end
def get_path(path, query_params = {}) def build_get_params(query_params)
url = build_url(path) params = { verify: false, query: query_params }
return params if username.blank? && password.blank?
if username.blank? && password.blank? query_params[:os_authType] = 'basic'
Gitlab::HTTP.get(url, verify: false, query: query_params) params[:basic_auth] = basic_auth
else params
query_params[:os_authType] = 'basic' end
Gitlab::HTTP.get(url,
verify: false, def basic_auth
query: query_params, { username: username, password: password }
basic_auth: {
username: username,
password: password
})
end
end end
end end
...@@ -8,9 +8,11 @@ describe BambooService, :use_clean_rails_memory_store_caching do ...@@ -8,9 +8,11 @@ describe BambooService, :use_clean_rails_memory_store_caching do
let(:bamboo_url) { 'http://gitlab.com/bamboo' } let(:bamboo_url) { 'http://gitlab.com/bamboo' }
let_it_be(:project) { create(:project) }
subject(:service) do subject(:service) do
described_class.create( described_class.create(
project: create(:project), project: project,
properties: { properties: {
bamboo_url: bamboo_url, bamboo_url: bamboo_url,
username: 'mic', username: 'mic',
...@@ -224,6 +226,19 @@ describe BambooService, :use_clean_rails_memory_store_caching do ...@@ -224,6 +226,19 @@ describe BambooService, :use_clean_rails_memory_store_caching do
is_expected.to eq(:error) is_expected.to eq(:error)
end end
Gitlab::HTTP::HTTP_ERRORS.each do |http_error|
it "sets commit status to :error with a #{http_error.name} error" do
WebMock.stub_request(:get, 'http://gitlab.com/bamboo/rest/api/latest/result/byChangeset/123?os_authType=basic')
.to_raise(http_error)
expect(Gitlab::ErrorTracking)
.to receive(:log_exception)
.with(instance_of(http_error), project_id: project.id)
is_expected.to eq(:error)
end
end
end end
end end
......
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