Thursday, June 5, 2014

How do you retrieve test results from Bamboo via a REST call?

Sorry for the lack of updates; I was recently in a pretty bad biking accident and I've only recently returned to work. So!

I was asked by someone how they can retrieve test results somehow from Bamboo, because they have a separate, custom dashboard they use for displaying test results.

I know Atlassian has a REST API for all of their main products, is it possible to get test results back using it?
Yes, it is possible and not too difficult. The main caveat is that the smallest (and largest) group of test results you can retrieve is job sized.

Here's the latest documentation on Bamboo's API:
https://docs.atlassian.com/bamboo/REST/5.6-SNAPSHOT/
https://developer.atlassian.com/display/BAMBOODEV/Using+the+Bamboo+REST+APIs
https://developer.atlassian.com/display/BAMBOODEV/Bamboo+REST+Resources

To retrieve results, we'll use curl (though Python and many other tools are available) and xmllint to output the results in pretty handsome xml. (so if you're using Ubuntu (and who isn't?) "sudo apt-get install curl libxml2-utils" will fix you up)

Our example Bamboo server's base rest url is:
https://bamboo.company.com/rest/api/latest/
We can get build results back by adding
/result/projectkey-buildkey-job/buildnumber 
to the url.

Test results will be brief until we expand the test results, so we add
?expand=testResults.allTests 
to the end of that. If you're just looking for failures, or a slew of other filters, they have that too.

It defaults to xml output I believe, but you can output JSON format by adding .json to the end of the url, before the arguments. Word is that JSON will be deprecated at some point, but for now you can still get results back that way. I'll show how in a minute.

I don't want to specify the build number, I just want the latest results, and Atlassian supports "latest" as the build number. So, here's our complete url, using a made up plan with unit tests as our example:
https://bamboo.company.com/rest/api/latest/result/PLAN-STAGE-JOB/latest?expand=testResults.allTests

If you want JSON output, it would be this:
https://bamboo.company.com/rest/api/latest/result/PLAN-STAGE-JOB/latest.json?expand=testResults.allTests

Now, it outputs a huge block of unformatted xml by default. If you want to to look nice, we pass it through xmllint, by appending this to the end:
  | xmllint --format -
By default, anonymous is disabled in our install, so we need to pass authentication. The simplest example is using basic auth, so the complete command line would be this, using your username and password:
curl -u username:password https://bamboo.company.com/rest/api/latest/result/PLAN-STAGE-JOB/latest?expand=testResults.allTests | xmllint --format -
And it outputs something like this:



There's much more you can do beyond retrieving test results. You can also use this to kick off plans, get deployment results, etc. And, each release of Bamboo expands the REST API.

-Kelly G. Schoenhofen