Skip to content

Commit

Permalink
🐛 fix(changelog): fix subCommitScope default value and reduceHeadingL…
Browse files Browse the repository at this point in the history
…evel (#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
  • Loading branch information
canisminor1990 committed Jun 12, 2023
1 parent e88caf5 commit e4da993
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 89 deletions.
4 changes: 4 additions & 0 deletions packages/changelog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ interface ChangelogConfig {
* @default false
*/
addBackToTop?: boolean;
/**
* Custom type display map
*/
customTypeMap?: { [key in CommitTypes]?: CustomTypeNameMap };
}
```

Expand Down
3 changes: 3 additions & 0 deletions packages/changelog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
"test": "jest"
},
"dependencies": {
"@ardatan/sync-fetch": "^0",
"@gitmoji/commit-types": "1.1.5",
"@gitmoji/parser-opts": "1.4.0",
"cosmiconfig": "^7",
"lodash": "^4",
"pangu": "^4"
},
"devDependencies": {
"@types/conventional-changelog-core": "^4",
"@types/lodash": "^4",
"better-than-before": "^1",
"conventional-changelog-core": "^4",
"conventional-changelog-writer": "^5",
Expand Down
7 changes: 7 additions & 0 deletions packages/changelog/src/customConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CommitTypes } from '@gitmoji/commit-types';
import { cosmiconfigSync } from 'cosmiconfig';
import { CustomTypeNameMap } from './transformer/typeDisplayName';

export interface CustomConfig {
/**
Expand Down Expand Up @@ -46,6 +47,12 @@ export interface CustomConfig {
* add back to top button
*/
addBackToTop?: boolean;
/**
* Custom type display map
*/
customTypeMap?: {
[key in CommitTypes]?: CustomTypeNameMap;
};
}

const explorer = cosmiconfigSync('changelog');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test transformCommitGroups should transform commitGroups correctly 1`] = `
exports[`finalizeContext should transform commitGroups correctly 1`] = `
{
"authors": [
{
"authorEmail": "test@test.com",
"authorName": "test",
"authorNameEncode": "test",
"authorAvatar": "https://avatars.githubusercontent.com/u/17870709?v=4",
"authorEmail": "i@canisminor.cc",
"authorName": "canisminor1990",
"authorUrl": "https://api.github.com/users/canisminor1990",
},
],
"commitGroups": [
{
"commits": [
{
"authorEmail": "test@test.com",
"authorName": "test",
"authorNameEncode": "test",
"authorAvatar": "https://avatars.githubusercontent.com/u/17870709?v=4",
"authorEmail": "i@canisminor.cc",
"authorName": "canisminor1990",
"authorUrl": "https://api.github.com/users/canisminor1990",
"first": true,
"hash": "1234",
"last": true,
Expand Down
280 changes: 223 additions & 57 deletions packages/changelog/src/finalizeContext/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Context } from 'conventional-changelog-writer';
import { CustomConfig } from '../customConfig';
import { typeMap } from '../transformer/typeDisplayName';
import finalizeContext from './index';

describe('test transformCommitGroups', () => {
const customConfig: CustomConfig = {
describe('finalizeContext', () => {
const customConfig = {
scopeDisplayName: {
'*': 'all',
},
customTypeMap: {},
};
const context: Context = {
const context = {
commitGroups: [
{
title: '✨ Features',
Expand All @@ -18,9 +18,10 @@ describe('test transformCommitGroups', () => {
subject: 'test commit',
scope: 'test',
title: '✨ Features',
authorName: 'test',
authorEmail: 'test@test.com',
authorNameEncode: 'test',
authorName: 'canisminor1990',
authorEmail: 'i@canisminor.cc',
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
authorUrl: 'https://api.github.com/users/canisminor1990',
},
],
},
Expand All @@ -29,55 +30,220 @@ describe('test transformCommitGroups', () => {
};

it('should transform commitGroups correctly', () => {
const subCommitScope = customConfig?.scopeDisplayName?.['*'] || '';
const authors = {};
context.commitGroups = context.commitGroups.map((item) => {
const subtitle = Object.values(typeMap).find(
(i) =>
item.title.includes(i['emoji']) ||
item.title.includes(i['en-US']) ||
item.title.includes(i['zh-CN']),
).subtitle;
let group;
let commits = item.commits.sort((a, b) => {
if (a.scope === subCommitScope && b.scope === subCommitScope) {
return 0;
} else if (a.scope === subCommitScope) {
return 1;
} else if (b.scope === subCommitScope) {
return -1;
} else {
return 0;
}
});
commits = commits.map((c, index) => {
if (group !== c.scope) {
group = c.scope;
c.first = true;
} else {
c.first = false;
}
if (!commits[index + 1] || group !== commits[index + 1].scope) {
c.last = true;
} else {
c.last = false;
}
if (c.authorNameEncode && !authors[c.authorNameEncode]) {
authors[c.authorNameEncode] = {
authorName: c.authorName,
authorEmail: c.authorEmail,
authorNameEncode: c.authorNameEncode,
};
}
return c;
});
return {
title: item.title,
subtitle,
commits,
};
const transformedContext = finalizeContext(customConfig)(context);
expect(transformedContext).toMatchSnapshot();
});

it('should set subCommitScope correctly when it is defined in customConfig', () => {
const customConfigWithSubCommitScope = {
...customConfig,
scopeDisplayName: {
'*': 'all',
test: 'test',
},
};
const transformedContext = finalizeContext(customConfigWithSubCommitScope)(context);
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
});

it('should set subCommitScope to null when it is not defined in customConfig', () => {
const transformedContext = finalizeContext(customConfig)(context);
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
});

it('should set subtitle correctly when title contains emoji', () => {
const contextWithTitleEmoji = {
...context,
commitGroups: [
{
title: '✨ Features',
commits: [],
},
],
};
const transformedContext = finalizeContext(customConfig)(contextWithTitleEmoji);
expect(transformedContext.commitGroups[0].subtitle).toBe("What's improved");
});

it('should set subtitle correctly when title contains en-US', () => {
const contextWithTitleEnUS = {
...context,
commitGroups: [
{
title: 'Features',
commits: [],
},
],
};
const transformedContext = finalizeContext(customConfig)({
...contextWithTitleEnUS,
commitGroups: [
{
...contextWithTitleEnUS.commitGroups[0],
title: `Features ${typeMap['feat']['en-US']}`,
},
],
});
expect(transformedContext.commitGroups[0].subtitle).toBe("What's improved");
});

it('should set subtitle correctly when title contains zh-CN', () => {
const contextWithTitleZhCN = {
...context,
commitGroups: [
{
title: 'Features',
commits: [],
},
],
};
const transformedContext = finalizeContext(customConfig)({
...contextWithTitleZhCN,
commitGroups: [
{
...contextWithTitleZhCN.commitGroups[0],
title: `Features ${typeMap['feat']['zh-CN']}`,
},
],
});
context.authors = Object.values(authors);
expect(context).toMatchSnapshot();
expect(transformedContext.commitGroups[0].subtitle).toBe("What's improved");
});

it('should sort commits correctly when subCommitScope is defined in customConfig', () => {
const customConfigWithSubCommitScope = {
...customConfig,
scopeDisplayName: {
'*': 'all',
test: 'test',
},
};
const contextWithMultipleCommits = {
...context,
commitGroups: [
{
title: '✨ Features',
commits: [
{
hash: '1234',
subject: 'test commit 1',
scope: 'test',
title: '✨ Features',
authorName: 'canisminor1990',
authorEmail: 'i@canisminor.cc',
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
authorUrl: 'https://api.github.com/users/canisminor1990',
},
{
hash: '5678',
subject: 'test commit 2',
scope: 'test',
title: '✨ Features',
authorName: 'canisminor1990',
authorEmail: 'i@canisminor.cc',
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
authorUrl: 'https://api.github.com/users/canisminor1990',
},
],
},
],
};
const transformedContext = finalizeContext(customConfigWithSubCommitScope)(
contextWithMultipleCommits,
);
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
expect(transformedContext.commitGroups[0].commits[1].first).toBe(false);
});

it('should set first and last correctly when commits have the same scope', () => {
const contextWithMultipleCommits = {
...context,
commitGroups: [
{
title: '✨ Features',
commits: [
{
hash: '1234',
subject: 'test commit 1',
scope: 'test',
title: '✨ Features',
authorName: 'canisminor1990',
authorEmail: 'i@canisminor.cc',
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
authorUrl: 'https://api.github.com/users/canisminor1990',
},
{
hash: '5678',
subject: 'test commit 2',
scope: 'test',
title: '✨ Features',
authorName: 'canisminor1990',
authorEmail: 'i@canisminor.cc',
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
authorUrl: 'https://api.github.com/users/canisminor1990',
},
],
},
],
};
const transformedContext = finalizeContext(customConfig)(contextWithMultipleCommits);
expect(transformedContext.commitGroups[0].commits[0].first).toBe(true);
expect(transformedContext.commitGroups[0].commits[0].last).toBe(false);
expect(transformedContext.commitGroups[0].commits[1].first).toBe(false);
expect(transformedContext.commitGroups[0].commits[1].last).toBe(true);
});

it('should set author correctly when authorAvatar is not empty', () => {
const contextWithAuthorNameEncode = {
...context,
commitGroups: [
{
title: '✨ Features',
commits: [
{
hash: '1234',
subject: 'test commit',
scope: 'test',
title: '✨ Features',
authorName: 'canisminor1990',
authorEmail: 'i@canisminor.cc',
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
authorUrl: 'https://api.github.com/users/canisminor1990',
},
],
},
],
};
const transformedContext = finalizeContext(customConfig)(contextWithAuthorNameEncode);
expect(transformedContext.authors).toEqual([
{
authorAvatar: 'https://avatars.githubusercontent.com/u/17870709?v=4',
authorEmail: 'i@canisminor.cc',
authorName: 'canisminor1990',
authorUrl: 'https://api.github.com/users/canisminor1990',
},
]);
});

it('should not set author when authorAvatar is empty', () => {
const contextWithoutAuthorNameEncode = {
...context,
commitGroups: [
{
title: '✨ Features',
commits: [
{
hash: '1234',
subject: 'test commit',
scope: 'test',
title: '✨ Features',
authorName: 'canisminor1990',
authorEmail: 'i@canisminor.cc',
},
],
},
],
};
const transformedContext = finalizeContext(customConfig)(contextWithoutAuthorNameEncode);
expect(transformedContext.authors).toEqual([]);
});
});

0 comments on commit e4da993

Please sign in to comment.