diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2492477..74f0731 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -72,7 +72,7 @@ jobs: run: pnpm pack --out cache-sentinel.tgz # The generated notes end each line with "by @ in "; - # scripts/release-notes.ts rewrites that to "(#)". + # scripts/release-notes.ts rewrites that to "(#)" and drops New Contributors. # `gh release create` fails if a Release for the tag already exists, so a # published asset is never overwritten. - name: Create the GitHub Release @@ -81,4 +81,9 @@ jobs: run: | gh api "repos/$GITHUB_REPOSITORY/releases/generate-notes" -f tag_name="$GITHUB_REF_NAME" --jq .body \ | node scripts/release-notes.ts > release-notes.md - gh release create "$GITHUB_REF_NAME" cache-sentinel.tgz --title "$GITHUB_REF_NAME" --notes-file release-notes.md --verify-tag + tag_version="${GITHUB_REF_NAME#v}" + prerelease_flag= + if echo "$tag_version" | grep -qE -- '-(rc|alpha|beta)(\.|-|$)'; then + prerelease_flag=--prerelease + fi + gh release create "$GITHUB_REF_NAME" cache-sentinel.tgz --title "$GITHUB_REF_NAME" --notes-file release-notes.md --verify-tag $prerelease_flag diff --git a/scripts/release-notes.ts b/scripts/release-notes.ts index 24cdbc5..5ceb992 100644 --- a/scripts/release-notes.ts +++ b/scripts/release-notes.ts @@ -4,11 +4,12 @@ * 目的: * 自動生成の各行は `* <タイトル> by @<作者> in https://github.com///pull/<番号>` で、 * 作者の @メンションは要らない。PR は `(#<番号>)` と書けば GitHub が同じ PR にリンクする。 + * `## New Contributors` 節も @メンションと PR URL だけなので丸ごと除く。 * * 使い方: * gh api repos///releases/generate-notes -f tag_name=<タグ> --jq .body | node scripts/release-notes.ts * - * 変換: 上の形の行だけを `* <タイトル> (#<番号>)` にし、ほかの行はそのまま出す。 + * 変換: PR 行を `* <タイトル> (#<番号>)` にし、New Contributors 節を除き、ほかの行はそのまま出す。 */ import { text } from 'node:stream/consumers' import { pathToFileURL } from 'node:url' @@ -16,7 +17,33 @@ import { pathToFileURL } from 'node:url' const PR_LINE = /^\* (.+) by @\S+ in https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/(\d+)$/gm export function tidyReleaseNotes(body: string): string { - return body.replace(PR_LINE, '* $1 (#$2)') + const rewritten = body.replace(PR_LINE, '* $1 (#$2)') + const withoutContributors = stripNewContributors(rewritten) + if (withoutContributors === rewritten) { + return withoutContributors + } + return withoutContributors.replace(/\n{3,}/g, '\n\n') +} + +function stripNewContributors(body: string): string { + const lines = body.split('\n') + const out: string[] = [] + let skip = false + for (const line of lines) { + if (/^## New Contributors\s*$/.test(line)) { + skip = true + continue + } + if (skip) { + if (/^## /.test(line) || /^\*\*Full Changelog\*\*/.test(line)) { + skip = false + out.push(line) + } + continue + } + out.push(line) + } + return out.join('\n') } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { diff --git a/tests/release-notes.test.ts b/tests/release-notes.test.ts index 4440d6c..5b4206f 100644 --- a/tests/release-notes.test.ts +++ b/tests/release-notes.test.ts @@ -24,18 +24,36 @@ test('tidyReleaseNotes は PR の行の @メンションと URL を (#番号) ) }) -test('tidyReleaseNotes は bot の作者も除き、PR の形でない行は変えない', () => { +test('tidyReleaseNotes は New Contributors 節を除き、bot の作者行も (#番号) にする', () => { const body = [ - '* chore(deps): bump esbuild by @dependabot[bot] in https://github.com/AliceSaikawa/cache-sentinel/pull/60', - '* 手で書いた行 by @AliceSaikawa', - 'Thanks @AliceSaikawa in https://github.com/AliceSaikawa/cache-sentinel/pull/61', + "## What's Changed", + '* feat: example by @dependabot[bot] in https://github.com/AliceSaikawa/cache-sentinel/pull/60', + '', + '## New Contributors', + '* @dsh-superagent-hope made their first contribution in https://github.com/AliceSaikawa/cache-sentinel/pull/51', + '', + '**Full Changelog**: https://github.com/AliceSaikawa/cache-sentinel/compare/v0.6.0...v0.7.0-rc.1', ].join('\n') + const out = tidyReleaseNotes(body) assert.equal( - tidyReleaseNotes(body), + out, [ - '* chore(deps): bump esbuild (#60)', - '* 手で書いた行 by @AliceSaikawa', - 'Thanks @AliceSaikawa in https://github.com/AliceSaikawa/cache-sentinel/pull/61', + "## What's Changed", + '* feat: example (#60)', + '', + '**Full Changelog**: https://github.com/AliceSaikawa/cache-sentinel/compare/v0.6.0...v0.7.0-rc.1', ].join('\n'), ) + assert.ok(!out.includes('@')) + assert.ok(!out.includes('/pull/')) +}) + +test('tidyReleaseNotes は PR の形でない行は変えない', () => { + const body = ['* 手で書いた行 by @AliceSaikawa', 'Thanks @AliceSaikawa in https://github.com/AliceSaikawa/cache-sentinel/pull/61'].join( + '\n', + ) + assert.equal( + tidyReleaseNotes(body), + ['* 手で書いた行 by @AliceSaikawa', 'Thanks @AliceSaikawa in https://github.com/AliceSaikawa/cache-sentinel/pull/61'].join('\n'), + ) })