powershellazure-devopstfstfvc

How to get a branch name for a dedicated changeset number via PowerShell


I am trying to get the branch name for a specific changeset but this information does not exist. I am using this:

GET https://dev.azure.com/fabrikam/_apis/tfvc/changesets/16?api-version=7.1

https://learn.microsoft.com/en-us/rest/api/azure/devops/tfvc/changesets/get?view=azure-devops-rest-7.1&tabs=HTTP

Is there an option to get the branch?


Solution

  • I am afraid that there is no out-of-box method can directly get the branch name for a changeset via single Rest API.

    Is there an option to get the branch?

    Yes. We can use PowerShell script to run the Rest APIs: Changesets - Get Changeset Changes and Branches - Get Branches. Then we can compare the change path and the TFVC branch path to determine which branch the changeset is located.

    Here is PowerShell sample:

    $token = "PAT"
    
    $ChangesetID = ChangesetID
    
    $OrgName= "OrganizationName"
    
    $ProjName= "ProjectName"
    
    $GetChangesetURL="https://dev.azure.com/$($OrgName)/_apis/tfvc/changesets/$($ChangesetID)/changes?api-version=7.1"
    
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    $GetChangesets= Invoke-RestMethod -Uri $GetChangesetURL -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
    
    foreach($Changeset in $GetChangesets.value)
    {
       $ChangePath = $Changeset.item.path
       echo $ChangePath
       $GetBranchURL="https://dev.azure.com/$($OrgName)/$($ProjName)/_apis/tfvc/branches?api-version=7.1"
       $GetBranches= Invoke-RestMethod -Uri $GetBranchURL -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
       foreach($Branch in $GetBranches.value)
       {
         $BranchPath= $Branch.path
         if($ChangePath.Contains($BranchPath))
         {
           echo "$ChangesetID belong to Branch: $BranchPath"
         }
       }
    
    
    }
    

    Result:

    Changeset belong to One TFVC Branch:

    enter image description here

    Changeset belong to multiple branches:

    enter image description here