Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to access comments from jira worklogs via python-jira

Kevin Munz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 17, 2024

I am trying to retreive worklog entries from Jira Service Management Cloud in python by using the jira-python library.

Everything works fine, except getting the comment from each worklog. The documentation is quite vague on this.

My code currently looks like this:

jira = JIRA(server, basic_auth=(email,token))

# define date filter
startDate = datetime.date(2024, 2, 1)
endDate = datetime.date(2024, 2, 27)

#define jql to filter issues
jql = "project in (XY) AND issuetype in ('[System] Incident', '[System] Service request') AND status = Closed AND resolutiondate >= {} AND resolutiondate <= {}".format(startDate, endDate)

#incremental fetch of issues and storage into issues_all
pos = 0;
batch = 100;
issues_all = []
while True:
    issues_batch = jira.search_issues(jql_str=jql, startAt=pos, maxResults=batch, fields=['worklog','customfield_10202','customfield_10198','customfield_10191'])
    if issues_batch == []:
        break
    else:
        issues_all += issues_batch
    pos += batch

with open('jiraExport.csv', 'w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file, delimiter=";")
    fields = ["Issue", "Issue ID","Warranty Basis","Warranty","Warranty Reason","Worklog ID", "Author Name", "Author Email","Date", "Time Spent", "Description"]

    writer.writerow(fields)

    for issue in issues_all:
        current_issue = jira.issue(issue)
        worklogs = current_issue.fields.worklog.worklogs
        for worklog in worklogs:
            worklog_fields = []
            worklog_fields.append(current_issue.key)
            worklog_fields.append(worklog.issueId)
            worklog_fields.append(current_issue.fields.customfield_10202)
            worklog_fields.append(current_issue.fields.customfield_10198)
            worklog_fields.append(current_issue.fields.customfield_10191)
            worklog_fields.append(worklog.id)
            worklog_fields.append(worklog.author.displayName)
            worklog_fields.append(worklog.author.emailAddress)
            worklog_fields.append(functions.datetime_to_date(worklog.started))
            worklog_fields.append(functions.seconds_to_industryHours(worklog.timeSpentSeconds))
            worklog_fields.append(worklog.comment) <-----
            writer.writerow(worklog_fields)



I found some stuff which referenced worklog.comment should work, but I always get the following error, when running my code:

Traceback (most recent call last):
  File "C:\Users\xy\AppData\Local\Programs\Python\Python312\Lib\site-packages\jira\resources.py", line 193, in __getattr__
    return self[item]  # type: ignore
           ~~~~^^^^^^
TypeError: 'Worklog' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\Development\Projects\Jira Interface\main.py", line 58, in <module>
    worklog_fields.append(worklog.comment)
                          ^^^^^^^^^^^^^^^
  File "C:\Users\xy\AppData\Local\Programs\Python\Python312\Lib\site-packages\jira\resources.py", line 198, in __getattr__
    raise AttributeError(
AttributeError: <class 'jira.resources.Worklog'> object has no attribute 'comment' ('Worklog' object is not subscriptable)

Any ideas how to get the comment of each worklog?

 

1 answer

1 accepted

1 vote
Answer accepted
Sebastian Krzewiński
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 17, 2024

Hi @Kevin Munz 

 

Did you check that this library is able to get worklog comment?

 

Regards,

Seba

Kevin Munz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 17, 2024

Hi @Sebastian Krzewiński 

Thanks for your reply. Obviously I checked this beforehand, but the documentation was quite limited.

But.. I figured it out in the meantime. The code is working, but I had a worklog in my project that had no comment. Therefore the worklog object had no "Comment" attribute.

By checking for the attribute, my code is now running:

if hasattr(worklog, 'comment') and worklog.raw['comment']:
     print("Worklog comment: ", worklog.raw['comment'])
else:
     print("Worklog has an empty comment or no comment attribute.")
Sebastian Krzewiński
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2024

Hi @Kevin Munz 

 

Thanks for nice explanation!

Suggest an answer

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

Atlassian Community Events