[英]In Azure Devops, how can I create a branch policy to require linked work items be resolved before a PR is merged?
My QA team has asked me to prevent pull requests from being merged until the associated work items have been marked as resolved (meaning the testing did not discover defects.) I already require that a member of their team approves the PR, but they are concerned about someone accidentally approving the wrong PR before it's ready.我的 QA 团队要求我在相关工作项被标记为已解决(意味着测试未发现缺陷)之前阻止拉取请求被合并。我已经要求他们团队的一名成员批准 PR,但他们担心关于某人在准备好之前不小心批准了错误的 PR。 They also would like this to be automated since they are already manually managing the work items.他们还希望这是自动化的,因为他们已经在手动管理工作项。 I can't say I disagree with their wishes.我不能说我不同意他们的意愿。
There is a similar option to automatically close linked work items when the PR is merged, but this seems backwards to me - I can't merge the changes into the next release until the work items are resolved and properly documented.有一个类似的选项可以在合并 PR 时自动关闭链接的工作项,但这对我来说似乎是倒退 - 在工作项得到解决并正确记录之前,我无法将更改合并到下一个版本中。
I checked the built-in branch policies, and none of them meet my requirements.我检查了内置的分支策略,没有一个符合我的要求。 The closest option is to require work items to be linked, but this alone does not prevent merging before testing is completed.最接近的选择是要求链接工作项,但这本身并不能阻止在测试完成之前进行合并。
Is what they're asking for an acceptable use of branch policies?他们要求的是可接受的分支机构政策使用吗? Or is our workflow just incompatible with this platform?还是我们的工作流程与这个平台不兼容?
Updated on 12/19 12月19日更新
For this scenario that you would like to validate the pull request with the specific work item state.对于您希望使用特定工作项状态验证拉取请求的这种情况。 I suppose that you could add a branch policy of Build Validation
.我想您可以添加Build Validation
的分支策略。
You could add the ps script below in powershell task
to check the work-item state linking to your target pr.您可以在powershell task
中添加下面的 ps 脚本来检查链接到您的目标 pr 的工作项状态。
# Define organization base url, PAT, linked wit, Target WIT state and API version variables
$orgUrl = "https://dev.azure.com/{yourORG}/{yourPROJECT}"
$pat = ""
$queryString = "fields=system.state&api-version=7.0"
$witID = {YourWitID}
$TargetState = {yourWITstateForPRapproval}
# Create header with PAT
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$header = @{authorization = "Basic $token"}
# Get the linked wit state
$projectsUrl = "$orgUrl/_apis/wit/workitems/$witID?$queryString"
$field = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -Headers $header | ConvertTo-Json | ConvertFrom-Json | Select-Object -ExpandProperty fields
write-host $field
$witstate = $field.'System.State'
Write-Host $witstate
#Compare the wit state with target approval state
if ($witstate -eq "$TargetState" ) {
Write-Host "Check Succeeded"
}
else {
Write-Host ("Check Failed")
exit 1
}
Only when the build succeeds, your pr could be complete.只有构建成功,你的 pr 才能完成。
============================================================== ================================================ ============
I suppose that you could check the setting for Check for linked work items .我想您可以检查Check for linked work items的设置。
You could set the policy with screenshots below.您可以使用下面的屏幕截图设置策略。 And you could also set the policy with rest api .您还可以使用rest api设置策略。 "type":{"id":"40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e"}
this type id in the body below is by design. "type":{"id":"40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e"}
下面正文中的此类型 id 是设计使然。
POST https://dev.azure.com/fabrikam/fabrikam-fiber-git/_apis/policy/configurations?api-version=7.0
Body
{"type":{"id":"40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e"},
"revision":1,
"isDeleted":false,
"isBlocking":true,
"isEnabled":true,
"settings":{
"scope":[{"repositoryId":"$(yourRepo)",
"refName":"refs/heads/$(yourBranch)",
"matchKind":"Exact"}]}}
Currently, there is no build-in feature to meet your requirements directly.目前,没有内置功能可以直接满足您的要求。
As a work around, for your scenario, I would suggest use Branch Policy Check for comment resolution作为解决方法,对于您的情况,我建议使用 Branch Policy Check 来解决评论问题
Enable this feature on your target branch.在目标分支上启用此功能。
Whoever creates the PR, ask him to leave a comment to check the related WIT to be resolved.谁创建了PR,请他留言查看相关WIT待解决。 The person who creates the PR could use "#" to mention the WIT创建 PR 的人可以使用“#”来提及 WIT
By checking the WIT to be resolved, PR approvers could resolve the comment to allow the merge.通过检查要解决的 WIT,PR 批准者可以解决评论以允许合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.