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
9 changes: 7 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
run: pnpm pack --out cache-sentinel.tgz

# The generated notes end each line with "by @<author> in <PR URL>";
# scripts/release-notes.ts rewrites that to "(#<number>)".
# scripts/release-notes.ts rewrites that to "(#<number>)" 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
Expand All @@ -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
31 changes: 29 additions & 2 deletions scripts/release-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,46 @@
* 目的:
* 自動生成の各行は `* <タイトル> by @<作者> in https://github.com/<owner>/<repo>/pull/<番号>` で、
* 作者の @メンションは要らない。PR は `(#<番号>)` と書けば GitHub が同じ PR にリンクする。
* `## New Contributors` 節も @メンションと PR URL だけなので丸ごと除く。
*
* 使い方:
* gh api repos/<owner>/<repo>/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'

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) {
Expand Down
34 changes: 26 additions & 8 deletions tests/release-notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
})
Loading