您如何通过 Azure DevOps REST API 使用 PowerShell 创建功能以及链接任务?

How do you create a feature along with linked tasks using PowerShell via the Azure DevOps REST API?

我正在尝试使用 PowerShell 调用 Azure DevOps API 来创建一个功能,然后创建链接到该功能的子任务。

我已经成功创建了一个功能;并且我已经成功创建了链接到上述功能的子任务,但是,我不确定如何在一个脚本中完成所有这些工作。我面临的问题是,当创建子任务时,如果所有任务都在一个脚本中完成,它们如何知道要链接到的父特征的 ID?

希望这是有道理的。

我已经创建了一个功能和该功能的子任务,但并非全部在同一个脚本中完成。

#Set some parameters, including the PAT (Personal Access Token) from Azure DevOps

Param (
    [string]$azureDevOpsAccount = "acccount",
    [string]$projectName = "project",
    [string]$workItemType = "Feature",
    [string]$buildNumber = "",
    [string]$keepForever = "true",
    [string]$user = "user",
    [string]$token = "token"
)

#Build the array of tasks
$tasks = @("Task 1","Task 2")

#Iterate over the array
foreach ($task in $tasks)
{
    #Build the JSON body (NOTE: The WorkItem ID needs to be changed in the 'url' property to match the parent WorkItem getting the new tasks. The 'AssignedTo' needs to be validated as well.)
    $body = @"
    [
        {
        "op": "add",
        "path": "/fields/System.Title",
        "from": null,
        "value": "$task"
        },
        {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            "url": "https://url/DefaultCollection/project/_apis/wit/workItems/447129"
                 },
        },
        {
        "op": "add",
        "path": "/fields/System.AssignedTo",
        "value": "Person Name"
        }
    ]
"@

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

    #Construct the URI
    $uri = "https://$($azureDevOpsAccount).visualstudio.com/DefaultCollection/$($projectName)/_apis/wit/workitems/`$Task`?api-version=5.0"

    #Invoke a REST API call for each task to be created
    $result = Invoke-RestMethod -Uri $uri -Method Patch -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body
    $result | ConvertTo-Json
}

它在工作项 44712​​9 上创建子任务,但我真的希望我的脚本创建主要功能,然后创建子任务,链接到该功能并通过 AssignedTo 分配给我选择的人。

当您使用 Invoke-RestMethod 创建功能时,您会收到详细信息的响应,其中包括您创建的功能的 id

{
     "id": 3124214,
     "rev": 1,
     ... and more
}

所以只要把它放在一个变量中,你就可以在创建任务时使用它:

$newFeature = Invoke-RestMethod ...
$newFeatureId = newFeature.id

如果您不打算通过 Invoke-RestMethod 直接使用 REST API,我建议为此使用 Azure Devops CLI,因为它会使您的脚本编写更加简单和更多 human-readable.

一旦你安装了它,下面的脚本将实现你想要做的事情:

$Project = '<YOUR PROJECT>'
$Organization = '<YOUR ORGANIZATION>'
$FeatureID = az boards work-item create --title 'Feature' --type Feature --project $Project --organization $Organization --output json --query 'id'; `
$TaskId = az boards work-item create --title 'Task' --type Task --project $Project --organization $Organization --output json --query 'id'; `
az boards work-item relation add --id $TaskID --relation-type child --target-id $FeatureID --organization $Organization