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 --changelog-preset option to customize --conventional-commits output #1111

Merged
merged 1 commit into from
Nov 16, 2017

Conversation

alan-agius4
Copy link
Contributor

@alan-agius4 alan-agius4 commented Nov 10, 2017

…gular`

Description

Lerna uses angular as changelog preset, in my case my lerna repo is hosted on a private bitbucket server. For the angular preset is built for usage with github in my case I want to use some other preset such as angular-bitbucket

Motivation and Context

this issue has already been highlighted in #850

How Has This Been Tested?

Unit Tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@alan-agius4 alan-agius4 changed the title feat(publish): Implement support for changelog presets other than `an… Implement support for changelog presets other than `an… Nov 10, 2017
@alan-agius4 alan-agius4 force-pushed the feature/changelog-preset branch 2 times, most recently from e667ab1 to ed47f70 Compare November 10, 2017 13:42
Copy link
Member

@evocateur evocateur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few tweaks and we're good to go. Thanks!

cwd: testDir
});

const execOptsWithChangelogPreset = (testDir) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used a handful of places (compared to the execOpts helper), just inline the expect.objectContaining() call.

@@ -878,7 +884,9 @@ describe("PublishCommand", () => {

it("should use conventional-commits utility to guess version bump and generate CHANGELOG", () => {
return run(testDir)(
"--conventional-commits"
"--conventional-commits",
"--changelog-preset",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in a separate test case (it()) in both modes to preserve the default cases. It only needs to assert one call, and none of the snapshots, of the existing --conventional-commits tests.

diff --git a/test/PublishCommand.js b/test/PublishCommand.js
index c6d14eb8..ae0e8dde 100644
--- a/test/PublishCommand.js
+++ b/test/PublishCommand.js
@@ -905,6 +905,33 @@ describe("PublishCommand", () => {
           });
         });
       });
+
+      it("accepts --changelog-preset option", () => {
+        return run(testDir)(
+          "--conventional-commits",
+          "--changelog-preset",
+          "foo-bar"
+        ).then(() => {
+          const name = "package-3";
+          const version = "3.0.0";
+          const location = path.join(testDir, "packages", name);
+
+          expect(ConventionalCommitUtilities.recommendIndependentVersion).toBeCalledWith(
+            expect.objectContaining({ name, version }),
+            expect.objectContaining({
+              cwd: testDir,
+              changelogPreset: "foo-bar",
+            })
+          );
+          expect(ConventionalCommitUtilities.updateIndependentChangelog).toBeCalledWith(
+            expect.objectContaining({ name, location }),
+            expect.objectContaining({
+              cwd: testDir,
+              changelogPreset: "foo-bar",
+            })
+          );
+        });
+      });
     });
 
     describe("fixed mode", () => {
@@ -960,6 +987,33 @@ describe("PublishCommand", () => {
           );
         });
       });
+
+      it("accepts --changelog-preset option", () => {
+        return run(testDir)(
+          "--conventional-commits",
+          "--changelog-preset",
+          "baz-qux"
+        ).then(() => {
+          const name = "package-5";
+          const version = "1.0.0";
+          const location = path.join(testDir, "packages", name);
+
+          expect(ConventionalCommitUtilities.recommendFixedVersion).toBeCalledWith(
+            expect.objectContaining({ name, version, location }),
+            expect.objectContaining({
+              cwd: testDir,
+              changelogPreset: "baz-qux",
+            })
+          );
+          expect(ConventionalCommitUtilities.updateFixedChangelog).toBeCalledWith(
+            expect.objectContaining({ name, location }),
+            expect.objectContaining({
+              cwd: testDir,
+              changelogPreset: "baz-qux",
+            })
+          );
+        });
+      });
     });
 
   });

rejectCycles: this.options.rejectCycles
})
: [this.packagesToPublish];
this.batchedPackagesToPublish = this.toposort
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this missing indent!

@@ -419,7 +425,12 @@ export default class PublishCommand extends Command {
version: update.package.version,
location: update.package.location
};
const recommendedVersion = recommendVersionFn(pkg, this.execOpts);

const changelogOptions = Object.assign({}, this.execOpts, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a lazy getter, so we only have to merge the options once:

diff --git a/src/commands/PublishCommand.js b/src/commands/PublishCommand.js
index 9edf8ad5..65222478 100644
--- a/src/commands/PublishCommand.js
+++ b/src/commands/PublishCommand.js
@@ -152,6 +152,20 @@ export default class PublishCommand extends Command {
     });
   }
 
+  get changelogOpts() {
+    if (!this._changelogOpts) {
+      const {
+        changelogPreset,
+      } = this.options;
+
+      this._changelogOpts = Object.assign({}, this.execOpts, {
+        changelogPreset,
+      });
+    }
+
+    return this._changelogOpts;
+  }
+
   initialize(callback) {
     this.gitRemote = this.options.gitRemote || "origin";
     this.gitEnabled = !(this.options.canary || this.options.skipGit);

Then just pass this.changelogOpts to the various functions.

@@ -574,18 +589,17 @@ export default class PublishCommand extends Command {

// we can now generate the Changelog, based on the
// the updated version that we're about to release.
if (this.options.conventionalCommits) {
if (conventionalCommits) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this was bugging me. :)

@alan-agius4 alan-agius4 force-pushed the feature/changelog-preset branch 2 times, most recently from 3bfe55d to 1a5026b Compare November 16, 2017 07:55
@alan-agius4
Copy link
Contributor Author

@evocateur, thanks for the feedback. I updated it :)

@evocateur evocateur changed the title Implement support for changelog presets other than `an… Add --changelog-preset option to customize --conventional-commits output Nov 16, 2017
@evocateur evocateur changed the title Add --changelog-preset option to customize --conventional-commits output Add --changelog-preset option to customize --conventional-commits output Nov 16, 2017
@evocateur evocateur merged commit da3e30f into lerna:master Nov 16, 2017
@evocateur
Copy link
Member

