release: promote the GitHub release step #63

Merged
chrnx merged 2 commits from develop into main 2026-08-19 19:33:59 +00:00
3 changed files with 113 additions and 1 deletions

No files matched your search

+24 -1
View File
@@ -16,7 +16,7 @@ jobs:
PKG: '@green-tea/core'
permissions:
id-token: write # OIDC for Trusted Publishing (also enables provenance)
contents: read
contents: write # create the GitHub release for this tag
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
@@ -94,3 +94,26 @@ jobs:
else
deno publish
fi
# The release page is where people who do not read a changelog find out what changed, and a
# repository with tags and no releases reads as abandoned. Notes come from CHANGELOG.md rather
# than from the commit range: the section was written for humans and already exists by the time
# a tag is cut, and generated notes would restate merge subjects nobody chose.
#
# Idempotent like the publishes above, so a re-run after a later failure does not stop here.
# `--verify-tag` refuses to invent a tag: the release attaches to the one that triggered this
# run and can never move it. A prerelease is marked as such, which is what keeps GitHub's
# "Latest" badge off a beta.
- name: Create the GitHub release
env:
GH_TOKEN: ${{ github.token }}
run: |
VERSION=$(node -p "require('./package.json').version")
TAG=${GITHUB_REF_NAME}
if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
echo "release $TAG already exists"; exit 0
fi
node scripts/changelog-section.mjs "$VERSION" "$TAG" > /tmp/notes.md
case "$VERSION" in *-*) PRE=--prerelease ;; *) PRE= ;; esac
gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --title "$TAG" \
--notes-file /tmp/notes.md --verify-tag $PRE
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env node
// Prints one version's section of CHANGELOG.md, for a GitHub release body.
//
// Reads the current file rather than the tag's copy on purpose: the changelog is the record, so a
// correction to an old entry should reach the release that describes it.
//
// Usage: node scripts/changelog-section.mjs <version> [tag]
// The optional tag makes relative links absolute — a release body resolves them against the release
// page, not the repository, and pinning to the tag keeps the link on the file as that version had it.
import { readFileSync } from 'node:fs';
const REPO = 'Expressive-Tea/green-tea';
const [version, tag] = process.argv.slice(2);
if (!version) {
console.error('usage: changelog-section.mjs <version> [tag]');
process.exit(2);
}
const lines = readFileSync(new URL('../CHANGELOG.md', import.meta.url), 'utf8').split('\n');
const isHeading = (l) => l.startsWith('## [');
// The `[label]: url` block at the bottom of the file belongs to no section. Without this the last
// section swallows it, since nothing else comes after.
const isLinkRef = (l) => /^\[[^\]]+\]:\s+\S/.test(l);
const start = lines.findIndex((l) => l.startsWith(`## [${version}]`));
if (start === -1) {
console.error(`no section for ${version}. Headings in CHANGELOG.md:\n${lines.filter(isHeading).join('\n')}`);
process.exit(1);
}
const rest = lines.slice(start + 1);
const stop = rest.findIndex((l) => isHeading(l) || isLinkRef(l));
const body = (stop === -1 ? rest : rest.slice(0, stop)).join('\n').trim();
const absolute = tag
? body.replace(/\]\(\.\/([^)\s]+)\)/g, `](https://github.com/${REPO}/blob/${tag}/$1)`)
: body;
console.log(absolute);
+51
View File
@@ -0,0 +1,51 @@
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';
// `scripts/changelog-section.mjs` writes the body of every GitHub release. It is the only place where
// a mistake is invisible until it is already published, so its boundaries are pinned here rather than
// checked by eye at release time.
const SCRIPT = resolve(process.cwd(), 'scripts/changelog-section.mjs');
const section = (version: string, tag?: string): string =>
execFileSync('node', [SCRIPT, version, ...(tag ? [tag] : [])], { encoding: 'utf8' });
const changelog = readFileSync(resolve(process.cwd(), 'CHANGELOG.md'), 'utf8');
const released = [...changelog.matchAll(/^## \[(\d[^\]]*)\]/gm)].map(([, v]) => v);
describe('changelog section extraction', () => {
it('finds every released version', () => {
expect(released.length).toBeGreaterThan(0);
for (const version of released) {
expect(section(version).trim(), version).not.toBe('');
}
});
it('stops at the next version, so a section never carries the one below it', () => {
for (const version of released) {
expect(section(version), version).not.toMatch(/^## \[/m);
}
});
// The oldest section is the one at risk: nothing follows it but the `[label]: url` block that
// defines the compare links, which belongs to no version and once ended up inside it.
it('stops at the link-reference block', () => {
for (const version of released) {
expect(section(version), version).not.toMatch(/^\[[^\]]+\]:\s+\S/m);
}
});
// A release body resolves relative links against the release page, not the repository, so every
// link has to be absolute by the time it is published.
it('rewrites relative links against the tag', () => {
for (const version of released) {
const links = [...section(version, `v${version}`).matchAll(/\]\(([^)\s]+)\)/g)].map(([, url]) => url);
expect(links.filter((url) => !url.startsWith('http')), version).toEqual([]);
}
});
it('fails rather than publishing an empty body for a version it cannot find', () => {
expect(() => section('0.0.0-nonexistent')).toThrow();
});
});