How to get total hours spent on an issue for a user JIRA REST API

Hassan Khan picture Hassan Khan · Mar 22, 2013 · Viewed 12k times · Source

I'm quite new to JIRA's REST API, I wanted to get the total time spent on an issue by a user. I tried using https://jira.domain.com/rest/api/2/search?jql=assignee="my.name", and I get the timespent field in the returned JSON response, but it displays a value which doesn't make any sense. In this case, the timespent field says 62760, when the actual time spent by me on the issue is less than that, the search is returning the total time spent on the issue, rather than just by me. How can I make it show just my time?

EDIT: Is there any way to do this with JQL, without having to do another API call for each issue?

Answer

Volodymyr Krupach picture Volodymyr Krupach · Jan 26, 2014

I understand that I am late for one year :-). Posting to help others who could face the same problem.

REST /rest/api/2/search?jql=assignee="my.name" will return worklogs of other users and could omit worklogs of "my.name" users. In JIRA you do not need to be assignee to report a time on an issue.

Looks like the easiest way to get "total hours spent on an issue for a user JIRA REST API" is to use:

/rest/api/2/issue/ issue.key /worklog

and then iterate worklogs filtering them by worklog.author.name. Good side of the api/2/issue/ issue.key /worklog is that it returns all worklogs without utilizing startAt and maxResults pagination. At least it's true for my local development JIRA where I generated about 1200 worklog records for a single issue. But you need to check it for your JIRA installation since the sartAt and maxResults are in docs for the method: https://docs.atlassian.com/jira/REST/latest/#d2e1411

To get worklogs for a set of users and issues I use

/rest/api/2/search?jql=updated > 2014-01-20 and project in (MWC,MWCS,RND) and timespent > 0&fields=summary,worklog&maxResults=1000"

Where 2014-01-20 is the first date in a period, project and timespent conditions refine filtering. This method utilize pagination and it will ignore maxResults that you pass if it's larger than configured on JIRA side. Also worklogs returned along with issues are paged with 20 records per page. So to get reporting I make search?jql= call, iterate worklogs for returned issues, when issue.fields.worklog.maxResults < issue.fields.worklog.total I call issue/ issue.key /worklog to get all worklogs for an issue. And for sure you need to filter based on worklog.author.name and worklog.started fields.