You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure DevOps Resultsbypipeline API续接令牌获取测试结果陷入循环

解决Azure DevOps Resultsbypipeline API分页循环问题

我通过Azure DevOps的Resultsbypipeline API获取指定构建的全部测试结果,前10000条能正常获取,但剩余数据需要使用continuationToken分页。按照文档设置continuationToken请求头后,始终重复获取前N条数据,陷入无限循环,响应头返回的x-ms-continuationtoken值始终相同。我尝试过将continuationToken作为查询参数传递,也试过用x-ms-continuationtoken作为请求头名称,都没有效果。

相关PowerShell代码如下:

$BuildId = 1234 # 有效的构建ID
function InvokeDevOpsApi($ContinuationToken) {
    # 测试时设置$top=10,不影响问题本质
    $url = "https://custom.tfs.instance.com/tfs/Org/Project/_apis/testresults/resultsbypipeline?pipelineId=$buildId&`$top=10&api-version=7.1-preview.1" 
    $headers = @{}
    if ($ContinuationToken) {
        $headers['continuationToken'] = $ContinuationToken
    }
    $response = Invoke-WebRequest -Uri $url -Method Get -UseDefaultCredentials -Headers $headers
    $continuationToken = $response.Headers['x-ms-continuationtoken']
    $res = $response.Content | ConvertFrom-Json -Depth 100
    [PSCustomObject]@{
        ContinuationToken = $continuationToken
        Tests = $res.value
    }
}

$tests = New-Object -TypeName System.Collections.ArrayList
$ContinuationToken = $null
do {
    $result = InvokeDevOpsApi $ContinuationToken
    $tests.AddRange($result.Tests) > $null
    $ContinuationToken = $result.ContinuationToken
} while ($ContinuationToken)

问题排查与解决方案

  1. 修正请求头名称
    部分Azure DevOps API对请求头名称的大小写有严格要求,需使用X-MS-ContinuationToken作为请求头名称,而非文档中提到的continuationToken。

  2. 切换API版本
    预览版API(如7.1-preview.1)可能存在分页逻辑bug,建议切换到稳定版API,比如7.0。

  3. 替换请求方法
    Invoke-WebRequest在处理请求头时可能存在自动大小写转换问题,改用Invoke-RestMethod能更准确地传递请求头。

修改后的代码示例:

$BuildId = 1234 # 有效的构建ID
function InvokeDevOpsApi($ContinuationToken) {
    $url = "https://custom.tfs.instance.com/tfs/Org/Project/_apis/testresults/resultsbypipeline?pipelineId=$buildId&`$top=10&api-version=7.0" 
    $headers = @{}
    if ($ContinuationToken) {
        # 严格匹配请求头名称大小写
        $headers['X-MS-ContinuationToken'] = $ContinuationToken
    }
    # 使用Invoke-RestMethod简化响应处理
    $response = Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredentials -Headers $headers
    # 兼容不同大小写的响应头字段
    $continuationToken = $response.Headers['x-ms-continuationtoken'] ?? $response.Headers['X-MS-ContinuationToken']
    [PSCustomObject]@{
        ContinuationToken = $continuationToken
        Tests = $response.value
    }
}

$tests = New-Object -TypeName System.Collections.ArrayList
$ContinuationToken = $null
do {
    $result = InvokeDevOpsApi $ContinuationToken
    $tests.AddRange($result.Tests) > $null
    $ContinuationToken = $result.ContinuationToken
} while ($ContinuationToken)

额外排查步骤

  • 用Fiddler或Postman手动发送请求,验证传递正确的continuationToken后是否能获取下一页数据,排除PowerShell代码的问题。
  • 检查API响应头中的link字段,部分Azure DevOps API会通过该字段返回下一页链接。
  • 确认目标构建的测试结果数量确实超过当前$top设置值,避免因无更多数据导致重复返回相同token。

内容的提问来源于stack exchange,提问作者Voo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:03:21