Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Atlassian Jira REST API to run super simple report on a filter

All I want is basically to login, display info from a simple filter like all open bugs in a milestone. Once I get that far ill be able to work off the documentation

I'm hoping to use simple PHP, that's because I'm new to PHP so the more complicated the coding is the harder it will be for me to figure it out.

This script is the closest I've come to being successful:

    <?php

define('JIRA_URL', 'https://mysite.atlassian.net');
define('USERNAME', '[email protected]');
define('PASSWORD', '11111');

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/2/' . $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);
}

function create_issue($issue) {
        return post_to('issue', $issue);
}

$new_issue = array(
        'fields' => array(
                'project' => array('key' => 'FOO'),
                'summary' => 'Test via REST',
                'description' => 'Description of issue goes here.',
                'issuetype' => array('name' => 'Story')
        )
);

$result = create_issue($new_issue);
if (property_exists($result, 'errors')) {
        echo "Error(s) creating issue:\n";
        var_dump($result);
} else {
        echo "New issue created at " . JIRA_URL ."/browse/{$result->key}\n";
        var_dump($result);
}
?>

It returns New issue created at http://mysite.atlassian.net/browse/

But when I put the var_dump ($result) in there that just returns null so I have no idea if the jira URL is wrong or my password or if its outdated code for the new rest API.

If I could just make 1 simple query to the API and see anything returned on my PHP page I would be a happy camper. The example above creates a new issue, that's just the example I was using but I'm fine with just returning any sort of data so my main issue is getting a valid connection and returning some info so I know its working and can't start trying some different things then.

I have admin access but I wasn't clear if I needed to enable something on the admin side.

like image 777
user1547410 Avatar asked Nov 21 '12 00:11

user1547410


People also ask

How do I get a list of issues in JIRA API?

select count(*) from jiraissue; This returns the total number of issues regardless of your Jira permissions to see them.

How do I increase Maxresults in JIRA REST API?

As per API documentation the maximum number of issue you can fetch from Jira using api is set to 1000. So that is the maximum limit. To by-pass this issue, you have to call API multiple times with start index defined.


2 Answers

I figured out what i needed to do, there are lots of examples on the net but i couldnt find one simple like this so hopefully it helps someone else

$username = 'xxx';
$password = 'xxx';

$url = 'https://xxx.atlassian.net/rest/api/2/Issue/Bug-5555';

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$issue_list = (curl_exec($curl));
echo $issue_list;
like image 126
user1547410 Avatar answered Sep 30 '22 14:09

user1547410


Following to this page...

https://developer.atlassian.com/jiradev/api-reference/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-basic-authentication

We need to use header authentication these days which looks - modifying the example in this tread - like:

$username = 'user';
$password = 'pass';

$url = 'https://www.jiradomain.com/rest/api/2/issue/PROJECT-321/worklog';

$curl = curl_init();

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode("$username:$password")) );**
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_VERBOSE, true);

$verbose = fopen('php://temp', 'rw+');
curl_setopt($curl, CURLOPT_STDERR, $verbose);

$issue_list = (curl_exec($curl));
echo $issue_list;

$result = curl_exec($curl);
if ($result === FALSE) {
    printf("cUrl error (#%d): %s<br>\n", curl_errno($curlHandle),
           htmlspecialchars(curl_error($curlHandle)));
}

rewind($verbose);
$verboseLog = stream_get_contents($verbose);

echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
like image 35
Holger Lersch Avatar answered Sep 30 '22 14:09

Holger Lersch