-
Notifications
You must be signed in to change notification settings - Fork 0
chore(release): create GitHub Releases from package tags #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
patoperpetua
wants to merge
4
commits into
main
Choose a base branch
from
chore/32-github-releases-from-tags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c644cda
chore(release): create GitHub Releases from package tags
patoperpetua 4fca035
fix(release): require remote tag before GitHub Release create
patoperpetua 09045d8
docs(release): use --verify-tag in backfill example
patoperpetua 8027ae8
fix(release): distinguish missing releases from gh lookup failures
patoperpetua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /** | ||
| * Create GitHub Releases for package version tags. | ||
| * | ||
| * Used by `scripts/release-changed.mjs` after tags are pushed. npm publish | ||
| * remains disabled — this only creates GitHub Release entries. | ||
| */ | ||
| import { execFileSync } from 'node:child_process'; | ||
|
|
||
| /** | ||
| * @typedef {'major' | 'minor' | 'patch'} Increment | ||
| * @typedef {{ name: string, version: string, next: string, increment: Increment, tag: string }} ReleaseSpec | ||
| */ | ||
|
|
||
| /** | ||
| * @param {ReleaseSpec} release | ||
| * @returns {string} | ||
| */ | ||
| export function formatReleaseTitle(release) { | ||
| return `${release.name}@${release.next}`; | ||
| } | ||
|
|
||
| /** | ||
| * @param {ReleaseSpec} release | ||
| * @returns {string} | ||
| */ | ||
| export function formatReleaseNotes(release) { | ||
| const bumpLine = | ||
| release.version === release.next | ||
| ? `## ${release.name} \`${release.next}\`` | ||
| : `## ${release.name} \`${release.version}\` → \`${release.next}\` (${release.increment})`; | ||
|
|
||
| return [ | ||
| bumpLine, | ||
| '', | ||
| 'See [CHANGELOG.md](https://github.com/singleton-sd/post-kit/blob/main/CHANGELOG.md) for monorepo release notes.', | ||
| '', | ||
| 'npm publish is not enabled yet for this package scope.', | ||
| ].join('\n'); | ||
| } | ||
|
|
||
| /** | ||
| * True when `gh release view` failed because the Release does not exist yet. | ||
| * @param {unknown} err | ||
| */ | ||
| export function isReleaseNotFoundError(err) { | ||
| const text = [ | ||
| err instanceof Error ? err.message : String(err), | ||
| err && typeof err === 'object' && 'stderr' in err ? String(err.stderr) : '', | ||
| ] | ||
| .join('\n') | ||
| .toLowerCase(); | ||
|
|
||
| return ( | ||
| text.includes('release not found') || | ||
| text.includes('could not find') || | ||
| text.includes('not found') || | ||
| text.includes('http 404') | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param {(args: string[]) => string} runGh | ||
| * @param {string} tag | ||
| * @returns {boolean} | ||
| */ | ||
| export function githubReleaseExists(runGh, tag) { | ||
| try { | ||
| runGh(['release', 'view', tag, '--json', 'tagName']); | ||
| return true; | ||
| } catch (err) { | ||
| if (isReleaseNotFoundError(err)) { | ||
| return false; | ||
| } | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @returns {(args: string[]) => string} | ||
| */ | ||
| function defaultRunGh() { | ||
| return (args) => | ||
| execFileSync('gh', args, { | ||
| encoding: 'utf8', | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| }).trim(); | ||
| } | ||
|
|
||
| /** | ||
| * Create one GitHub Release per package tag. Skips tags that already have a Release. | ||
| * | ||
| * @param {ReleaseSpec[]} releases | ||
| * @param {{ runGh?: (args: string[]) => string, log?: (msg: string) => void }} [options] | ||
| * @returns {{ created: string[], skipped: string[] }} | ||
| */ | ||
| export function createGitHubReleases(releases, options = {}) { | ||
| const runGh = options.runGh ?? defaultRunGh(); | ||
| const log = options.log ?? console.log; | ||
|
|
||
| /** @type {string[]} */ | ||
| const created = []; | ||
| /** @type {string[]} */ | ||
| const skipped = []; | ||
|
|
||
| for (const release of releases) { | ||
| if (githubReleaseExists(runGh, release.tag)) { | ||
| log(`GitHub Release already exists for ${release.tag}; skipping.`); | ||
| skipped.push(release.tag); | ||
| continue; | ||
| } | ||
|
|
||
| runGh([ | ||
| 'release', | ||
| 'create', | ||
| release.tag, | ||
| '--verify-tag', | ||
| '--title', | ||
| formatReleaseTitle(release), | ||
| '--notes', | ||
| formatReleaseNotes(release), | ||
| ]); | ||
| log(`Created GitHub Release for ${release.tag}.`); | ||
| created.push(release.tag); | ||
| } | ||
|
|
||
| return { created, skipped }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import test from 'node:test'; | ||
| import { | ||
| createGitHubReleases, | ||
| formatReleaseNotes, | ||
| formatReleaseTitle, | ||
| githubReleaseExists, | ||
| isReleaseNotFoundError, | ||
| } from './github-releases.mjs'; | ||
|
|
||
| /** @type {import('./github-releases.mjs').ReleaseSpec} */ | ||
| const sample = { | ||
| name: '@singleton-sd/post-kit-publisher', | ||
| version: '0.1.0', | ||
| next: '0.2.0', | ||
| increment: 'minor', | ||
| tag: '@singleton-sd/post-kit-publisher@0.2.0', | ||
| }; | ||
|
|
||
| test('formatReleaseTitle uses name@version', () => { | ||
| assert.equal(formatReleaseTitle(sample), '@singleton-sd/post-kit-publisher@0.2.0'); | ||
| }); | ||
|
|
||
| test('formatReleaseNotes includes bump and npm-disabled note', () => { | ||
| const notes = formatReleaseNotes(sample); | ||
| assert.match(notes, /0\.1\.0.*→.*0\.2\.0.*minor/); | ||
| assert.match(notes, /CHANGELOG\.md/); | ||
| assert.match(notes, /npm publish is not enabled/); | ||
| }); | ||
|
|
||
| test('isReleaseNotFoundError recognizes gh not-found output', () => { | ||
| assert.equal(isReleaseNotFoundError(new Error('release not found')), true); | ||
| assert.equal(isReleaseNotFoundError(new Error('HTTP 401: Bad credentials')), false); | ||
| }); | ||
|
|
||
| test('githubReleaseExists is true when gh release view succeeds', () => { | ||
| const runGh = () => '{"tagName":"@singleton-sd/post-kit-publisher@0.2.0"}'; | ||
| assert.equal(githubReleaseExists(runGh, sample.tag), true); | ||
| }); | ||
|
|
||
| test('githubReleaseExists is false when gh release view reports not found', () => { | ||
| const runGh = () => { | ||
| throw new Error('release not found'); | ||
| }; | ||
| assert.equal(githubReleaseExists(runGh, sample.tag), false); | ||
| }); | ||
|
|
||
| test('githubReleaseExists rethrows non-not-found lookup failures', () => { | ||
| const runGh = () => { | ||
| throw new Error('HTTP 401: Bad credentials'); | ||
| }; | ||
| assert.throws(() => githubReleaseExists(runGh, sample.tag), /401/); | ||
| }); | ||
|
|
||
| test('createGitHubReleases creates missing releases and skips existing', () => { | ||
| /** @type {string[][]} */ | ||
| const calls = []; | ||
| const existing = new Set(['@singleton-sd/post-kit-types@0.2.0']); | ||
|
|
||
| const runGh = (/** @type {string[]} */ args) => { | ||
| calls.push(args); | ||
| if (args[0] === 'release' && args[1] === 'view') { | ||
| if (existing.has(args[2])) return JSON.stringify({ tagName: args[2] }); | ||
| throw new Error('HTTP 404: release not found'); | ||
| } | ||
| if (args[0] === 'release' && args[1] === 'create') { | ||
| return ''; | ||
| } | ||
| throw new Error(`unexpected gh args: ${args.join(' ')}`); | ||
| }; | ||
|
|
||
| /** @type {string[]} */ | ||
| const logs = []; | ||
| const result = createGitHubReleases( | ||
| [ | ||
| sample, | ||
| { | ||
| name: '@singleton-sd/post-kit-types', | ||
| version: '0.1.0', | ||
| next: '0.2.0', | ||
| increment: 'minor', | ||
| tag: '@singleton-sd/post-kit-types@0.2.0', | ||
| }, | ||
| ], | ||
| { runGh, log: (m) => logs.push(m) }, | ||
| ); | ||
|
|
||
| assert.deepEqual(result.created, ['@singleton-sd/post-kit-publisher@0.2.0']); | ||
| assert.deepEqual(result.skipped, ['@singleton-sd/post-kit-types@0.2.0']); | ||
|
|
||
| const createCall = calls.find((a) => a[1] === 'create'); | ||
| assert.ok(createCall); | ||
| assert.equal(createCall[2], sample.tag); | ||
| assert.equal(createCall[3], '--verify-tag'); | ||
| assert.equal(createCall[createCall.indexOf('--title') + 1], sample.tag); | ||
| assert.match(createCall[createCall.indexOf('--notes') + 1], /0\.1\.0.*→.*0\.2\.0/); | ||
| assert.match(logs.join('\n'), /Created GitHub Release/); | ||
| assert.match(logs.join('\n'), /already exists/); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.