Can't retrieve worklog comments using python-jira library

I’m working with the python-jira library to extract worklog data from Jira Cloud. My script successfully pulls most worklog information but I’m stuck on getting the comment field from each worklog entry.

Here’s my current approach:

connection = JIRA(jira_url, basic_auth=(username, api_token))

# set up date range
from_date = datetime.date(2024, 3, 1)
to_date = datetime.date(2024, 3, 31)

# create query for filtering tickets
query = "project = ABC AND type in ('Bug', 'Task') AND status = Done AND resolved >= {} AND resolved <= {}".format(from_date, to_date)

# batch processing to get all tickets
offset = 0
page_size = 50
all_tickets = []
while True:
    ticket_batch = connection.search_issues(jql_str=query, startAt=offset, maxResults=page_size, fields=['worklog', 'customfield_12345'])
    if not ticket_batch:
        break
    all_tickets.extend(ticket_batch)
    offset += page_size

for ticket in all_tickets:
    ticket_details = connection.issue(ticket)
    work_entries = ticket_details.fields.worklog.worklogs
    for entry in work_entries:
        print(f"Time: {entry.timeSpentSeconds}")
        print(f"Author: {entry.author.displayName}")
        print(f"Comment: {entry.comment}")  # This line fails

When I try to access entry.comment, I get an AttributeError saying the Worklog object doesn’t have a comment attribute. The documentation isn’t clear about how to properly access worklog comments. Has anyone figured out the right way to get these comments?

The python-jira library doesn’t fetch complete worklog data by default. When you use ticket_details.fields.worklog.worklogs, you get a truncated version without comments due to API limits.

I fixed this by fetching each worklog individually using its ID. After you get your work_entries, make separate API calls for each one:

for entry in work_entries:
    full_worklog = connection.worklog(ticket.key, entry.id)
    print(f"Time: {full_worklog.timeSpentSeconds}")
    print(f"Author: {full_worklog.author.displayName}")
    print(f"Comment: {full_worklog.comment}")

This creates way more API calls though, so add rate limiting if you’re processing tons of data. The individual worklog endpoint gives you complete objects with all fields, including the comment you need.

yeah, oscars spot on about the api limit. you can also use the expand parameter when fetching the issue to grab all worklog data upfront. try connection.issue(ticket, expand='changelog,worklog') - this pulls complete worklog objects with comments so you dont need separate calls for each entry.