Return All Issues for All Jira Cloud Projects, Using Python (Quick Bite)

 

 

The challenge in working with ScriptRunner is that returning and exporting large amounts of data can be tricky. It’s doable, especially if you have access to the filesystem, but not always the best way to approach the issue.

Say you wanted to return a list of every issue for every project in the instance.  What if there were a million issues? 

It’s easier and cleaner to approach this with an external tool, such as a Python script.

This solution is geared toward interacting with Jira Cloud. Most of the concepts will work with Jira DC, but it’s not a direct translation.

 

import requests
import json
import base64

cloud_username = "<Atlassian Cloud login email"
cloud_token = "<Atlassian Cloud token"
cloud_url = "<Atlassian Cloud URL"


cloud_credentials_string = f'{cloud_username}:{cloud_token}'
cloud_input_bytes = cloud_credentials_string.encode('utf-8')
cloud_encoded_bytes = base64.b64encode(cloud_input_bytes)
cloud_encoded_string = cloud_encoded_bytes.decode('utf-8')


cloud_headers = {
    'Authorization': f'Basic {cloud_encoded_string}',
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}

cloud_login_request = requests.get(f'{cloud_url}/rest/api/latest/project', headers=cloud_headers)
#Initiate the connection requests to the server and cloud instances

project_json = json.loads(cloud_login_request.content)

project_ids = []

#/rest/api/2/search?jql=project=ABC&maxResults=1000
for project in project_json:
    if not project['isPrivate'] == True:
        project_key = project['key']

        get_info_request = requests.get(f'{cloud_url}/rest/api/2/search?jql=project={project_key}&maxResults=1000', headers=cloud_headers)

        results = json.loads(get_info_request.content)

        total_issues = results['total']
        start_at = 0

        paginate = True

        while(paginate):
            get_issues_request = requests.get(f'{cloud_url}/rest/api/2/search?jql=project={project_key}&maxResults=1000&startAt={start_at}', headers=cloud_headers)

            issues_json = json.loads(get_issues_request.content)

            for issue in issues_json['issues']:
                print(issue['id'] + " - " + issue['key'])

            if start_at >= total_issues:
                break
            else:
                start_at += 50

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *