12

I'm writing an application using PHP and the Jira REST API which is required to generate a report for a particular period of time with the accumulation of hours spent by a person on a particular project.

For this I will need a call which will give something like this.

e.g: For the period 01/01/2012 - 31/01/2012 give me the worklogs for project X.

The method I found so far, was to get the updated issues after the start date and filter the worklogs for each issue by the period again.

Is there a better alternative?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
hpd
  • 475
  • 1
  • 4
  • 16
  • 5
    It's a shame Jira does not support such basic feature through API. – Josef Sábl Mar 17 '14 at 16:26
  • I just realized your method will not work and there actually seems to be no viable solution (apart from traversing ALL of the issues in the history which is not viable). The problem is that your solution will only work when your TO_DATE is now. You cannot use this method for the period in the past as issues that have been updated after your TO_DATE will not count althought they might have worklogs. Another problem is that Jira allows to enter worked time to different dates than NOW, query to filter tasks updated in the given period will not work then. – Josef Sábl Mar 18 '14 at 15:06
  • instead of "get the updated issues" , filter by worklogDate : add to your search jql : 'AND worklogDate > + AND worklogDate < ' Then add to params: "&fields=worklog" – Elazaron Nov 15 '18 at 11:55

4 Answers4

4

As many have said, there's no direct way. However, if you narrow down the search space efficiently, it's not so bad. The following PHP code runs quite fast on my setup, but of course, your mileage may vary:

<?php
$server   = 'jira.myserver.com';
$fromDate = '2012-01-01';
$toDate   = '2012-01-31';
$project  = 'X';
$assignee = 'bob';

$username = 'my_name';
$password = 'my_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 < $toDate  AND  updated > $fromDate
#  AND timespent > 0
curl_setopt($curl, CURLOPT_URL, 
            "https://$server/rest/api/2/search?startIndex=0&jql=".
            "assignee+%3D+$assignee+and+project+%3D+$project+".
            "and+created+%3C+$toDate+and+updated+%3E+$fromDate+".
            "and+timespent+%3E+0&fields=key&maxResults=1000");

$issues = json_decode(curl_exec($curl), true);
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;
    }
}
# Show Result:
#  echo json_encode($periodLog);
#  var_dump($periodLog);
?>
mstormo
  • 51
  • 1
  • Had pretty much the same idea using MS-HTAs... you just saved me a lot of documentation reading. – Jefferey Cave Feb 26 '16 at 22:11
  • 1
    One problem of this script is that some of the issues the user worked on may be already reassigned to other users and so they won't be included in the result. – Sebastian Mar 31 '16 at 15:18
3

If you can't find the an out-of-the-box function that does what you've asked for, I can think of three other solutions other than yours:

  1. Query the DB directly so you could get the work logs using one query. Be sure not to insert/delete/update the DB directly, but only to query it.
  2. Use something like Jira Scripting Suite or Behaviours Plugin to add scripts that will write the work-logs somewhere on the disk. Then use another app to read the written information from the disk and display it to the users.
  3. Use the Tempo plugin
Kuf
  • 17,318
  • 6
  • 67
  • 91
2

It is worth pointing out that Jira queries have an expand option which allows you to specify which fields you want attached to your search:

// Javascript
$jql = 'project = MyProject and updated > 2016-02-01 and updated < 2016-03-01';

// note this definition
$fields = 'key,summary,worklog';

$query = "https://{server}/rest/api/2/search?maxResults=100&fields={fields}&jql={jql}"
  .replace(/{server}/g,$server)
  .replace(/{jql}/g,encodeURIComponent($jql))
  .replace(/{fields}/g,$fields)
  ;

The returned JSON object returned will be a list of tickets, and each ticket will have a collection of work items attached (potentially zero length).

Javascript rather than PHP, but the same idea holds:

function getJql(params){
    $.ajax({
        url: getJiraUrl() 
            + "/rest/api/2/search?startIndex=0&fields=worklog,assignee,status,key,summary&maxResults=1000&jql=" 
            + encodeURI(params.jql),
        success: function (resp) {
            resp.issues.forEach(function(issue) {
                issue.fields.worklog.worklogs.forEach(function(work){
                    alert(JSON.stringify(work));
                    db.AddWork(work);
                });
            });
        }
    });
}

posted on GitLab: https://gitlab.com/jefferey-cave/ProductivityBlockers/blob/5c4cb33276e8403443d4d766fc94ab2f92292da6/plugin-data-jira.js

Jefferey Cave
  • 2,507
  • 1
  • 27
  • 46
0

The approach I've personally used for the same kind of an application is to get ALL records from JIRA on a weekly basis and then generate reports from the database they're stored in.

This way you will also have the data available if a major JIRA crash occurs. Our company went through such a problem with a OnDemand instance when a RAID Array burned and most of the data was unrecoverable.

Borislav Sabev
  • 4,776
  • 1
  • 24
  • 30
  • The Jira that is being used is a hosted one. So my interest is to get only the worklog data for a particular time for reporting and not have to query them by issues and filter – hpd Oct 10 '12 at 04:19