Search code examples
azure-devops-rest-api

Adding a deployment status to a PBI


I want to add the deployment status like here through powershell calling azure devops api enter image description here

Here is the powershell script that will be run as part of a release pipeline (classic pipeline)

# Define your Azure DevOps organization, project, and personal access token (PAT)
$organizationUrl = "https://dev.azure.com/ORG"
$organization = "ORG"
$projectName = "PROJ"

# Define the work item ID you want to retrieve
$workItemId = 14846
$accessToken = $env:SYSTEM_ACCESSTOKEN
$basicAuthValue = "Bearer $accessToken"
$headers = @{
    Authorization = $basicAuthValue
    "Content-Type" = "application/json-patch+json"
}
$headers
# Define the URL for the PATCH request
$apiUrl = "$organizationUrl/$projectName/_apis/wit/workitems/14846?api-version=7.0"
$apiUrl

# Define the JSON body for the PATCH request
$jsonBody = @(
    @{
      op = "test"
      path = "/rev"
      value = 46
    }
    @{
      op= "add"
      path= "/relations/-"
      value= @{
        rel= "ArtifactLink"
        url= "vstfs:///ReleaseManagement/ReleaseEnvironment/41d718f0-8259-494b-9ad0-5a72348ff1fc:1450:6941"
      }
    }
)

# Convert the JSON body to a string
$jsonString = $jsonBody | ConvertTo-Json -Depth 5
$jsonString

# Send the PATCH request
try {
    $response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Patch -Body $jsonString
   $response
    Write-Host "Work item updated successfully."
} catch {
    Write-Host "Error occurred: $($_.Exception.Message)"
    $_.Exception
}

when run the above script, i receive 400 bad request with no indication of which part of the request is wrong.


Solution

  • Test with the same PowerShell script sample, I can reproduce the 400 error.

    The cause of the issue is that you are missing the attributes parameters value in the jsonbody.

    To solve this issue, you can modify the jsonbody with the following content:

    $jsonBody = @(
        @{
          op = "test"
          path = "/rev"
          value = 4
        }
        @{
          op= "add"
          path= "/relations/-"
          value= @{
            rel= "ArtifactLink"
            url= "vstfs:///ReleaseManagement/ReleaseEnvironment/41d718f0-8259-494b-9ad0-5a72348ff1fc:1450:6941"
            attributes= @{
              comment= "Link to release"
              name= "Integrated in release environment"
          }
          }
        }
    )
    

    Then the PowerShell script can run as expected.

    If the 400 error still exists, you may need to check if the link already exists in the work item link page.

    On the other hand, after successful operation, the Rest API will add a link of Integrated in Release stage to the work item.

    For example:

    enter image description here

    But it still not shows the Deployment status as expected. To show the deployment status in the work item, you need to manually configure the pipeline with the steps in the doc: Link work items to builds and deployments in Azure Boards

    I am afraid that Rest API doesn't support setting the deployment status in the work item for the time being.