Trouble extracting worklog comments using Python-Jira library

I’m working on a Python script to pull worklog data from Jira Service Management Cloud. The Python-Jira library is great, but I’m stuck on one thing: getting the comments for each worklog entry.

Here’s what I’ve got so far:

from jira import JIRA
import datetime
import csv

# Set up Jira connection
jira = JIRA('https://mycompany.atlassian.net', basic_auth=('[email protected]', 'my_api_token'))

# Date range for query
start = datetime.date(2024, 3, 1)
end = datetime.date(2024, 3, 31)

# JQL query
query = f'project = HELPDESK AND type in (Incident, "Service Request") AND status = Done AND resolved >= {start} AND resolved <= {end}'

# Fetch issues
issues = jira.search_issues(query, maxResults=500)

# Process worklogs
for issue in issues:
    worklogs = jira.worklogs(issue.id)
    for log in worklogs:
        # How do I get the comment here?
        print(f'Issue: {issue.key}, Author: {log.author.displayName}, Time: {log.timeSpent}')

The code runs fine, but I can’t figure out how to access the comment for each worklog. I’ve tried log.comment and log['comment'], but neither works. The docs aren’t very clear on this.

Any ideas on how to grab those comments? Thanks!

I’ve dealt with this exact problem in a recent project. The Python-Jira library doesn’t expose worklog comments directly, but there’s a way around it. You need to use the ‘getWorklogs()’ method on each issue, which returns more detailed worklog information.

Here’s how you can modify your code:

for issue in issues:
    worklogs = jira.issue(issue.id).getWorklogs()
    for log in worklogs:
        comment = log.comment if hasattr(log, 'comment') else 'No comment'
        print(f'Issue: {issue.key}, Author: {log.author.displayName}, Time: {log.timeSpent}, Comment: {comment}')

This approach fetches the full worklog data for each issue, including comments. It’s more efficient than accessing raw JSON and should be more reliable across different Jira versions. Just remember to handle cases where a comment might not exist.

I’ve run into this issue before with the Python-Jira library. The worklog comments aren’t directly accessible through the ‘log’ object you get from jira.worklogs(). Here’s a workaround I’ve used:

Try fetching the full worklog data using the issue.raw[‘fields’][‘worklog’] attribute. This should give you access to the comments. You might need to adjust your code like this:

for issue in issues:
    worklogs = issue.raw['fields']['worklog']['worklogs']
    for log in worklogs:
        comment = log.get('comment', '')
        print(f'Issue: {issue.key}, Author: {log['author']['displayName']}, Time: {log['timeSpent']}, Comment: {comment}')

This method accesses the raw JSON data returned by the Jira API, which includes the comments. It’s not as elegant as using the library’s methods, but it gets the job done. Just be aware that this approach might be more sensitive to changes in the Jira API structure.

hey swiftcoder, i ran into this too. try using the jira.issue() method to get the full issue data, then access the worklogs like this:

full_issue = jira.issue(issue.id)
for worklog in full_issue.fields.worklog.worklogs:
    comment = worklog.comment
    print(f'Comment: {comment}')

this should give u the comments. hope it helps!