How to access the next page using JIRA -REST-API for python
Asked Answered
S

3

6

I am trying to fetch all issues related to a project. When I execute the below code, I get only 50 results. I need to navigate all pages and get all the bugs.Please help

all_issues = jira.search_issues('project=ProjectName')
    each_issue = sorted([issue.key for issue in all_issues])
    for item in each_issue:
        print item

This gives me only 50 issues since the page has default value of 50. I need to get all the issues.

Stramonium answered 19/11, 2015 at 22:15 Comment(0)
M
9

-- Update 18/Oct/2021

As discovered in the answer below, setting maxResults to False appears to remove the limit on the result set.

all_issues = jira.search_issues('project=ProjectName', maxResults=False)

-- Original Post

Try;

all_issues = jira.search_issues('project=ProjectName', maxResults=50, startAt=50)

The results from the REST API are paged, with the default number of results being 50. You can supply the startAt value to start the results from a point in the result set. By default this value is 0.

So, your original query would get results 0-49, the query above would get results 50-99 and changing startAt to 100 would get 100-149, and so on.

You can also increase the value of maxResults to return more results per page. However, this is limited to the max value of jira.search.views.default.max configured in your JIRA instance (set to 1000 by default).

It is not possible to make the API return all issues without paging. You would have to configure jira.search.views.default.max to a very large value and supply that value as maxResults.

Miasma answered 20/11, 2015 at 17:27 Comment(7)
Thanks a lot! It works awesome. As you said it limits the value to 1000. I tried setting the value for 'jira.search.views.default.max'. But I get an error 'AttributeError: 'JIRA' object has no attribute 'search''. I want to fetch about 10000 issues. Is there a way?Stramonium
Where are you setting jira.search.views.default.max? This is a JIRA global configuration, it needs to be set in jira-config.properties for the JIRA installation.Miasma
@rakesh: is there any way to paginate it easily? Can i predict how many items could be returned in total?Eluvium
@Eluvium I guess number of items returned depends on the value that you set for maxResults.Stramonium
This answer is not correct, since you will always do a new request when you change the startAt. If you e.g. process issues in a way that they won't appear in the search afterwards you will skip some issuesIndian
@AndréDüwel I think stating "it is incorrect" is a bit far; it is a "limitation" that needs to be considered for sure; but with the unfortunate max limit imposed in the search, there isn't another way, unless perhaps you can provide your wisdom?Miasma
@Miasma sorry for not directly posting the solution, I found your answer and used it since I just had to accomplish one small task and during that I realized that "limitation". Sorry if you felt offended, that wasn't my intention. I just wanted to share my finding. And I assumed that there will be a better way, since the returned object contains a boolean property: isLast After taking the time to look up the source code I found the solution an posted as here as an alternative answer https://mcmap.net/q/1647125/-how-to-access-the-next-page-using-jira-rest-api-for-python . Hoping it will help other people in the future. :)Indian
T
3

According to the source code: https://github.com/pycontribs/jira/blob/f5d7dd032e719fe35f5fc377f302200f6c69afd4/jira/client.py#L2737

Setting maxResults=False should do the trick, so your example would look like:

all_issues = jira.search_issues('project=ProjectName', maxResults=False)
    each_issue = sorted([issue.key for issue in all_issues])
    for item in each_issue:
        print item

I shortly tested it right now and it worked here.

Tenor answered 17/10, 2021 at 20:44 Comment(0)
L
1

Here's a way to get all issues - or a desired max number - using the pagination capability of the API. This gets as many issues as possible with each call to search_issues, stopping when the requested max number is reached or there are no more available issues.

def get_issues(jira, query, max=(1<<31)-1):
    issues = []
    start = 0
    done = False
    while not done:
        nextpage = jira.search_issues(query, startAt=start, maxResults=(max - start))
        issues.extend(nextpage)
        start = start + len(nextpage)
        done = (start >= max) or (len(nextpage) == 0)
    return issues

With this, the OP's issue search becomes:

all_issues = get_issues(jira, 'project=ProjectName')

But if you only want the first 100 issues, use this instead:

all_issues = get_issues(jira, 'project=ProjectName', max=100)

This is more efficient than calling search_issues with maxResults=False, which internally paginates through all issues.

The default value of max is 2^31 - 1, the largest positive signed 32-bit integer, which is the highest value supported by the JIRA API.

Legging answered 8/12, 2023 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.