Skip to content

Commit 042cd63

Browse files
authored
Add option to disable commit hashes in changelogs (#1052)
1 parent d2d0344 commit 042cd63

6 files changed

Lines changed: 65 additions & 13 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Add option to disable commit hashes in changelogs (`options.changelog.includeCommitHashes`, default true)",
4+
"packageName": "beachball",
5+
"email": "elcraig@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

src/__tests__/changelog/getPackageChangelogs.test.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import { describe, expect, it, jest } from '@jest/globals';
1+
import { afterEach, describe, expect, it, jest } from '@jest/globals';
22
import { getPackageChangelogs } from '../../changelog/getPackageChangelogs';
33
import type { BumpInfo } from '../../types/BumpInfo';
44
import type { ChangeFileInfo, ChangeSet } from '../../types/ChangeInfo';
55
import type { PackageInfos } from '../../types/PackageInfo';
66
import { makePackageInfos } from '../../__fixtures__/packageInfos';
7+
import { getFileAddedHash } from 'workspace-tools';
78

89
const commit = 'deadbeef';
910
const author = 'something@something.com';
1011

1112
// Mock the methods used from workspace-tools so we don't access the filesystem
1213
jest.mock('workspace-tools', () => ({
1314
findProjectRoot: () => '.',
14-
getFileAddedHash: () => commit,
15+
getFileAddedHash: jest.fn(() => commit),
1516
}));
1617

17-
function makeChangeInfo(pkg: string, overrides?: Partial<ChangeFileInfo>): ChangeSet[number] {
18+
function makeChangeInfo(pkg: string, overrides?: Partial<ChangeFileInfo>, filename?: string): ChangeSet[number] {
1819
return {
19-
changeFile: `${pkg}.json`,
20+
changeFile: filename || `${pkg}.json`,
2021
change: {
2122
comment: `comment for ${pkg}`,
2223
dependentChangeType: 'patch',
@@ -28,12 +29,18 @@ function makeChangeInfo(pkg: string, overrides?: Partial<ChangeFileInfo>): Chang
2829
};
2930
}
3031

31-
const options: Parameters<typeof getPackageChangelogs>[1] = {
32+
const options = {
3233
path: '.',
3334
changeDir: 'change',
3435
};
3536

3637
describe('getPackageChangelogs', () => {
38+
const getFileAddedHashMock = getFileAddedHash as jest.MockedFunction<typeof getFileAddedHash>;
39+
40+
afterEach(() => {
41+
getFileAddedHashMock.mockClear();
42+
});
43+
3744
it('generates correct changelog entries for a single package', () => {
3845
const changeFileChangeInfos: ChangeSet = [
3946
makeChangeInfo('foo'),
@@ -60,7 +67,9 @@ describe('getPackageChangelogs', () => {
6067
tag: 'foo_v1.0.0',
6168
version: '1.0.0',
6269
});
63-
expect(changelogs.foo.comments.patch).toHaveLength(1);
70+
71+
// the fake change files use the same default filename
72+
expect(getFileAddedHashMock).toHaveBeenCalledTimes(1);
6473
});
6574

6675
it('generates correct changelog entries for multiple packages', () => {
@@ -97,6 +106,8 @@ describe('getPackageChangelogs', () => {
97106
tag: 'bar_v2.0.0',
98107
version: '2.0.0',
99108
});
109+
110+
expect(getFileAddedHashMock).toHaveBeenCalledTimes(2);
100111
});
101112

102113
it('preserves custom properties from change files', () => {
@@ -155,7 +166,7 @@ describe('getPackageChangelogs', () => {
155166
tag: 'bar_v2.0.0',
156167
version: '2.0.0',
157168
});
158-
expect(Object.keys(changelogs.bar.comments.patch!)).toHaveLength(1);
169+
expect(getFileAddedHashMock).toHaveBeenCalledTimes(1);
159170
});
160171

161172
it('records multiple comment entries when a package has a change file AND was part of a dependent bump', () => {
@@ -220,4 +231,22 @@ describe('getPackageChangelogs', () => {
220231
expect(changelogs.bar).toBeTruthy();
221232
expect(changelogs['private-pkg']).toBeUndefined();
222233
});
234+
235+
it('omits commit hashes if requested', () => {
236+
const changeFileChangeInfos: ChangeSet = [makeChangeInfo('foo')];
237+
const packageInfos = makePackageInfos({ foo: { version: '1.0.0' } });
238+
239+
const changelogs = getPackageChangelogs(
240+
{
241+
changeFileChangeInfos,
242+
calculatedChangeTypes: { foo: 'patch' },
243+
packageInfos,
244+
},
245+
{ ...options, changelog: { includeCommitHashes: false } }
246+
);
247+
248+
expect(changelogs.foo.comments.patch).toHaveLength(1);
249+
expect(changelogs.foo.comments.patch![0].commit).toBeUndefined();
250+
expect(getFileAddedHashMock).not.toHaveBeenCalled();
251+
});
223252
});

src/changelog/getPackageChangelogs.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ const commitNotAvailable = 'not available';
2020
export function getPackageChangelogs(
2121
bumpInfo: Pick<BumpInfo, 'changeFileChangeInfos' | 'calculatedChangeTypes' | 'packageInfos'> &
2222
Partial<Pick<BumpInfo, 'dependentChangedBy'>>,
23-
options: Pick<BeachballOptions, 'path' | 'changeDir'>
23+
options: Pick<BeachballOptions, 'path' | 'changeDir' | 'changelog'>
2424
): Record<string, PackageChangelog> {
2525
const { changeFileChangeInfos, calculatedChangeTypes, dependentChangedBy = {}, packageInfos } = bumpInfo;
26+
const includeCommitHashes = options.changelog?.includeCommitHashes !== false;
2627

2728
const changelogs: Record<string, PackageChangelog> = {};
2829
const changeFileCommits: { [changeFile: string]: string } = {};
@@ -32,8 +33,10 @@ export function getPackageChangelogs(
3233
const { packageName, type: changeType, dependentChangeType, email, ...rest } = change;
3334
changelogs[packageName] ??= createPackageChangelog(packageInfos[packageName]);
3435

35-
changeFileCommits[changeFile] ??=
36-
getFileAddedHash(path.join(changePath, changeFile), options.path) || commitNotAvailable;
36+
if (includeCommitHashes) {
37+
changeFileCommits[changeFile] ??=
38+
getFileAddedHash(path.join(changePath, changeFile), options.path) || commitNotAvailable;
39+
}
3740

3841
changelogs[packageName].comments ??= {};
3942
changelogs[packageName].comments[changeType] ??= [];
@@ -71,7 +74,7 @@ export function getPackageChangelogs(
7174
// split publishing into two commits (one for bumps and one for changelog updates),
7275
// there's no way to know the hash yet. It's better to record nothing than incorrect info.
7376
// https://github.com/microsoft/beachball/issues/901
74-
commit: commitNotAvailable,
77+
...(includeCommitHashes && { commit: commitNotAvailable }),
7578
});
7679
}
7780
}

src/types/ChangeInfo.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ export interface ChangeFileInfo {
2222
* Info saved in each change file, plus the commit hash.
2323
*/
2424
export interface ChangeInfo extends ChangeFileInfo {
25-
commit: string;
25+
/**
26+
* Commit hash where the change was made, if available.
27+
* Will be undefined if `options.change.includeCommitHashes` is false.
28+
*/
29+
commit?: string;
2630
}
2731

2832
/**

src/types/ChangeLog.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ export interface ChangelogEntry {
2525
*
2626
* Could also be `"not available"` for other commits if there was an issue determing the hash
2727
* at changelog generation time.
28+
*
29+
* Will be undefined if `options.change.includeCommitHashes` is false.
2830
*/
29-
commit: string;
31+
commit?: string;
3032
/** Package name the change was in */
3133
package: string;
3234
/** Extra info added to the change file via custom prompts */

src/types/ChangelogOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ export interface ChangelogOptions {
5353
* (If the md file is truncated, it will include a comment about referring to git for older entries.)
5454
*/
5555
maxVersions?: number;
56+
57+
/**
58+
* If true (the default), each `CHANGELOG.json` entry and `ChangeInfo` object will include the
59+
* commit hash where the change was made. This can be disabled for performance reasons.
60+
* @default true
61+
*/
62+
includeCommitHashes?: boolean;
5663
}
5764

5865
/**

0 commit comments

Comments
 (0)