Skip to content

Commit

Permalink
Merge pull request #22 from capricorn86/21-add-support-for-fallback-g…
Browse files Browse the repository at this point in the history
…ithub-username

feat: [#21] Adds support for a fallback username
  • Loading branch information
capricorn86 authored Jan 31, 2024
2 parents 8a6031a + c21c274 commit a342722
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 39 deletions.
7 changes: 5 additions & 2 deletions bin/happy-release-notes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getArguments() {
from: null,
to: null,
versionHeader: false,
author: null
authorUsername: null
};

for (const arg of process.argv) {
Expand All @@ -32,6 +32,8 @@ function getArguments() {
args.to = arg.split('=')[1];
} else if (arg.startsWith('--author=')) {
args.author = arg.split('=')[1];
} else if (arg.startsWith('--authorUsername=')) {
args.authorUsername = arg.split('=')[1];
} else if (arg.startsWith('--versionHeader')) {
args.versionHeader = true;
}
Expand All @@ -56,7 +58,8 @@ async function main() {
fromVersion: args.from ? args.from : null,
toVersion: args.to ? args.to : null,
versionHeader: args.versionHeader,
author: args.author
author: args.author,
authorUsername: args.authorUsername
});

console.log(releaseNotes);
Expand Down
31 changes: 22 additions & 9 deletions src/ConventionalCommitReleaseNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ export default class ConventionalCommitReleaseNotes {
* @param [options.toVersion] To version.
* @param [options.versionHeader] "true" to show version header.
* @param [options.author] "githubUsername" or "nameAndEmail".
* @param [options.authorUsername] Use this username for author when "author" is not specified or if it was not possible to retrieve the Github username based on email.
* @returns Release notes.
*/
public static async getReleaseNotes(options?: {
fromVersion?: string;
toVersion?: string;
versionHeader?: boolean;
author?: 'githubUsername' | 'nameAndEmail';
authorUsername?: string;
}): Promise<string> {
const releases = await this.getReleases(options);
let output = '';
Expand All @@ -61,18 +63,28 @@ export default class ConventionalCommitReleaseNotes {
const message = change.message.endsWith('.')
? change.message.slice(0, -1)
: change.message;
const author =
options?.author === 'githubUsername' && change.author.githubUsername
? change.author.githubUsername
: options?.author === 'nameAndEmail'
? `${change.author.name} (${change.author.email})`
: null;
let author = '';

switch (options?.author) {
case 'githubUsername':
author =
change.author.githubUsername || options?.authorUsername
? `@${change.author.githubUsername || options?.authorUsername}`
: null;
break;
case 'nameAndEmail':
author = `${change.author.name} (${change.author.email})`;
break;
default:
author = options?.authorUsername ? `@${options?.authorUsername}` : null;
break;
}

let userAndTask = '';
if (author && change.taskId) {
userAndTask = ` - By **@${author}** in task ${change.taskId}`;
userAndTask = ` - By **${author}** in task ${change.taskId}`;
} else if (author) {
userAndTask = ` - By **@${author}**`;
userAndTask = ` - By **${author}**`;
} else if (change.taskId) {
userAndTask = ` - In task ${change.taskId}`;
}
Expand Down Expand Up @@ -295,7 +307,8 @@ export default class ConventionalCommitReleaseNotes {
const [message, userName, userEmail] = row.trim().split('|');
if (message) {
commits.push(<ICommit>{
message,
// Remove @ from message to avoid Github to link to a user.
message: message.replace('@', ''),
author: {
name: userName,
email: userEmail,
Expand Down
Loading

0 comments on commit a342722

Please sign in to comment.