How to destroy a branch in TFVC using the REST API?

Viewed 185

I am looking to delete and then destroy a branch in TFVC (in Azure Devops) using the REST API and Powershell, but after checking the documentation I have to ask: is this possible using the API?

Using the GET documentation as a guide I could take a guess at it and run something similar to the following:

DELETE https://dev.azure.com/{organization}/{project}/_apis/tfvc/branches?path={path}&api-version=6.0

but given the destructive nature of these calls I am reluctant to guess. And there is no obvious way to invoke the destroy functionality.

Alternatively, is there a way to trace the API call that must (probably?) be executed if I was to run the command line tf vc destroy "$/MyBranch/Path"? Or am I going to have to resort to using a Powershell snapin for this?

2 Answers

Why not use tf vc destroy from powershell? Most of TFVC is handled by the old SOAP API and doesn't have a REST equivalent. The investment in more REST APIs for TFVC is unlikely. You can invoke tf vc from powershell assuming you have Team Explorer installed on the machine.

Alternatively, you can load the Client Object Model into PowerShell directly and invoke the destroy calls from it. The call is pretty straightforward. That way the Client Object Model will do all the API wrestling. You can get the assemblies from NuGet without installing Team Explorer to a machine.

To get an instance of the VersionControlServer class you can look at my TFVC tasks. The code below is untested, but should get you really close:

[System.Reflection.Assembly]::LoadFrom("Newtonsoft.Json.dll") 
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.Common")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.VersionControl.Client")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.WorkItemTracking.Client")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.Diff")

$OnNonFatalError = [Microsoft.TeamFoundation.VersionControl.Client.ExceptionEventHandler] {
    param($sender, $e)

    if ($e.Exception -ne $null -and $e.Exception.Message -ne $null)
    {
        Write-Message -Type Warning  $e.Exception.Message
    }
    if ($e.Failure -ne $null -and $e.Failure.Message -ne $null)
    {
        Write-Message -Type Warning  $e.Failure.Message
        if ($e.Failure.Warnings -ne $null -and $e.Failure.Warnings.Length -gt 0)
        {
            foreach ($warning in $e.Failure.Warnings)
            {
                Write-Message -Type Warning $warning.ParentOrChildTask 
            }
        }
    }
}

function Get-TfsVersionControlServer()
{
    [cmdletbinding()]
    param(
        [string] $ProjectCollectionUri
    )
        
    $collection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(
        $ProjectCollectionUri)
    $collection.EnsureAuthenticated()

    $versionControlServer = 
$provider.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
    $versionControlServer.add_NonFatalError($OnNonFatalError)
    return $versionControlServer 
}

Get-TfsVersionControlServer().Destroy(...)

Then call the destroy function from there.

I looked at the API documentation as well. In my experience, Azure keeps their API documents up to date pretty well. The absence of the DELETE operation in their documentation indicates that the DELETE operation is not yet supported. Even if it 'worked', I wouldn't use it until it was supported in their API - especially with something dealing with version control.

Related