如果功能分支落后,则限制 PR 完成。 Azure 开发运维
Restrict PR completion if feature branch is behind. Azure Devops
无论如何,如果我的功能分支在比较分支(在本例中为 Master)之后,我可以限制 PR 完成。
我想创建 2 个 PR
- feature/1发展。因为,feature/1落后master 3 我想限制这个PR的完成。
- feature/2发展。由于此功能分支与主分支同步,因此不应对此 PR 施加任何限制。
注意:功能分支是从master
创建的
谢谢
到目前为止,我们还没有在分支策略中提供这种开箱即用的功能来帮助您实现这一目标。
但是有一个变通方法可以考虑使用:在Build pipeline via api中判断behind
的值是否为0,然后指定这个pipeline为[=67] =]在分支策略中构建验证。
步骤 1:
创建构建管道,并在其中添加名为 BehindNoVerify
.
的 Powershell
任务
第 2 步:
选择inline
模式后在该任务中添加以下脚本:
[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
}
注:以上powershell脚本的工作逻辑为1).首先从master
分支获取最新的commit id
。 2). 使用此 commit id
作为 目标版本 与 develop
分支的提交进行比较,然后 calculate/obtain ahead
和 develop
号码通过此 api。 3)一旦behind
账户为非零,修改退出代码使任务手动失败。
以上脚本可以应用于任何组织和项目,因为我使用环境变量来获取 Token
、org name
、project name
和 repos name
.只需启用 System.AccessToken 即可。
步骤 3
转到相应分支的branch policy
=> Build validation
=> Add build policy
=> 选择我们之前定义的Build pipeline BehindNoVerify
。
选择Trigger
作为自动,选择Policy requirement
作为必需。
现在pull request只允许pipeline(BehindNoVerify
)运行成功时completed
,即behind
的值为0。
为了让 Mengdi Liang 的代码片段按我想要的方式工作,我不得不费了点功夫,所以我想我应该分享我的更改,以免其他人遭受类似的痛苦!
我所做的是将其通用化为由 PR 政策触发,并将 master 与 PR 的 'source' 分支进行比较,而不是专门“开发”
我还解决了可能是 $result -ne 0
引用 $result 而不是 $results.behindcount 的拼写错误,因此总是失败。
测试并于 2021-11-22
开始工作
[String]$project = "$env:SYSTEM_TEAMPROJECT"
[String]$OrgUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"
[String]$Repo = "$env:BUILD_REPOSITORY_NAME"
[String]$branch = "$env:SYSTEM_PULLREQUEST_SOURCEBRANCH"
$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 $headers -Method Get
#filter out the behind data of current PR branch
$results = $response.value | where-object { $_.name.indexof($branch.replace("refs/heads/","")) -ge 0 }
Write-Host "results = ${results.behindCount}"
#Fail current task once behind value is non-zero
if ($results.behindCount -ne 0 ) {
exit 128
}
无论如何,如果我的功能分支在比较分支(在本例中为 Master)之后,我可以限制 PR 完成。
我想创建 2 个 PR
- feature/1发展。因为,feature/1落后master 3 我想限制这个PR的完成。
- feature/2发展。由于此功能分支与主分支同步,因此不应对此 PR 施加任何限制。
注意:功能分支是从master
创建的谢谢
到目前为止,我们还没有在分支策略中提供这种开箱即用的功能来帮助您实现这一目标。
但是有一个变通方法可以考虑使用:在Build pipeline via api中判断behind
的值是否为0,然后指定这个pipeline为[=67] =]在分支策略中构建验证。
步骤 1:
创建构建管道,并在其中添加名为 BehindNoVerify
.
Powershell
任务
第 2 步:
选择inline
模式后在该任务中添加以下脚本:
[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
}
注:以上powershell脚本的工作逻辑为1).首先从master
分支获取最新的commit id
。 2). 使用此 commit id
作为 目标版本 与 develop
分支的提交进行比较,然后 calculate/obtain ahead
和 develop
号码通过此 api。 3)一旦behind
账户为非零,修改退出代码使任务手动失败。
以上脚本可以应用于任何组织和项目,因为我使用环境变量来获取 Token
、org name
、project name
和 repos name
.只需启用 System.AccessToken 即可。
步骤 3
转到相应分支的branch policy
=> Build validation
=> Add build policy
=> 选择我们之前定义的Build pipeline BehindNoVerify
。
选择Trigger
作为自动,选择Policy requirement
作为必需。
现在pull request只允许pipeline(BehindNoVerify
)运行成功时completed
,即behind
的值为0。
为了让 Mengdi Liang 的代码片段按我想要的方式工作,我不得不费了点功夫,所以我想我应该分享我的更改,以免其他人遭受类似的痛苦!
我所做的是将其通用化为由 PR 政策触发,并将 master 与 PR 的 'source' 分支进行比较,而不是专门“开发”
我还解决了可能是 $result -ne 0
引用 $result 而不是 $results.behindcount 的拼写错误,因此总是失败。
测试并于 2021-11-22
[String]$project = "$env:SYSTEM_TEAMPROJECT"
[String]$OrgUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"
[String]$Repo = "$env:BUILD_REPOSITORY_NAME"
[String]$branch = "$env:SYSTEM_PULLREQUEST_SOURCEBRANCH"
$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 $headers -Method Get
#filter out the behind data of current PR branch
$results = $response.value | where-object { $_.name.indexof($branch.replace("refs/heads/","")) -ge 0 }
Write-Host "results = ${results.behindCount}"
#Fail current task once behind value is non-zero
if ($results.behindCount -ne 0 ) {
exit 128
}