Michael
Michael

Reputation: 33

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

I am trying to use PowerShell to call the Azure DevOps API to create a Feature and then create child Tasks that are linked to that Feature.

I have successfully created a Feature; and I have successfully created child Tasks that are linked to said Feature, however, I am not sure how to do it all in one script. The problem I face is that when the child Tasks are created, how do they know the ID of the parent Feature to be linked to, if it is all being done in a single script?

Hope this makes sense.

I have created a Feature and child Tasks to that Feature, but not all done within the same script.

#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
}

It creates child Tasks on work item 447129 but I really want my script to create the main Feature and then child Tasks, linked to that Feature and assigned to the person of my choosing via AssignedTo.

Upvotes: 1

Views: 914

Answers (2)

Dexter Lakin
Dexter Lakin

Reputation: 68

If you aren't committed to using the REST API directly via Invoke-RestMethod I would recommend using the Azure Devops CLI for this as it will make your scripting a lot simpler and more human-readable.

Once you have it installed the following script will achieve what you are trying to do:

$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

Upvotes: 1

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

When you create the Feature with Invoke-RestMethod you got a response with details include the id of the feature you created:

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

So just put it in a variable and you can use it when you create the tasks:

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

Upvotes: 2

Related Questions