I’m trying to grab all issues in a project that match my criteria. Up until now, it’s been fine, because we’ve had fewer results than the default maxResults.
Using PHP/cURL, this worked correctly for returning fewer than 50 results:
https://mysite.atlassian.net/rest/api/3/search?jql=issuetype%3DVulnerability%26resolution%3DUnresolved
We just added a number of new issues, and now I’m only getting back the first 50 of 53; here’s the json decoded result (not including the actual issues):
"expand":"schema,names","startAt":0,"maxResults":50,"total":53
I found the documentation indicating that I’d need to paginate my results, and it seems like I should be able to add the startAt param to my querystring.
So I run an initial call to determine how many possible results there are, then determine how many times I’ll need to run the call (total / maxResults). I then do a for loop based on the number of calls, and spit the resulting issues into a separate array for each pass. I’d expect that the first pass would have 50, and the second pass would have 3. But that’s not what I’m seeing. Instead, both passes are returning the first 50 results.
(The code and logic is really messy; bear with me, it’s a first pass.)
// removed authentication headers for the community post; they do exist in my code
// run initial call to get total and figure out number of calls required
$ch = curl_init();
$url = "https://mysite.atlassian.net/rest/api/3/search?jql=issuetype%3DVulnerability%26resolution%3DUnresolved";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$array = json_decode($result,TRUE);
$searchTotal = $array['total'];
$maxResults = $array['maxResults'];
$numCalls = ceil($searchTotal/$maxResults);
$newStartWith = 0;
error_log("total: $searchTotal"); // this is 53, as expected
for($x = 1; $x <= $numCalls; $x++) {
error_log("newStartsWith on pass $x: $newStartWith");
// on pass 1, $newStartsWith = 0; on pass 2, $newStartsWith = 50 - this is the expected result
$url = "https://mysite.atlassian.net/rest/api/3/search?startAt%3D{$newStartWith}%26jql%3Dissuetype%3DVulnerability%26resolution%3DUnresolved%20order%20by%20issuekey";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$array = json_decode($result,TRUE);
$newStartWith += count($array['issues']);
$tmpVar = $array['issues'];
foreach($tmpVar as $issue) {
error_log($x . " " . $issue['key']);
// this prints out 1. WH-123, 1. WH-234, etc. for first pass
// and 2.WH-123, 2.WH-234 for second pass
// this is where the problem is coming in; I'm getting the same 50 results for both passes,
// instead of the first 50 results in pass 1 and the final 3 in pass 2.
}
error_log("----------------------------------------------"); // just to make it easy to see end of each pass
}
curl_close($ch);
Is there anything obviously wrong with what I’m trying to do? Or am I using startAt incorrectly?