如何循环调用Jira REST API突破100条限制获取全部Issue
Got it, let's solve this problem of grabbing all 500+ Jira issues when the REST API caps you at 100 results per request. Here are two solid methods you can use, including the JQL-based approach you're considering:
1. Use Jira's Built-in Pagination Parameters (Recommended)
Jira's REST API has native support for pagination with the startAt and maxResults parameters—this is the most reliable way to get all your data without messing with JQL filters.
maxResults: Set this to 100 (the API's hard limit for single requests).startAt: This defines the index of the first issue to return. Start at 0, then increment by 100 after each request until you get fewer than 100 results (or untilstartAtexceeds the total number of issues reported in the API response).
Example Workflow (Pseudocode):
total_issues = None start_at = 0 all_issues = [] while True: # Make API request (replace with your actual request logic) response = send_request(f"/rest/api/2/search?jql=&maxResults=100&startAt={start_at}") data = response.json() # Initialize total issue count on first run if total_issues is None: total_issues = data["total"] # Add current batch to our full list all_issues.extend(data["issues"]) # Check if we've fetched all issues start_at += 100 if start_at >= total_issues: break
Once you have all_issues, you can format them into your desired output (like DT-1469;Done;My Summary;ServiceDesk 1; ...).
2. Use JQL with Issue Key Filtering (Your Proposed Approach)
If you prefer to use JQL to paginate, this works too—just make sure you sort issues by their key to ensure consistent batches.
Steps:
- First, fetch the initial batch with a JQL that sorts by key ascending:
ORDER BY key ASC(this ensures the last issue in your batch has the highest key in that set). - For each subsequent request, use a JQL filter like
key > "LAST_ISSUE_KEY" ORDER BY key ASCto get the next 100 issues. - Stop when the API returns fewer than 100 issues.
Example Pseudocode:
last_key = "" all_issues = [] while True: # Build JQL query if last_key == "": jql = "ORDER BY key ASC" else: jql = f'key > "{last_key}" ORDER BY key ASC' # Encode JQL for URL and make request encoded_jql = urllib.parse.quote(jql) response = send_request(f"/rest/api/2/search?jql={encoded_jql}&maxResults=100") data = response.json() if len(data["issues"]) == 0: break # Add batch to full list and update last key all_issues.extend(data["issues"]) last_key = data["issues"][-1]["key"] # Stop if we got fewer than 100 results (end of dataset) if len(data["issues"]) < 100: break
Important Note:
Always include ORDER BY key ASC in your JQL—without sorting, the "last issue key" from each batch won't reliably point to the next set of issues, leading to missing or duplicate data.
Final Tip
Whichever method you choose, remember to handle potential API rate limits (Jira might throttle requests if you hit it too fast) and add basic error handling for failed requests (e.g., retry logic for 5xx errors).
内容的提问来源于stack exchange,提问作者Seko

