Search code examples
restazure-devopstfstfvcchangeset

How to associate work item with changeset (TFVC)


all

I want to associated work items with the changeset for Azure DevOps Services (TFS) via REST API. I have been successful in creating a changeset, but if I try also to link a work item to it, nothing happened, the changeset is created without linked work item

Postman console:

POST https://dev.azure.com/{organization}/{project}/_apis/tfvc/changesets?api-version=7.1-preview
{
   "changes":[
      {
         "item":{
            "path":"$/TFVCPath/archive.zip",
            "version":60404,
            "contentMetadata":{
               "encoding":-1
            }
         },
         "changeType":"edit",
         "newContent":{
            "content":"base64",
            "contentType":"base64Encoded"
         }
      }
   ],
  "workItems": [
        {
            "id": 390074,
            "assignedTo": "Roman Konrad",
            "state": "In Progress",
            "title": "Title",
            "workItemType": "task",
            "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/390074",
            "webUrl": "https://dev.azure.com/{organization}/{project}/_workitems/edit/390074/"
        }
    ],
   "comment":"File from postman"
}

Response: 201 created

I also tried different properties combination of workItems json object and API versions

Any ideas?


Solution

  • Tried it, it doesn't work. As a workaround, you can call Work Items - Update rest api to associate work item with changeset (TFVC).

    Once the changeset is created, you can get the changeset ID, then associate the work items with the changeset ID.

    Below PowerShell for your reference:

    Param(
       [string]$collectionurl = "http://xxx/tfs/Collection", #Or the Azure DevOps organization URL
       [string]$projectName = "ProjectName",
       [string]$WorkitemID = "194",
       [string]$ChangesetID = "439",
       [string]$user = "",
       [string]$token = "PAT"
    )
    
    # Base64-encodes the Personal Access Token (PAT) appropriately
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    function CreateJsonBody
    {
    
        $value = @"
    [
        {
        "op": "add",
        "path": "/relations/-",
        "value": {
          "rel": "ArtifactLink",
          "url": "vstfs:///VersionControl/Changeset/$ChangesetID",
          "attributes": {
            "name": "Fixed in Changeset"
          }
        }
      }
    ]
    
    "@
    
     return $value
    }
    
    $json = CreateJsonBody
    
    $uri = "$($collectionurl)/_apis/wit/workitems/$($WorkitemID)?api-version=6.0"
    $result = Invoke-RestMethod -Uri $uri -Method Patch -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}