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
This commit is contained in:
Vlad Frangu
2022-06-02 02:33:55 +03:00
committed by GitHub
parent 93eb3d3af6
commit 895083b8ab
2 changed files with 29 additions and 3 deletions

View File

@@ -54,6 +54,8 @@ jobs:
git add --all .
git commit -m "chore(release): $(jq --raw-output '.version' package.json) 🎉" -m "Build ran for ${GITHUB_SHA}"
git push -u origin "chore/release/$(jq --raw-output '.version' package.json)"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Pull Request
run: node ./scripts/actions/create-pr.mjs

View File

@@ -5,6 +5,10 @@ import { Octokit } from '@octokit/action';
const packageJson = JSON.parse(await readFile(new URL('../../package.json', import.meta.url), { encoding: 'utf8' }));
const octokit = new Octokit();
const [OWNER, REPOSITORY] = process.env.GITHUB_REPOSITORY.split('/');
const prPattern = new RegExp(
`\\(\\[#(?<prNumber>\\d+)\\]\\(https:\\/\\/github\\.com\\/${OWNER}\\/${REPOSITORY}\\/(?:issues|pulls)\\/(?:\\d+)\\)\\)`,
'gi',
);
console.log('👀 Getting the previous release version');
const previousReleases = await octokit.repos.listReleases({
@@ -19,6 +23,8 @@ console.log('👀 Previous release version:', previousRelease?.tag_name);
const releaseChangelog = [];
const changelogContent = await readFile(new URL('../../CHANGELOG.md', import.meta.url), { encoding: 'utf8' });
let contentToParseAndAdd = '';
if (previousRelease) {
// find difference between previous release and current version
const maybeMinorIndex = changelogContent.indexOf(`## [${previousRelease.tag_name}](https://github.com`);
@@ -26,14 +32,32 @@ if (previousRelease) {
if (maybeMinorIndex === -1) {
// find major version
const maybeMajorIndex = changelogContent.indexOf(`# [${previousRelease.tag_name}](https://github.com`);
releaseChangelog.push(changelogContent.slice(0, maybeMajorIndex));
contentToParseAndAdd = changelogContent.slice(0, maybeMajorIndex);
} else {
releaseChangelog.push(changelogContent.slice(0, maybeMinorIndex));
contentToParseAndAdd = changelogContent.slice(0, maybeMinorIndex);
}
} else {
releaseChangelog.push(changelogContent);
contentToParseAndAdd = changelogContent;
}
for (const [input, prNumber] of contentToParseAndAdd.matchAll(prPattern)) {
try {
const prData = await octokit.pulls.get({
owner: OWNER,
repo: REPOSITORY,
pull_number: Number(prNumber),
});
const replaced = input.replace('))', `) by @${prData.data.user?.login ?? 'ghost'})`);
contentToParseAndAdd = contentToParseAndAdd.replace(input, replaced);
} catch (err) {
console.error(`Failed to fetch PR #${prNumber}`, err);
}
}
releaseChangelog.push(contentToParseAndAdd);
const { data } = await octokit.repos.generateReleaseNotes({
owner: OWNER,
repo: REPOSITORY,