To enhance the provided script and make it capable of handling nested structures or more complex relationships in the JSON output, we can implement a few modifications. Here’s how the script could be modified:
- Nesting Assignee and Issue Type: Instead of just extracting the
assignee and issuetype, we can nest these within their own objects for clarity.
- Adding an Array for Comments: If you wish to include comments for each issue, you could fetch that data (though that requires an additional API call).
- Improved Error Handling: Ensure the script gracefully handles errors while fetching projects or issues.
Here is the modified version of your script incorporating these changes:
import os
import requests
import json
def main():
# Loading the Env Variables
jira_domain = os.getenv("JIRA_DOMAIN")
api_token = os.getenv("JIRA_API_TOKEN")
username = os.getenv("JIRA_USERNAME")
# Validate environment variables
if not jira_domain or not api_token or not username:
print("Error: Missing required environment variables")
return
# Setting up the API endpoint and headers for fetching projects
projects_url = f"{jira_domain}/rest/api/3/project"
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
all_filtered_issues = []
try:
# Get all projects
project_response = requests.get(projects_url, headers=headers, auth=requests.auth.HTTPBasicAuth(username, api_token))
# Check for successful project retrieval
if project_response.status_code == 200:
projects = project_response.json()
project_keys = [project["id"] for project in projects] # Extract all project keys
print(f"Found project keys: {project_keys}")
# Setting up the API endpoint for issues
issues_url = f"{jira_domain}/rest/api/3/search"
# Loop through each project key and fetch issues
for project_key in project_keys:
print(f"Fetching issues for project: {project_key}")
# Setting up JQL for each project
query = {
"jql": f"project = {project_key}",
"fields": ["issuetype", "project", "assignee", "description", "comments"]
}
# Make the API request for each project
response = requests.get(issues_url, headers=headers, params=query, auth=requests.auth.HTTPBasicAuth(username, api_token))
# Process response
if response.status_code == 200:
tasks = response.json()
filtered_issues = []
# Extract required fields
for issue in tasks.get('issues', []):
filtered_data = {
"id": issue["id"],
"key": issue["key"],
"project": {
"id": issue["fields"]["project"]["id"],
"name": issue["fields"]["project"]["name"],
"key": issue["fields"]["project"]["key"]
},
"assignee": {
"name": issue["fields"]["assignee"]["displayName"] if issue["fields"].get("assignee") else None,
"email": issue["fields"]["assignee"]["emailAddress"] if issue["fields"].get("assignee") else None
},
"issuetype": {
"name": issue["fields"]["issuetype"]["name"],
"id": issue["fields"]["issuetype"]["id"]
},
"description": issue["fields"].get("description"),
"comments": issue["fields"].get("comments", {}).get("comments", [])
}
filtered_issues.append(filtered_data)
# Append fetched issues to the all_filtered_issues list
all_filtered_issues.extend(filtered_issues)
print(f"Fetched {len(filtered_issues)} issues for project: {project_key}")
else:
print(f"Failed to fetch data for project {project_key}:", response.status_code, response.text)
# Save all issues to a single output file
output_file = "filtered_jira_tasks.json"
with open(output_file, "w") as json_file:
json.dump({"issues": all_filtered_issues}, json_file, indent=4)
print(f"Filtered data for all projects saved to {output_file}")
print(json.dumps(all_filtered_issues, indent=4))
else:
print("Failed to fetch projects:", project_response.status_code, project_response.text)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()
Key Modifications:
-
Nesting Structure:
- The
assignee and issuetype fields have been converted to objects, containing relevant identifiers and display names.
-
Comments Handling:
- The comments field has been added. It extracts the comments for each issue, wrapping them in an array for easier usage.
-
Improved Readability:
- Clear separation and nesting make the output JSON more structured and understandable.
-
Null Checks:
- Where applicable, conditional checks (
if issue["fields"].get("assignee")) to avoid potential errors due to missing fields.
These changes will help you manage more complex data structures effectively during your Jira data export to JSON.