admin管理员组

文章数量:1406060

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
  • Judging from the documentation, you'll want to replace searchCriteria.closedDate>$startDate with searchCriteria.minTime=$startDate&searchCriteria.queryTimeRangeType=closed – Mathias R. Jessen Commented Mar 6 at 17:10
  • Still getting the same amount of records that are outside the start date – Rod Commented Mar 6 at 19:28
  • What happens if you enter the URL into a browser? There may be an error message in the browser that will help debug the issue. It may be the type of browser you are using and may need to add the User-Agent http header to the request. – jdweng Commented Mar 6 at 22:36
  • Note that what the .ToString() call operates on is a local date, so the Z suffix in the formatted output is inappropriate, unless your local time zone coincides with UTC. – mklement0 Commented Mar 6 at 22:44
Add a comment  | 

2 Answers 2

Reset to default 1

On 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.

本文标签: Trying to use PowerShell 51 to get completed PRs for a date range in my Azure DevOpsStack Overflow