Thanks!

@engfragui
Copy link

engfragui commented Feb 23, 2018

@alan-agius4 This --changelog-preset option to customize --conventional-commits is exactly what I need right now, since my organization hosts code on bitbucket as well (I know...).

However this new and exciting option doesn't seem to work for me. I'm using lerna v2.9.0 (but I also tried with v2.6.0), yarn 1.3.2, node 8.

I'm trying to run lerna publish this way: lerna publish --skip-npm --skip-git --conventional-commits --changelog-preset=angular-bitbucket but I get the same result of lerna publish --skip-npm --skip-git --conventional-commits (same command, but without --changelog-preset option).

It makes me think that --changelog-preset gets completely ignored, also because I get the same result even if I do --changelog-preset=foo-bar or passing any other invalid changelog preset.

Should I add anything to my package.json and/or install additional yarn dependencies?

Thank you for your help

Update: the result I'm referring to is that a CHANGELOG.md gets generated with the right commit messages, but the links to the commits are github-style and not bitbucket-style.

@evocateur
Copy link
Member

evocateur commented Feb 23, 2018 via email

@alan-agius4
Copy link
Contributor Author

alan-agius4 commented Feb 23, 2018

Yes, you need to install this
https://www.npmjs.com/package/conventional-changelog-angular-bitbucket

@evocateur the reason why the preset is only passed to the conventional-changelog is
because of issue #1185 and if you try to pass preset to recommanded bump, you’ll get an error.

I’ll try to test the alpha soon, to see if thats still a problem

@engfragui
Copy link

engfragui commented Feb 23, 2018

Thank you both. I just updated my previous comment, clarifying what result I expect (CHANGELOG.md with bitbucket-style links), and what is the one I'm actually getting (CHANGELOG.md with github-style links).

From your answers it looks like the bitbucket preset should correctly be passed to conventional-changelog already in the 2.6+ versions and I should get a bitbucket-style changelog, which unfortunately is not what's happening at the moment.

I tried installing https://www.npmjs.com/package/conventional-changelog-angular-bitbucket and then lerna publish again, but it made no difference.

@alan-agius4
Copy link
Contributor Author

alan-agius4 commented Feb 23, 2018

Hey @engfragui,

Sorry that I didn't provide a lot of info earlier but I was getting on a plane.

I tried things out a bit on one of my bitbuckets repos, and you need to do a couple of things.

Install the conventional-changelog-angular-bitbucket in the root
In each of the packages/libs package.json you need to specify the below

  "repository": "https://bitbucketsson.betsson.local/projects/WF/repos/obg.ngx.packages", // note, this should not end with `.git` and this is typically the url you use to browse the repo.
  "bugs": {
    "url": "https://jirasson.betsson.local/browse" // this can be a link to your Jira. If you don't have one the whole section can be omitted.
  },

in lerna.json add the below;

"publish": {
   "changelogPreset": "angular-bitbucket",
    "conventionalCommits": true		
},

Alternatively, you can use the command line argument --changelog-preset=angular-bitbucket

Note re invalid presets
If you provide an invalid preset for instance foo, No error will be thrown. I am not sure if this is something not handled by lerna or by conventional-changelog, but properly we should also look into this,

@engfragui
Copy link

engfragui commented Feb 23, 2018

Thank you @alan-agius4, your comment was extremely helpful.

In the end it was sufficient to add:

"repository": {
    "type": "git",
    "url": "https://bitbucketsson.betsson.local/projects/WF/repos/obg.ngx.packages" // note, this should not end with `.git` and this is typically the url you use to browse the repo.
  },

to the root level package.json (in my case there was no need to add it into each of the packages/package.json).

My mistake was that I didn't have "repository" at all. For some reason I assumed it was not needed since the CHANGELOG.md that was generated via lerna publish had links to my bitbucket repo already without me having to specify "repository" (even though they were github-style and not bitbucket-style like I wanted).

Just for the sake of future readers, the few lines that need to be add to lerna.json are:

"command": {
    "publish": {
      "conventionalCommits": true,
      "changelogPreset": "angular-bitbucket"
    }
  }

("publish" needs to be inside "command").

Thank you again for your help, I would buy you lunch for a week if I could.

@evocateur
Copy link
Member

Thanks @alan-agius4!

@alan-agius4
Copy link
Contributor Author

alan-agius4 commented Feb 24, 2018

Glad to hear the issue is solved 😁

If yo are not using the ‘bugs’ field, indeed the root package.json sill suffice.

@bertho-zero
Copy link

bertho-zero commented Apr 6, 2018

How to use a custom preset? the -p option is used for an official preset but for a local config file you have to use the -n option of conventional-changelog.

@bertho-zero
Copy link

@evocateur When I use the changelogPreset option with the value @openagenda/oa, it's works with:

./node_modules/.bin/lerna publish

But the following command ignore my preset:

lerna publish

The two lerna have the same version (2.9.0), @openagenda/oa is replaced by @openagenda/conventional-changelog-oa by conventional-changelog but not found/used by my global lerna.

@evocateur
Copy link
Member

evocateur commented Apr 6, 2018 via email

@bertho-zero
Copy link

No my preset is in my lerna monorepo, but I am in it (it's my current working directory).

@evocateur
Copy link
Member

If I had to guess, it's a limitation of how conventional-changelog-cli resolves its preset argument. A similar limitation for eslint plugins/config that will fail if a global eslint is used (instead of the locally-installed copy).

Lerna 3 always uses the locally-installed version, even when called from a global installation. Also, conventional-changelog-cli has been replaced by direct conventional-changelog API calls, with a much better custom preset experience overall.

@mocheng
Copy link

mocheng commented Jun 12, 2018

I have a customized preset installed at root node_modules. For lerna v2.6.0, it is interesting that publish command can find the preset with argument --canary, but fail without --canary.

Upgrade to v3 beta version solves the problem.

@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants