Skip to content

Commit 895083b

Browse files
authored
chore(Changelog): show who created the pull request that was merged (#329)
* feat: say who submitted the PR that was merged * fix: actually make the changelog generation work * chore: cleanup * chore: safety * fix: fetch PRs in a better way, and replace usernames with diff casings correctly * fix: only fetch PRs if GITHUB_TOKEN is in the env * fix: add github token to branch creation * fix: actually patch the module on install * chore: cleanup some more of the PR * chore: make the script more agnostic * chore: cleanup package-lock * chore: use split instead for more readable code
1 parent 93eb3d3 commit 895083b

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

.github/workflows/create-pr-for-release-and-publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ jobs:
5454
git add --all .
5555
git commit -m "chore(release): $(jq --raw-output '.version' package.json) 🎉" -m "Build ran for ${GITHUB_SHA}"
5656
git push -u origin "chore/release/$(jq --raw-output '.version' package.json)"
57+
env:
58+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5759

5860
- name: Create Pull Request
5961
run: node ./scripts/actions/create-pr.mjs

scripts/actions/create-release.mjs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { Octokit } from '@octokit/action';
55
const packageJson = JSON.parse(await readFile(new URL('../../package.json', import.meta.url), { encoding: 'utf8' }));
66
const octokit = new Octokit();
77
const [OWNER, REPOSITORY] = process.env.GITHUB_REPOSITORY.split('/');
8+
const prPattern = new RegExp(
9+
`\\(\\[#(?<prNumber>\\d+)\\]\\(https:\\/\\/github\\.com\\/${OWNER}\\/${REPOSITORY}\\/(?:issues|pulls)\\/(?:\\d+)\\)\\)`,
10+
'gi',
11+
);
812

913
console.log('👀 Getting the previous release version');
1014
const previousReleases = await octokit.repos.listReleases({
@@ -19,21 +23,41 @@ console.log('👀 Previous release version:', previousRelease?.tag_name);
1923
const releaseChangelog = [];
2024
const changelogContent = await readFile(new URL('../../CHANGELOG.md', import.meta.url), { encoding: 'utf8' });
2125

26+
let contentToParseAndAdd = '';
27+
2228
if (previousRelease) {
2329
// find difference between previous release and current version
2430
const maybeMinorIndex = changelogContent.indexOf(`## [${previousRelease.tag_name}](https://github.com`);
2531

2632
if (maybeMinorIndex === -1) {
2733
// find major version
2834
const maybeMajorIndex = changelogContent.indexOf(`# [${previousRelease.tag_name}](https://github.com`);
29-
releaseChangelog.push(changelogContent.slice(0, maybeMajorIndex));
35+
contentToParseAndAdd = changelogContent.slice(0, maybeMajorIndex);
3036
} else {
31-
releaseChangelog.push(changelogContent.slice(0, maybeMinorIndex));
37+
contentToParseAndAdd = changelogContent.slice(0, maybeMinorIndex);
3238
}
3339
} else {
34-
releaseChangelog.push(changelogContent);
40+
contentToParseAndAdd = changelogContent;
3541
}
3642

43+
for (const [input, prNumber] of contentToParseAndAdd.matchAll(prPattern)) {
44+
try {
45+
const prData = await octokit.pulls.get({
46+
owner: OWNER,
47+
repo: REPOSITORY,
48+
pull_number: Number(prNumber),
49+
});
50+
51+
const replaced = input.replace('))', `) by @${prData.data.user?.login ?? 'ghost'})`);
52+
53+
contentToParseAndAdd = contentToParseAndAdd.replace(input, replaced);
54+
} catch (err) {
55+
console.error(`Failed to fetch PR #${prNumber}`, err);
56+
}
57+
}
58+
59+
releaseChangelog.push(contentToParseAndAdd);
60+
3761
const { data } = await octokit.repos.generateReleaseNotes({
3862
owner: OWNER,
3963
repo: REPOSITORY,

0 commit comments

Comments
 (0)