Lorna Jane Mitchell is a PHP developer, blogger, trainer and evangelist from Leeds in the UK. She is active with phpwomen.org and her local user group PHP North West, and writes for a variety of outlets, including her own blog at lornajane.net. She is an active member of the PHP and open source communities and contributes to the joind.in event feedback project. When she's not at her computer, Lorna enjoys yarn craft, hobby electronics, and her home renovation project. Lorna is a DZone MVB and is not an employee of DZone and has posted 80 posts at DZone. You can read more from them at their website. View Full User Profile

Using JIRA's REST API to Create a Dashboard

03.28.2012
| 5816 views |
  • submit to reddit

If 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, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)