Programatically pull work-logs of all the users of admin account. Need REST API

Rashi Goyal September 25, 2020

Hi,

I am looking for the API from where I can fetch work-logs of all the users under the ADMIN account.

I want to generate a report having details :

  • Full Name
  • Jira User Name
  • Email ID
  • Project Manager
  • Skill Set

Please share the reference API asap.

Thanks & Regards,

Rashi

2 answers

1 accepted

1 vote
Answer accepted
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 25, 2020

Hi @Rashi Goyal 

You seem to contradict what you're looking for.

To me work logs are the time that a user has logged against various issues, but then in your report you only mention user-specific info. Using the following API call

rest/api/3/user/search?query=%

will return all users that you have permissions to view. It will return the accountId and dispayName for each user. The other items are not available (project manager and skill set are not part of Jira data)

Rashi Goyal September 25, 2020

Hi @Warren ,

Thanks for the answer.

I want to generate a report where I can list user and their time logged for the particular day.

Like User, A logged 7 hrs for the particular day.

So may I consider the above-mentioned solution for the same? Please confirm

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2020

Hi @Rashi Goyal 

Okay, I now understand what you're trying to do.

The API query that I gave above could be used to get the list of all users, or you can just supply the list. 

If you use Tempo (Jira's time tracking app) then getting logs is quite simple, you use the following API call

https://api.tempo.io/core/3/worklogs/user/UserID?from=StartDate&to=EndDate

where the dates have the format yyyy-MM-dd

If you don't use Tempo, then you need to crawl through the history of each ticket to get the details. You'll set up an API call and include expand=changelog which gives you access to the history - you will then need to work through each history checking for logged time and build up each user's count.

Like Rashi Goyal likes this
Rashi Goyal September 29, 2020

Hi @Warren ,

I am not able to get the records of a particular date that I am passing. Please check my code-shared below:

$url = "https://netsmartz.atlassian.net/rest/api/2/search";

$r = '{
"jql": "project = NFLTV AND worklogDate=2020-09-29",
"maxResults": 1,
"fields": [
"summary",
"status",
"assignee",
"worklog"
]
}';

$this->curl_method($url, $r);
die('------');

Here, the worklogDate filter is not working. Rest is working fine for me. Please suggest the solution for the same. I am using POST for this.

 

Thanks in advance 

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 29, 2020

Hi @Rashi Goyal 

Firstly, why are you using POST? You're just querying data, so it should be a GET. POST is used when creating or editing an issue.

Your date should be in double quotes (although it seems to be okay without), so

"jql": "project = NFLTV AND worklogDate="2020-09-29""

What you have in the "jql" section should work perfectly if you try it in the Issue Navigator within Jira - if it doesn't work there, you will at least get a meaningful error message

Like Rashi Goyal likes this
Rashi Goyal September 29, 2020

I am using query

$url = "https://xyz.atlassian.net/rest/api/2/search?jql=project=NFLTV&worklogDate=2020-09-29&fields=worklog&maxResults=1";

 

This is giving me an error

tdClass Object
(
    [errorMessages] => Array
        (
            [0] => No content to map to Object due to end of input
        )

)

Please suggest if I am doing something wrong.

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 29, 2020

Hi @Rashi Goyal 

The JQL part usually uses AND instead of &, although that does seem to work.

I believe that fields=worklog is causing your error, it should be expand=changelog as stated above. 

By default maxResults is 50 (I think), you don't really need to restrict it 1. You would typically use this parameter if you wanted more than 50, up to the maximum of 100.

Try the simplest query first and then build it up, so start with

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=NFLTV

and if it works, then add the bit to filter on the date

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=NFLTV AND worklogDate=2020-09-29

and then add the bit to get the history

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=NFLTV AND worklogDate=2020-09-29&expand=changelog

That way you will know which section is causing the error

Like Rashi Goyal likes this
Rashi Goyal September 29, 2020

Hi @Warren ,

Thanks for the prompt reply and help.

Please explain that when using this

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=NFLTV AND worklogDate=2020-09-29

which field will describe that this worklog data is of the particular date i.e. 29 as mentioned in our query.

Because I am getting complete details.

Thanks,

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 29, 2020

Hi @Rashi Goyal 

My previous reply suggested you slowly build up the API call to see where it's going wrong, which you haven't commented on here. 

