From e629b93ebf8c4c243ef0c90175a4be573830bc33 Mon Sep 17 00:00:00 2001 From: Goran Dodig Date: Thu, 5 Nov 2026 01:47:52 +0100 Subject: [PATCH 1/6] ci(release): record stable releases in Linear Each stable release syncs to Linear with the PRs merged since the previous one, found through GitHub so rebase merges count, and the GitHub release text as notes, its #N references linked to this repo. Log-only until LINEAR_RELEASE_MODE is live. --- .github/scripts/record-linear-release.cjs | 173 ++++++++++++++++++++++ .github/workflows/release.yml | 45 +++++- 2 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 .github/scripts/record-linear-release.cjs diff --git a/.github/scripts/record-linear-release.cjs b/.github/scripts/record-linear-release.cjs new file mode 100644 index 0000000..1266c99 --- /dev/null +++ b/.github/scripts/record-linear-release.cjs @@ -0,0 +1,173 @@ +// Records a deploy or a release as a Linear release. Run it from actions/github-script: +// +// script: return require('./.github/scripts/record-linear-release.cjs')({ github, context, core }) +// +// Environment: +// LINEAR_ACCESS_KEY The pipeline's access key. Without one, nothing is recorded. +// LINEAR_RELEASE_MODE "live" records the release. Anything else only logs what would be sent. +// RELEASE_SHA The commit that was deployed or tagged. +// RELEASE_VERSION The release tag, or for a deploy its short SHA (the default). +// RELEASE_BRANCH The branch PRs merge into. Defaults to main. +// RELEASE_NOTES "prs" (the default) lists the PRs. "github-release" copies the body of +// the GitHub release tagged RELEASE_VERSION. +// +// Output: release-url, the release's page in Linear. It stays empty when the release is only logged. +'use strict'; + +const LINEAR_API = 'https://api.linear.app/graphql'; +const MAX_BASE_CANDIDATES = 20; + +// On GitHub, "#12" is a PR or issue of the same repo. Linear matches a bare "#12" against every +// repo it knows, so it can link a PR from another repo. Every reference is therefore written out +// as a full link, except in code, existing links and URLs. GitHub redirects /pull/N to /issues/N +// when N is an issue. +function linkReferences(text, owner, repo) { + const untouched = /(`[^`\n]*`|\[[^\]\n]*\]\([^)\n]*\)|<[^>\n]*>|https?:\/\/[^\s)]+)/; + return text + .split(untouched) + .map((part, i) => + i % 2 + ? part + : part + .replace( + /(^|[^\w/#&])([\w.-]+)\/([\w.-]+)#(\d+)\b/g, + (_, pre, o, r, n) => `${pre}[${o}/${r}#${n}](https://github.com/${o}/${r}/pull/${n})`, + ) + .replace( + /(^|[^\w/#&[]|\bPR ?)#(\d+)\b/g, + (_, pre, n) => `${pre}[#${n}](https://github.com/${owner}/${repo}/pull/${n})`, + ), + ) + .join(''); +} + +function prNotes(prs, owner, repo) { + const link = (n) => `[#${n}](https://github.com/${owner}/${repo}/pull/${n})`; + const dependabot = prs.filter((pr) => pr.user.login === 'dependabot[bot]'); + const lines = prs + .filter((pr) => !dependabot.includes(pr)) + .map((pr) => `- ${link(pr.number)} ${linkReferences(pr.title, owner, repo)} · ${pr.user.login}`); + if (dependabot.length) { + const s = dependabot.length > 1 ? 's' : ''; + lines.push(`- ${dependabot.length} Dependabot update${s}: ${dependabot.map((pr) => link(pr.number)).join(', ')}`); + } + return lines.join('\n'); +} + +// The PRs merged into `branch` since the newest earlier release in this pipeline. +async function pullRequestsSince({ github, owner, repo, branch, head, releases }) { + const compare = async (base, target, page = 1, perPage = 1) => + (await github.rest.repos.compareCommitsWithBasehead({ owner, repo, basehead: `${base}...${target}`, page, per_page: perPage })) + .data; + + // Testing can be deployed from a branch. Such a deploy gets no PRs; they count once they reach the branch. + if (!['ahead', 'identical'].includes((await compare(head, branch)).status)) { + return { prs: [], reason: `${head.slice(0, 7)} is not on ${branch}` }; + } + + // Sorted by completion time, since an older release whose notes were edited can be listed first. + // A release that is not on the branch is a branch deploy and is never a base. + const candidates = releases + .filter((r) => r.commitSha && r.commitSha !== head) + .sort((a, b) => Date.parse(b.completedAt || b.createdAt) - Date.parse(a.completedAt || a.createdAt)) + .slice(0, MAX_BASE_CANDIDATES); + let base; + for (const release of candidates) { + const { status } = await compare(release.commitSha, head); + if (status === 'diverged') continue; + // "behind": the newest release is ahead of this commit, so this deploy is a rollback. + if (status === 'behind') return { prs: [], reason: `rollback: ${release.version} is newer` }; + base = release; + break; + } + if (!base) return { prs: [], reason: 'no earlier release on the branch' }; + + const commits = []; + for (let page = 1; ; page++) { + const data = await compare(base.commitSha, head, page, 100); + commits.push(...data.commits); + if (data.commits.length < 100 || commits.length >= data.total_commits) break; + } + + // GitHub knows each commit's PR, whatever the merge method. + const prs = new Map(); + for (const { sha } of commits) { + const { data } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ owner, repo, commit_sha: sha }); + for (const pr of data) { + if (pr.merged_at && pr.base.ref === branch && !prs.has(pr.number)) prs.set(pr.number, pr); + } + } + return { prs: [...prs.values()], base }; +} + +module.exports = async ({ github, context, core }) => { + const key = process.env.LINEAR_ACCESS_KEY; + if (!key) { + core.notice('No Linear access key for this pipeline, so the release is not recorded.'); + return; + } + const required = (name) => { + if (!process.env[name]) throw new Error(`${name} is not set`); + return process.env[name]; + }; + const head = required('RELEASE_SHA'); + const version = process.env.RELEASE_VERSION || head.slice(0, 7); + const branch = process.env.RELEASE_BRANCH || 'main'; + const live = process.env.LINEAR_RELEASE_MODE === 'live'; + const { owner, repo } = context.repo; + + const linear = async (query, variables) => { + const res = await fetch(LINEAR_API, { + method: 'POST', + headers: { 'Content-Type': 'application/json', Authorization: key }, + body: JSON.stringify({ query, variables }), + }); + const body = await res.json().catch(() => ({})); + if (!res.ok || body.errors) throw new Error(`Linear API ${res.status}: ${JSON.stringify(body.errors || body)}`); + return body.data; + }; + + const { recentReleasesByAccessKey: releases } = await linear( + 'query { recentReleasesByAccessKey(limit: 50) { commitSha version completedAt createdAt } }', + ); + const { prs, base, reason } = await pullRequestsSince({ github, owner, repo, branch, head, releases }); + + const links = [{ label: 'GitHub run', url: `${context.serverUrl}/${owner}/${repo}/actions/runs/${context.runId}` }]; + let notes; + if (process.env.RELEASE_NOTES === 'github-release') { + const { data } = await github.rest.repos.getReleaseByTag({ owner, repo, tag: version }); + notes = linkReferences((data.body || '').replace(/\r\n/g, '\n').trim(), owner, repo); + links.unshift({ label: 'GitHub release', url: data.html_url }); + } else { + notes = prNotes(prs, owner, repo); + } + + const input = { + name: version, + version, + commitSha: head, + repository: { owner, name: repo, provider: 'github', url: `https://github.com/${owner}/${repo}` }, + pullRequestReferences: prs.map((pr) => ({ repositoryOwner: owner, repositoryName: repo, number: pr.number })), + links, + ...(notes ? { releaseNotes: { title: version, content: notes } } : {}), + }; + + const since = base ? `since ${base.version}` : `none (${reason})`; + core.info(`PRs ${since}: ${prs.map((pr) => `#${pr.number}`).join(', ') || '-'}`); + if (!live) { + core.notice(`Log-only: ${version} would be recorded in Linear with ${prs.length} PRs. Set LINEAR_RELEASE_MODE=live to record it.`); + core.info(JSON.stringify(input, null, 2)); + return; + } + + const { releaseSyncByAccessKey: result } = await linear( + 'mutation ($input: ReleaseSyncInputBase!) { releaseSyncByAccessKey(input: $input) { success release { url } } }', + { input }, + ); + core.setOutput('release-url', result.release.url); + core.notice(`Recorded ${version} in Linear: ${result.release.url}`); +}; + +module.exports.linkReferences = linkReferences; +module.exports.prNotes = prNotes; +module.exports.pullRequestsSince = pullRequestsSince; diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ec4794c..ea17cfd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -124,12 +124,50 @@ jobs: VERSION: ${{ steps.version.outputs.version }} run: echo "Released v${VERSION} at $(git rev-parse --short HEAD)." >> "$GITHUB_STEP_SUMMARY" + # Stable releases only; pre-releases stay out of the Linear pipeline. + linear-release: + name: Record release in Linear + needs: release + if: ${{ needs.release.outputs.prerelease == 'false' }} + runs-on: ubuntu-latest + # pull-requests lets the step look up each commit's PR. + permissions: + contents: read + pull-requests: read + outputs: + # The notify job links the release, or says recording it failed. + release-url: ${{ steps.linear.outputs.release-url }} + outcome: ${{ steps.linear.outcome }} + + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # The release gets the PRs merged since the previous one, and the GitHub + # release text as notes; a re-run updates it. Log-only until + # LINEAR_RELEASE_MODE is "live". Non-fatal: the release is already out. + - name: Record release in Linear + id: linear + continue-on-error: true + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + LINEAR_ACCESS_KEY: ${{ secrets.LINEAR_ACCESS_KEY }} + LINEAR_RELEASE_MODE: ${{ vars.LINEAR_RELEASE_MODE }} + RELEASE_VERSION: v${{ needs.release.outputs.version }} + RELEASE_SHA: ${{ github.sha }} + RELEASE_BRANCH: dev + RELEASE_NOTES: github-release + with: + script: return require('./.github/scripts/record-linear-release.cjs')({ github, context, core }) + # End-of-run notices run in their own job: a failed Slack post fails only this # job, not the release, and "Re-run failed jobs" re-posts without releasing # again. notify: name: Notify Slack - needs: release + # linear-release is skipped for pre-releases and failed releases; this job still runs. + needs: [release, linear-release] # Pushes to dev that are not release commits skip the release job and post # nothing. if: ${{ !cancelled() && vars.SLACK_CHANNEL_ID != '' && contains(fromJSON('["success", "failure"]'), needs.release.result) }} @@ -180,6 +218,9 @@ jobs: - name: Notify Slack (release published) if: ${{ needs.release.result == 'success' && needs.release.outputs.prerelease == 'false' }} + env: + # Empty while the release step only logs, or when it failed. + RELEASE_URL: ${{ needs.linear-release.outputs.release-url }} uses: slackapi/slack-github-action@dcb1066f776dd043e64d0e8ba94ca15cc7e1875d # v4.0.0 with: method: chat.postMessage @@ -190,7 +231,7 @@ jobs: thread_ts: ${{ env.THREAD_TS }} text: >- *pyro*: `v${{ env.VERSION }}` released :white_check_mark: and `main` moved to it. - Update with `claude plugin marketplace update pyro && claude plugin update pyro`, then restart. <${{ github.server_url }}/${{ github.repository }}/releases/tag/v${{ env.VERSION }}|Release notes>. + Update with `claude plugin marketplace update pyro && claude plugin update pyro`, then restart. <${{ github.server_url }}/${{ github.repository }}/releases/tag/v${{ env.VERSION }}|Release notes>.${{ env.RELEASE_URL != '' && format(' <{0}|View release>.', env.RELEASE_URL) || needs.linear-release.outputs.outcome == 'failure' && ' Recording it in Linear failed :warning:.' || '' }} # Success also puts a ✅ on the start notice, so the result shows in the # channel without another message. Needs the app's reactions:write scope. From 41a852bbd3c026b22a332ee209d853c0882577c1 Mon Sep 17 00:00:00 2001 From: Goran Dodig Date: Thu, 5 Nov 2026 01:53:29 +0100 Subject: [PATCH 2/6] docs: mention that stable releases are recorded in Linear --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3eed00a..2c079df 100644 --- a/README.md +++ b/README.md @@ -153,4 +153,6 @@ There are two kinds of release, and they differ in how the PR opens: Merges into `dev` and releases post to the team's Slack channel. A release posts a start message and replies with the result in its thread; failures also -go to the channel, and success adds a ✅ to the start message. +go to the channel, and success adds a ✅ to the start message. Stable releases +are also recorded in the team's Linear release tracker, with the PRs merged since +the previous release and the GitHub release notes. From c42c6aedb94dbecc77c07fa7afe99f0c33189cec Mon Sep 17 00:00:00 2001 From: Goran Dodig Date: Thu, 5 Nov 2026 01:53:29 +0100 Subject: [PATCH 3/6] ci(release): time out the Linear release job after 10 minutes A hung Linear or GitHub call would otherwise hold the job, and the Slack notice that waits for it, until the 6-hour job limit. Linear calls also give up after 30 seconds. --- .github/scripts/record-linear-release.cjs | 1 + .github/workflows/release.yml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.github/scripts/record-linear-release.cjs b/.github/scripts/record-linear-release.cjs index 1266c99..c741960 100644 --- a/.github/scripts/record-linear-release.cjs +++ b/.github/scripts/record-linear-release.cjs @@ -121,6 +121,7 @@ module.exports = async ({ github, context, core }) => { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: key }, body: JSON.stringify({ query, variables }), + signal: AbortSignal.timeout(30_000), }); const body = await res.json().catch(() => ({})); if (!res.ok || body.errors) throw new Error(`Linear API ${res.status}: ${JSON.stringify(body.errors || body)}`); diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ea17cfd..cf5f4a8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -150,6 +150,8 @@ jobs: - name: Record release in Linear id: linear continue-on-error: true + # A hung API call must not hold up the Slack notice. + timeout-minutes: 10 uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 env: LINEAR_ACCESS_KEY: ${{ secrets.LINEAR_ACCESS_KEY }} From 6d6d5baeed394742ce3eeaf2b0b59ab308e4fb4f Mon Sep 17 00:00:00 2001 From: Goran Dodig Date: Thu, 5 Nov 2026 01:56:20 +0100 Subject: [PATCH 4/6] ci(release): fix edge cases in the Linear release step - Leave #N inside fenced code alone. - A new tag on an already released commit gets no PRs. - Fail in live mode without an access key, and when Linear reports a failed sync. - Warn in Slack when the job fails before the step runs. --- .github/scripts/record-linear-release.cjs | 20 ++++++++++++-------- .github/workflows/release.yml | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.github/scripts/record-linear-release.cjs b/.github/scripts/record-linear-release.cjs index c741960..a5f6832 100644 --- a/.github/scripts/record-linear-release.cjs +++ b/.github/scripts/record-linear-release.cjs @@ -19,10 +19,10 @@ const MAX_BASE_CANDIDATES = 20; // On GitHub, "#12" is a PR or issue of the same repo. Linear matches a bare "#12" against every // repo it knows, so it can link a PR from another repo. Every reference is therefore written out -// as a full link, except in code, existing links and URLs. GitHub redirects /pull/N to /issues/N -// when N is an issue. +// as a full link, except in code (fenced or inline), existing links and URLs. GitHub redirects +// /pull/N to /issues/N when N is an issue. function linkReferences(text, owner, repo) { - const untouched = /(`[^`\n]*`|\[[^\]\n]*\]\([^)\n]*\)|<[^>\n]*>|https?:\/\/[^\s)]+)/; + const untouched = /(```[\s\S]*?```|~~~[\s\S]*?~~~|`[^`\n]*`|\[[^\]\n]*\]\([^)\n]*\)|<[^>\n]*>|https?:\/\/[^\s)]+)/; return text .split(untouched) .map((part, i) => @@ -55,7 +55,7 @@ function prNotes(prs, owner, repo) { } // The PRs merged into `branch` since the newest earlier release in this pipeline. -async function pullRequestsSince({ github, owner, repo, branch, head, releases }) { +async function pullRequestsSince({ github, owner, repo, branch, head, version, releases }) { const compare = async (base, target, page = 1, perPage = 1) => (await github.rest.repos.compareCommitsWithBasehead({ owner, repo, basehead: `${base}...${target}`, page, per_page: perPage })) .data; @@ -66,9 +66,10 @@ async function pullRequestsSince({ github, owner, repo, branch, head, releases } } // Sorted by completion time, since an older release whose notes were edited can be listed first. - // A release that is not on the branch is a branch deploy and is never a base. + // A release that is not on the branch is a branch deploy and is never a base. This release itself + // (a re-run) is skipped, but another version at the same commit is a base, and leaves no new PRs. const candidates = releases - .filter((r) => r.commitSha && r.commitSha !== head) + .filter((r) => r.commitSha && r.version !== version) .sort((a, b) => Date.parse(b.completedAt || b.createdAt) - Date.parse(a.completedAt || a.createdAt)) .slice(0, MAX_BASE_CANDIDATES); let base; @@ -102,7 +103,9 @@ async function pullRequestsSince({ github, owner, repo, branch, head, releases } module.exports = async ({ github, context, core }) => { const key = process.env.LINEAR_ACCESS_KEY; + const live = process.env.LINEAR_RELEASE_MODE === 'live'; if (!key) { + if (live) throw new Error('LINEAR_RELEASE_MODE is live, but this pipeline has no Linear access key'); core.notice('No Linear access key for this pipeline, so the release is not recorded.'); return; } @@ -113,7 +116,6 @@ module.exports = async ({ github, context, core }) => { const head = required('RELEASE_SHA'); const version = process.env.RELEASE_VERSION || head.slice(0, 7); const branch = process.env.RELEASE_BRANCH || 'main'; - const live = process.env.LINEAR_RELEASE_MODE === 'live'; const { owner, repo } = context.repo; const linear = async (query, variables) => { @@ -131,7 +133,7 @@ module.exports = async ({ github, context, core }) => { const { recentReleasesByAccessKey: releases } = await linear( 'query { recentReleasesByAccessKey(limit: 50) { commitSha version completedAt createdAt } }', ); - const { prs, base, reason } = await pullRequestsSince({ github, owner, repo, branch, head, releases }); + const { prs, base, reason } = await pullRequestsSince({ github, owner, repo, branch, head, version, releases }); const links = [{ label: 'GitHub run', url: `${context.serverUrl}/${owner}/${repo}/actions/runs/${context.runId}` }]; let notes; @@ -165,6 +167,8 @@ module.exports = async ({ github, context, core }) => { 'mutation ($input: ReleaseSyncInputBase!) { releaseSyncByAccessKey(input: $input) { success release { url } } }', { input }, ); + // Linear can report a failed sync with HTTP 200. + if (!result.success || !result.release) throw new Error(`Linear did not record ${version}: ${JSON.stringify(result)}`); core.setOutput('release-url', result.release.url); core.notice(`Recorded ${version} in Linear: ${result.release.url}`); }; diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cf5f4a8..f9432a7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -233,7 +233,7 @@ jobs: thread_ts: ${{ env.THREAD_TS }} text: >- *pyro*: `v${{ env.VERSION }}` released :white_check_mark: and `main` moved to it. - Update with `claude plugin marketplace update pyro && claude plugin update pyro`, then restart. <${{ github.server_url }}/${{ github.repository }}/releases/tag/v${{ env.VERSION }}|Release notes>.${{ env.RELEASE_URL != '' && format(' <{0}|View release>.', env.RELEASE_URL) || needs.linear-release.outputs.outcome == 'failure' && ' Recording it in Linear failed :warning:.' || '' }} + Update with `claude plugin marketplace update pyro && claude plugin update pyro`, then restart. <${{ github.server_url }}/${{ github.repository }}/releases/tag/v${{ env.VERSION }}|Release notes>.${{ env.RELEASE_URL != '' && format(' <{0}|View release>.', env.RELEASE_URL) || (needs.linear-release.result == 'failure' || needs.linear-release.outputs.outcome == 'failure') && ' Recording it in Linear failed :warning:.' || '' }} # Success also puts a ✅ on the start notice, so the result shows in the # channel without another message. Needs the app's reactions:write scope. From 73d06b073afd153874fc8e22ff730a220dfc77b5 Mon Sep 17 00:00:00 2001 From: Goran Dodig Date: Thu, 5 Nov 2026 16:38:37 +0100 Subject: [PATCH 5/6] ci(release): match code fences fully and fix re-run bases - A code fence closes only on a line of its own, with the same character and at least as many; one that never closes runs to the end. - A re-run of an older version only looks at releases before it. - JSDoc on the script's functions. --- .github/scripts/record-linear-release.cjs | 96 +++++++++++++++++------ 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/.github/scripts/record-linear-release.cjs b/.github/scripts/record-linear-release.cjs index a5f6832..9e0cf2b 100644 --- a/.github/scripts/record-linear-release.cjs +++ b/.github/scripts/record-linear-release.cjs @@ -17,30 +17,72 @@ const LINEAR_API = 'https://api.linear.app/graphql'; const MAX_BASE_CANDIDATES = 20; -// On GitHub, "#12" is a PR or issue of the same repo. Linear matches a bare "#12" against every -// repo it knows, so it can link a PR from another repo. Every reference is therefore written out -// as a full link, except in code (fenced or inline), existing links and URLs. GitHub redirects -// /pull/N to /issues/N when N is an issue. +/** + * Splits markdown into fenced code blocks and the text between them. A fence opens with three or more + * backticks or tildes, and closes with the same character, at least as many, on a line of its own. + * A fence that never closes runs to the end, as in CommonMark. + * @returns {{ code: boolean, text: string }[]} + */ +function splitFences(text) { + const parts = []; + let buffer = ''; + let fence = null; + for (const line of text.split(/(?<=\n)/)) { + if (!fence) { + const open = line.match(/^ {0,3}(`{3,}|~{3,})/); + // A backtick fence's info string can't contain a backtick. + if (open && !(open[1][0] === '`' && line.slice(open[0].length).includes('`'))) { + parts.push({ code: false, text: buffer }); + buffer = line; + fence = open[1]; + } else { + buffer += line; + } + } else { + buffer += line; + const close = line.match(/^ {0,3}(`{3,}|~{3,})[ \t]*\r?\n?$/); + if (close && close[1][0] === fence[0] && close[1].length >= fence.length) { + parts.push({ code: true, text: buffer }); + buffer = ''; + fence = null; + } + } + } + parts.push({ code: fence !== null, text: buffer }); + return parts; +} + +/** + * Writes every GitHub reference out as a full link. On GitHub, "#12" is a PR or issue of the same repo; + * Linear matches a bare "#12" against every repo it knows, so it can link a PR from another repo. + * Code (fenced or inline), existing links and URLs are left alone. GitHub redirects /pull/N to + * /issues/N when N is an issue. + */ function linkReferences(text, owner, repo) { - const untouched = /(```[\s\S]*?```|~~~[\s\S]*?~~~|`[^`\n]*`|\[[^\]\n]*\]\([^)\n]*\)|<[^>\n]*>|https?:\/\/[^\s)]+)/; - return text - .split(untouched) - .map((part, i) => - i % 2 - ? part - : part - .replace( - /(^|[^\w/#&])([\w.-]+)\/([\w.-]+)#(\d+)\b/g, - (_, pre, o, r, n) => `${pre}[${o}/${r}#${n}](https://github.com/${o}/${r}/pull/${n})`, - ) - .replace( - /(^|[^\w/#&[]|\bPR ?)#(\d+)\b/g, - (_, pre, n) => `${pre}[#${n}](https://github.com/${owner}/${repo}/pull/${n})`, - ), - ) + const untouched = /(`[^`\n]*`|\[[^\]\n]*\]\([^)\n]*\)|<[^>\n]*>|https?:\/\/[^\s)]+)/; + const link = (prose) => + prose + .split(untouched) + .map((part, i) => + i % 2 + ? part + : part + .replace( + /(^|[^\w/#&])([\w.-]+)\/([\w.-]+)#(\d+)\b/g, + (_, pre, o, r, n) => `${pre}[${o}/${r}#${n}](https://github.com/${o}/${r}/pull/${n})`, + ) + .replace( + /(^|[^\w/#&[]|\bPR ?)#(\d+)\b/g, + (_, pre, n) => `${pre}[#${n}](https://github.com/${owner}/${repo}/pull/${n})`, + ), + ) + .join(''); + return splitFences(text) + .map((part) => (part.code ? part.text : link(part.text))) .join(''); } +/** The release notes of a deploy: one line per PR, with Dependabot's PRs folded into one. */ function prNotes(prs, owner, repo) { const link = (n) => `[#${n}](https://github.com/${owner}/${repo}/pull/${n})`; const dependabot = prs.filter((pr) => pr.user.login === 'dependabot[bot]'); @@ -54,7 +96,7 @@ function prNotes(prs, owner, repo) { return lines.join('\n'); } -// The PRs merged into `branch` since the newest earlier release in this pipeline. +/** The PRs merged into `branch` since the newest earlier release in this pipeline. */ async function pullRequestsSince({ github, owner, repo, branch, head, version, releases }) { const compare = async (base, target, page = 1, perPage = 1) => (await github.rest.repos.compareCommitsWithBasehead({ owner, repo, basehead: `${base}...${target}`, page, per_page: perPage })) @@ -66,11 +108,14 @@ async function pullRequestsSince({ github, owner, repo, branch, head, version, r } // Sorted by completion time, since an older release whose notes were edited can be listed first. - // A release that is not on the branch is a branch deploy and is never a base. This release itself - // (a re-run) is skipped, but another version at the same commit is a base, and leaves no new PRs. + // A release that is not on the branch is a branch deploy and is never a base. A re-run of a + // recorded version only looks at releases before it. Another version at the same commit is a + // base, and leaves no new PRs. + const time = (r) => Date.parse(r.completedAt || r.createdAt); + const own = releases.find((r) => r.version === version); const candidates = releases - .filter((r) => r.commitSha && r.version !== version) - .sort((a, b) => Date.parse(b.completedAt || b.createdAt) - Date.parse(a.completedAt || a.createdAt)) + .filter((r) => r.commitSha && r.version !== version && (!own || time(r) < time(own))) + .sort((a, b) => time(b) - time(a)) .slice(0, MAX_BASE_CANDIDATES); let base; for (const release of candidates) { @@ -101,6 +146,7 @@ async function pullRequestsSince({ github, owner, repo, branch, head, version, r return { prs: [...prs.values()], base }; } +/** Records the release in Linear, or logs what it would record. */ module.exports = async ({ github, context, core }) => { const key = process.env.LINEAR_ACCESS_KEY; const live = process.env.LINEAR_RELEASE_MODE === 'live'; From b13a14ed446a29423d0c6f1c49feffcc0936f6a7 Mon Sep 17 00:00:00 2001 From: Goran Dodig Date: Thu, 5 Nov 2026 17:12:11 +0100 Subject: [PATCH 6/6] ci(release): leave quoted fences and multi-backtick code unlinked - Fences inside block quotes count, and end with their quote. - Inline code with longer backtick runs (``like this``) is left alone. --- .github/scripts/record-linear-release.cjs | 76 +++++++++++++---------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/.github/scripts/record-linear-release.cjs b/.github/scripts/record-linear-release.cjs index 9e0cf2b..39ad3d5 100644 --- a/.github/scripts/record-linear-release.cjs +++ b/.github/scripts/record-linear-release.cjs @@ -20,7 +20,8 @@ const MAX_BASE_CANDIDATES = 20; /** * Splits markdown into fenced code blocks and the text between them. A fence opens with three or more * backticks or tildes, and closes with the same character, at least as many, on a line of its own. - * A fence that never closes runs to the end, as in CommonMark. + * Fences inside block quotes count, and end with their quote. A fence that never closes runs to the + * end, as in CommonMark. * @returns {{ code: boolean, text: string }[]} */ function splitFences(text) { @@ -28,24 +29,32 @@ function splitFences(text) { let buffer = ''; let fence = null; for (const line of text.split(/(?<=\n)/)) { - if (!fence) { - const open = line.match(/^ {0,3}(`{3,}|~{3,})/); - // A backtick fence's info string can't contain a backtick. - if (open && !(open[1][0] === '`' && line.slice(open[0].length).includes('`'))) { - parts.push({ code: false, text: buffer }); - buffer = line; - fence = open[1]; - } else { - buffer += line; - } - } else { + const quote = line.match(/^(?: {0,3}> ?)*/)[0]; + const depth = (quote.match(/>/g) || []).length; + const rest = line.slice(quote.length); + if (fence && depth < fence.depth) { + parts.push({ code: true, text: buffer }); + buffer = ''; + fence = null; + } + if (fence) { buffer += line; - const close = line.match(/^ {0,3}(`{3,}|~{3,})[ \t]*\r?\n?$/); - if (close && close[1][0] === fence[0] && close[1].length >= fence.length) { + const close = rest.match(/^ {0,3}(`{3,}|~{3,})[ \t]*\r?\n?$/); + if (close && close[1][0] === fence.marker[0] && close[1].length >= fence.marker.length) { parts.push({ code: true, text: buffer }); buffer = ''; fence = null; } + continue; + } + const open = rest.match(/^ {0,3}(`{3,}|~{3,})/); + // A backtick fence's info string can't contain a backtick. + if (open && !(open[1][0] === '`' && rest.slice(open[0].length).includes('`'))) { + parts.push({ code: false, text: buffer }); + buffer = line; + fence = { marker: open[1], depth }; + } else { + buffer += line; } } parts.push({ code: fence !== null, text: buffer }); @@ -55,30 +64,29 @@ function splitFences(text) { /** * Writes every GitHub reference out as a full link. On GitHub, "#12" is a PR or issue of the same repo; * Linear matches a bare "#12" against every repo it knows, so it can link a PR from another repo. - * Code (fenced or inline), existing links and URLs are left alone. GitHub redirects /pull/N to - * /issues/N when N is an issue. + * Code (fenced, or inline with any number of backticks), existing links and URLs are left alone. + * GitHub redirects /pull/N to /issues/N when N is an issue. */ function linkReferences(text, owner, repo) { - const untouched = /(`[^`\n]*`|\[[^\]\n]*\]\([^)\n]*\)|<[^>\n]*>|https?:\/\/[^\s)]+)/; - const link = (prose) => - prose - .split(untouched) - .map((part, i) => - i % 2 - ? part - : part - .replace( - /(^|[^\w/#&])([\w.-]+)\/([\w.-]+)#(\d+)\b/g, - (_, pre, o, r, n) => `${pre}[${o}/${r}#${n}](https://github.com/${o}/${r}/pull/${n})`, - ) - .replace( - /(^|[^\w/#&[]|\bPR ?)#(\d+)\b/g, - (_, pre, n) => `${pre}[#${n}](https://github.com/${owner}/${repo}/pull/${n})`, - ), + const untouched = /(?\n]*>|https?:\/\/[^\s)]+/g; + const linkPlain = (plain) => + plain + .replace( + /(^|[^\w/#&])([\w.-]+)\/([\w.-]+)#(\d+)\b/g, + (_, pre, o, r, n) => `${pre}[${o}/${r}#${n}](https://github.com/${o}/${r}/pull/${n})`, ) - .join(''); + .replace(/(^|[^\w/#&[]|\bPR ?)#(\d+)\b/g, (_, pre, n) => `${pre}[#${n}](https://github.com/${owner}/${repo}/pull/${n})`); + const linkProse = (prose) => { + let out = ''; + let last = 0; + for (const match of prose.matchAll(untouched)) { + out += linkPlain(prose.slice(last, match.index)) + match[0]; + last = match.index + match[0].length; + } + return out + linkPlain(prose.slice(last)); + }; return splitFences(text) - .map((part) => (part.code ? part.text : link(part.text))) + .map((part) => (part.code ? part.text : linkProse(part.text))) .join(''); }