如果 PR 标题不包含 Jira Story id,我需要在 git merges 中实施检查。这是我迄今为止在 github 工作流程中实现的内容
name: "PR Title Check"
on:
push:
branches: [ dev ]
pull_request:
types: [opened, edited, synchronize]
jobs:
check-title:
runs-on: ubuntu-latest
steps:
- name: Check PR title
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const payload = context.payload
const prTitle = payload.pull_request.title
// The pattern for JIRA ticket format
const jiraPattern = /[A-Z]+-\d+/g
if (!jiraPattern.test(prTitle)) {
console.log('The PR title does not match JIRA ticket format! The correct format is XX-XXXX <Story Details>')
// Fails the workflow
core.setFailed('PR title does not match JIRA ticket format! The correct format is XX-XXXX <Story Details>')
} else {
console.log('PR title format is correct.')
}
上面代码的问题是它不允许我合并来自
x <- y
的代码。我究竟做错了什么?限制应该是只合并到 dev 分支。
您似乎想确保针对 dev 分支的拉取请求具有与
JIRA story ID
格式匹配的标题。让我们检查一下您的工作流程文件,看看如何实现这一目标。
您可以尝试以下步骤来解决您的问题:
on 触发器调整:我们应该调整 on 触发器以确保此工作流程仅针对针对 dev 分支的 PR 运行:
on:
pull_request:
branches:
- dev
types: [opened, edited, synchronize]
通过这样做,您可以确保检查仅在 PR 针对 dev 分支时运行,无论其来源如何。
导入核心库:在脚本中,您使用了 core.setFailed(...),但似乎您忘记从 @actions/core 导入所需的核心库。让我们添加一下。
正则表达式检查:尽管 JIRA ID 的正则表达式看起来不错,但由于 JavaScript 中全局正则表达式模式的状态性,直接在正则表达式上使用 .test 可能会产生意外行为。在这种特定的上下文中,这可能不是问题,因为每次运行脚本时都会重新创建正则表达式,但为了保持一致性并避免潜在的陷阱,您可以使用不同的方法。
以下是经过这些更改后的更新工作流程:
name: "PR Title Check"
on:
pull_request:
branches:
- dev
types: [opened, edited, synchronize]
jobs:
check-title:
runs-on: ubuntu-latest
steps:
- name: Check PR title
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const core = require('@actions/core');
const payload = context.payload;
const prTitle = payload.pull_request.title;
// The pattern for the JIRA ticket format
const jiraPattern = /[A-Z]+-\d+/;
if (!jiraPattern.test(prTitle)) {
console.log('The PR title does not match JIRA ticket format! The correct format is XX-XXXX <Story Details>')
// Fails the workflow
core.setFailed('PR title does not match JIRA ticket format! The correct format is XX-XXXX <Story Details>');
} else {
console.log('PR title format is correct.');
}
工作流程现在应该只对针对 dev 分支的
JIRA ID
强制执行 PR titles
检查。如果 PR 标题与预期格式不匹配,则会阻止 PR 被合并。