Walter U.
Walter U.

Reputation: 341

Get list of all JIRA issues (python)

I am trying to get a list of all JIRA issues so that I may iterate through them in the following manner:

from jira import JIRA

jira = JIRA(basic_auth=('username', 'password'), options={'server':'https://MY_JIRA.atlassian.net'})

issue = jira.issue('ISSUE_KEY')
print(issue.fields.project.key) 
print(issue.fields.issuetype.name) 
print(issue.fields.reporter.displayName)
print(issue.fields.summary)
print(issue.fields.comment.comments)

The code above returns the desired fields (but only an issue at a time), however, I need to be able to pass a list of all issue keys into:

issue = jira.issue('ISSUE_KEY')

The idea is to write a for loop that would go through this list and print the indicated fields.

I have not been able to populate this list.

Can someone point me in the right direction please?

Upvotes: 7

Views: 51205

Answers (5)

Mani Abi Anand
Mani Abi Anand

Reputation: 1335

options = {'server': 'YOUR SERVER NAME'}
jira = JIRA(options, basic_auth=('YOUR EMAIL', 'YOUR PASSWORD'))
size = 100
initial = 0
while True:
    start= initial*size
    issues = jira.search_issues('project=<NAME OR ID>',  start,size)
    if len(issues) == 0:
        break
    initial += 1
    for issue in issues:
        print 'ticket-no=',issue
        print 'IssueType=',issue.fields.issuetype.name
        print 'Status=',issue.fields.status.name
        print 'Summary=',issue.fields.summary
       

The first 3 arguments of jira.search_issues() are the jql query, starting index (0 based hence the need for multiplying on line 6) and the maximum number of results.

Upvotes: 8

frans
frans

Reputation: 9808

Starting with Python3.8 reading all issues can be done relatively short and elegant:

    issues = []
    while issues_chunk := jira.search_issues('project=PROJ', startAt=len(issues)):
        issues += list(issue issues_chunk)

(since we need len(issues) in every step we cannot use a list comprehension, can we?

Together with initialization and cashing and "preprocessing" (e.g. just taking issue.raw) you could write something like this:

jira = jira.JIRA(
    server="https://jira.at-home.com",
    basic_auth=json.load(open(os.path.expanduser("~/.jira-credentials")))
    validate=True,
)

issues = json.load(open("jira_issues.json"))

while issues_chunk := jira.search_issues('project=PROJ', startAt=len(issues)):
    issues += [issue.raw for issue issues_chunk]
    
json.dump(issues, open("jira_issues.json", "w"))

Upvotes: 2

Ivan Bryzzhin
Ivan Bryzzhin

Reputation: 2155

def get_all_issues(jira_client, project_name, fields):
    issues = []
    i = 0
    chunk_size = 100
    while True:
        chunk = jira_client.search_issues(f'project = {project_name}', startAt=i, maxResults=chunk_size, fields=fields)
        i += chunk_size
        issues += chunk.iterable
        if i >= chunk.total:
            break
    return issues
issues = get_all_issues(jira, 'JIR', ["id", "fixVersion"])

Upvotes: 18

ZeddZull
ZeddZull

Reputation: 288

You can use the jira.search_issues() method to pass in a JQL query. It will return the list of issues matching the JQL:

issues_in_proj = jira.search_issues('project=PROJ')

This will give you a list of issues that you can iterate through

Upvotes: 3

Roberto Russo
Roberto Russo

Reputation: 853

You can execute a search instead of a single issue get.

Let's say your project key is PRO-KEY, to perform a search, you have to use this query:

https://MY_JIRA.atlassian.net/rest/api/2/search?jql=project=PRO-KEY

This will return the first 50 issues of the PRO-KEY and a number, in the field maxResults, of the total number of issues present.

Taken than number, you can perform others searches adding the to the previous query:

&startAt=50

With this new parameter you will be able to fetch the issues from 51 to 100 (or 50 to 99 if you consider the first issue 0).

The next query will be &startAt=100 and so on until you reach fetch all the issues in PRO-KEY.

If you wish to fetch more than 50 issues, add to the query:

&maxResults=200

Upvotes: 3

Related Questions