Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PR authors in the changelog message #349

Open
diegohaz opened this issue Jul 8, 2018 · 4 comments
Open

Add PR authors in the changelog message #349

diegohaz opened this issue Jul 8, 2018 · 4 comments

Comments

@diegohaz
Copy link

diegohaz commented Jul 8, 2018

I've been playing around with standard-changelog on these projects:

Using https://github.com/diegohaz/singel/blob/master/CHANGELOG.md as an example, I was looking for a way to append something like , thanks to @ts-de (PR author) to the changelog message, but I can't see if that's possible with the CLI.

@samkelleher
Copy link
Contributor

I've configured this to do the same, however, you cannot get the github username/link because git only contains the commiter name+email, if you wanted the link to profile, you'd have to look it up seperatly.

When running you can provide a custom format, it's gitlog syntax

const gitRawCommitsOpts = {
        format: '%B%n-hash-%n%H%n-gitTags-%n%d%n-committerDate-%n%ci%n-authorName-%n%an%n-authorEmail-%n%ae%n-gpgStatus-%n%G?%n-gpgSigner-%n%GS',
};

But here it now includes extra keys authorName and authorEmail.

Whatever writer you're using now has access to these keys to use them in their template.

@stevemao
Copy link
Member

stevemao commented Jun 1, 2019

Thanks @samkelleher , you might be able to use GitHub API to fetch your username.

@medikoo
Copy link

medikoo commented Feb 27, 2020

When running you can provide a custom format, it's gitlog syntax

I'm running this on repository with long history, and unfortunately process hangs with this custom format setting.

As I inspected it's due to fact that for some reason in all cases information for all repository commits is retrieved (not just for range which is target of changelog), and custom --format implies more information to be returned in git log

@ghiscoding
Copy link

ghiscoding commented Jul 30, 2022

hey guys, I was actually looking at how to add this to Lerna-Lite (a lighter fork of Lerna with extra features) and after a couple weeks and 2 implementations I think I got it all working with decent code, so I decided to post some piece of it which might help some of you. The 1st implementation was string manipulation before the save, it was working but not exactly ideal especially on the perf side. The 2nd implementation, which you can see in this Lerna-Lite PR extends both the writerOpts transform and also extending the commitPartial template to add the author info. Also the challenging part is that Lerna/Lerna-Lite allows to load whichever changelog preset config user might want (angular being the default) and so the code must be able to stay dynamic and I basically cannot overwrite/reimplement the entire transform function for the simple fact that there are multiple implementation for each preset and the same goes for the commit templates so I had to come up with a flexible approach.

The first thing to note is that Lerna-Lite uses https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-core and so the pieces of code shown below are what that lib

The first question of this issue of how to add the Git Author Name is actually easy to implement (once I understood the documentation) and we can modify, as suggested here, the gitRawCommitsOpts by adding the author name/email and then just modify the commitPartial template. Also remember I must keep dynamic since I don't know which preset is use, so for that I read the preset config which give me access to the chosen preset commitPartial and from there I just append the git author info to the end of the template

import conventionalChangelogCore from 'conventional-changelog-core';

const GIT_COMMIT_WITH_AUTHOR_FORMAT = '%B%n-hash-%n%H%n-gitTags-%n%d%n-committerDate-%n%ci%n-authorName-%n%an%n-authorEmail-%n%ae%n-gpgStatus-%n%G?%n-gpgSigner-%n%GS';
const config = getChangelogConfig(changelogPreset, rootPath); // Lerna-Lite way of reading changelog preset config
setConfigChangelogCommitGitAuthor(config, gitRawCommitsOpts, writerOpts, changelogIncludeCommitsGitAuthor);
const changelogStream = conventionalChangelogCore(options, context, gitRawCommitsOpts, undefined, writerOpts);

export function setConfigChangelogCommitGitAuthor(config, gitRawCommitsOpts, writerOpts, commitCustomFormat) {
  gitRawCommitsOpts.format = GIT_COMMIT_WITH_AUTHOR_FORMAT;
  const extraCommitMsg = `by {{authorName}} ({{authorEmail}})`; // we can also customize this in different ways, which I did in Lerna-Lite

  // read chosen preset's `commitPartial` and append our extra commit message to it
  writerOpts.commitPartial =
    config.writerOpts.commitPartial!.replace(/\n*$/, '') + ` {{#if @root.linkReferences~}}${extraCommitMsg}{{~/if}}\n`;
}

I also wanted to have a way to add remote client login name and that was a bit more challenging and also as suggested we can use the GitHub API (Graphql in my case), for Lerna-Lite implementation I decided to read all commits since the last release which you can see the implementation in this file and this provides me an array of commits which shouldn't be too large since it's against the last release. I also did it that way because the transform is synchronous, so I can't use async API fetching. Then I call the original transform function from whichever changelog preset the user chose and execute it, then I execute my own little extended transform which I add the remote client login info and again append that to the commitPartial like I did in the previous code

import conventionalChangelogCore from 'conventional-changelog-core';

const GIT_COMMIT_WITH_AUTHOR_FORMAT = '%B%n-hash-%n%H%n-gitTags-%n%d%n-committerDate-%n%ci%n-authorName-%n%an%n-authorEmail-%n%ae%n-gpgStatus-%n%G?%n-gpgSigner-%n%GS';
const config = getChangelogConfig(changelogPreset, rootPath); // Lerna-Lite way of reading changelog preset config
setConfigChangelogCommitClientLogin(config, gitRawCommitsOpts, writerOpts, commitsSinceLastRelease, changelogIncludeCommitsClientLogin);
const changelogStream = conventionalChangelogCore(options, context, gitRawCommitsOpts, undefined, writerOpts);


export function setConfigChangelogCommitClientLogin(config, gitRawCommitsOpts, writerOpts, commitsSinceLastRelease, commitCustomFormat) {
  gitRawCommitsOpts.format = GIT_COMMIT_WITH_AUTHOR_FORMAT;
  const extraCommitMsg = `by @{{userLogin}}`;

  writerOpts.commitPartial =
    config.writerOpts.commitPartial!.replace(/\n*$/, '') + ` {{#if @root.linkReferences~}}${extraCommitMsg}{{~/if}}\n`;

  // add commits since last release into the transform function
  writerOpts.transform = writerOptsTransform.bind(null, config.writerOpts.transform, commitsSinceLastRelease);
}

/**
 * Extend the writerOpts transform function from whichever preset config is loaded
 * We will execute the original writerOpts transform function, then from it we'll add extra properties to the commit object
 */
export function writerOptsTransform(originalTransform, commitsSinceLastRelease, commit, context) {
  // execute original writerOpts transform
  const extendedCommit = originalTransform(commit, context);

  // then add client remote detail (login) to the commit object and return it
  if (extendedCommit) {
    // search current commit with the commits since last release array returned from fetching GitHub API
    const remoteCommit = commitsSinceLastRelease.find((c) => c.shortHash === commit.shortHash);
    if (remoteCommit?.login) {
      commit.userLogin = remoteCommit.login;
    }
  }

  return extendedCommit;
}

and that's it, I now have 2 different new features to chose from, 1st one is to add author's name/email (from git history) or the second feature is to add the remote client login name (from GitHub API). The reason that I have both is that I only implemented this for the GitHub client and so users of GitLab or other type of client would only be able to use the 1st feature because reading git history works for everyone and isn't limited to GitHub client.

If you have any questions or comments, feel free to ask and if you want to see a live demo of this on a real project, then you can take a look at Lerna-Lite Releases which now uses these 2 options, the release v1.8.0 was with 1st implementation (git author name) while v1.9.0 was with the 2nd feature to fetch commits from GitHub API. Also to complete the picture, here's Lerna-Lite docs for these 2 features --changelog-include-commits-git-author and --changelog-include-commits-client-login

I must say that the documentation on how and what to change for these things was really hard to understand and piece everything together, I had to search for a lot of issues opened in this project to find how to do what I wanted to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants