I'm trying to use PowerShell 5.1 to get completed PRs for a date range in Azure DevOps. I'm getting a response, but not respecting the closed date parameter. I know I can filter the list afterwards, but that seems inefficient.
$startDate = (Get-Date -Date "2025-01-31T00:00:00Z").ToString("yyyy-MM-ddTHH:mm:ssZ")
$url = "$anization/$project/_apis/git/repositories/$repositoryId/pullrequests?searchCriteria.status=completed&searchCriteria.targetRefName=refs/heads/master&searchCriteria.closedDate>$startDate&api-version=6.0"
I'm trying to use PowerShell 5.1 to get completed PRs for a date range in Azure DevOps. I'm getting a response, but not respecting the closed date parameter. I know I can filter the list afterwards, but that seems inefficient.
$startDate = (Get-Date -Date "2025-01-31T00:00:00Z").ToString("yyyy-MM-ddTHH:mm:ssZ")
$url = "$anization/$project/_apis/git/repositories/$repositoryId/pullrequests?searchCriteria.status=completed&searchCriteria.targetRefName=refs/heads/master&searchCriteria.closedDate>$startDate&api-version=6.0"
Share
Improve this question
edited Mar 7 at 3:16
Bright Ran-MSFT
14.5k1 gold badge12 silver badges28 bronze badges
asked Mar 6 at 16:50
RodRod
15.5k35 gold badges134 silver badges264 bronze badges
4
|
2 Answers
Reset to default 1On the api-version=6.0
of Azure DevOps REST API "Pull Requests - Get Pull Requests", the searchCriteria.closedDate
is not an available/valid parameter for the request URI.
Since the api-version=7.1
of this API, there are some new URI parameters are introduced. For your case, you can use this API version with the parameters searchCriteria.minTime
, searchCriteria.maxTime
and searchCriteria.queryTimeRangeType
like as below.
GET https://dev.azure/{anization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?searchCriteria.status=completed&searchCriteria.targetRefName=refs/heads/master&searchCriteria.minTime={minDataTime}&searchCriteria.maxTime={maxDataTime}&searchCriteria.queryTimeRangeType=closed&api-version=7.1
`closedDate` parameter is being formatted or interpreted in the URL differently. Maybe adding endDate parameter and reformatting can help you pass the date correctly and recognized by the API
$startDate = (Get-Date -Date "2025-01-31T00:00:00Z").ToString("yyyy-MM-ddTHH:mm:ssZ")
$endDate = (Get-Date -Date "2025-02-28T23:59:59Z").ToString("yyyy-MM-ddTHH:mm:ssZ")
$url = "$anization/$project/_apis/git/repositories/$repositoryId/pullrequests?searchCriteria.status=completed&searchCriteria.targetRefName=refs/heads/master&searchCriteria.closedDate=$startDate&searchCriteria.closedDate=$endDate&api-version=6.0"
$response = Invoke-RestMethod -Uri $url -Method Get -Headers @{Authorization = "Bearer $token"}
$completedPRs = $response.value | Where-Object { $_.closedDate -ge $startDate -and $_.closedDate -le $endDate }
$completedPRs
Note: The API call is not tested.
searchCriteria.closedDate>$startDate
withsearchCriteria.minTime=$startDate&searchCriteria.queryTimeRangeType=closed
– Mathias R. Jessen Commented Mar 6 at 17:10.ToString()
call operates on is a local date, so theZ
suffix in the formatted output is inappropriate, unless your local time zone coincides with UTC. – mklement0 Commented Mar 6 at 22:44