|
1 | 1 | # PR Admission Gate |
2 | 2 | # |
3 | | -# Required status checks (register ALL jobs in repo Settings → Branches → branch protection): |
4 | | -# - scope-gate |
5 | | -# - check-product |
6 | | -# - test |
7 | | -# - build-matrix (6 × platform) |
| 3 | +# Required status checks (repo Settings → Branches → branch protection): |
| 4 | +# - owner-gate (product-autonomous vs platform verdict; base-version owners) |
| 5 | +# - hold-gate (red while a do-not-merge/hold label is present) |
| 6 | +# - scope-gate (conventional commit title) |
| 7 | +# - check-product (rule1–8 boundary checks) |
| 8 | +# - test (build + vet + offline tests) |
| 9 | +# - build-matrix (6 × platform) |
8 | 10 | # |
9 | | -# Merge Queue enablement: repo Settings → Merge Queue → "Require merge queue" (Task E9). |
| 11 | +# Plus repo Settings → General → "Allow auto-merge" ✓ (and "Allow squash merging"), |
| 12 | +# and a branch-protection rule requiring ≥1 approval (the platform-PR human gate). |
10 | 13 | # |
11 | | -# IMPORTANT: The `merge_group` trigger is required. Without it, required checks stay in |
12 | | -# "Pending" state inside the GitHub Merge Queue and PRs can never be merged. |
| 14 | +# Auto-merge: when owner-gate判定 a PR product-autonomous & eligible, the |
| 15 | +# auto-merge job runs `gh pr merge --auto`. GitHub merges only once ALL required |
| 16 | +# checks above are green and required approvals are met — so a red hold-gate |
| 17 | +# (hold label) or a failing test keeps the PR un-merged (fail-closed). |
| 18 | +# |
| 19 | +# Merge-queue note: the `merge_group` trigger is kept so check-product/test/ |
| 20 | +# build-matrix can report inside a queue if one is enabled later (ROADMAP §5). |
13 | 21 |
|
14 | 22 | name: PR Gate |
15 | 23 |
|
16 | 24 | on: |
17 | 25 | pull_request: |
18 | | - merge_group: # REQUIRED — without this, checks stay pending in the merge queue |
| 26 | + merge_group: |
19 | 27 |
|
20 | 28 | permissions: |
21 | 29 | contents: read |
22 | | - pull-requests: write # for the gate bot to comment |
| 30 | + pull-requests: write |
23 | 31 |
|
24 | 32 | jobs: |
25 | 33 | scope-gate: |
26 | | - # Fast structural judgment. Only meaningful on pull_request events. |
| 34 | + # Conventional commit title only. Product/platform/cross-product/owner |
| 35 | + # routing now lives in owner-gate (Go, base-version aware). |
27 | 36 | if: github.event_name == 'pull_request' |
28 | 37 | runs-on: ubuntu-latest |
29 | 38 | steps: |
30 | | - - uses: actions/checkout@v4 |
31 | | - with: { fetch-depth: 0 } |
32 | | - - name: Check scope, conventional title, protected paths |
| 39 | + - name: Conventional commit title |
33 | 40 | env: |
34 | 41 | PR_TITLE: ${{ github.event.pull_request.title }} |
35 | | - BASE_SHA: ${{ github.event.pull_request.base.sha }} |
36 | | - HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
37 | 42 | run: | |
38 | 43 | set -euo pipefail |
39 | | - FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA") |
40 | | - echo "Changed files:"; echo "$FILES" |
41 | | -
|
42 | | - # Protected paths require platform review (cannot be auto-admitted). |
43 | | - PROTECTED='^(\.github/workflows/|hack/|pkg/|go\.mod$|go\.sum$|\.goreleaser\.yaml$|\.svu\.yaml$)' |
44 | | - touches_protected=$(echo "$FILES" | grep -E "$PROTECTED" || true) |
45 | | -
|
46 | | - # Product scope: all changed files under a single products/<name>/ ? |
47 | | - products_touched=$(echo "$FILES" | grep -E '^products/' | sed -E 's#^products/([^/]+)/.*#\1#' | sort -u || true) |
48 | | - n_products=$(echo "$products_touched" | grep -c . || true) |
49 | | -
|
50 | | - # Conventional commit title check (feat|fix|refactor|build|ci|docs|test|chore|perf, optional scope). |
51 | 44 | if ! echo "$PR_TITLE" | grep -qE '^(feat|fix|refactor|build|ci|docs|test|chore|perf)(\([a-z0-9_-]+\))?!?: .+'; then |
52 | 45 | echo "::error::PR title is not a conventional commit: $PR_TITLE" |
53 | 46 | exit 1 |
54 | 47 | fi |
| 48 | + echo "scope-gate: conventional title OK" |
55 | 49 |
|
56 | | - if [ -n "$touches_protected" ]; then |
57 | | - echo "::notice::PR touches protected/platform paths — requires platform review (not auto-admit)." |
58 | | - elif [ "$n_products" -gt 1 ]; then |
59 | | - echo "::error::PR touches multiple products ($products_touched) — split into per-product PRs." |
60 | | - exit 1 |
| 50 | + owner-gate: |
| 51 | + # Decides product-autonomous (auto-merge) vs platform (human approval) and |
| 52 | + # surfaces the reason to the PR. Green on a successful judgment — eligibility |
| 53 | + # is consumed by the auto-merge job; routing is not a failure. |
| 54 | + if: github.event_name == 'pull_request' |
| 55 | + runs-on: ubuntu-latest |
| 56 | + outputs: |
| 57 | + auto_merge: ${{ steps.gate.outputs.autoMergeEligible }} |
| 58 | + steps: |
| 59 | + - uses: actions/checkout@v4 |
| 60 | + with: { fetch-depth: 0 } # need base+head for git diff / git show |
| 61 | + - uses: actions/setup-go@v5 |
| 62 | + with: { go-version: '1.25' } |
| 63 | + - name: Judge admission |
| 64 | + id: gate |
| 65 | + env: |
| 66 | + OWNER_GATE_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 67 | + OWNER_GATE_BASE_SHA: ${{ github.event.pull_request.base.sha }} |
| 68 | + HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 69 | + GOPROXY: 'https://goproxy.cn,direct' |
| 70 | + run: | |
| 71 | + set -euo pipefail |
| 72 | + git diff --name-status "$OWNER_GATE_BASE_SHA" "$HEAD_SHA" \ |
| 73 | + | go run ./hack/owner-gate |
| 74 | + - name: Post verdict to PR (sticky comment) |
| 75 | + env: |
| 76 | + GH_TOKEN: ${{ github.token }} |
| 77 | + REASON: ${{ steps.gate.outputs.reason }} |
| 78 | + PR: ${{ github.event.pull_request.number }} |
| 79 | + run: | |
| 80 | + set -euo pipefail |
| 81 | + MARKER='<!-- owner-gate-verdict -->' |
| 82 | + BODY="$MARKER"$'\n'"$REASON" |
| 83 | + ID=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \ |
| 84 | + --jq "map(select(.body|startswith(\"$MARKER\")))|.[0].id // empty") |
| 85 | + if [ -n "$ID" ]; then |
| 86 | + gh api -X PATCH "repos/$GITHUB_REPOSITORY/issues/comments/$ID" -f body="$BODY" >/dev/null |
| 87 | + else |
| 88 | + gh api -X POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -f body="$BODY" >/dev/null |
61 | 89 | fi |
| 90 | + echo "$REASON" |
| 91 | +
|
| 92 | + hold-gate: |
| 93 | + # /hold emergency brake: red while the PR carries do-not-merge/hold, which |
| 94 | + # blocks auto-merge via branch protection without dropping approvals. |
| 95 | + if: github.event_name == 'pull_request' |
| 96 | + runs-on: ubuntu-latest |
| 97 | + steps: |
| 98 | + - name: Fail if on hold |
| 99 | + if: contains(github.event.pull_request.labels.*.name, 'do-not-merge/hold') |
| 100 | + run: | |
| 101 | + echo "::error::PR is on hold (do-not-merge/hold label present) — remove the label to allow merge." |
| 102 | + exit 1 |
| 103 | + - run: echo "hold-gate - not on hold" |
62 | 104 |
|
63 | | - echo "scope-gate passed" |
| 105 | + auto-merge: |
| 106 | + # Native GitHub auto-merge. Enabled only for product-autonomous eligible PRs; |
| 107 | + # GitHub enforces required checks + approval before the actual merge. |
| 108 | + if: github.event_name == 'pull_request' && needs.owner-gate.outputs.auto_merge == 'true' |
| 109 | + needs: [owner-gate] |
| 110 | + runs-on: ubuntu-latest |
| 111 | + steps: |
| 112 | + - name: Enable auto-merge |
| 113 | + env: |
| 114 | + GH_TOKEN: ${{ github.token }} |
| 115 | + run: gh pr merge --auto --squash "${{ github.event.pull_request.number }}" --repo "$GITHUB_REPOSITORY" |
64 | 116 |
|
65 | 117 | check-product: |
66 | 118 | runs-on: ubuntu-latest |
|
72 | 124 | env: { GOPROXY: 'https://goproxy.cn,direct' } |
73 | 125 |
|
74 | 126 | test: |
75 | | - # NOTE: Only OFFLINE-safe packages are tested here. |
76 | | - # The `cmd` integration tests hit a live UCloud API and are excluded. |
77 | | - # Run `go test ./cmd/...` manually or in a separate protected environment with credentials. |
| 127 | + # NOTE: Only OFFLINE-safe packages. The `cmd` integration tests hit a live |
| 128 | + # UCloud API and are excluded; run `go test ./cmd/...` separately with creds. |
78 | 129 | runs-on: ubuntu-latest |
79 | 130 | steps: |
80 | 131 | - uses: actions/checkout@v4 |
|