Skip to content

Commit fdd75ef

Browse files
author
Benjamin E. Coe
authored
feat(changelog.json): add pr suffix to issues array (#1839)
In discussion with downstream implementers, we decideed that it would make it easier to customize the CHANGELOG generated if we pre-parsed the PR # suffix that GitHub adds to squashed commits.
1 parent 7f2d31b commit fdd75ef

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

__snapshots__/changelog-json.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
exports['changelog.json adds PR # suffix to issues array 1'] = `
2+
{
3+
"repository": "foo/bar",
4+
"entries": [
5+
{
6+
"changes": [
7+
{
8+
"type": "feat",
9+
"sha": "81228f3507ad6f742242474628ff58b2",
10+
"message": "some feature",
11+
"issues": []
12+
},
13+
{
14+
"type": "fix",
15+
"sha": "4e34bedd7131c9e6b0060038b0eba8cf",
16+
"message": "Support TOML up to v1.0.0-rc.1 spec.",
17+
"issues": [
18+
"1837"
19+
]
20+
},
21+
{
22+
"type": "docs",
23+
"sha": "abbf5480ac552b33404be825a817df2a",
24+
"message": "some documentation",
25+
"issues": []
26+
}
27+
],
28+
"version": "14.0.0",
29+
"language": "JAVA",
30+
"artifactName": "foo-artifact",
31+
"id": "abc-123-efd-qwerty",
32+
"createTime": "2023-01-05T16:42:33.446Z"
33+
},
34+
{},
35+
{}
36+
],
37+
"updateTime": "2023-01-05T16:42:33.446Z"
38+
}
39+
`
40+
141
exports['changelog.json prepends latest release to existing changelog 1'] = `
242
{
343
"repository": "foo/bar",

src/updaters/changelog-json.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {randomUUID} from 'crypto';
1919

2020
const BREAKING_CHANGE_TITLE = 'BREAKING CHANGE';
2121
const COMMIT_PREFIX = /^[^:]+: ?/;
22+
const PR_SUFFIX_REGEX = / ?\(#(?<pr>[0-9]+)\)$/;
2223

2324
interface ChangelogJsonOptions extends UpdateOptions {
2425
artifactName: string;
@@ -65,13 +66,28 @@ export class ChangelogJson extends DefaultUpdater {
6566
logger.info(`adding release ${this.version} for ${this.artifactName}`);
6667
const changes = [];
6768
for (const commit of this.commits) {
69+
const issues = new Set<string>();
6870
// The commit.message field contains the type/scope prefix.
69-
const message = commit.message.replace(COMMIT_PREFIX, '');
71+
let message = commit.message.replace(COMMIT_PREFIX, '');
72+
// When squashing commits, GitHub adds a suffix refrencing
73+
// the # of the PR, e.g., chore(main): release 15.5.1 (#1838)
74+
// this logic removes this suffix and prepends it to the
75+
// issues array.
76+
const match = message.match(PR_SUFFIX_REGEX);
77+
if (match && match.groups?.pr) {
78+
message = message.replace(match[0], '');
79+
issues.add(match.groups.pr);
80+
}
81+
// Array.from(someSet) will maintain elements in insertion
82+
// order, given this we add references after the pr suffix.
83+
for (const ref of commit.references) {
84+
issues.add(ref.issue);
85+
}
7086
const change: Change = {
7187
type: commit.type,
7288
sha: commit.sha,
7389
message: message,
74-
issues: commit.references.map(ref => ref.issue),
90+
issues: Array.from(issues),
7591
};
7692
if (commit.scope) change.scope = commit.scope;
7793
for (const note of commit.notes) {

test/updaters/changelog-json.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,29 @@ describe('changelog.json', () => {
6969
.replace(ISO_DATE_REGEX, '2023-01-05T16:42:33.446Z')
7070
);
7171
});
72+
// In discussion with downstream implementers, we decideed that it would
73+
// make it easier to customize the CHANGELOG generated if we pre-parsed
74+
// the PR # suffix that GitHub adds to squashed commits.
75+
it('adds PR # suffix to issues array', async () => {
76+
const oldContent = '{"repository": "foo/bar", "entries": [{}, {}]}';
77+
const commits = [
78+
buildMockCommit('feat: some feature'),
79+
buildMockCommit('fix: Support TOML up to v1.0.0-rc.1 spec. (#1837)'),
80+
buildMockCommit('docs: some documentation'),
81+
];
82+
const conventionalCommits = parseConventionalCommits(commits);
83+
const changelogJson = new ChangelogJson({
84+
version: Version.parse('14.0.0'),
85+
artifactName: 'foo-artifact',
86+
language: 'JAVA',
87+
commits: conventionalCommits,
88+
});
89+
const newContent = changelogJson.updateContent(oldContent);
90+
snapshot(
91+
newContent
92+
.replace(/\r\n/g, '\n') // make newline consistent regardless of OS.
93+
.replace(UUID_REGEX, 'abc-123-efd-qwerty')
94+
.replace(ISO_DATE_REGEX, '2023-01-05T16:42:33.446Z')
95+
);
96+
});
7297
});

0 commit comments

Comments
 (0)