-->

How to get all work logs for a period of time from

2020-06-09 07:55发布

问题:

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?

回答1:

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


回答2:

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);
?>


回答3:

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



回答4:

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.



标签: rest jira