【问题标题】:Can I add to a Pipeline Triggers 'Branch Filters' list through the API我可以通过 API 添加到 Pipeline Triggers 'Branch Filters' 列表吗
【发布时间】:2020-11-20 01:11:25
【问题描述】:

希望你能帮忙!

Azure DevOps API 的新手,但我只是希望能够通过 API 更新管道触发器下的分支过滤器列表。

是否存在这样的 API 方法,如果不存在,是否有人成功地做到了这一点以及如何做到的? 谢谢!

【问题讨论】:

    标签: c# api azure-devops triggers azure-pipelines


    【解决方案1】:

    但我只是希望能够通过 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)}
    

    测试结果:

    【讨论】:

    • 非常感谢!我会试一试并相应更新!
    • @CJH,永远欢迎你 :)。如果您有任何更新,请免费告诉我。祝你有美好的一天!
    • 真的很痛苦,请问您能否提供 C# 中的示例代码?如果没有,我会看看我是否可以转录它:)
    • @CJH,对不起,我没有使用 C# 编写这样的示例,但您可以查看此文档中的示例:docs.microsoft.com/en-us/rest/api/azure/devops/…stackoverflow.com/questions/52205058/…
    • 嘿,对不起,我本来打算回到这个,但上周在家里很忙。在我尝试您的建议之前,不再需要我正在做的工作 - 因此无法确认它是否“正确”。我想我从您的屏幕截图中看到它看起来已经有效,因此在此基础上会将其标记为已接受的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2012-01-17
    相关资源
    最近更新 更多