First get the call working, without worrying about the details of what is returned. When you are able to run the full API call successfully

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=NFLTV AND worklogDate=2020-09-29&expand=changelog

then we can discuss the details of what is being returned

Like Rashi Goyal likes this
Rashi Goyal September 30, 2020

Hi @Warren,

Ya, I am able to call the API successfully with the step wise guide you mentioned in the previous comments. Thanks for that 

Please let me know what to do further.

Regards,

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2020

Hi @Rashi Goyal 

Okay good, that's progress ;-)

If you run the call

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=NFLTV AND worklogDate=2020-09-29&expand=changelog

part of the JSON returned will be histories. You will need to loop through all the histories checking for items like

 {
"field": "timespent",
"fieldtype": "jira",
"fieldId": "timespent",
"from": "4500",
"fromString": "4500",
"to": "6300",
"toString": "6300"
},

All times are in seconds, here the original amount of time logged (4500) is 1h 15m and it changed to (6300) 1h 45m, so 30m was logged. When you find an entry like this, you can check the displayName and created fields to get who logged the time and when they did.

Even though you've passed a date for worklogs, for a specific issue, it is giving the whole history so you will need to filter out time logged on other days.

I hope this is clear and helps you.

Like Rashi Goyal likes this
Rashi Goyal September 30, 2020

@Warren ya now I am able to create a clear picture.

Stuck in one thing i.e. from & to fields. For testing purpose, I logged 2 hours in my task and the result I got is 

[1] => stdClass Object
(
[field] => timespent
[fieldtype] => jira
[fieldId] => timespent
[from] => 135600
[fromString] => 135600
[to] => 142800
[toString] => 142800
)

from = 37.6666667

to = 39.6666667

So, total time logged =  2hrs that is correct but I also want to now that on what basis this from and to time is collected.

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2020

Hi @Rashi Goyal 

I'm pleased that you're seeing the numbers that you are expecting.

Unfortunately I don't understand your question - what do you mean by what basis?

Like Rashi Goyal likes this
Rashi Goyal September 30, 2020

@Warren,

I mean how do API collect the data of from and to variables.

As I have seen that sometimes from variable is empty.

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2020

Ah, okay, I understand your question now @Rashi Goyal 

If you create a new ticket, then log some time against it, the from will be blank (empty) and the to will have the amount of time logged. From then on, each time a worklog is added, the from should have the value of the previous time and shouldn't be blank.

This applies to most items - if you assign the ticket to someone for the first time (or add a label or add a component or ...), the from would be blank as well

Like Rashi Goyal likes this
Rashi Goyal September 30, 2020

@Warren thankyou my doubts are clear.

Will get back to you in case of any major concern.

Goodbye!

Rashi Goyal October 1, 2020

Hi @Warren,

Please tell one thing that if I am using API with URL

https://ab.atlassiacn.net/rest/api/2/search?jql=project=OPS&worklogDate=2020-09-30&expand=changelog&maxResults=2

Then this API will return me all the records in one go or pagination is working by default? 

Means I want to ensure that if I will get all records of the master account in one go or I have to add something custom.

Please help me out in this scenario.

Thanks

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2020

@Rashi Goyal 

Pagination is required to get all records, depending on your query and data.

By default, 50 records are returned at a time, but the API call that you've given has maxResults=2, so you will only get 2 records back.

I would suggest that :

  • you don't pass the maxResults parameter at all
  • you implement a paging mechanism because you will need it at some point.

You need to check the initial bit of JSON for the fields maxResults (tells you how many records have been returned) and total (tells you how many records there are in total)

