You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何要求GitHub贡献者添加指定PR评论后才能启用合并按钮?

问题:如何通过GitHub Actions阻止PR合并直到作者添加特定评论?

问题描述

我需要阻止PR被合并,直到PR作者在PR中添加特定评论(如"It's my turn in the queue"),以此确认他们已加入团队的Slack合并队列。我配置了如下GitHub Actions工作流(.github/workflows/check-comment.yml):

name: Check PR Comment for Queue Message

on:
  pull_request:
    types: [ready_for_review]

jobs:
  check-comment:
    runs-on: ubuntu-latest
    steps:
    - name: Check for required comment
      uses: actions/github-script@v6
      with:
        script: |
          const { data: comments } = await github.issues.listComments({
            owner: context.repo.owner,
            repo: context.repo.repo,
            issue_number: context.issue.number,
          });

          const requiredComment = "It's my turn in the queue";
          const hasComment = comments.some(comment => comment.body.includes(requiredComment));

          if (!hasComment) {
            throw new Error(`PR cannot be merged until the comment: '${requiredComment}' is present.`);
          }

但添加到main分支后并未生效,在仓库分支保护规则的状态检查列表中也显示"No checks have been added"。

遗漏的步骤及修复方案

1. 补充工作流触发事件

当前工作流仅在PR标记为ready_for_review时触发,无法覆盖作者添加评论后、PR更新后等场景,导致检查不会重新执行。需要扩展触发事件:

on:
  pull_request:
    types: [ready_for_review, opened, synchronize]
  issue_comment:
    types: [created]
  • pull_request.opened:PR创建时触发
  • pull_request.synchronize:PR有新提交时触发
  • issue_comment.created:PR中新增评论时触发

2. 限制评论必须来自PR作者

原逻辑允许任何人添加指定评论通过检查,存在漏洞。需要修改脚本,仅识别PR作者的评论:

const requiredComment = "It's my turn in the queue";
const prAuthor = context.payload.pull_request?.user.login || context.payload.issue.user.login;
const hasComment = comments.some(comment => 
  comment.user.login === prAuthor && comment.body.includes(requiredComment)
);

3. 手动触发工作流,让GitHub识别状态检查

新添加的工作流需要至少运行一次,才会出现在分支保护的状态检查列表中:

  • 打开仓库的Actions页面,找到Check PR Comment for Queue Message工作流
  • 点击"Run workflow",选择main分支手动运行一次;或者提交一个测试PR,触发工作流执行

4. 配置分支保护规则的必填状态检查

工作流运行成功后,回到分支保护规则页面:

  • 勾选"Require status checks to pass before merging"
  • 在搜索框中输入工作流的job名称check-comment,选中该状态检查并添加到必填列表
  • 保存分支保护规则

修改后的完整工作流

name: Check PR Comment for Queue Message

on:
  pull_request:
    types: [ready_for_review, opened, synchronize]
  issue_comment:
    types: [created]

jobs:
  check-comment:
    runs-on: ubuntu-latest
    steps:
    - name: Check for required comment from PR author
      uses: actions/github-script@v6
      with:
        script: |
          // 获取PR编号和作者信息
          const issueNumber = context.issue.number;
          const prAuthor = context.payload.pull_request?.user.login || context.payload.issue.user.login;
          
          // 获取PR的所有评论
          const { data: comments } = await github.issues.listComments({
            owner: context.repo.owner,
            repo: context.repo.repo,
            issue_number: issueNumber,
          });

          const requiredComment = "It's my turn in the queue";
          // 检查是否有PR作者发布的包含指定内容的评论
          const hasValidComment = comments.some(comment => 
            comment.user.login === prAuthor && comment.body.includes(requiredComment)
          );

          if (!hasValidComment) {
            throw new Error(`PR cannot be merged until you add the comment: '${requiredComment}' to confirm you're in the Slack merge queue.`);
          }

内容的提问来源于stack exchange,提问作者Richie Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 15:22:26