Using JIRA’s REST API to Create a Dashboard
- 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 "fields->issuetype->iconUrl . "\" alt=\""
. $issue->fields->issuetype->name . "\" /> \n";
echo "" . $issue->fields->summary . "\n";
if(isset($issue->fields->assignee->displayName)) {
echo " (" . $issue->fields->assignee->displayName . ")\n";
}
echo "
\n";
echo "key . "\">Link \n";
$updated = new DateTime($issue->fields->updated);
echo "last updated: " . $updated->format('jS M') . "\n";
echo "
\n";
echo "
\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)
Atlassian are pretty good with their SOAP and now REST API’s. I’ve used them quite heavily for intranet/extranet portal projects. The Confluence API is great for surfacing content to users when they’re in other applications.
Steve: Thanks for this – I’m just picking up their APIs and it’s reassuring to hear from someone already ahead of me on that journey, cheers for commenting!
Hi. I have a question. Do I need any specific setup in JIRA to begin using the API?
You do need to have their XML-RPC API enabled in your JIRA installation – ours is the hosted JIRA solution (I think it’s called JIRA Studio just now), and it was already enabled for that
Hi LORNAJANE,
my name is Itai I’m trying to build an app that will write an issue to JIRA using REST API,
I tried to implement it in PHP using CURL but i get an error telling me “The specified HTTP method is not allowed for the requested resource (Method Not Allowed).” can you take a look and tell me what am i doing wrong?:
[code]
<?php
$handle=curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Basic YWRtaW46eWFoYWxh'
);
$data = <<‘http://localhost:8084/rest/api/2/issue/’,
CURL_POST=>true,
//CURLOPT_HTTPGET =>true,
CURLOPT_VERBOSE=>1,
CURLOPT_POSTFIELDS=>’?expand=names,renderedFields’,
CURLOPT_SSL_VERIFYHOST=> 0,
CURLOPT_SSL_VERIFYPEER=> 0,
CURLOPT_RETURNTRANSFER=>true,
CURL_HEADER=>false,
CURLOPT_HTTPHEADER=> $headers,
CURLOPT_USERPWD=>”admin:yahala”,
CURLOPT_CUSTOMREQUEST=>”POST”
)
);
$result=curl_exec($handle);
$ch_error = curl_error($handle);
if ($ch_error) {
echo “cURL Error: $ch_error”;
} else {
echo $result;
}
curl_close($handle);
?>
[/code]
I haven’t created issues over the API, my guess is that either you’re posting to the wrong endpoint (so the API responds to say it doesn’t know what you’re asking it to do) or you’re not authenticated. Check those first and let us know how you get on
in the line “CURL_HEADER=>false,” you should use CURLOPT_HEADER=>false,
also CURLOPT_POST=>…
This code really helped me but I was wondering how to get additional information to display. I also want to get the description for each issue. Is that something I set in $params?
Pingback: How to implement JIRA api with ci application? | BlogoSfera
WOW just what I was searching for. Came here by
searching for web
Pingback: Lunch Links - JIRA BrewJIRA Brew