|
| 1 | +name: Validate Issue Template Tags & Categories |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - 'scripts.json' |
| 7 | + - 'scripts.schema.json' |
| 8 | + - '.github/ISSUE_TEMPLATE/add-script.yml' |
| 9 | + - '.github/ISSUE_TEMPLATE/update-script.yml' |
| 10 | + - 'tools/sync_issue_template_options.py' |
| 11 | + - 'tools/test_sync_issue_template_options.py' |
| 12 | + - '.github/workflows/validate-issue-templates.yml' |
| 13 | + pull_request: |
| 14 | + paths: |
| 15 | + - 'scripts.json' |
| 16 | + - 'scripts.schema.json' |
| 17 | + - '.github/ISSUE_TEMPLATE/add-script.yml' |
| 18 | + - '.github/ISSUE_TEMPLATE/update-script.yml' |
| 19 | + - 'tools/sync_issue_template_options.py' |
| 20 | + - 'tools/test_sync_issue_template_options.py' |
| 21 | + - '.github/workflows/validate-issue-templates.yml' |
| 22 | + pull_request_target: |
| 23 | + paths: |
| 24 | + - 'scripts.json' |
| 25 | + - 'scripts.schema.json' |
| 26 | + - '.github/ISSUE_TEMPLATE/add-script.yml' |
| 27 | + - '.github/ISSUE_TEMPLATE/update-script.yml' |
| 28 | + - 'tools/sync_issue_template_options.py' |
| 29 | + - 'tools/test_sync_issue_template_options.py' |
| 30 | + - '.github/workflows/validate-issue-templates.yml' |
| 31 | + |
| 32 | +permissions: |
| 33 | + contents: read |
| 34 | + |
| 35 | +jobs: |
| 36 | + # Read-only check. Runs for every PR, including forks (which get a |
| 37 | + # restricted, non-writable GITHUB_TOKEN here — that's fine, this job never |
| 38 | + # writes anything). |
| 39 | + check: |
| 40 | + if: github.event_name == 'pull_request' |
| 41 | + runs-on: ubuntu-latest |
| 42 | + steps: |
| 43 | + - uses: actions/checkout@v6 |
| 44 | + - uses: astral-sh/setup-uv@v8.1.0 |
| 45 | + with: |
| 46 | + enable-cache: false |
| 47 | + - name: Run sync script tests |
| 48 | + run: uv run tools/test_sync_issue_template_options.py |
| 49 | + - name: Check tags/category sync |
| 50 | + run: uv run tools/sync_issue_template_options.py --check |
| 51 | + |
| 52 | + # Self-heals same-repo branches (this workflow's own commit is made with |
| 53 | + # GITHUB_TOKEN, so it never re-triggers itself or any other workflow — |
| 54 | + # GitHub Actions doesn't fire new workflow-triggering events for |
| 55 | + # GITHUB_TOKEN-authored pushes). |
| 56 | + sync: |
| 57 | + if: github.event_name == 'push' |
| 58 | + runs-on: ubuntu-latest |
| 59 | + permissions: |
| 60 | + contents: write |
| 61 | + steps: |
| 62 | + - uses: actions/checkout@v6 |
| 63 | + - uses: astral-sh/setup-uv@v8.1.0 |
| 64 | + with: |
| 65 | + enable-cache: false |
| 66 | + - name: Run sync script tests |
| 67 | + run: uv run tools/test_sync_issue_template_options.py |
| 68 | + - name: Sync tags/category |
| 69 | + run: uv run tools/sync_issue_template_options.py --write | tee /tmp/sync-output.txt |
| 70 | + - name: Annotate new categories |
| 71 | + run: | |
| 72 | + line=$(grep '^new_categories=' /tmp/sync-output.txt || true) |
| 73 | + if [ -n "$line" ]; then |
| 74 | + echo "::warning::New categories introduced: ${line#new_categories=} — please confirm these aren't duplicates/typos before merging." |
| 75 | + fi |
| 76 | + - name: Annotate unused categories |
| 77 | + run: | |
| 78 | + line=$(grep '^unused_categories=' /tmp/sync-output.txt || true) |
| 79 | + if [ -n "$line" ]; then |
| 80 | + echo "::notice::Categories with no current entries: ${line#unused_categories=} — consider pruning scripts.schema.json if any of these are stale." |
| 81 | + fi |
| 82 | + - name: Commit corrections if needed |
| 83 | + run: | |
| 84 | + if ! git diff --quiet -- scripts.schema.json .github/ISSUE_TEMPLATE/add-script.yml .github/ISSUE_TEMPLATE/update-script.yml; then |
| 85 | + git config user.name "github-actions[bot]" |
| 86 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 87 | + git add scripts.schema.json .github/ISSUE_TEMPLATE/add-script.yml .github/ISSUE_TEMPLATE/update-script.yml |
| 88 | + git commit -m "chore: sync issue template tags/categories" |
| 89 | + git push origin "HEAD:${{ github.ref_name }}" |
| 90 | + fi |
| 91 | +
|
| 92 | + # Fork PRs can never be auto-committed to (GITHUB_TOKEN cannot push to a |
| 93 | + # different repository under any trigger, pull_request_target included — |
| 94 | + # that event only grants a writable token for *this* repo). Instead this |
| 95 | + # job safely computes the same fix and posts it as a sticky PR comment. |
| 96 | + suggest-fix-for-fork-prs: |
| 97 | + if: github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository |
| 98 | + runs-on: ubuntu-latest |
| 99 | + permissions: |
| 100 | + pull-requests: write |
| 101 | + contents: read |
| 102 | + steps: |
| 103 | + # Deliberately no `ref:` override: this checks out the *base* branch's |
| 104 | + # trusted copy of the sync script, never the PR's own version — a |
| 105 | + # malicious PR can't smuggle in a modified script for this elevated |
| 106 | + # job to execute. |
| 107 | + - uses: actions/checkout@v6 |
| 108 | + - uses: astral-sh/setup-uv@v8.1.0 |
| 109 | + with: |
| 110 | + enable-cache: false |
| 111 | + - name: Fetch PR's scripts.json as data only (never executed) |
| 112 | + run: | |
| 113 | + git fetch origin "pull/${{ github.event.pull_request.number }}/head" |
| 114 | + git show FETCH_HEAD:scripts.json > /tmp/pr-scripts.json |
| 115 | + - name: Compute what --write would change, without committing |
| 116 | + id: diff |
| 117 | + run: | |
| 118 | + uv run tools/sync_issue_template_options.py --write --scripts-json /tmp/pr-scripts.json || true |
| 119 | + if git diff --quiet -- scripts.schema.json .github/ISSUE_TEMPLATE/add-script.yml .github/ISSUE_TEMPLATE/update-script.yml; then |
| 120 | + echo "has_diff=false" >> "$GITHUB_OUTPUT" |
| 121 | + else |
| 122 | + git diff -- scripts.schema.json .github/ISSUE_TEMPLATE/add-script.yml .github/ISSUE_TEMPLATE/update-script.yml | head -n 200 > /tmp/sync.diff |
| 123 | + echo "has_diff=true" >> "$GITHUB_OUTPUT" |
| 124 | + fi |
| 125 | + # Discard the local write — nothing from this job is ever committed. |
| 126 | + git checkout -- scripts.schema.json .github/ISSUE_TEMPLATE/add-script.yml .github/ISSUE_TEMPLATE/update-script.yml |
| 127 | + - name: Post or update sticky PR comment |
| 128 | + uses: actions/github-script@v9 |
| 129 | + with: |
| 130 | + script: | |
| 131 | + const fs = require('fs'); |
| 132 | + const marker = '<!-- sync-issue-template-options-bot -->'; |
| 133 | + const hasDiff = '${{ steps.diff.outputs.has_diff }}' === 'true'; |
| 134 | + let body; |
| 135 | + if (hasDiff) { |
| 136 | + const diff = fs.existsSync('/tmp/sync.diff') ? fs.readFileSync('/tmp/sync.diff', 'utf8') : ''; |
| 137 | + body = marker + '\n\n⚠️ This PR introduces a new tag or category not yet reflected in `scripts.schema.json` / the issue form templates.\n\n' + |
| 138 | + 'Since this PR comes from a fork, this can\'t be fixed automatically here. To resolve it:\n' + |
| 139 | + '- a maintainer can pull this branch into the repo (the automatic fix runs on push), or\n' + |
| 140 | + '- you (or a maintainer) can run `uv run tools/sync_issue_template_options.py --write` locally and commit the result to this branch.\n\n' + |
| 141 | + '<details><summary>Diff that would be applied (truncated to 200 lines)</summary>\n\n```diff\n' + diff + '\n```\n</details>'; |
| 142 | + } else { |
| 143 | + body = marker + '\n\n✅ Tags/categories are in sync.'; |
| 144 | + } |
| 145 | + const { data: comments } = await github.rest.issues.listComments({ |
| 146 | + owner: context.repo.owner, |
| 147 | + repo: context.repo.repo, |
| 148 | + issue_number: context.issue.number, |
| 149 | + }); |
| 150 | + const existing = comments.find(c => c.body.includes(marker)); |
| 151 | + if (existing) { |
| 152 | + await github.rest.issues.updateComment({ |
| 153 | + owner: context.repo.owner, repo: context.repo.repo, comment_id: existing.id, body, |
| 154 | + }); |
| 155 | + } else if (hasDiff) { |
| 156 | + await github.rest.issues.createComment({ |
| 157 | + owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number, body, |
| 158 | + }); |
| 159 | + } |
0 commit comments