I'm providing an answer in python as I believe this might be more useful to some than a powershell or postman script, as this is what I ended up using in my final implementation.
Hopefully it helps some others!
First here is the code...
def count_PRs():
skip = 0
count = 0
while True:
# Retrieve next Pull Requests based on pager value, or all remaining if less than value
req_url=("https://%s/%s//%s/_apis/git/repositories/%s/pullrequests?$top=%s&$skip=%s&searchCriteria.status=all&api-version=%s" % (instance, organization, project, repository, pager, skip, api_version))
response = requests.get(req_url, auth=(username, password)).json()
# If no data returned, break out of loop, otherwise count items returned
if len(response["value"]) == 0:
break
else:
count += len(response["value"])
skip += pager
return count
Now an explanation of what's happening here so that you can UNDERSTAND it, not just use it blindly...
First, the request URL is created with variables that you should previously define.
req_url=("https://%s/%s//%s/_apis/git/repositories/%s/pullrequests?$top=%s&$skip=%s&searchCriteria.status=all&api-version=%s" % (instance, organization, project, repository, pager, skip, api_version))
These are: instance, organization, project, repository, pager, skip, api_version
Instance, organization, project, and repository are based on your use case so I can't help you there.
The "pager" value is how many items are returned per call, from my use I've noticed that the Azure API currently caps this at 1,000 but that may change in the future, I will try to update that if I notice it.
The "skip" value represents what pull requests have already been counted, so it starts at 0 and is then incremented by the pager value for each iteration through the loop so that it does not count the same PR multiple times.
The next line sends the request and saves the reply to the response variable:
response = requests.get(req_url, auth=(username, password)).json()
There is an authentication header which contains the username and password. I have previously set up a Personal Access Token through the Azure DevOps page, and that PAT is what you should use for your password here, this is significantly easier than trying to authenticate with OAuth2.0 and I would recommend doing this before anything else.
This section checks to see if you are still getting new pull requests in the response, and if you aren't, it breaks out of the while loop to return the count.
if len(response["value"]) == 0:
break
If you do receive a response, this section counts the pull requests and adds it to the count, then increments the skip variable before moving onto the next iteration
else:
count += len(response["value"])
skip += pager
This took me forever to figure out and I really hope that it can help you in the future, if so, consider upvoting! Any questions you've got drop them in the comments and I'll try to help you out as soon as I can.