Skip to content

Commit

Permalink
feat(version): add flag to include changelog commit author, close #248
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Jul 8, 2022
1 parent e214051 commit 0c8b3e3
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"createRelease": "github",
"gitDryRun": false,
"syncWorkspaceLock": true,
"message": "chore(release): publish new version %v",
"changelogHeaderMessage": "### Automate your Workspace Versions, Changelogs & Publish with [Lerna-Lite](https://github.com/ghiscoding/lerna-lite) 🚀"
"changelogIncludeCommitAuthor": true,
"changelogHeaderMessage": "### Automate your Workspace Versions, Changelogs & Publish with [Lerna-Lite](https://github.com/ghiscoding/lerna-lite) 🚀",
"message": "chore(release): publish new version %v"
},
"run": {
"cmdDryRun": false
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/cli-commands/cli-version-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ exports.builder = (yargs, composed) => {
requiresArg: true,
type: 'string',
},
'changelog-include-commit-author': {
describe: "Specify if we want to include the commit author's name when using conventional-commits with changelog",
group: 'Version Command Options:',
type: 'boolean',
},
'changelog-version-message': {
describe:
'Add a custom message as a prefix to each new version in your "changelog.md" which is located in the root of your project. This option only works when using --conventional-commits.',
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"@types/glob-parent": "^5.1.1",
"@types/inquirer": "^8.2.1",
"@types/load-json-file": "^5.1.0",
"@types/minimatch": "^3.0.5",
"@types/npm-package-arg": "^6.1.1",
"@types/p-map": "^2.0.0",
"@types/semver": "^7.3.10",
Expand Down
46 changes: 44 additions & 2 deletions packages/core/src/conventional-commits/update-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function updateChangelog(
rootPath,
tagPrefix = 'v',
version = undefined,
changelogIncludeCommitAuthor = false,
changelogHeaderMessage = '',
changelogVersionMessage = '',
} = updateOptions;
Expand All @@ -47,6 +48,13 @@ export async function updateChangelog(
// NOTE: must pass as positional argument due to weird bug in merge-config
const gitRawCommitsOpts = Object.assign({}, options.config.gitRawCommitsOpts);

// when including commit author's name, we need to change the conventional commit format
// and later do string extract, of ">>author=%an<<", and replacement to get a commit string that would add (@authorName) to the end of the commit string, ie:
// **deps:** update all non-major dependencies ([ed1db35](https://github.com/ghiscoding/lerna-lite/commit/ed1db35)) (@Renovate-Bot)
if (changelogIncludeCommitAuthor) {
gitRawCommitsOpts.format = '%B%n-hash-%n%H>>author=%an<<';
}

if (type === 'root') {
context.version = version;

Expand Down Expand Up @@ -76,9 +84,13 @@ export async function updateChangelog(
const changelogStream = conventionalChangelogCore(options, context, gitRawCommitsOpts);

return Promise.all([
(getStream as any)(changelogStream).then(makeBumpOnlyFilter(pkg)),
// prettier-ignore
getStream(changelogStream).then(makeBumpOnlyFilter(pkg)),
readExistingChangelog(pkg),
]).then(([newEntry, [changelogFileLoc, changelogContents]]) => {
]).then(([inputEntry, [changelogFileLoc, changelogContents]]) => {
// are we including commit author's name in changelog?
const newEntry = changelogIncludeCommitAuthor ? includeChangelogCommitAuthorName(inputEntry) : inputEntry;

log.silly(type, 'writing new entry: %j', newEntry);

const changelogVersion = type === 'root' ? changelogVersionMessage : '';
Expand All @@ -102,3 +114,33 @@ export async function updateChangelog(
});
});
}

/**
* From an input entry string, ie
* deps: update all non-major dependencies ([ed1db35](https://github.com/ghiscoding/lerna-lite/commit/ed1db35>>author=Renovate Bot<<))
* we will extract the commit author's name and form a new string that will look like below
* deps: update all non-major dependencies ([ed1db35](https://github.com/ghiscoding/lerna-lite/commit/ed1db35)) (@Renovate-Bot)
* @param inputEntry
* @returns
*/
function includeChangelogCommitAuthorName(inputEntry: string) {
let author = '';

// the input entry string can contain multiple commits but at most 1 per line,
// so we can split for each line break and rejoin them once we're done
let lineEntries: string[] = [];

for (const entry of inputEntry.split('\n')) {
const authorStrMatches = entry.match(/(.*)(>>author=.*<<)(\)*)/);
if (Array.isArray(authorStrMatches) && authorStrMatches.length > 2) {
const escapedEntry = authorStrMatches[1] + (authorStrMatches.length >= 3 ? authorStrMatches[3] : '');

// commit author, it might include spaces and if it does we will replace by hypen (-)
author = authorStrMatches[2].replace(/(>>author=|<<)/g, '').replace(' ', '-') || '';
lineEntries.push(`${escapedEntry} (@${author})`);
} else {
lineEntries.push(entry);
}
}
return lineEntries.join('\n');
}
3 changes: 3 additions & 0 deletions packages/core/src/models/command-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ export interface VersionCommandOption {
/** Add a custom message at the top of your "changelog.md" which is located in the root of your project. This option only works when using --conventional-commits. */
changelogHeaderMessage?: string;

/** Specify if we want to include the commit author's name when using conventional-commits with changelog */
changelogIncludeCommitAuthor?: boolean;

/** Add a custom message as a prefix to each new version in your "changelog.md" which is located in the root of your project. This option only works when using --conventional-commits. */
changelogVersionMessage?: string;

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface UpdateChangelogOption {
changelogHeaderMessage?: string;
changelogVersionMessage?: string;
changelogPreset?: string;
changelogIncludeCommitAuthor?: boolean;
rootPath?: string;
tagPrefix?: string;
version?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/version/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@types/fs-extra": "^9.0.13",
"@types/js-yaml": "^4.0.5",
"@types/load-json-file": "^5.1.0",
"@types/minimatch": "^3.0.5",
"@types/p-map": "^2.0.0",
"@types/semver": "^7.3.10",
"@types/write-json-file": "^3.2.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/version/src/version-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ export class VersionCommand extends Command<VersionCommandOption> {
changelogPreset,
rootPath,
tagPrefix: this.tagPrefix,
changelogIncludeCommitAuthor: this.options.changelogIncludeCommitAuthor,
changelogHeaderMessage: this.options.changelogHeaderMessage,
changelogVersionMessage: this.options.changelogVersionMessage,
}).then(({ logPath, newEntry }) => {
Expand Down Expand Up @@ -683,6 +684,7 @@ export class VersionCommand extends Command<VersionCommandOption> {
rootPath,
tagPrefix: this.tagPrefix,
version: this.globalVersion,
changelogIncludeCommitAuthor: this.options.changelogIncludeCommitAuthor,
changelogHeaderMessage: this.options.changelogHeaderMessage,
changelogVersionMessage: this.options.changelogVersionMessage,
}).then(({ logPath, newEntry }) => {
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c8b3e3

Please sign in to comment.