Using JIRA's REST API to Create a Dashboard
Join the DZone community and get the full member experience.
Join For Freeif you read this blog often, you'll know that i am:
- crazy about apis
- living with some accessibility issues
put these two things together and what do you get? actually don't answer that! today what you get is an example of integrating with jira's rest api, because their recent "upgrade" locked me out of the issue listings pages completely and i really do need to be able to see a list of bugs! their bug editing screen is quite usable, so it's just the list that i need here, but you could easily call their other api methods as you need to.
these examples are php and use the pecl_http extension, because it's awesome, but these examples could be easily adapted to use another language or library.
what's the rest url?
we have hosted jira from atlassian, which i think is now called jira studio, because they give free pretty-much-unlimited licenses to open source projects, and for that i don't think i'll ever be able to thank them enough. not enough organisations recognise the value of open source!
our "normal" jira url: http://joindin.jira.com.
the url to access the restful service: https://joindin.jira.com/rest/api. note this requires ssl.
getting a list of issues
this should be easy, but many services make it hard. not this one, here's my code:
$request = new httprequest(); $request->seturl('https://joindin.jira.com/rest/api/latest/search'); $request->send(); $issue_list = json_decode($request->getresponsebody());
now i have all the issues in $issue_list - this is paginated, you can control how many issues you get in a list; the default is 50 i think. you can also pass in any search query - basically the same as the search options that you see in jira itself. this uses a language called jql, and you can find some jql documents here .
i added some parameters to only show me open and reopened issues, so my code sample now looks like this:
$request = new httprequest(); $request->seturl('https://joindin.jira.com/rest/api/latest/search'); $params = array( "jql" => "status in (open, reopened)" ); $request->setquerydata($params); $request->send(); $issue_list = json_decode($request->getresponsebody());
this just limits the results to what you're interested in; you could easily add things assigned to yourself, or whatever suits your needs.
displaying results
to see what you've got, use print_r on that list of issues! with this information, i made a screen that looks like this (design is not my strong point!):
the simplest is perhaps just to show the code to generate this output - the "link" actually goes to the web version of the bug since i can easily access that.
foreach($issue_list->issues as $issue) { echo "<img src=\"" . $issue->fields->issuetype->iconurl . "\" alt=\"" . $issue->fields->issuetype->name . "\" />Â \n"; echo "<b>" . $issue->fields->summary . "</b>\n"; if(isset($issue->fields->assignee->displayname)) { echo " (" . $issue->fields->assignee->displayname . ")\n"; } echo "<br />\n"; echo "<a href=\"http://joindin.jira.com/browse/" . $issue->key . "\">link</a> \n"; $updated = new datetime($issue->fields->updated); echo "last updated: " . $updated->format('js m') . "\n"; echo "<br />\n"; echo "<hr />\n"; }
since i whipped up this script and will be using it as part of my dashboard of tools, i thought i'd share - i hadn't seen any integration with jira before, but atlassian are focusing all their future efforts on this rest service and my first impressions are that it seems pretty good.
(this isn't production code, you probably want to check responses and things before you unpack them, but this got me started and i hope it helps you too)
Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments