Azure DevOps 更新参考 - 通过 API 删除分支

发布于 2025-01-10 02:49:36 字数 1639 浏览 3 评论 0原文

实现了一个删除 azure devops 上的分支的函数:

function Remove-RemoteBranch {
param (
    [Parameter(Mandatory = $true)]
    [String]
    $CollectionUri,
    [Parameter(Mandatory = $true)]
    [String]
    $TeamProject,
    [Parameter(Mandatory = $true)]
    [String]
    $Repository,   
    [Parameter(Mandatory = $true)]
    [string]
    $BranchName, 
    [Parameter(Mandatory = $true)]
    [string]
    $BranchID,
    [Parameter(Mandatory = $true)]
    [String]$AccessToken
)
#create PAT in B64 format"
$B64_PAT = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AccessToken)"))

#Header for Authorization
$header = @{Authorization = 'Basic ' + $B64_PAT }

$body = ConvertTo-Json (
    @{
        name        = $BranchName;
        oldObjectId = $BranchID;
        newObjectId = "0000000000000000000000000000000000000000";
    })

$response = (Invoke-RestMethod -Method Post `
        -Uri "$($CollectionUri)/$($TeamProject)/_apis/git/repositories/$($Repository)/refs/?api-version=4.1" `
        -Body $body `
        -ContentType "application/json" `
        -Headers $header `
        -UseBasicParsing)

if ($response.StatusCode -notmatch "200") {
    throw "Statuscode from RestRequest is $($Response.status)" 
}

}

我还找不到解决我的问题的方法。如果我向服务器发送请求,则会收到错误“值不能为空。\r\n参数名称:refUpdates”。我在 MS docu 中找不到任何建议。也许你们中的一些人有一个想法。

先感谢您。

编辑:

我已经找到答案了。身体必须看起来像这样:

$body = ConvertTo-Json (
    @(
       @{
          name        = $BranchName;
          oldObjectId = $BranchID;
          newObjectId = "0000000000000000000000000000000000000000";
        }
     )
)

implemented a function to delete a branch on the azure devops:

function Remove-RemoteBranch {
param (
    [Parameter(Mandatory = $true)]
    [String]
    $CollectionUri,
    [Parameter(Mandatory = $true)]
    [String]
    $TeamProject,
    [Parameter(Mandatory = $true)]
    [String]
    $Repository,   
    [Parameter(Mandatory = $true)]
    [string]
    $BranchName, 
    [Parameter(Mandatory = $true)]
    [string]
    $BranchID,
    [Parameter(Mandatory = $true)]
    [String]$AccessToken
)
#create PAT in B64 format"
$B64_PAT = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AccessToken)"))

#Header for Authorization
$header = @{Authorization = 'Basic ' + $B64_PAT }

$body = ConvertTo-Json (
    @{
        name        = $BranchName;
        oldObjectId = $BranchID;
        newObjectId = "0000000000000000000000000000000000000000";
    })

$response = (Invoke-RestMethod -Method Post `
        -Uri "$($CollectionUri)/$($TeamProject)/_apis/git/repositories/$($Repository)/refs/?api-version=4.1" `
        -Body $body `
        -ContentType "application/json" `
        -Headers $header `
        -UseBasicParsing)

if ($response.StatusCode -notmatch "200") {
    throw "Statuscode from RestRequest is $($Response.status)" 
}

}

I couldnt find a solution to my issue yet. I get the error "Value cannot be null.\r\nParameter name: refUpdates" if I send a request to the server. I couldn't find any advices in the MS docu. May be some of you folks have an idea.

Thank you in advance.

Edit:

I have found the answer. The body has to look like this:

$body = ConvertTo-Json (
    @(
       @{
          name        = $BranchName;
          oldObjectId = $BranchID;
          newObjectId = "0000000000000000000000000000000000000000";
        }
     )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2025-01-17 02:49:36

我遇到了同样的问题,发现我的 $body 周围没有方括号(按照文档)

[
  {
    name        = $BranchName;
    oldObjectId = $BranchID;
    newObjectId = "0000000000000000000000000000000000000000";
  }
]

I had the same issue and found my $body didn't have square brackets around it as per the docs

[
  {
    name        = $BranchName;
    oldObjectId = $BranchID;
    newObjectId = "0000000000000000000000000000000000000000";
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文