{"expand":"schema,names","startAt":0,"maxResults":50,"total":9065,"issues"

If total is less than or equal to maxResults then you wouldn't need to paginate, otherwise you would.

For pagination, you pass in the parameter startAt. As you will see, your first API call defaults to startAt=0, so your next call would need to set startAt to 0 + 50 (i.e. startAt=50) and the call after that startAt=100 etc.

Rashi Goyal October 4, 2020

Hi @Warren ,

I checked the result i.e.

[expand] => schema,names
    [startAt] => 0
    [maxResults] => 7
    [total] => 707

If I am using maxresult=2 I am getting total as 707 so that means I have to paginate. Right?

Can you please guide me how to add pagination ?
It will be done using some custom php standards or jira provide us the way to directly pass it to the url ?

URL : "https://netsmartz.atlassian.net/rest/api/2/search?jql=project=SKL&worklogDate=2020-09-30&expand=changelog&maxResults=2";

Please share how to add pagination.

Also one more requirement, I want to fetch the email id of the users as well.Please guide for that also.

I want to implement this asap. So, it will be good if you can share the answer asap.

Thanks
Rashi
Michael Raj October 4, 2020

Hi @Warren ,

Mentioned by you "fields=worklog" is not giving an error. It's a regular field in Jira API to get up to 20 worklogs from the issue.

I think Rashi took it from my answer to his duplicate topic: https://community.atlassian.com/t5/Jira-Software-questions/API-Reference-required-to-get-the-worklogs-of-all-the-users/qaq-p/1490596

The idea is to collect worklogs using search method instead of going one-by-one. The most issues in my projects have less than 20 worklog entries to it's enough to get them all. If there is an issue with more than 20 worklog entries, I'm doing another round of one-by-one collection. This hybrid method (search + one-by-one) is more efficient in terms of tool used as well as Jira API requests number.

 

And since Rashi asks for worklogs in my opinion going into changelogs is way too much to get the results (less efficient).

 

Best

Michael

0 votes
Rashi Goyal October 4, 2020

Hi @Warren ,

Can you please check my above query and guide asap? I want to resolve this asap.

Rashi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 2020

Hi @Rashi Goyal 

Yes you're correct, you will need to paginate if you have 707 total records. I'm not sure why you keep passing in maxResults=2, that would mean over 350 API calls! My previous answer described how to paginate

For pagination, you pass in the parameter startAt. As you will see, your first API call defaults to startAt=0, so your next call would need to set startAt to 0 + 50 (i.e. startAt=50) and the call after that startAt=100 etc.

But I'll expand on that for you.

Using the API query that you've supplied, I'll expand it to show you what you need to do. For your first call, you would use 

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=SKL&worklogDate=2020-09-30&expand=changelog&maxResults=50&startAt=0

For your next call, it would be

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=SKL&worklogDate=2020-09-30&expand=changelog&maxResults=50&startAt=50

Then the next one would be

https://netsmartz.atlassian.net/rest/api/2/search?jql=project=SKL&worklogDate=2020-09-30&expand=changelog&maxResults=50&startAt=100

So you can see that with each successive call, the only change is startAt=xx, where xx increases by 50 each time because maxResults is set to 50. You will need to code your solution to keep increasing the startAt value until startAt > total.

Now for the user's ID, if you look at the screenshot below, each history has a section which lists the user that made the change, so you can get it from there without doing anything else

Screenshot_4.jpg

Michael Raj October 5, 2020

Or you can set maxResults to 1000 and it will work...

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 5, 2020

Hi @Michael Raj 

No that won't work! Since about 2017 Atlassian have limited the number of returned records. The absolute maximum you are able to get from one call is 100 records, but I believe that you need to have changed a setting within Jira and I didn't want to get into that with @Rashi Goyal at the moment. By default, the limit is set to 50, hence my description of getting 50 records at a time

Michael Raj October 5, 2020

Thanks @Warren for that info. Looks like my Jira has different limits.

Good to know!

 

Best

Michael

Rashi Goyal October 5, 2020

@Warren thanks..

I got an idea that we have to iterate our loop to get this pagination done.

maxresult I was passing because I want to check the array structure.

Now, I am working as mentioned above.

 

One more thing @Warren I need the email-id & project manager of the user, not user id. How to achieve that?

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 5, 2020

Hi @Rashi Goyal 

Sorry, I have no idea what email-id is - if you mean the email address of the user, that is no longer available because of GDPR concerns.

Also project manager is not a field that I'm aware of - if this is a customfield that you have added, then you will need to know the field number, because customfields are referenced in the API with the format customfield_12345. If it is one of your customfields, you should be able to find it in the JSON returned from your API call

Rashi Goyal October 5, 2020

@Warren while using JIRA I think every user's e-mail address is linked.

So, don't we have any way out in API Calls to get the same?

 

If not the project manager then I think it is possible to get an assignee?

 

If yes please let me know

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 5, 2020

Hi @Rashi Goyal 

No you are able to get the user's displayName, see my screenshot further up.

Yes the field assignee is available

Rashi Goyal October 5, 2020

Ok cool.

Thanks

Rashi

Rashi Goyal October 6, 2020

Hi @Warren 

Any other way to achieve this with only valid records of the particular date only.

Rashi

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events