繁体   English   中英

如何通过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,因为它将使您的脚本更加简单易懂。

安装完成后,以下脚本将实现您想要做的事情:

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM