Search code examples
azurepowershellazure-devopsazure-rest-api

How to make inherited process work Item editable?


In a nutshell, I created an inherited process from the REST API, when I try to add a state or a field, I get an error because it uses the reference name from the original work item, not it's own.

For example: Microsoft.VSTS.WorkItemTypes.Bug instead of processName.Bug.

function newProcess {
param (
    [Parameter(Mandatory)] [string]     $organization,
    [Parameter(Mandatory)] [string]     $process,
    [Parameter(Mandatory)] [string]     $description,
    [Parameter(Mandatory)] [string]     $parent,
    [Parameter(Mandatory)] [hashtable]  $header
)

[string] $uri = "https://dev.azure.com/{0}/_apis/work/processes?{1}" -f $organization, $apiVersion
[string] $body = @{ name = $process; parentProcessTypeId = (getProcessId -organization $organization -process $parent -header $header); description = $description } | ConvertTo-Json

try {
    [string] $id = (Invoke-RestMethod -Uri $uri -Method Post -ContentType application/json -Body $body -Headers $header).typeId

    Start-Sleep -Seconds 1.0
    if ( $id -eq (getProcessId -organization $organization -process $process -header $header) ) {
        Write-Host " - Process '$process' successfully created in organization $organization !"
        return ($id)
    } else {
        throw
    }

} catch {
    Write-Host "##[error] An error occurred while creating the Process $process in the organization $organization at $uri"
    Get-Error
    throw
}
}

If I update the state or field from Azure manually, it will change the name, how can I automate that from the Azure REST API?


Solution

  • For the newly created inherited process, you should create an inherited work item type before editing it as mentioned by @Shamrai Aleksander. For each work item type, if you want to change it, you need to create a corresponding inherited work item type. Change the value of ParentWorkItemType below.

    $token = "{PAT}"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $head = @{ Authorization =" Basic $token" }
    $org = "{Org name}"
    
    # Create a new inherited process
    $url1="https://dev.azure.com/$org/_apis/work/processes?api-version=7.1-preview.2"
    $body1 = @'
    {
      "name": "MyNewAgileProcessTest",
      "parentProcessTypeId": "adcc42ab-9882-485e-a3ed-7678f01f66bc",
      "description": "My new process"
    }
    '@
    
    $newprocess = Invoke-RestMethod -Uri $url1 -Method Post -Headers $head -Body $body1 -ContentType application/json
    $newprocess| ConvertTo-Json
    $newprocessid = $newprocess.typeId
    $newprocessname = $newprocess.name
    
    # Define the parent work item type
    $ParentWorkItemType = "Bug"
    # Define the inherited work item type. It is "inherited process name +parent work item type".
    $witRefName = $newprocessname + "." + $ParentWorkItemType
    
    
    # Create a new inherited work item type. 
    $body2 = @{
      "color" = "CC293D"
      "description" = "Describes a divergence between required and actual behavior, and tracks the work done to correct the defect and verify the correction."
      "icon" = "icon_insect"
      "inheritsFrom" = $null
      "name" = $null
      "isDisabled" = $false
    }
    $body2["name"] = $ParentWorkItemType
    $body2["inheritsFrom"] = "Microsoft.VSTS.WorkItemTypes.$ParentWorkItemType"
    $body2 = $body2 | ConvertTo-Json
    
    $url2="https://dev.azure.com/$org/_apis/work/processes/$newprocessid/workItemTypes?api-version=5.0"
    Invoke-RestMethod -Uri $url2 -Method Post -Headers $head -Body $body2 -ContentType application/json
    
    
    #Add a state to the inherited work item type
    $url3="https://dev.azure.com/$org/_apis/work/processes/$newprocessid/workItemTypes/$witRefName/states?api-version=7.1-preview.1"
    $body3 = @'
    {
      "name": "Ready to test",
      "color": "b2b2b2",
      "stateCategory": "Proposed"
    }
    '@
    Invoke-RestMethod -Uri $url3 -Method Post -Headers $head -Body $body3 -ContentType application/json
    

    For questions related to the REST API, you can grab the network trace when you perform the same operation on the UI to see the specific API used, which can help a lot.