I am trying to use a new version of the Jira REST API to pull Jira Issue ID and the comments from the Issue ID using python.
I have my email and token used for access already.
I am struggling to set up the code correctly to get a positive response and then to also break down the paginated responses into one table.
Any help would be greatly appreciated!
Hello, here is a working example that you can use:
import requests
from requests.auth import HTTPBasicAuth
JIRA_URL = "Jira URL"
EMAIL = "your_email"
API_TOKEN = "your_api_token"
PROJECT_KEY = "PROJECT"
MAX_RESULTS = 5
auth = HTTPBasicAuth(EMAIL, API_TOKEN)
headers = {"Accept": "application/json"}
def get_latest_issues(project_key, max_results=5):
url = f"{JIRA_URL}/rest/api/3/search/jql"
params = {
"jql": f"project={project_key} ORDER BY created DESC",
"maxResults": max_results,
"fields": "summary"
}
response = requests.get(url, headers=headers, auth=auth, params=params)
response.raise_for_status()
return response.json()["issues"]
def get_issue_comments(issue_key):
url = f"{JIRA_URL}/rest/api/3/issue/{issue_key}/comment"
response = requests.get(url, headers=headers, auth=auth)
response.raise_for_status()
return response.json()["comments"]
latest_issues = get_latest_issues(PROJECT_KEY, MAX_RESULTS)
issue_comments = {}
for issue in latest_issues:
key = issue["key"]
comments = get_issue_comments(key)
issue_comments[key] = comments
print(f"Issue: {key}")
for c in comments:
author = c['author']['displayName']
body = ""
if c['body']['content']:
try:
body = c['body']['content'][0]['content'][0]['text']
except (KeyError, IndexError, TypeError):
body = "[Non-standard comment format]"
print(f" Comment by {author}: {body}")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.