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

Support for parserOpts #8

Closed
jreilly-lukava opened this issue Mar 15, 2022 · 11 comments · Fixed by #9
Closed

Support for parserOpts #8

jreilly-lukava opened this issue Mar 15, 2022 · 11 comments · Fixed by #9
Assignees
Labels
enhancement New feature or request released

Comments

@jreilly-lukava
Copy link
Contributor

jreilly-lukava commented Mar 15, 2022

I'm unfortunately work in an Azure DevOps environment and they have non-github standard naming for PRs and thusly I need to be able to configure the parserOpts. I'd love to see support for this if it's not already possible. I tried including the below in my "options" but with no luck.

"plugins": [
  [
    "@semantic-release/commit-analyzer",
    {
      "preset": "angular",
      "parserOpts": {
        "mergePattern": "/^Merged PR (\\d+): (\\w*)(?:\\(([\\w$.\\-* ]*)\\))?: (.*)$/",
        "mergeCorrespondence": ["pr", "type", "scope", "subject"],
      }
    }
  ]

Likewise I'd love to be able to configure writerOps as well.

@jreilly-lukava
Copy link
Contributor Author

jreilly-lukava commented Mar 15, 2022

Took a stab at some stuff and got at least a decent start going. Not sure I have enough to make a full blown PR for you with tests and all. It would probably make more since in config file so it could be usable by all the projects in my monorepo without having to repeat myself. Here's what I've got.

plugins.ts

diff --git a/packages/nx-semantic-release/src/executors/semantic-release/plugins.ts b/packages/nx-semantic-release/src/executors/semantic-release/plugins.ts
index 446b35f9d386c2f970c5bbea725161a55ccded42..d8c63c20bb68096ef4e4da65af8885f1200d542b 100644
--- a/packages/nx-semantic-release/src/executors/semantic-release/plugins.ts
+++ b/packages/nx-semantic-release/src/executors/semantic-release/plugins.ts
@@ -52,9 +52,18 @@ export const resolvePlugins = (
 
   const emptyArray = [] as unknown as release.PluginSpec;
   const defaultPlugins: release.PluginSpec[] = [
-    '@semantic-release/commit-analyzer',
-    '@semantic-release/release-notes-generator',
-
+    [
+      '@semantic-release/commit-analyzer',
+      {
+        parserOpts: options.parserOpts,
+      },
+    ],
+    [
+      '@semantic-release/release-notes-generator',
+      {
+        parserOpts: options.parserOpts,
+      },
+    ],
     ...(options.changelog
       ? [
           [

scheme.json

diff --git a/packages/nx-semantic-release/src/executors/semantic-release/schema.json b/packages/nx-semantic-release/src/executors/semantic-release/schema.json
index c999e2b543e2a7c3e9aa7419f9bec1418bd99ece..cce599ef3e60badc47ff0354313fa2e2e60001a3 100644
--- a/packages/nx-semantic-release/src/executors/semantic-release/schema.json
+++ b/packages/nx-semantic-release/src/executors/semantic-release/schema.json
@@ -62,6 +62,10 @@
       "type": "string",
       "default": "chore(release): ${nextRelease.version} [skip ci]\\n\\n${nextRelease.notes}"
     },
+    "parserOpts": {
+      "description": "Parser options used by commit-analyzer and @semantic-release/release-notes-generator and @semantic-release/changelog",
+      "type": "object"
+    },
     "gitAssets": {
       "description": "Path to assets to include in git commit.",
       "type": "array",

semantic-release.ts

diff --git a/packages/nx-semantic-release/src/executors/semantic-release/semantic-release.ts b/packages/nx-semantic-release/src/executors/semantic-release/semantic-release.ts
index 486f7b5910e2edebf0663bc3602a105f5392a3c5..f01d26f65c337eafa6313b87e0a047456c606b19 100644
--- a/packages/nx-semantic-release/src/executors/semantic-release/semantic-release.ts
+++ b/packages/nx-semantic-release/src/executors/semantic-release/semantic-release.ts
@@ -16,6 +16,9 @@ export type SemanticReleaseOptions = Omit<release.Options, 'extends'> & {
   commitMessage: string;
   gitAssets?: string[];
   packageJsonDir?: string;
+  parserOpts?: {
+    [key: string]: string;
+  };
 };
 
 export async function semanticRelease(

@TheUnderScorer
Copy link
Owner

Hey jreilly-lukava,

thanks for the suggestion! Seems like having option to customize the parserOpts would be a good idea regardless.
I will try to look through your changes during a weekend and come up with a PR.

Thanks!

@TheUnderScorer TheUnderScorer self-assigned this Mar 18, 2022
@TheUnderScorer TheUnderScorer added the enhancement New feature or request label Mar 18, 2022
@jreilly-lukava
Copy link
Contributor Author

I'm new to semantic-release so I've been beating my head against the wall for a long time on and off trying to implement something that worked for me. While the above would be useful for per project customization I found that I could add customization to a .releaserc.js or such that could contain global customization applied to every project, which works great for me. So I ended up with something like this.

.releaserc.js

module.exports = {
  branches: ['main'],
  preset: 'conventionalcommits',

  analyzeCommits: {
    parserOpts: {
      headerPattern: '^Merged PR (\\d+): (\\w*)(?:\\(([\\w$.\\-* ]*)\\))?: (.*)$',
      headerCorrespondence: ['pr', 'type', 'scope', 'subject']
    },
  },
  generateNotes: {
    linkReferences: true,
    linkCompare: false,
    parserOpts: {
      headerPattern: '^Merged PR (\\d+): (\\w*)(?:\\(([\\w$.\\-* ]*)\\))?: (.*)$',
      headerCorrespondence: ['pr', 'type', 'scope', 'subject']
    },
    writerOpts: {
      mainTemplate: readFileSync(join(__dirname, 'templates/template.hbs'), 'utf-8'),
      headerPartial: readFileSync(join(__dirname, 'templates/header.hbs'), 'utf-8'),
      commitPartial: readFileSync(join(__dirname, 'templates/commit.hbs'), 'utf-8'),
      footerPartial: readFileSync(join(__dirname, 'templates/footer.hbs'), 'utf-8'),
    }
  }
}

@jreilly-lukava
Copy link
Contributor Author

I guess I still don't know what I'm doing :) Those setting in .releaserc.js are messing with the commit filtering. Back to the drawing board.

@TheUnderScorer
Copy link
Owner

Yeah, unfortunately seems like releaserc.js causes the semantic-release to ignore filtered commits from my plugin 😁.

I've got some POC for now that allows passing parserOpts and writerOpts basing on what you've done, next is adding some kind of configuration file. Feel free to test it to see if it works correctly for you!

@jreilly-lukava
Copy link
Contributor Author

Wow, that was quicker than I expected. I really appreciate your effort! The changes work as I would expect them to. However after working with semantic-release there are still some pitfalls I've run into.

  • Duplicating all these config options across multiple projects in my nx monorepo is tedious, granted that may be what some people are looking for, per project options
  • Having to inline handlebar templates is a pain
  • I've come across other settings that I'd like to change also (for example, linkCompare: false, comparing tags (even if they are package specific, app-a-v1.0.0 to app-a-v1.1.0), isn't so useful if app-b has had a bunch of commits during that time.

I'm going to keep watching this repo though and am happy to help out in any way I can as I still don't have a good solution for this in my situtation.

@jreilly-lukava
Copy link
Contributor Author

I managed coble something together using cosmiconfig with a .nx-releaserc.js style configuration. Should be pretty easy to extends things to read from the project config in angular/nx.json file first and then the config file, and lastly nothing if neither of those are configured. I don't have time to get a PR to you. If you want I could get you something to look at next week.

@TheUnderScorer
Copy link
Owner

TheUnderScorer commented Mar 19, 2022

I've come across other settings that I'd like to change also (for example, linkCompare: false, comparing tags (even if they are package specific, app-a-v1.0.0 to app-a-v1.1.0), isn't so useful if app-b has had a bunch of commits during that time.

I'm curious about this one (comparing tags), could you elaborate further? 😄

I managed coble something together using cosmiconfig with a .nx-releaserc.js style configuration. Should be pretty easy to extends things to read from the project config in angular/nx.json file first and then the config file, and lastly nothing if neither of those are configured. I don't have time to get a PR to you. If you want I could get you something to look at next week.

Sounds good! I will release what I have so far, because for now I also don't really have much time to dig into this config file stuff. Fell free to open a PR when you will have something, thanks!

Since this issue was about parserOpts/writerOpts that I've added, I will close it.
In some free time I will try to investigate adding more options that you've mentioned (ex. linkCompare) and adding better support for handlebar templates.

@TheUnderScorer
Copy link
Owner

🎉 This issue has been resolved in version 1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jreilly-lukava
Copy link
Contributor Author

I'm curious about this one (comparing tags), could you elaborate further? 😄

Sure. Take this compare link from the conventional-changelog monorepo. It's using the tags of conventional-commits-parser-v3.2.3 and conventional-commits-parser-v3.2.4 for it's comparison. But when you look at the list of commits you see commits like 5917ad2 which is isolated to another package and not even a package that is a dependency for the commits parser.

So it's for this reason I personally don't find much use for the compare link in monorepos.

@TheUnderScorer
Copy link
Owner

Ahh got it, makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants