From 6e8d32d126c1b974a20205e8d6b62968079a67c4 Mon Sep 17 00:00:00 2001 From: Adam J Esslinger Date: Thu, 3 Dec 2026 13:42:18 -0500 Subject: [PATCH] ci: post version suggestions via workflow_run instead of pull_request_target GitHub blocks pull_request_target by default on public repositories since 2026-12-02, so every Version suggestion run has been a startup_failure. Split the job: version-suggestion.yml now runs on pull_request with a read-only token, computes the suggestion and uploads it as an artifact; the new version-suggestion-comment.yml runs on workflow_run, validates the artifact and that its PR matches the run's head commit, and posts the comment. Fork PRs keep working (#115) without an admin policy. Co-Authored-By: Adam J Esslinger --- .../workflows/version-suggestion-comment.yml | 146 ++++++++++++++++++ .github/workflows/version-suggestion.yml | 136 +++++----------- README.md | 5 +- 3 files changed, 191 insertions(+), 96 deletions(-) create mode 100644 .github/workflows/version-suggestion-comment.yml diff --git a/.github/workflows/version-suggestion-comment.yml b/.github/workflows/version-suggestion-comment.yml new file mode 100644 index 0000000..beea0cc --- /dev/null +++ b/.github/workflows/version-suggestion-comment.yml @@ -0,0 +1,146 @@ +name: Version suggestion comment + +# Posts the comment for version-suggestion.yml. `workflow_run` runs in the base +# repository with a writable token, also for fork PRs, and GitHub does not block +# it the way it blocks `pull_request_target` on public repositories (since +# 2026-12-02). This job never checks out or runs PR code. The artifact it reads +# comes from a run a fork can influence, so every field is validated before use +# and the PR number must match the triggering run's own head branch and commit. +on: + workflow_run: + workflows: ["Version suggestion"] + types: [completed] + +jobs: + comment: + name: Version suggestion comment + runs-on: ubuntu-latest + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' + permissions: + actions: read + pull-requests: write + steps: + - uses: actions/download-artifact@v8 + with: + name: version-suggestion + path: suggestion + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ github.token }} + + - name: Validate suggestion, manage comment + uses: actions/github-script@v9 + with: + script: | + const fs = require("fs"); + const run = context.payload.workflow_run; + const s = JSON.parse(fs.readFileSync("suggestion/suggestion.json", "utf8")); + + const ok = + Number.isInteger(s.pr) && s.pr > 0 && + typeof s.type === "string" && /^[a-zA-Z]{1,20}$/.test(s.type) && + ["major", "minor", "patch", "none"].includes(s.class) && + ["alpha", "beta", "rc", "stable"].includes(s.channel) && + typeof s.breaking === "boolean" && + /^\d+\.\d+\.\d+(-[a-zA-Z]+\.\d+)?$/.test(s.baseline) && + (s.class === "none" ? s.suggested === "" : /^\d+\.\d+\.\d+(-[a-zA-Z]+\.\d+)?$/.test(s.suggested)); + if (!ok) { + core.setFailed(`Rejected malformed suggestion artifact: ${JSON.stringify(s)}`); + return; + } + const type = s.type; + + // The PR must be the one this run was for: same head commit and an + // open PR into main. A fork's `workflow_run.pull_requests` is empty, + // so look the PR up and compare instead of trusting the artifact. + const { data: pr } = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: s.pr, + }); + if (pr.head.sha !== run.head_sha || pr.base.ref !== "main") { + core.setFailed(`PR #${s.pr} does not match run ${run.id} (head ${run.head_sha})`); + return; + } + + const prNumber = s.pr; + const classification = s.class; + const channel = s.channel; + const breaking = s.breaking; + const baselineStr = s.baseline; + const marker = "`, + }); + } + // Otherwise: never suggested anything, or already said "none" — stay silent. + return; + } + + if (previous && previousClassification === currentClassification) { + // Meaningful classification hasn't changed since the last comment. + return; + } + + const tag = `v${s.suggested}`; + + await minimizePrevious(); + + const breakingNote = breaking ? " (breaking change)" : ""; + const body = [ + `### Version suggestion`, + ``, + `Based on this PR's title (\`${type}\`${breakingNote}) and the \`prerelease:${channel}\` label:`, + ``, + `| | |`, + `|---|---|`, + `| Current | \`${baselineStr}\` |`, + `| Suggested next tag | \`${tag}\` |`, + ``, + `This is informational only — no tag or release is created automatically yet.`, + ``, + `${marker} classification=${currentClassification} -->`, + ].join("\n"); + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + body, + }); diff --git a/.github/workflows/version-suggestion.yml b/.github/workflows/version-suggestion.yml index cbe6b5e..35e5b65 100644 --- a/.github/workflows/version-suggestion.yml +++ b/.github/workflows/version-suggestion.yml @@ -1,26 +1,25 @@ name: Version suggestion -# Runs as pull_request_target (not pull_request) so GITHUB_TOKEN gets the -# base repo's write permissions even on fork PRs — GitHub Actions issues a -# read-only token for pull_request runs triggered from a fork, no matter -# what the job's own `permissions:` block requests (see issue #115). Safe -# here because this job never checks out or executes PR content — it only -# reads github.event.pull_request.title/body/labels, which are already -# present on the event payload without a checkout. +# Computes the next-version suggestion for a PR and saves it as an artifact. +# The comment itself is posted by version-suggestion-comment.yml, which runs on +# `workflow_run` with a writable token. That split exists because GitHub blocks +# `pull_request_target` by default on public repositories since 2026-12-02, and +# a `pull_request` run from a fork only gets a read-only token (#115). on: - pull_request_target: + pull_request: branches: [main] # Default types are opened/synchronize/reopened, which miss title-only # edits entirely. labeled/unlabeled added so changing the prerelease:* # label alone re-triggers this job. types: [opened, edited, reopened, synchronize, labeled, unlabeled] +permissions: + contents: read + jobs: version-suggestion: name: Version suggestion runs-on: ubuntu-latest - permissions: - pull-requests: write steps: - uses: actions/checkout@v7 with: @@ -88,17 +87,15 @@ jobs: fi echo "version=$BASELINE" >> "$GITHUB_OUTPUT" - - name: Compute suggestion, manage comment + - name: Compute suggestion + id: suggest uses: actions/github-script@v9 + env: + CLASS: ${{ steps.classify.outputs.class }} + CHANNEL: ${{ steps.classify.outputs.channel }} + BASELINE: ${{ steps.baseline.outputs.version }} with: script: | - const classification = "${{ steps.classify.outputs.class }}"; - const channel = "${{ steps.classify.outputs.channel }}"; - const type = "${{ steps.classify.outputs.type }}"; - const breaking = "${{ steps.classify.outputs.breaking }}" === "true"; - const baselineStr = "${{ steps.baseline.outputs.version }}"; - const marker = "`, - }); - } - // Otherwise: never suggested anything, or already said "none" — stay silent. - return; - } + const cls = process.env.CLASS; + core.setOutput("suggested", cls === "none" ? "" : computeNextVersion(process.env.BASELINE, cls, process.env.CHANNEL)); - if (previous && previousClassification === currentClassification) { - // Meaningful classification hasn't changed since the last comment. - return; - } - - async function minimizePrevious() { - if (!previous) return; - // REST comment objects expose node_id directly — no separate - // lookup needed to get the GraphQL node id. - await github.graphql( - `mutation($id: ID!) { minimizeComment(input: { subjectId: $id, classifier: OUTDATED }) { clientMutationId } }`, - { id: previous.node_id } - ); - } - - const suggested = computeNextVersion(baselineStr, classification, channel); - const tag = `v${suggested}`; - - await minimizePrevious(); - - const breakingNote = breaking ? " (breaking change)" : ""; - const body = [ - `### Version suggestion`, - ``, - `Based on this PR's title (\`${type}\`${breakingNote}) and the \`prerelease:${channel}\` label:`, - ``, - `| | |`, - `|---|---|`, - `| Current | \`${baselineStr}\` |`, - `| Suggested next tag | \`${tag}\` |`, - ``, - `This is informational only — no tag or release is created automatically yet.`, - ``, - `${marker} classification=${currentClassification} -->`, - ].join("\n"); - - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: prNumber, - body, - }); + - name: Save suggestion for the comment workflow + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + TYPE: ${{ steps.classify.outputs.type }} + CLASS: ${{ steps.classify.outputs.class }} + CHANNEL: ${{ steps.classify.outputs.channel }} + BREAKING: ${{ steps.classify.outputs.breaking }} + BASELINE: ${{ steps.baseline.outputs.version }} + SUGGESTED: ${{ steps.suggest.outputs.suggested }} + run: | + mkdir -p suggestion + jq -n \ + --argjson pr "$PR_NUMBER" \ + --arg type "$TYPE" --arg class "$CLASS" --arg channel "$CHANNEL" \ + --arg breaking "$BREAKING" --arg baseline "$BASELINE" --arg suggested "$SUGGESTED" \ + '{pr: $pr, type: $type, class: $class, channel: $channel, breaking: ($breaking == "true"), baseline: $baseline, suggested: $suggested}' \ + > suggestion/suggestion.json + cat suggestion/suggestion.json + + - uses: actions/upload-artifact@v7 + with: + name: version-suggestion + path: suggestion/suggestion.json + retention-days: 1 diff --git a/README.md b/README.md index 251cfa5..05852e2 100644 --- a/README.md +++ b/README.md @@ -555,7 +555,10 @@ CI posts a comment on the PR suggesting the next tag/version based on the title's type and the `prerelease:*` label — informational only, nothing is tagged or released automatically (yet). The suggestion updates (and marks the previous suggestion as outdated) only when the underlying -classification actually changes, not on every edit to the title text. +classification actually changes, not on every edit to the title text. The +suggestion is computed on `pull_request` and the comment is posted by a +separate `workflow_run` workflow, so it also works on fork PRs without +`pull_request_target`, which GitHub blocks by default on public repositories. ### Tech Stack