pull-request-automation

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pull Request Automation Skill

Pull Request自动化技能

Step 1 — Audit Current PR Workflow

步骤1 — 审核当前PR工作流程

Check what already exists:
  • Is there a
    .github/pull_request_template.md
    ?
  • Is there a
    .github/CODEOWNERS
    file?
  • Are there existing labelling or size-check actions?
  • Are branch protection rules configured for
    main
    ?
检查现有配置:
  • 是否存在
    .github/pull_request_template.md
    文件?
  • 是否存在
    .github/CODEOWNERS
    文件?
  • 是否已有标签或大小检查相关的Actions?
  • 是否为
    main
    分支配置了分支保护规则?

Step 2 — PR Description Template

步骤2 — PR描述模板

Create
.github/pull_request_template.md
:
markdown
undefined
创建
.github/pull_request_template.md
文件:
markdown
undefined

Summary

Summary

<!-- What does this PR do? Why? -->
<!-- What does this PR do? Why? -->

Changes

Changes

<!-- Bullet list of key changes -->
<!-- Bullet list of key changes -->

Test Plan

Test Plan

  • Unit tests added / updated
  • Manual testing performed: <describe steps>
  • Breaking changes documented
  • Unit tests added / updated
  • Manual testing performed: <describe steps>
  • Breaking changes documented

Related Issues

Related Issues

Closes #

Keep the template short — the goal is consistency, not bureaucracy.
Closes #

保持模板简洁——目标是一致性,而非繁琐流程。

Step 3 — Auto-Labelling

步骤3 — 自动标签

Use
actions/labeler
to apply labels automatically based on changed file paths:
yaml
undefined
使用
actions/labeler
根据文件变更路径自动添加标签:
yaml
undefined

.github/labeler.yml

.github/labeler.yml

frontend:
  • changed-files:
    • any-glob-to-any-file: "src/features/**" backend:
  • changed-files:
    • any-glob-to-any-file: "internal/**" docs:
  • changed-files:
    • any-glob-to-any-file: "docs/**" infrastructure:
  • changed-files:
    • any-glob-to-any-file: "deployments/**"

```yaml
frontend:
  • changed-files:
    • any-glob-to-any-file: "src/features/**" backend:
  • changed-files:
    • any-glob-to-any-file: "internal/**" docs:
  • changed-files:
    • any-glob-to-any-file: "docs/**" infrastructure:
  • changed-files:
    • any-glob-to-any-file: "deployments/**"

```yaml

.github/workflows/labeler.yml

.github/workflows/labeler.yml

name: Label PR on: [pull_request_target] jobs: label: runs-on: ubuntu-latest steps: - uses: actions/labeler@v5 with: repo-token: ${{ secrets.GITHUB_TOKEN }}
undefined
name: Label PR on: [pull_request_target] jobs: label: runs-on: ubuntu-latest steps: - uses: actions/labeler@v5 with: repo-token: ${{ secrets.GITHUB_TOKEN }}
undefined

Step 4 — CODEOWNERS

步骤4 — CODEOWNERS

Define ownership in
.github/CODEOWNERS
. GitHub requests reviews from owners automatically when their files are touched:
undefined
.github/CODEOWNERS
中定义代码所有权。当相关文件被修改时,GitHub会自动请求对应所有者进行审核:
undefined

Global fallback

Global fallback

  •              @org/platform-team
  •              @org/platform-team

Frontend

Frontend

/src/features/ @org/frontend-team
/src/features/ @org/frontend-team

Infrastructure

Infrastructure

/deployments/ @org/infra-team /build/ @org/infra-team
/deployments/ @org/infra-team /build/ @org/infra-team

Database migrations

Database migrations

/internal/infrastructure/migrations/ @org/dba-team
undefined
/internal/infrastructure/migrations/ @org/dba-team
undefined

Step 5 — PR Size Check

步骤5 — PR大小检查

Flag PRs that are too large to review effectively. Add a size-check action:
yaml
undefined
标记过大的PR,以便更有效地进行审核。添加大小检查Action:
yaml
undefined

.github/workflows/pr-size.yml

.github/workflows/pr-size.yml

name: PR Size Check on: [pull_request] jobs: size: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: { fetch-depth: 0 } - name: Check PR size run: | LINES=$(git diff --stat origin/${{ github.base_ref }}...HEAD | tail -1 | grep -oE '[0-9]+ insertions' | grep -oE '[0-9]+' || echo 0) if [ "$LINES" -gt 500 ]; then echo "::warning::PR adds $LINES lines. Consider splitting into smaller PRs." fi

Adjust the threshold to match your team's preferences (200–500 lines is typical).
name: PR Size Check on: [pull_request] jobs: size: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: { fetch-depth: 0 } - name: Check PR size run: | LINES=$(git diff --stat origin/${{ github.base_ref }}...HEAD | tail -1 | grep -oE '[0-9]+ insertions' | grep -oE '[0-9]+' || echo 0) if [ "$LINES" -gt 500 ]; then echo "::warning::PR adds $LINES lines. Consider splitting into smaller PRs." fi

可根据团队偏好调整阈值(通常为200–500行)。

Step 6 — Branch Protection Rules

步骤6 — 分支保护规则

Verify (or configure via
gh api
) that
main
has:
  • Required status checks before merge.
  • At least 1 required approving review.
  • Dismiss stale reviews on new commits.
  • No force pushes allowed.
  • No deletions allowed.
bash
gh api repos/{owner}/{repo}/branches/main/protection \
  --method PUT --input - <<'EOF'
{
  "required_status_checks": {"strict": true, "contexts": ["ci"]},
  "enforce_admins": false,
  "required_pull_request_reviews": {
    "required_approving_review_count": 1,
    "dismiss_stale_reviews": true
  },
  "restrictions": null,
  "allow_force_pushes": false,
  "allow_deletions": false
}
EOF
验证(或通过
gh api
配置)
main
分支是否具备以下规则:
  • 合并前需通过必要的状态检查。
  • 至少需要1个批准的审核意见。
  • 新提交后自动驳回过时的审核意见。
  • 不允许强制推送。
  • 不允许删除分支。
bash
gh api repos/{owner}/{repo}/branches/main/protection \
  --method PUT --input - <<'EOF'
{
  "required_status_checks": {"strict": true, "contexts": ["ci"]},
  "enforce_admins": false,
  "required_pull_request_reviews": {
    "required_approving_review_count": 1,
    "dismiss_stale_reviews": true
  },
  "restrictions": null,
  "allow_force_pushes": false,
  "allow_deletions": false
}
EOF

Verify

验证清单

  • .github/pull_request_template.md
    exists and is used on new PRs.
  • Labels are applied automatically on open.
  • CODEOWNERS triggers review requests on relevant paths.
  • Large PRs receive a size warning.
  • Branch protection is active on
    main
    .
  • .github/pull_request_template.md
    已存在且在新建PR时生效。
  • 打开PR时自动添加标签。
  • 修改相关路径时,CODEOWNERS会触发审核请求。
  • 大型PR会收到大小警告。
  • main
    分支的保护规则已生效。