如果功能分支落后,则限制PR完成。 Azure Devops

时间:2020-03-10 18:36:02

标签: git azure-devops pull-request

无论如何,如果我的功能分支位于比较分支(在本例中为Master)的后面,则可以限制PR完成。

我要创建2个PR

  1. 功能/ 1开发。因为feature / 1比master落后3,所以我想限制此PR的完成。
  2. 功能/ 2开发。由于此功能分支与主分支同步,因此此PR不受任何限制。

注意:功能分支是从母版创建的

谢谢

enter image description here

1 个答案:

答案 0 :(得分:1)

到目前为止,我们尚未在分支机构策略中提供此类现成的功能来帮助您实现此目标。

但是您可以考虑使用一种解决方法:通过api在Build管道中判断behind的值是否为0,然后将此管道指定为 Build validate 在分支机构政策中。


第1步

创建构建管道,并在其中添加名为Powershell的{​​{1}}任务。

第2步

在选择BehindNoVerify模式之后,在该任务中添加以下脚本:

inline

注意: 上面的powershell脚本的工作逻辑为 1)。 Obtain来自{ [String]$project = "$env:SYSTEM_TEAMPROJECT" [String]$OrgUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" [String]$Repo = "$env:BUILD_REPOSITORY_NAME" $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } $commiturl =$OrgUri + $project + "/_apis/git/repositories/" + $Repo + "/commits?searchCriteria.itemVersion.version=master&api-version=5.1" $comresponse = Invoke-RestMethod -Uri $commiturl -Headers $headers -Method Get #Obtain the latest commit id value from master branch $latestcommitid= $($comresponse.value.commitid[0]) $behindurl =$OrgUri + $project + "/_apis/git/repositories/" + $Repo + "/stats/branches?baseVersionDescriptor.versionOptions=none&baseVersionDescriptor.version=" + $latestcommitid + "&baseVersionDescriptor.versionType=commit&api-version=5.1" $response = Invoke-RestMethod -Uri $behindurl -Headers @{Authorization = "Basic $token"} -Method Get #filter out the behind data of develop branch $results = $response.value | Where {$_.name -eq "develop"} #| Write-Host "results = $($results.behindCount | ConvertTo-Json -Depth 100)" #Fail current task once behind value is non-zero if ($result -ne 0 ) { exit 128 } 首先分支。 2)。使用此commit id作为目标版本master分支的提交进行比较,然后计算/获得{{1 }}和commit id通过此api进行编号。 3) develop帐户为非零后,请修改退出代码以使任务手动失败。

上述脚本可以应用于任何组织和项目,因为我使用环境变量来获取aheaddevelopbehindToken。只需启用System.AccessToken即可。

enter image description here

第3步

转到相应分支的org name => project name => repos name =>选择我们先前定义的构建管道branch policy

选择Build validation自动Add build policy必需


现在,仅当管道(BehindNoVerify)成功运行,即Trigger的值为0时,拉取请求才允许为Policy requirement