【问题标题】:How to destroy a branch in TFVC using the REST API?如何使用 REST API 销毁 TFVC 中的分支?
【发布时间】:2021-09-06 14:36:44
【问题描述】:

我希望使用 REST API 和 Powershell 删除然后销毁 TFVC(在 Azure Devops 中)中的一个分支,但在查看文档后我不得不问:这可能使用 API 吗?

使用the GET documentation 作为指导,我可以猜测它并运行类似于以下的内容:

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

但鉴于这些电话的破坏性,我不愿猜测。并且没有明显的方法来调用销毁功能。

或者,如果我要运行命令行tf vc destroy "$/MyBranch/Path",是否有办法跟踪必须(可能?)执行的 API 调用?还是我将不得不为此使用 Powershell 管理单元?

【问题讨论】:

  • 您可以使用预览 API 版本 6.1/6.2 吗?到目前为止,似乎只记录了 git 分支的删除分支。您始终可以使用wireshark 并设置ssl 解密,但这可能不值得为此付出努力。您可能会在开发者社区论坛上获得更好的答案:developercommunity.visualstudio.com/t/…

标签: 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 支持它——尤其是处理版本控制的东西。

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2022-11-25
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2017-09-23
      相关资源
      最近更新 更多