执行拉取请求标题

nro*_*fis 4 azure-devops

我使用了一些 GitHub 门来按规则强制执行 Pull 请求标题。但我没有找到任何方法在Azure DevOps上做到这一点(代码托管在Azure DevOps Repo上)。

如何创建一个强制执行拉取请求标题的 PR 门?

Vin*_*ren 5

您可以使用 powershell 任务定义一个小型管道,该任务检查拉取请求标题并检查其是否有效。

然后,配置 master 分支以将该管道包含在构建验证策略中;管道将自动运行,并自动控制 PR。

笔记; 您必须调用 Azure Devops REST API 来获取 PR 名称,因为虽然预定义的系统变量中会自动提供拉取请求 ID ,但拉取请求名称不会。


rsy*_*rsy 5

我实施了Vince的很好的建议。首先,我创建了一个简单的 powershell 脚本 ( validate_conventional_commit.ps1) 来验证字符串是否符合我想要匹配的正则表达式。在这种情况下,我希望 PR 标题符合常规提交

Param(
    [Parameter(Mandatory = $True)]
    [string]$CommitMessage
)

$Regex="^(fix|feat):"

if ($CommitMessage -match $Regex) {
  Write-Host "The commit message complies with conventional commits."
}
else {
  Throw "Error : Invalid commit message!"
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了一个管道来检查 PR 标题并检查它是否有效。

trigger: none

pool:
  vmImage: 'ubuntu-latest'
steps:
  - bash: |
      pr_title="$(curl --silent -u azdo:$SYSTEM_ACCESSTOKEN \
       $(System.CollectionUri)_apis/git/repositories/$(Build.Repository.ID)/pullRequests/$(System.PullRequest.PullRequestId)?api-version=5.1 \
       | jq -r .title)"

      echo "##vso[task.setvariable variable=pr_title;]'$pr_title'"
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    displayName: Extract pull request title

  - task: PowerShell@2
    displayName: 'Validate the PR title, which will be used as the commit message when merging to the target branch'
    inputs:
      targetType: 'filePath'
      filePath: 'validate_conventional_commit.ps1'
      arguments: '$(pr_title)'
      failOnStderr: true
Run Code Online (Sandbox Code Playgroud)

最后,我通过构建验证分支策略创建了一个PR 触发器,当针对目标分支创建 PR 时,它将触发管道。