Azure DevOps 更新参考 - 通过 API 删除分支
实现了一个删除 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,发现我的 $body 周围没有方括号(按照文档)
I had the same issue and found my $body didn't have square brackets around it as per the docs