Get list of all JIRA issues (python)

23,174

Solution 1

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

Solution 2

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"])

Solution 3

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.

Solution 4

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

Solution 5

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"))
Share:
23,174
Walter U.
Author by

Walter U.

Updated on May 05, 2021

Comments

  • Walter U.
    Walter U. about 3 years

    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?

  • Walter U.
    Walter U. over 6 years
    Ruso Where in the code would that search query go? I am really trying to get all bugs, not just the ones associated with some project key.
  • Roberto Russo
    Roberto Russo over 6 years
    @JohnWayne360 jql=project=PRO-KEY and issueType = Bug is the query to search only for the bugs.
  • Walter U.
    Walter U. over 6 years
    @ Roberto Russo: Say I want bugs, improvements, story, etc.. Would I have to update those every time? I am doing jira.search_issues('issuetype=Bug', maxResults=200, startAt=100) but say I want issues regardless of 'issueType'
  • Roberto Russo
    Roberto Russo over 6 years
    @JohnWayne360 Remove issueType then, it should fetch you every issues in PRO-KEY
  • Walter U.
    Walter U. over 6 years
    @ Roberto Russo: Is it possible to get all issues regardless of issue key? I'd like to avoid having to update PRO-KEY
  • Roberto Russo
    Roberto Russo over 6 years
    PRO-KEY is the project key, not the issue key.
  • Roberto Russo
    Roberto Russo over 6 years
  • Walter U.
    Walter U. over 6 years
    Russo: Thanks for the help Robert, but I think I haven't explained the issue properly. I need to query all issues regardless of type (bug, story, etc..) and regardless of project key. In other words, if it appears at all in my lira, I need to be able to query it without having to update 'issueType', PRO-KEY, etc. Sorry for the confusion.
  • Walter U.
    Walter U. over 6 years
    'project is not empty' fixed it. Thanks Robert.
  • Alec Fenichel
    Alec Fenichel over 6 years
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • frans
    frans over 3 years
    because this has been upvoted: please be careful with the second snippet, it contains a subtle bug: since search_issues always returns the newest tickets first, startAt=len(issues) will NOT return the missing tickets - I'll update this as soon as possible!
  • Asher
    Asher almost 3 years
    have you updated the code now to fix the mssing tickets issue?