如何从 azure devops 中获取工作项的过去和交互 rest api

how to get past n interations of workitems from azure devops rest api

我需要动态拉取具有特定字段的工作项的当前迭代和过去 n 次迭代。

向您详细解释Past n iterations。 假设我的 n 是 5,那么我将需要来自当前迭代、过去迭代到当前迭代以及过去到过去到当前迭代的工作项。

我总共有 400 多个迭代,我需要从中提取最近的 5 个迭代工作项。

要从当前迭代中获取具有特定字段的工作项,这是我编写的 powershell 脚本

$token = 'your PAT'
$url="https://dev.azure.com/Organizationname/_apis/wit/wiql?api-version=5.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
   "query": "SELECT [System.Id], [System.WorkItemType],  [System.State],[System.AreaPath],[System.Tags],[System.CommentCount],[System.ChangedDate] 
                   FROM workitems 
              Where [System.AreaPath] = 'area path' 
                 and [System.IterationPath] = 'iteration path' 
                 and [System.WorkItemType] = 'Story' 
              order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
$listOfTasks = New-Object Collections.Generic.List[String]
ForEach( $workitem in $response.workItems ) {
  $listOfTasks.Add($workitem.id)
}
$listOfTasks = $listOfTasks -join ','
$listOfTasks
$JSON1 = @"
{
  
  "ids": [$listOfTasks],
  "fields": [
    "System.Id",
    "System.Title",
    "System.WorkItemType",
    "System.IterationPath",
  ]      
}
"@
$JSON1
$url1="https://dev.azure.com/Organizationname/_apis/wit/workitemsbatch?api-version=5.0"
$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON1 -ContentType application/json
Write-Host "result = $($response1 | ConvertTo-Json -Depth 100)"

有人可以帮我调整这个逻辑,以便我可以获得当前和过去的 n 次迭代工作项。 非常感谢!!

过去和当前的迭代在团队设置中。因此,您必须知道团队名称才能获得迭代列表。那么:

  1. 在团队设置中您可以获得迭代:Iterations - List

https://dev.azure.com/{organization}/{project}/{team}/_apis/work/teamsettings/iterations?api-version=6.1-preview.1

  1. 每次迭代都包含日期和时间范围(当前、未来、过去)。
  2. 您可以在源代码中使用列表中的迭代或使用 Iterations - Get Iteration Work Items 获取迭代工作项:

https://dev.azure.com/{organization}/{project}/{team}/_apis/work/teamsettings/iterations/{iterationId}/workitems?api-version=6.1-preview.1

获取过去 5 次迭代的示例:

$user = ""
$token = "<PAT>"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$org = "<ORG_NAME>"
$teamProject = "<PROJECT_NAME>"
$teamName = "<TEAM_NAME>"

$restApiGetIterations = "https://dev.azure.com/$org/$teamProject/$teamName/_apis/work/teamsettings/iterations?api-version=6.1-preview.1"

$restApiGetIterations

function InvokeGetRequest ($GetUrl)
{   
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
}


$iterations = InvokeGetRequest $restApiGetIterations

$pastIterations = $iterations.value | Where-Object {$_.attributes.timeFrame -eq "past"} 

$past5Iterations = $pastIterations | Sort-Object -Property {$_.attributes.finishDate} | Select-Object -Last 5


$past5Iterations