At my last job I needed generate release notes for each version of software that we shipped to customers. I created a PHP script to pull the status of issues in JIRA and generate HTML output which contained key fields such as key, summary, priority, and resolution. I could link that output to the release. Why not use the JIRA issues macro in Confluence? The JIRA issues macro in Confluence does a great job highlighting the current status of issues in JIRA. When software is in active development it’s a great way to keep everyone on the same page. This task required a solution that would freeze the status in time. If a bug was fixed in a release that later gets reopened, we didn’t want the release notes to show that bug having an open status. We believed that issue was fixed at the time of release. PHP code is easy for me to write as I’ve been using it to automate mundane tasks over the years. The problem was I’d not used it RESTfully before. I Googled “JIRA PHP” and didn’t find many examples of how to use JIRA with PHP. I wanted to create an article that summarizes how to do the basic issue operations using PHP and JIRA’s REST API. Many JIRA users have come to rely on the JIRA Command Line interface (me included) to automate operations inside of JIRA. I needed a higher level of control where I could manipulate individual fields on a large scale. Using JIRA’s REST API was the right solution for the job. JIRA’s REST API is super flexible and allows many of the same operations a user could do programmatically. PHP uses the cURL object to access JIRA. How does it work? Let’s take a look using an example to search for a set of issues created in the last day.
Searching for issues
We can use the search API available at http://example.com/rest/api/2/search with the JQL “created > -1d”.
Let’s take a look at the get_from function to see how PHP is interacting with JIRA.
function get_from($resource, $data) { //convert array to JSON string $jdata = json_encode($data); $ch = curl_init(); //configure CURL curl_setopt_array($ch, array( CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource, CURLOPT_USERPWD => USERNAME . ':' . PASSWORD, CURLOPT_POSTFIELDS => $jdata, CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_RETURNTRANSFER => true )); $result = curl_exec($ch); curl_close($ch); //convert JSON data back to PHP array return json_decode($result); }
Creating Issues
function post_to($resource, $data) { $jdata = json_encode($data); $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_POST => 1, CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource, CURLOPT_USERPWD => USERNAME . ':' . PASSWORD, CURLOPT_POSTFIELDS => $jdata, CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_RETURNTRANSFER => true )); $result = curl_exec($ch); curl_close($ch); return json_decode($result); }
Editing issues
function put_to($resource, $data) { $jdata = json_encode($data); $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_CUSTOMREQUEST=>"PUT", CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource, CURLOPT_USERPWD => USERNAME . ':' . PASSWORD, CURLOPT_POSTFIELDS => $jdata, CURLOPT_HTTPHEADER => array( 'Accept: application/json', 'Content-Type: application/json' ), CURLOPT_RETURNTRANSFER => true )); $result = curl_exec($ch); curl_close($ch); return json_decode($result); }
Full source can be had here.
Hello,
I would like to ask if you have come across any code to get and display the fixVersions for the result set of issues in JIRA ?
I’m looking for similar php code for the above.
Thanks,
Anh
Anh-
The FixVersions attribute is an array off of the issue. To print out a list of versions an issue is targeted for you can use something simple like:
foreach ($issue->fields->fixVersions as &$version) {
echo($version->name . ” “);
}
drafting off the code above.
Hello,
I would like to ask if you have come across any code to get and display the Affected Version/s for the result set of issues in JIRA ?
I’m looking for similar php code for the above.
Thanks,
Anh
Anh-
Just change the line above: ‘jql’ => ‘created > -1d’ to ‘jql’ => ‘affectedversion in (1.0, 1.5)’
Hello,
I would like to ask a question if anyone has had this issue that using Curl function get_from only returns max 50 issue records from JIRA. Is that a default? And I am not aware of where this limit is set that would impose 50 returned records. Do you have a solution or insight how to avoid the imposed limit?
Thank you very much for your help,
Anh
You have to pass the maxresults parameter. Check out these two links:
https://confluence.atlassian.com/display/JIRAKB/Changing+maxResults+Parameter+for+JIRA+REST+API
https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-query-issues
Hello Dan,
I would like to ask a question if you have come across the solution to for the above get_from function to set the maxResults to a certain number and not to limit to the default setting for JIRA API. Do you have a solution or insight how to avoid the imposed limit?
Thank you very much for your help,
Anh
See my other response Anh.
Hello,
I am trying to create a issue in Jira using http-streams.
I am able to make a GET request with a proper response, but the problem arises when i make a POST request.
Here is the question that i posted on stackoverflow
http://stackoverflow.com/questions/28436188/http-request-failed-http-1-1-400-bad-request
Can you help me solve this problem.
Thanks,
vaibhav
is there a perl equivalent? I need some perl error processing logic for my curl commands that create tickets and sub-task tickets
I just want to ask if I want to access all jira fields means key,summary, status etc.
how to do it?
For reading they should all be there, though some are nested in the JSON. For writing (update and create) the field needs to be on the appropriate screen when you do the write. For example, if the field is not on the create screen, you can’t add it via the API.
Hi, I have a question. How do you show the fields like “Assignee”, “Priority”, “Status” etc.?
Hello Hazel, you can pull them from the JSON piece that JIRA returns. Choose a JQL statement that returns 1 issue like key=”abc-123″ with the function get_from. Then var_dump the return value from that function that returns.
I am trying to achieve the same solution, but with Service Desk, still no luck
Should be the same process with JIRA Service Desk as long as the user you log in with has access to see the issues.
while editing an issue, its shows me the msg that editing completed but nothing has been modified in the issue…any help?
Are you aware of the following JIRA app?, https://marketplace.atlassian.com/apps/6398/jira-command-line-interface-cli?hosting=cloud&tab=overview.
Do you know if it could provide the same that you wrote yourself?.
Hey hablutzel1-
I love the Jira Command Line Interface. I mainly wrote this blog as I was looking to do more manipulation with the data after pulling it out of Jira. To get a sense of what the JCLI does, I often live by this page: https://bobswift.atlassian.net/wiki/spaces/JCLI/pages/6684682/Examples.
The JCLI has a lot of “admin-y” features in it. If you are looking to work more as an end user, I’d take a look at this project: https://github.com/Netflix-Skunkworks/go-jira
Hi, this has worked great for me, so thank you! Really helped me get off the ground. The one I’m struggling to output is the Status of the issue.
I’ve output the whole array and the way it’s set up means that I should be able to call it like this:
$issue->fields->currentStatus->status
All the equivalents work:
$issue->fields->project->name
However even though I can see the data in the array it isn’t being displayed. Have you ever encountered this?
What version of JIRA are you using?
JIRA Service Desk 3.16.0 🙂