Correctly process Bamboo API results

Bamboo API returns an array of results as per
https://docs.atlassian.com/atlassian-bamboo/REST/6.2.5/#d2e786
This commit is contained in:
Alex Lossent 2018-09-27 18:17:20 +02:00
parent d76c647797
commit 60937adacb
3 changed files with 16 additions and 5 deletions

View file

@ -80,13 +80,18 @@ class BambooService < CiService
private private
def get_build_result_index
# When Bamboo returns multiple results for a given changeset, arbitrarily assume the most relevant result to be the last one.
-1
end
def read_build_page(response) def read_build_page(response)
if response.code != 200 || response['results']['results']['size'] == '0' if response.code != 200 || response.dig('results', 'results', 'size') == '0'
# If actual build link can't be determined, send user to build summary page. # If actual build link can't be determined, send user to build summary page.
URI.join("#{bamboo_url}/", "browse/#{build_key}").to_s URI.join("#{bamboo_url}/", "browse/#{build_key}").to_s
else else
# If actual build link is available, go to build result page. # If actual build link is available, go to build result page.
result_key = response['results']['results']['result']['planResultKey']['key'] result_key = response.dig('results', 'results', 'result', get_build_result_index, 'planResultKey', 'key')
URI.join("#{bamboo_url}/", "browse/#{result_key}").to_s URI.join("#{bamboo_url}/", "browse/#{result_key}").to_s
end end
end end
@ -94,10 +99,10 @@ class BambooService < CiService
def read_commit_status(response) def read_commit_status(response)
return :error unless response.code == 200 || response.code == 404 return :error unless response.code == 200 || response.code == 404
status = if response.code == 404 || response['results']['results']['size'] == '0' status = if response.code == 404 || response.dig('results', 'results', 'size') == '0'
'Pending' 'Pending'
else else
response['results']['results']['result']['buildState'] response.dig('results', 'results', 'result', get_build_result_index, 'buildState')
end end
if status.include?('Success') if status.include?('Success')

View file

@ -0,0 +1,5 @@
---
title: "Correctly process Bamboo API result array"
merge_request: 21970
author: Alex Lossent
type: fixed

View file

@ -245,6 +245,7 @@ describe BambooService, :use_clean_rails_memory_store_caching do
end end
def bamboo_response(result_key: 42, build_state: 'success', size: 1) def bamboo_response(result_key: 42, build_state: 'success', size: 1)
%Q({"results":{"results":{"size":"#{size}","result":{"buildState":"#{build_state}","planResultKey":{"key":"#{result_key}"}}}}}) # reference: https://docs.atlassian.com/atlassian-bamboo/REST/6.2.5/#d2e786
%Q({"results":{"results":{"size":"#{size}","result":[{"buildState":"#{build_state}","planResultKey":{"key":"#{result_key}"}}]}}})
end end
end end