Search code examples
powershellazure-devopsworkitem

Associate Work Item With Current Build and attach file to work item using powershell


How i can Associate Work Item With Current Build and attach file json to work item using powershell ? I use Azure Devops Service and i have this script:

$connectionToken="<my_token>"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$url= 'https://dev.azure.com/{organization}/{project_name}/_apis/wit/workitems/$Task?api-version=6.0'

$body=@"
  [
    {
      "op": "add",
      "path": "/fields/System.WorkItemType",
      "value": "Risk"
    },
    {
      "op": "add",
      "path": "/fields/System.Title",
      "value": "Test"
    },
    {
      "op": "add",
      "path": "/fields/System.Tags",
      "value": "test"
    },
    {
      "op": "add",
      "path": "/fields/System.Description",
      "value": "test"
    },
    {
      "op": "add",
      "path": "/fields/Mitigation",
      "value": "test"
    },
    {
      "op": "add",
      "path": "/fields/Risk",
      "value": "1 - High"
    },
    {
      "op": "add",
      "path": "/fields/Microsoft.VSTS.Build.IntegrationBuild",
      "value":  `"{value}`"}]"
    }
 ]
"@

Write-Host "$url"
$response= Invoke-RestMethod -Uri $url  -ContentType "application/json-patch+json" -Body $body -headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST

But link for associate with current build not work, and i cant find what i need to add to attach json file to work item from my Azure Pipeline


Solution

    1. To attach a file, you have to upload the file (Upload a text file) and then add the link to your work item (Add an attachment): Attach a file and create new workitem
    2. Microsoft.VSTS.Build.IntegrationBuild is just a field without any links. You have to add ArtifactLink link with Integrated in build type: Add build link to work item using REST API