Azure DevOps Rest API - 创建迭代不起作用

问题描述 投票:0回答:1

首先,我调用项目中的所有迭代 - 它运行良好,然后我想通过 API 创建一个新的迭代。 ,但该方法不起作用,我收到错误:

远程服务器返回错误:(400)无效请求

这是否存在问题,无法正常工作,或者我做错了什么?

# *the Powershell code:*

# Variables
$organization = "x"
$project = "y"
$pat = "z"

$baseUrl = "https://dev.azure.com/$organization/$project/_apis/work/teamsettings/iterations?api-version=6.0"

# authentication
$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
}

# HTTP GET
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -Headers $headers

# list iterations
$response.value | ForEach-Object {
    Write-Output "Iteration Name: $($_.name)"
    Write-Output "Start Date: $($_.attributes.startDate)"
    Write-Output "End Date: $($_.attributes.finishDate)"
    Write-Output "Path: $($_.path)"
    Write-Output "----------------------------------"
}

# data for new iteration
$body = @{
    name = "blabla"
    attributes = @{
        startDate = (Get-Date).AddDays(1).ToString("yyyy-MM-ddTHH:mm:ssZ")
        finishDate = (Get-Date).AddDays(14).ToString("yyyy-MM-ddTHH:mm:ssZ")
    }
} | ConvertTo-Json 

# HTTP POST
function Add-Iteration {
    param (
        [string]$url,
        [hashtable]$headers,
        [string]$body
    )
    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
        Write-Output "Iteration successfully added:"
        Write-Output $response
    }
    catch {
        Write-Output "Error adding iteration:"
        Write-Output $_.Exception.Message
    }
}

# add iteration
Add-Iteration -url $baseUrl -headers $headers -body $iterationData
powershell post azure-devops iteration
1个回答
0
投票

在官方文档中,它提供了一个示例来向团队添加迭代。 在示例中,我们可以看到示例请求正文的内容是迭代的 Id。因此,在您可以使用 API 将迭代添加到团队之前,先创建迭代。

我们还可以从 Azure DevOps 门户中的 UI 进行检查。当我们需要新的迭代时,我们需要打开项目设置>项目配置并创建迭代路径。

Project configuration

然后我们可以打开项目设置 > 面板 > 团队配置 > 团队迭代来选择新的迭代并将其添加到团队中。

Team Configuration

但是,没有记录的 Azure DevOps Rest API 来创建项目配置的迭代。

我们可以通过在 Microsoft Edge 的开发者工具中检查网络跟踪来获取实际的 REST API 命令。然后我们就可以找到我们需要的迭代Id。

enter image description here

获取迭代ID后,我们可以在请求体中使用它,如下示例。

# Variables
$organization = ""
$project = ""
$pat = ""

$baseUrl = "https://dev.azure.com/$organization/$project/_apis/work/teamsettings/iterations?api-version=6.0"

# authentication
$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
}


# HTTP GET
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -Headers $headers

# list iterations
$response.value | ForEach-Object {
    Write-Output "Iteration Name: $($_.name)"
    Write-Output "Start Date: $($_.attributes.startDate)"
    Write-Output "End Date: $($_.attributes.finishDate)"
    Write-Output "Path: $($_.path)"
    Write-Output "----------------------------------"
}

#  new iteration id
$body = @"
{
    "id":  "fd61c80c-c03f-4b26-bdf6-51ceb80f190c"     # change to your iteration id
}
"@
# Add an iteration to the team
$response = Invoke-RestMethod -Uri $baseUrl -Method Post -Headers $headers -Body $body -ContentType 'application/json'
# By the way, in your script, you missed the -ContentType 'application/json' in the post request.
$response | ConvertTo-Json 
© www.soinside.com 2019 - 2024. All rights reserved.