I’m working on a Python script to pull worklog data from Jira Service Management Cloud using the jira-python library. Most of the data comes through fine, but I’m stuck on getting the comments for each worklog entry.
Here’s what I’ve tried:
import jira
import csv
import datetime
# Set up Jira connection
jira_client = jira.JIRA(url='your_jira_url', basic_auth=('your_email', 'your_token'))
# Query issues
start_date = datetime.date(2024, 2, 1)
end_date = datetime.date(2024, 2, 27)
query = f"project = XYZ AND type in (Incident, 'Service Request') AND status = Done AND resolved >= {start_date} AND resolved <= {end_date}"
issues = jira_client.search_issues(query, maxResults=1000)
# Process worklogs
with open('jira_export.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
writer.writerow(['Issue Key', 'Worklog ID', 'Author', 'Time Spent', 'Comment'])
for issue in issues:
for worklog in issue.fields.worklog.worklogs:
try:
comment = worklog.comment # This line causes an error
writer.writerow([issue.key, worklog.id, worklog.author.displayName, worklog.timeSpent, comment])
except AttributeError:
print(f"No comment found for worklog {worklog.id}")
When I try to access worklog.comment, I get an AttributeError saying the Worklog object has no ‘comment’ attribute. The documentation isn’t clear on this. Does anyone have any ideas on how to properly access the worklog comments?
I’ve encountered a similar issue when working with Jira’s API. The problem is that the worklog object doesn’t automatically include the comment field when fetched. You need to explicitly request it.
Try modifying your code to use the jira_client.worklogs() method instead. This allows you to specify which fields you want to retrieve, including comments. Here’s an example:
for issue in issues:
worklogs = jira_client.worklogs(issue.id)
for worklog in worklogs:
comment = worklog.comment if hasattr(worklog, 'comment') else ''
writer.writerow([issue.key, worklog.id, worklog.author.displayName, worklog.timeSpent, comment])
This approach should give you access to the worklog comments. Remember to handle cases where a comment might not exist to avoid potential errors.
I’ve been down this road before, and it can be frustrating. One thing that worked for me was using the ‘expand’ parameter when fetching issues. This tells Jira to include additional fields, including worklog comments. Try modifying your search_issues call like this:
Then, you should be able to access the comment directly:
comment = worklog.comment if hasattr(worklog, ‘comment’) else ‘’
This approach is more efficient than fetching each worklog separately, especially if you’re dealing with a large number of issues. It might take some trial and error, but it should get you the comments you need without significantly impacting performance.
hey dancingbird, i ran into this too. try using the jira_client.worklog(worklog.id) method to fetch full worklog details with comments. its a bit slower but works. like this: