【发布时间】:2020-11-20 01:11:25
【问题描述】:
希望你能帮忙!
Azure DevOps API 的新手,但我只是希望能够通过 API 更新管道触发器下的分支过滤器列表。
是否存在这样的 API 方法,如果不存在,是否有人成功地做到了这一点以及如何做到的? 谢谢!
【问题讨论】:
标签: c# api azure-devops triggers azure-pipelines
希望你能帮忙!
Azure DevOps API 的新手,但我只是希望能够通过 API 更新管道触发器下的分支过滤器列表。
是否存在这样的 API 方法,如果不存在,是否有人成功地做到了这一点以及如何做到的? 谢谢!
【问题讨论】:
标签: c# api azure-devops triggers azure-pipelines
但我只是希望能够通过 API 更新管道触发器下的分支过滤器列表。
答案是肯定的。
您可以使用 REST API Definitions - Update 来更新触发器。
使用 REST API Definitions - Get 我们可以知道 branchFilters 是数组,所以我们不能只编辑它,您需要编辑 triggers[0],与 triggers 相同:
现在,我们可以使用以下 powershell 脚本调用 REST API 来更新触发器:
$connectionToken="Your PAT here."
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$url = "https://dev.azure.com/<YourOrganization>/<YourProject>/_apis/build/definitions/139?api-version=6.0"
$pipelineInfo = (Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
Write-Host "TriggerInfo= $($pipelineInfo.triggers[0].branchFilters[0] | ConvertTo-Json -Depth 100)"
#update the branch trigger from "master" to "Dev1"
$pipelineInfo.triggers[0].branchFilters[0] = "+refs/heads/Dev1"
$body = $pipelineInfo | ConvertTo-Json -Depth 99
$updateTrigger = Invoke-RestMethod -Uri $url -Method Put -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
测试结果:
【讨论】: