Creating issues in Jira for multiple users with Jira API

September 12, 2015

1. Creating Jira issue

   $jiraUsername = 'Jira login username';
   $jiraPassword = 'Jira login password';
   $jiraHost     = 'http://my-jira-host.com/';
   $data = array(
        'fields' => array(
            'project' => array('key' => 'PR'/* Project Key*/),
            'summary'=> 'Issue title',
            'description' => 'Some text here, no html tags',
            'labels' => array('API', 'IvanGospodinow'/* Put the username so you know which one added it */),
            'issuetype' => array('id' => 1 /* or similar */)
        )
    );

    $ch = curl_init();
    $headers = array(
        'Accept: application/json',
        'Content-Type: application/json'
    );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_URL, $jiraHost . 'rest/api/2/issue/');
    curl_setopt($ch, CURLOPT_USERPWD, "$jiraUsername:$jiraPassword");
    $response = curl_exec($ch);
    $error = curl_error($ch);
    if (!empty($error)) {
        throw new Exception('EQ Error: ' . $error);
    }
    curl_close($ch)

2. Getting all issues for user

        $ch = curl_init();
        $headers = array(
            'Accept: application/json',
            'Content-Type: application/json'
        );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $jiraHost . 'rest/api/2/search?jql=' . urlencode('labels in (API, 'IvanGospodinow')'));
        curl_setopt($ch, CURLOPT_USERPWD, "$jiraUsername:$jiraPassword");
        $response = curl_exec($ch);
        curl_close($ch);

Simple as that.

tags: , ,
posted in php by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /home/c2kblate/sites/ivangospodinow.com/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org