【问题标题】:How to destroy a branch in TFVC using the REST API?如何使用 REST API 销毁 TFVC 中的分支?
【发布时间】:2021-09-06 14:36:44
【问题描述】:
【问题讨论】:
标签:
powershell
azure-devops
azure-devops-rest-api
tfvc
【解决方案1】:
为什么不使用 powershell 中的tf vc destroy?大多数 TFVC 由旧的 SOAP API 处理,并且没有 REST 等价物。不太可能为 TFVC 投资更多 REST API。假设您在计算机上安装了 Team Explorer,您可以从 powershell 调用 tf vc。
或者,您可以将客户端对象模型直接加载到 PowerShell 中并从中调用销毁调用。 The call is pretty straightforward。这样,客户端对象模型将完成所有的 API 角力。 You can get the assemblies from NuGet 无需将 Team Explorer 安装到机器上。
要获取 VersionControlServer 类的实例,您可以look at my TFVC tasks。下面的代码未经测试,但应该让你非常接近:
[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(...)
然后从那里调用destroy函数。
【解决方案2】:
我也查看了 API 文档。根据我的经验,Azure 可以很好地保持他们的 API 文档是最新的。他们的文档中没有 DELETE 操作表明尚不支持 DELETE 操作。即使它“有效”,我也不会使用它,直到他们的 API 支持它——尤其是处理版本控制的东西。