Skip to content

Commit 844aacd

Browse files
author
Benjamin E. Coe
authored
fix: filter changelog.json commits based on changelog sections (#1827)
1 parent 36fc501 commit 844aacd

4 files changed

Lines changed: 159 additions & 16 deletions

File tree

src/strategies/java-yoshi-mono-repo.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ import {
3030
import {ConventionalCommit} from '../commit';
3131
import {Java, JavaBuildUpdatesOption} from './java';
3232
import {JavaUpdate} from '../updaters/java/java-update';
33-
34-
const BREAKING_CHANGE_NOTE = 'BREAKING CHANGE';
33+
import {filterCommits} from '../util/filter-commits';
3534

3635
export class JavaYoshiMonoRepo extends Java {
3736
private versionsContent?: GitHubFileContents;
@@ -200,12 +199,7 @@ export class JavaYoshiMonoRepo extends Java {
200199
includeEmpty: false,
201200
});
202201
const splitCommits = cs.split(
203-
options.commits.filter(commit => {
204-
const isBreaking = commit.notes.find(note => {
205-
return note.title === BREAKING_CHANGE_NOTE;
206-
});
207-
return commit.type !== 'chore' || isBreaking;
208-
})
202+
filterCommits(options.commits, this.changelogSections)
209203
);
210204
for (const path of Object.keys(splitCommits)) {
211205
const repoMetadata = await this.getRepoMetadata(path);

src/strategies/node.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import {Changelog} from '../updaters/changelog';
2121
import {PackageJson} from '../updaters/node/package-json';
2222
import {GitHubFileContents} from '@google-automations/git-file-utils';
2323
import {FileNotFoundError, MissingRequiredFileError} from '../errors';
24-
25-
const BREAKING_CHANGE_NOTE = 'BREAKING CHANGE';
24+
import {filterCommits} from '../util/filter-commits';
2625

2726
export class Node extends BaseStrategy {
2827
private pkgJsonContents?: GitHubFileContents;
@@ -73,12 +72,7 @@ export class Node extends BaseStrategy {
7372

7473
// If a machine readable changelog.json exists update it:
7574
if (options.commits && packageName) {
76-
const commits = options.commits.filter(commit => {
77-
const isBreaking = commit.notes.find(note => {
78-
return note.title === BREAKING_CHANGE_NOTE;
79-
});
80-
return commit.type !== 'chore' || isBreaking;
81-
});
75+
const commits = filterCommits(options.commits, this.changelogSections);
8276
updates.push({
8377
path: 'changelog.json',
8478
createIfMissing: false,

src/util/filter-commits.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {ChangelogSection} from '../changelog-notes';
16+
import {ConventionalCommit} from '../commit';
17+
18+
const BREAKING_CHANGE_NOTE = 'BREAKING CHANGE';
19+
20+
const DEFAULT_CHANGELOG_SECTIONS = [
21+
{type: 'feat', section: 'Features'},
22+
{type: 'fix', section: 'Bug Fixes'},
23+
{type: 'perf', section: 'Performance Improvements'},
24+
{type: 'revert', section: 'Reverts'},
25+
{type: 'chore', section: 'Miscellaneous Chores', hidden: true},
26+
{type: 'docs', section: 'Documentation', hidden: true},
27+
{type: 'style', section: 'Styles', hidden: true},
28+
{type: 'refactor', section: 'Code Refactoring', hidden: true},
29+
{type: 'test', section: 'Tests', hidden: true},
30+
{type: 'build', section: 'Build System', hidden: true},
31+
{type: 'ci', section: 'Continuous Integration', hidden: true},
32+
];
33+
34+
/**
35+
* Given a set of conventional commits and the configured
36+
* changelog sections provided by the user, return the set
37+
* of commits that should be displayed:
38+
*
39+
* @param commits
40+
* @param changelogSections
41+
* @returns ConventionalCommit[]
42+
*/
43+
export function filterCommits(
44+
commits: ConventionalCommit[],
45+
changelogSections?: ChangelogSection[]
46+
): ConventionalCommit[] {
47+
changelogSections = changelogSections ?? DEFAULT_CHANGELOG_SECTIONS;
48+
const hiddenSections: Array<string> = [];
49+
const visibleSections: Array<string> = [];
50+
for (const section of changelogSections) {
51+
if (!section.hidden) visibleSections.push(section.type);
52+
else hiddenSections.push(section.type);
53+
}
54+
return commits.filter(commit => {
55+
const isBreaking = commit.notes.find(note => {
56+
return note.title === BREAKING_CHANGE_NOTE;
57+
});
58+
return (
59+
visibleSections.includes(commit.type) ||
60+
(isBreaking && hiddenSections.includes(commit.type))
61+
);
62+
});
63+
}

test/util/filter-commits.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {filterCommits} from '../../src/util/filter-commits';
16+
import {describe, it} from 'mocha';
17+
import {expect} from 'chai';
18+
19+
const BREAKING_CHANGE_NOTE = 'BREAKING CHANGE';
20+
21+
describe('filterCommits', () => {
22+
it('removes unknown commit types', () => {
23+
const commits = filterCommits([
24+
{
25+
type: 'User-Name',
26+
notes: [],
27+
references: [],
28+
bareMessage: '',
29+
message: '',
30+
scope: null,
31+
breaking: false,
32+
sha: 'deadbeef',
33+
},
34+
]);
35+
expect(commits.length).to.equal(0);
36+
});
37+
it('removes unknown commit type for breaking change', () => {
38+
const commits = filterCommits([
39+
{
40+
type: 'User-Name',
41+
notes: [
42+
{
43+
title: BREAKING_CHANGE_NOTE,
44+
text: 'foo breaking change',
45+
},
46+
],
47+
references: [],
48+
bareMessage: '',
49+
message: '',
50+
scope: null,
51+
breaking: false,
52+
sha: 'deadbeef',
53+
},
54+
]);
55+
expect(commits.length).to.equal(0);
56+
});
57+
it('removes hidden commit types for non-breaking changes', () => {
58+
const commits = filterCommits([
59+
{
60+
type: 'chore',
61+
notes: [],
62+
references: [],
63+
bareMessage: '',
64+
message: '',
65+
scope: null,
66+
breaking: false,
67+
sha: 'deadbeef',
68+
},
69+
]);
70+
expect(commits.length).to.equal(0);
71+
});
72+
it('includes hidden commit types for non-breaking changes', () => {
73+
const commits = filterCommits([
74+
{
75+
type: 'chore',
76+
notes: [
77+
{
78+
title: BREAKING_CHANGE_NOTE,
79+
text: 'foo breaking change',
80+
},
81+
],
82+
references: [],
83+
bareMessage: '',
84+
message: '',
85+
scope: null,
86+
breaking: false,
87+
sha: 'deadbeef',
88+
},
89+
]);
90+
expect(commits.length).to.equal(1);
91+
});
92+
});

0 commit comments

Comments
 (0)