Skip to content

Commit e4da993

Browse files
🐛 fix(changelog): fix subCommitScope default value and reduceHeadingLevel (#670)
* 🐛 fix(changelog): fix subCommitScope default value to null The commit includes changes in the packages/changelog/.changelogrc.js, packages/changelog/src/finalizeContext/index.ts, and packages/changelog/src/templates files. It fixes the default value of subCommitScope from an empty string to null when it is not defined in customConfig.scopeDisplayName. The change ensures that commits with no scope are appropriately sorted in commitGroups. * 💄 style: Update typeDisplayName.test.ts to use more appropriate emoji and wording * 💄 style: Update display names for commit types in changelog transformer This commit updates the display names for `build`, `chore`, and `ci` commit types in the `typeDisplayName` module of the changelog transformer. The emojis used to represent these types have been changed from 📦, 🔧, and 👷 to 👷, 🎫, and 🔧 respectively. This change applies to both English and Chinese display names. The update only affects style and does not impact the functionality of the code. * ✨ feat(changelog): Add support for custom type display map and scope display name This commit adds support for a custom type display map and scope display name to the changelog package. The `customTypeMap` property in the `ChangelogConfig` interface allows users to define a custom type display map, while the `scopeDisplayName` property allows users to specify a default display name for all scopes. These new features can be configured in the `customConfig.ts` file. This commit also includes some code refactoring and dependency updates. * ✅ test(change): fix test * 🔧 chore(changelog): update dependencies * ♻️ refactor(changelog): Use null instead of empty string for subCommitScope in finalizeContext test This commit refactors the code in finalizeContext index.test.ts file by using null instead of an empty string for subCommitScope. This change is done to fix a bug related to custom configuration scopeDisplayName. The commit does not add any new features nor affect performance. * ♻️ refactor(changelog): change typeDisplayName.ts to accept all CommitTypes in customTypeMap parameter * ♻️ refactor(changelog): Clean up finalizeContext test This commit refactors the finalizeContext test in the changelog package by removing unused imports, variables, and commented-out code. It also improves readability by removing unnecessary ternary operations and renaming variables for clarity. * ✅ test(changelog): fix test snapshots * ✨ feat(changelog): Add an option to show authors' avatars in summary template This commit adds an option to show authors' avatars in the summary template of the changelog package. The new functionality is achieved by introducing a new handlebar partial called "summary-avatar," which contains the code that generates avatars for each author. The mainTemplate variable is updated to include this new partial and replace the {{gitUserInfo}} placeholder with the appropriate code based on the showAuthor and showAuthorAvatar options from the customConfig object. * ♻️ refactor(changelog): refactor get user avatar func by github api
1 parent e88caf5 commit e4da993

15 files changed

+342
-89
lines changed

packages/changelog/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ interface ChangelogConfig {
7878
* @default false
7979
*/
8080
addBackToTop?: boolean;
81+
/**
82+
* Custom type display map
83+
*/
84+
customTypeMap?: { [key in CommitTypes]?: CustomTypeNameMap };
8185
}
8286
```
8387

packages/changelog/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@
3333
"test": "jest"
3434
},
3535
"dependencies": {
36+
"@ardatan/sync-fetch": "^0",
3637
"@gitmoji/commit-types": "1.1.5",
3738
"@gitmoji/parser-opts": "1.4.0",
3839
"cosmiconfig": "^7",
40+
"lodash": "^4",
3941
"pangu": "^4"
4042
},
4143
"devDependencies": {
4244
"@types/conventional-changelog-core": "^4",
45+
"@types/lodash": "^4",
4346
"better-than-before": "^1",
4447
"conventional-changelog-core": "^4",
4548
"conventional-changelog-writer": "^5",

packages/changelog/src/customConfig.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CommitTypes } from '@gitmoji/commit-types';
22
import { cosmiconfigSync } from 'cosmiconfig';
3+
import { CustomTypeNameMap } from './transformer/typeDisplayName';
34

45
export interface CustomConfig {
56
/**
@@ -46,6 +47,12 @@ export interface CustomConfig {
4647
* add back to top button
4748
*/
4849
addBackToTop?: boolean;
50+
/**
51+
* Custom type display map
52+
*/
53+
customTypeMap?: {
54+
[key in CommitTypes]?: CustomTypeNameMap;
55+
};
4956
}
5057

5158
const explorer = cosmiconfigSync('changelog');

packages/changelog/src/finalizeContext/__snapshots__/index.test.ts.snap

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`test transformCommitGroups should transform commitGroups correctly 1`] = `
3+
exports[`finalizeContext should transform commitGroups correctly 1`] = `
44
{
55
"authors": [
66
{
7-
"authorEmail": "test@test.com",
8-
"authorName": "test",
9-
"authorNameEncode": "test",
7+
"authorAvatar": "https://avatars.githubusercontent.com/u/17870709?v=4",
8+
"authorEmail": "i@canisminor.cc",
9+
"authorName": "canisminor1990",
10+
"authorUrl": "https://api.github.com/users/canisminor1990",
1011
},
1112
],
1213
"commitGroups": [
1314
{
1415
"commits": [
1516
{
16-
"authorEmail": "test@test.com",
17-
"authorName": "test",
18-
"authorNameEncode": "test",
17+
"authorAvatar": "https://avatars.githubusercontent.com/u/17870709?v=4",
18+
"authorEmail": "i@canisminor.cc",
19+
"authorName": "canisminor1990",
20+
"authorUrl": "https://api.github.com/users/canisminor1990",
1921
"first": true,
2022
"hash": "1234",
2123
"last": true,
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import type { Context } from 'conventional-changelog-writer';
2-
import { CustomConfig } from '../customConfig';
31
import { typeMap } from '../transformer/typeDisplayName';
2+
import finalizeContext from './index';
43

5-
describe('test transformCommitGroups', () => {
6-
const customConfig: CustomConfig = {
4+
describe('finalizeContext', () => {
5+
const customConfig = {
76
scopeDisplayName: {
87
'*': 'all',
98
},
9+
customTypeMap: {},
1010
};
11-
const context: Context = {
11+
const context = {
1212
commitGroups: [
1313
{
1414
title: '✨ Features',
@@ -18,9 +18,10 @@ describe('test transformCommitGroups', () => {
1818
subject: 'test commit',
1919
scope: 'test',
2020
title: '✨ Features',
21-
authorName: 'test',
22-
authorEmail: 'test@test.com',
23-
authorNameEncode: 'test',
21+
authorName: 'canisminor1990',
22+
authorEmail: 'i@canisminor.cc',
23+
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
24+
authorUrl: 'https://api.github.com/users/canisminor1990',
2425
},
2526
],
2627
},
@@ -29,55 +30,220 @@ describe('test transformCommitGroups', () => {
2930
};
3031

3132
it('should transform commitGroups correctly', () => {
32-
const subCommitScope = customConfig?.scopeDisplayName?.['*'] || '';
33-
const authors = {};
34-
context.commitGroups = context.commitGroups.map((item) => {
35-
const subtitle = Object.values(typeMap).find(
36-
(i) =>
37-
item.title.includes(i['emoji']) ||
38-
item.title.includes(i['en-US']) ||
39-
item.title.includes(i['zh-CN']),
40-
).subtitle;
41-
let group;
42-
let commits = item.commits.sort((a, b) => {
43-
if (a.scope === subCommitScope && b.scope === subCommitScope) {
44-
return 0;
45-
} else if (a.scope === subCommitScope) {
46-
return 1;
47-
} else if (b.scope === subCommitScope) {
48-
return -1;
49-
} else {
50-
return 0;
51-
}
52-
});
53-
commits = commits.map((c, index) => {
54-
if (group !== c.scope) {
55-
group = c.scope;
56-
c.first = true;
57-
} else {
58-
c.first = false;
59-
}
60-
if (!commits[index + 1] || group !== commits[index + 1].scope) {
61-
c.last = true;
62-
} else {
63-
c.last = false;
64-
}
65-
if (c.authorNameEncode && !authors[c.authorNameEncode]) {
66-
authors[c.authorNameEncode] = {
67-
authorName: c.authorName,
68-
authorEmail: c.authorEmail,
69-
authorNameEncode: c.authorNameEncode,
70-
};
71-
}
72-
return c;
73-
});
74-
return {
75-
title: item.title,
76-
subtitle,
77-
commits,
78-
};
33+
const transformedContext = finalizeContext(customConfig)(context);
34+
expect(transformedContext).toMatchSnapshot();
35+
});
36+
37+
it('should set subCommitScope correctly when it is defined in customConfig', () => {
38+
const customConfigWithSubCommitScope = {
39+
...customConfig,
40+
scopeDisplayName: {
41+
'*': 'all',
42+
test: 'test',
43+
},
44+
};
45+
const transformedContext = finalizeContext(customConfigWithSubCommitScope)(context);
46+
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
47+
});
48+
49+
it('should set subCommitScope to null when it is not defined in customConfig', () => {
50+
const transformedContext = finalizeContext(customConfig)(context);
51+
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
52+
});
53+
54+
it('should set subtitle correctly when title contains emoji', () => {
55+
const contextWithTitleEmoji = {
56+
...context,
57+
commitGroups: [
58+
{
59+
title: '✨ Features',
60+
commits: [],
61+
},
62+
],
63+
};
64+
const transformedContext = finalizeContext(customConfig)(contextWithTitleEmoji);
65+
expect(transformedContext.commitGroups[0].subtitle).toBe("What's improved");
66+
});
67+
68+
it('should set subtitle correctly when title contains en-US', () => {
69+
const contextWithTitleEnUS = {
70+
...context,
71+
commitGroups: [
72+
{
73+
title: 'Features',
74+
commits: [],
75+
},
76+
],
77+
};
78+
const transformedContext = finalizeContext(customConfig)({
79+
...contextWithTitleEnUS,
80+
commitGroups: [
81+
{
82+
...contextWithTitleEnUS.commitGroups[0],
83+
title: `Features ${typeMap['feat']['en-US']}`,
84+
},
85+
],
86+
});
87+
expect(transformedContext.commitGroups[0].subtitle).toBe("What's improved");
88+
});
89+
90+
it('should set subtitle correctly when title contains zh-CN', () => {
91+
const contextWithTitleZhCN = {
92+
...context,
93+
commitGroups: [
94+
{
95+
title: 'Features',
96+
commits: [],
97+
},
98+
],
99+
};
100+
const transformedContext = finalizeContext(customConfig)({
101+
...contextWithTitleZhCN,
102+
commitGroups: [
103+
{
104+
...contextWithTitleZhCN.commitGroups[0],
105+
title: `Features ${typeMap['feat']['zh-CN']}`,
106+
},
107+
],
79108
});
80-
context.authors = Object.values(authors);
81-
expect(context).toMatchSnapshot();
109+
expect(transformedContext.commitGroups[0].subtitle).toBe("What's improved");
110+
});
111+
112+
it('should sort commits correctly when subCommitScope is defined in customConfig', () => {
113+
const customConfigWithSubCommitScope = {
114+
...customConfig,
115+
scopeDisplayName: {
116+
'*': 'all',
117+
test: 'test',
118+
},
119+
};
120+
const contextWithMultipleCommits = {
121+
...context,
122+
commitGroups: [
123+
{
124+
title: '✨ Features',
125+
commits: [
126+
{
127+
hash: '1234',
128+
subject: 'test commit 1',
129+
scope: 'test',
130+
title: '✨ Features',
131+
authorName: 'canisminor1990',
132+
authorEmail: 'i@canisminor.cc',
133+
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
134+
authorUrl: 'https://api.github.com/users/canisminor1990',
135+
},
136+
{
137+
hash: '5678',
138+
subject: 'test commit 2',
139+
scope: 'test',
140+
title: '✨ Features',
141+
authorName: 'canisminor1990',
142+
authorEmail: 'i@canisminor.cc',
143+
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
144+
authorUrl: 'https://api.github.com/users/canisminor1990',
145+
},
146+
],
147+
},
148+
],
149+
};
150+
const transformedContext = finalizeContext(customConfigWithSubCommitScope)(
151+
contextWithMultipleCommits,
152+
);
153+
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
154+
expect(transformedContext.commitGroups[0].commits[1].first).toBe(false);
155+
});
156+
157+
it('should set first and last correctly when commits have the same scope', () => {
158+
const contextWithMultipleCommits = {
159+
...context,
160+
commitGroups: [
161+
{
162+
title: '✨ Features',
163+
commits: [
164+
{
165+
hash: '1234',
166+
subject: 'test commit 1',
167+
scope: 'test',
168+
title: '✨ Features',
169+
authorName: 'canisminor1990',
170+
authorEmail: 'i@canisminor.cc',
171+
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
172+
authorUrl: 'https://api.github.com/users/canisminor1990',
173+
},
174+
{
175+
hash: '5678',
176+
subject: 'test commit 2',
177+
scope: 'test',
178+
title: '✨ Features',
179+
authorName: 'canisminor1990',
180+
authorEmail: 'i@canisminor.cc',
181+
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
182+
authorUrl: 'https://api.github.com/users/canisminor1990',
183+
},
184+
],
185+
},
186+
],
187+
};
188+
const transformedContext = finalizeContext(customConfig)(contextWithMultipleCommits);
189+
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
190+
expect(transformedContext.commitGroups[0].commits[0].last).toBe(false);
191+
expect(transformedContext.commitGroups[0].commits[1].first).toBe(false);
192+
expect(transformedContext.commitGroups[0].commits[1].last).toBe(true);
193+
});
194+
195+
it('should set author correctly when authorAvatar is not empty', () => {
196+
const contextWithAuthorNameEncode = {
197+
...context,
198+
commitGroups: [
199+
{
200+
title: '✨ Features',
201+
commits: [
202+
{
203+
hash: '1234',
204+
subject: 'test commit',
205+
scope: 'test',
206+
title: '✨ Features',
207+
authorName: 'canisminor1990',
208+
authorEmail: 'i@canisminor.cc',
209+
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
210+
authorUrl: 'https://api.github.com/users/canisminor1990',
211+
},
212+
],
213+
},
214+
],
215+
};
216+
const transformedContext = finalizeContext(customConfig)(contextWithAuthorNameEncode);
217+
expect(transformedContext.authors).toEqual([
218+
{
219+
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
220+
authorEmail: 'i@canisminor.cc',
221+
authorName: 'canisminor1990',
222+
authorUrl: 'https://api.github.com/users/canisminor1990',
223+
},
224+
]);
225+
});
226+
227+
it('should not set author when authorAvatar is empty', () => {
228+
const contextWithoutAuthorNameEncode = {
229+
...context,
230+
commitGroups: [
231+
{
232+
title: '✨ Features',
233+
commits: [
234+
{
235+
hash: '1234',
236+
subject: 'test commit',
237+
scope: 'test',
238+
title: '✨ Features',
239+
authorName: 'canisminor1990',
240+
authorEmail: 'i@canisminor.cc',
241+
},
242+
],
243+
},
244+
],
245+
};
246+
const transformedContext = finalizeContext(customConfig)(contextWithoutAuthorNameEncode);
247+
expect(transformedContext.authors).toEqual([]);
82248
});
83249
});

0 commit comments

Comments
 (0)