简体   繁体   English

在 Azure Devops 中,如何创建分支策略以要求在合并 PR 之前解析链接的工作项?

[英]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.

相关问题 在 azure devops 的目标分支中获得 PR 批准后,我可以自动创建标签吗? - Can I create a tag automatically after PR approved in target branch in azure devops? 如何触发构建管道取决于在 Azure DevOps 中创建 PR 请求源分支 - How to Trigger a build pipeline depends on create PR request source branch in Azure DevOps 是否可以在 Azure DevOps 中将评论添加为“已解决”到 PR? - Is it possible to add comment as 'resolved' to PR in Azure DevOps? 如何在 azure devops 工作项目屏幕中显示所有工作项目 - How can I show all work items in azure devops work items screen Azure DevOps:分支创建策略 - Azure DevOps: policy for branch creation Azure DevOps 查询今天和失败或解决的工作项 - Azure DevOps query for today and failed or resolved work items 在 Azure 中,如何配置拉取请求,以便某些任务必须成功运行才能合并 PR? - In Azure, how do you configure pull requests so that some tasks must run successfully before a PR can be merged? Azure devops,工作项如何通过“工作项提及”链接到 git 提交? - Azure devops, how are work items linked to git commits by “work item mentioning”? 如何获得在 Azure DevOps (VSTS) 中有相关测试的工作项? - How can I get work items that have a related test in Azure DevOps (VSTS)? 如何在 Azure DevOps/GitHub 中设置 PR 验证? - How do I set up PR validations in Azure DevOps/GitHub?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM