Get worklog of individual user from jira using python

Hi Guys,

I want to get details logged by user on daily basis and details should be of last nine weeks.

Could you please suggest and check my code that I am going in right direction or not.

example :-

Key  worklogtime  logged_date   logged_by

203    2h                 2018-04-08       xyz

203    1h                 2018-04-07       abc

201     4h                2018-04-07       blaah

 

 

 

Piece of Code:-

 

import jira.client
from jira.client import JIRA

options = {'server': 'https:/example.com', 'verify':False}
jira = JIRA(options, basic_auth=('user', 'password))
issues_in_project = jira.search_issues('project=11372 and Assignee in(xyz,abc)',maxResults=1000)

for value in issues_in_project:
   try:

          worklogs = jira.worklogs(value.key)
          print worklogs.timespent,worklogs.update, worklogs.updateAuthor, worklogs.updated
except:
          print("no worklogs")

 

Got this error :-

Traceback (most recent call last):
File "jira_test_Time_log.py", line 88, in <module>
print worklogs.timespent,worklogs.author,worklogs.updated
AttributeError: 'list' object has no attribute 'timespent'
Exception Exception: Exception('Exception caught in workbook destructor. Explicit close() may be required for workbook.',) in <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x2bbafd0>> ignored

 

3 answers

0 vote
Warren Levy Community Champion Apr 09, 2018

Hi

I think that you need to use timeSpentSeconds rather than timespent (please note case sensitivity as well) because all Jira time is held in seconds. You can then convert to hours as required.

Hope this helps

@Shaun Seaver@Warren Levy  :- Please have a look below and please suggest.

I am using this :- 

for value in issues_in_project:
 worklogs = jira.worklogs(value.key)
 print worklogs.author

getting error as :-

Traceback (most recent call last):
File "jira_test_Time_log.py", line 89, in <module>
print worklogs.author
AttributeError: 'list' object has no attribute 'author'

I am not getting any output for any object lke author,timespent and timeSpentSeconds

 

But when I changed my Code :-

for value in issues_in_project:
 worklogs = jira.worklogs(value.key)
 print worklogs

It is giving the results like :-

[<JIRA Worklog: id=u'372113'>]

 

[<JIRA Worklog: id=u'374071'>, <JIRA Worklog: id=u'374364'>, <JIRA Worklog: id=u'374517'>, <JIRA Worklog: id=u'374819'>, <JIRA Worklog: id=u'374890'>, <JIRA Worklog: id=u'375013'>, <JIRA Worklog: id=u'375105'>, <JIRA Worklog: id=u'375347'>]

 

[<JIRA Worklog: id=u'374239'>, <JIRA Worklog: id=u'374240'>]

May be I am using wrong loop or something related to worklog

@Shaun Seaver @Warren Levy 

I want to do something like below piece of code :- want to iterate for number of worklogs entries for particular value.key

Code:-

i = 0
for value in issues_in_project:
for i in value:
print value.fields.worklog.worklogs[i].timeSpent, value.fields.worklog.worklogs[i].updated,value.fields.worklog.worklogs[i].updateAuthor,value.key
i = i + 1

Got error :-

Traceback (most recent call last):
File "jira_test_Time_log.py", line 89, in <module>
for i in value:

TypeError: 'Issue' object is not iterable

 

But I was able to get results from below for 2 indexes:-

for value in issues_in_project:

print value.fields.worklog.worklogs[0].timeSpent, value.fields.worklog.worklogs[0].updated,value.fields.worklog.worklogs[0].updateAuthor,value.key

print value.fields.worklog.worklogs[1].timeSpent, value.fields.worklog.worklogs[1].updated,value.fields.worklog.worklogs[1].updateAuthor,value.key

Hi Himanshu,

 

I'm able to return the worklog data related to search results using the following code.

NOTE:  The fields="worklog" option needs to be included in the issues_in_project definition for the code to work. 

 

issues_in_project = jira.search_issues('project=11372 and Assignee in(xyz,abc)',maxResults=1000,fields="worklog")

for value in issues_in_project:
try:
print value.fields.worklog.worklogs[0].timeSpent, value.fields.worklog.worklogs[0].update, value.fields.worklog.worklogs[0].updateAuthor
except:
print "Issue has no worklogs"

If you need to retrieve all the worklogs for a given issue you can loop over the index:

 

for value in issues_in_project:
log_entry_count = len(value.fields.worklog.worklogs)
for i in range(log_entry_count):
print value.key, value.fields.worklog.worklogs[i].timeSpent, value.fields.worklog.worklogs[i].updated, value.fields.worklog.worklogs[i].updateAuthor

@Shaun Seaver @Warren Levy I want to do something like below piece of code :- want to iterate for number of worklogs entries for particular value.key

Code:-

i = 0
for value in issues_in_project:
for i in value:
print value.fields.worklog.worklogs[i].timeSpent, value.fields.worklog.worklogs[i].updated,value.fields.worklog.worklogs[i].updateAuthor,value.key
i = i + 1

Got error :-

Traceback (most recent call last):
File "jira_test_Time_log.py", line 89, in <module>
for i in value:

TypeError: 'Issue' object is not iterable

 

But I was able to get results from below for 2 indexes:-

for value in issues_in_project:

print value.fields.worklog.worklogs[0].timeSpent, value.fields.worklog.worklogs[0].updated,value.fields.worklog.worklogs[0].updateAuthor,value.key

print value.fields.worklog.worklogs[1].timeSpent, value.fields.worklog.worklogs[1].updated,value.fields.worklog.worklogs[1].updateAuthor,value.key

Not able to post things

I want to do something like below piece of code :- want to iterate for number of worklogs entries for particular value.key

Code:-

i = 0
for value in issues_in_project:
for i in value:
print value.fields.worklog.worklogs[i].timeSpent, value.fields.worklog.worklogs[i].updated,value.fields.worklog.worklogs[i].updateAuthor,value.key
i = i + 1

Got error :-

Traceback (most recent call last):
File "jira_test_Time_log.py", line 89, in <module>
for i in value:

TypeError: 'Issue' object is not iterable

 

But I was able to get results from below for 2 indexes:-

for value in issues_in_project:

print value.fields.worklog.worklogs[0].timeSpent, value.fields.worklog.worklogs[0].updated,value.fields.worklog.worklogs[0].updateAuthor,value.key

print value.fields.worklog.worklogs[1].timeSpent, value.fields.worklog.worklogs[1].updated,value.fields.worklog.worklogs[1].updateAuthor,value.key

@Himanshu Pant Sorry that you seemed to be unable to post to this thread.  However it appears our system flagged your messages as spam for some reason.  Once this happens, all the subsequent messages you attempted to post were then also flagged as spam.   I have restored these messages, and in turn removed the duplicates as best I could as a moderator.   This means we had to remove a number of duplicate posts from this thread.

Sorry this happened, but it is still a manual process to unflag such posts and then manage them.

0 vote
Warren Levy Community Champion Apr 10, 2018

Hi

I think I've spotted what may be the problem, although I'm not familiar with the language you're using, but try this

for value in issues_in_project:
for i in value.fields.worklog.worklogs:

print i.timeSpentSeconds

The second for has changed - you need to tell the second loop where to get the values

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

478 views 1 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you