Skip to content

Instantly share code, notes, and snippets.

@isalgueiro
Created November 7, 2017 17:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isalgueiro/cd69c30ce5623b7ce1d26b565e0e1ed7 to your computer and use it in GitHub Desktop.
Save isalgueiro/cd69c30ce5623b7ce1d26b565e0e1ed7 to your computer and use it in GitHub Desktop.
How to get list of worklogs through JIRA REST API
<?php
// https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-get-list-of-worklogs-through-JIRA-REST-API/qaq-p/533633
$server = 'jira.example.com';
$fromDate = '2017-11-07';
$toDate = '2012-11-08';
$project = 'X';
$assignee = 'bob';
$username = 'username';
$password = 'password';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
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);
# Give me up to 1000 search results with the Key, where
# assignee = $assignee AND project = $project
# AND created &lt; $toDate AND updated &gt; $fromDate
# AND timespent &gt; 0
$url = "https://$server/rest/api/2/search?startIndex=0&amp;jql=".
"assignee+%3D+$assignee".
// "+and+project+%3D+$project".
"+and+created+%3C+$toDate+and+updated+%3E+$fromDate".
"+and+timespent+%3E+0&amp;fields=key&amp;maxResults=1000";
echo "$url\n";
curl_setopt($curl, CURLOPT_URL, $url);
$issues = json_decode(curl_exec($curl), true);
if (!$issues) {
exit(1);
}
$periodLog = [];
foreach ($issues['issues'] as $issue) {
$key = $issue['key'];
# for each issue in result, give me the full worklog for that issue
curl_setopt($curl, CURLOPT_URL,
"https://$server/rest/api/2/issue/$key/worklog");
$worklog = json_decode(curl_exec($curl), true);
foreach ($worklog['worklogs'] as $entry) {
$shortDate = substr($entry['started'], 0, 10);
# keep a worklog entry on $key item,
# iff within the search time period
if ($shortDate >= $fromDate && $shortDate <= $toDate)
$periodLog[$key][] = $entry;
}
}
# echo json_encode($periodLog);
var_dump($periodLog);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment