Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions .github/workflows/version-suggestion-comment.yml
Original file line number Diff line number Diff line change
@@ -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 = "<!-- version-suggestion-bot";

// Find our most recent, not-yet-minimized comment on this PR.
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
});
const ours = comments.filter(c => c.body.includes(marker));
const previous = ours.length ? ours[ours.length - 1] : null;

let previousClassification = null;
if (previous) {
const m = previous.body.match(/classification=([\w-]+:[\w-]+:[\w-]+)/);
previousClassification = m ? m[1] : null;
}
const currentClassification = `${type}:${classification}:${channel}`;

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 }
);
}

if (classification === "none") {
if (previous && previousClassification !== currentClassification) {
// Was suggesting something (or saying "none" for a different
// reason/channel), now saying "none" for this reason — say so
// once, then stop.
await minimizePrevious();
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `No release needed for this PR (\`${type}\`).\n\n${marker} classification=${currentClassification} -->`,
});
}
// 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,
});
136 changes: 41 additions & 95 deletions .github/workflows/version-suggestion.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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 = "<!-- version-suggestion-bot";

function parseVersion(v) {
const m = v.match(/^(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z]+)\.(\d+))?$/);
if (!m) throw new Error(`Cannot parse version: ${v}`);
Expand Down Expand Up @@ -137,81 +134,30 @@ jobs:
return formatVersion({ ...base, stage: channelLabel, stageNum: 1 });
}

const prNumber = context.payload.pull_request.number;

// Find our most recent, not-yet-minimized comment on this PR.
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
});
const ours = comments.filter(c => c.body.includes(marker));
const previous = ours.length ? ours[ours.length - 1] : null;

let previousClassification = null;
if (previous) {
const m = previous.body.match(/classification=([\w-]+:[\w-]+:[\w-]+)/);
previousClassification = m ? m[1] : null;
}
const currentClassification = `${type}:${classification}:${channel}`;

if (classification === "none") {
if (previous && previousClassification !== currentClassification) {
// Was suggesting something (or saying "none" for a different
// reason/channel), now saying "none" for this reason — say so
// once, then stop.
await minimizePrevious();
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `No release needed for this PR (\`${type}\`).\n\n${marker} classification=${currentClassification} -->`,
});
}
// 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading