Giter VIP home page Giter VIP logo

Comments (11)

antoinezanardi avatar antoinezanardi commented on August 15, 2024 2

I'm closing this issue with the following solution. Thanks again @amariq for your help.

In order to sort the commit groups, you must use a .releaserc.js file (not json).

First, declare the desired order for groups.

const commitGroupsOrder = {
  features: 1,
  bugfixes: 2,
  docs: 3,
  styles: 4,
  refactor: 5,
  performances: 6,
  tests: 7,
  ci: 8,
  chore: 9,
};

Note here : the keys are named after the section title. It may defer on your project.

Then, here's my release-notes-generator config definition :

      "@semantic-release/release-notes-generator",
      {
        preset: "conventionalcommits",
        presetConfig: {
          types: [
            {
              type: "feat",
              section: "๐Ÿš€ Features",
              hidden: false,
            },
            {
              type: "fix",
              section: "๐Ÿ› Bug Fixes",
              hidden: false,
            },
            {
              type: "docs",
              section: "๐Ÿ“– Docs",
              hidden: false,
            },
            {
              type: "style",
              section: "๐ŸŽจ Styles",
              hidden: false,
            },
            {
              type: "refactor",
              section: "๐Ÿ”ฉ Refactor",
              hidden: false,
            },
            {
              type: "perf",
              section: "โšก๏ธ Performances",
              hidden: false,
            },
            {
              type: "test",
              section: "โœ… Tests",
              hidden: false,
            },
            {
              type: "ci",
              section: "๐Ÿ” CI",
              hidden: false,
            },
            {
              type: "chore",
              section: "๐Ÿงน Chore",
              hidden: false,
            },
          ],
        },
        writerOpts: {
          groupBy: "type",
          commitGroupsSort: (commitGroupA, commitGroupB) => {
            const commitGroupTitleA = commitGroupA.title.replace(/[^a-zA-Z]/gu, "").toLowerCase();
            const commitGroupTitleB = commitGroupB.title.replace(/[^a-zA-Z]/gu, "").toLowerCase();

            if (commitGroupsOrder[commitGroupTitleA] === undefined || commitGroupsOrder[commitGroupTitleB] === undefined) {
              return 0;
            }
            if (commitGroupsOrder[commitGroupTitleA] < commitGroupsOrder[commitGroupTitleB]) {
              return -1;
            }
            if (commitGroupsOrder[commitGroupTitleA] > commitGroupsOrder[commitGroupTitleB]) {
              return 1;
            }
            return 0;
          },
        },
      },
    ],

Two important parts here :

  • In presetConfig.types, I declare the commit section titles. For example, the feat section will be renamed to ๐Ÿš€ Features.
  • In writerOpts.commitGroupsSort, I write a sort function which will compare sections and sort them by title. As you can see, I used replace and some regexp to keep letters only. For example, ๐Ÿ› Bug Fixes will be transformed in bugfixes, and that's the reason why it's named that way in the commitGroupsOrder array. The order is based on the commitGroupsOrder values : the lower the section is, the higher it will appear in the CHANGELOG.md.

Feel free to use this short code and try it out with semantic-release --dry-run.

from release-notes-generator.

amariq avatar amariq commented on August 15, 2024 1

๐Ÿ‘

Is there a way to print on stdout the generated changelog

Sure, you can use --dry-run to test things out as far as I know.

semantic-release --dry-run

from release-notes-generator.

amariq avatar amariq commented on August 15, 2024 1

Ah, it looks like you put your writerOpts inside presetConfig.
Instead, place the writerOpts next to presetConfig (on the same level). Should work then ๐Ÿ‘Œ

      {
        "preset": "conventionalcommits",
        "presetConfig": {
          "types": [...]
        },
        "writerOpts": {
          "commitGroupsSort": <your function here>
        }
      }

from release-notes-generator.

amariq avatar amariq commented on August 15, 2024 1

Yeah, it also took me a good while to find out where the heck do the options get finally merged.

For me it was at conventional-changelog-writer/index.js:42. And at the line 77 the commitGroupsSort becomes a function. You can put console.log somere before that to see if your value gets there.

So, overall, conventionalcommits preset sets default commitGroupsSort here and then that value can be overridden using plugin's writerOpts, which happens in this call chain:

  1. semantic-release/release-notes-generator/.../index.js:32
  2. conventional-changelog/conventional-changelog/.../conventional-changelog-writer/index.js:87
  3. conventional-changelog/conventional-changelog/.../conventional-changelog-writer/index.js:42

from release-notes-generator.

subhamagr avatar subhamagr commented on August 15, 2024 1

Thank you, @amariq . I have updated my comment to say it worked with your fix. It was my fault to not test with multiple types of commits, duh! I then tried to test it multiple types of commits (feat, fix, breaking etc) and it worked! Thank you again, for debugging this for me. Really appreciate it.

from release-notes-generator.

amariq avatar amariq commented on August 15, 2024

I had the same issue, and its because commitGroupsSort accepts not commit types but some object's property name/path and then the value of that property is used to sort the commits.

So by default it's title property, and if you want to sort by type then you need to provide commitGroupsSort as a comparator function (example). It would require to use .releaserc.js as a config though.

from release-notes-generator.

antoinezanardi avatar antoinezanardi commented on August 15, 2024

Thanks a lot for the clarification, I will try it out as soon as possible. Is there a way to print on stdout the generated changelog in order to test ?

When I'll have the right solution, I'll post it here for future readers with the same problem as mine.

from release-notes-generator.

subhamagr avatar subhamagr commented on August 15, 2024

I have tried it with .releaserc.js, but it doesn't seem to overwrite the commitGroupsSort function. Could you please provide an example where it works with preset set to conventionalcommits for @semantic-release/release-notes-generator plugin?

from release-notes-generator.

amariq avatar amariq commented on August 15, 2024

๐Ÿค”
It should be something like that, I think:

      "@semantic-release/release-notes-generator",
      {
        "preset": "conventionalcommits",
        "writerOpts": {
          "commitGroupsSort": <your function here>
        }
      }

from release-notes-generator.

subhamagr avatar subhamagr commented on August 15, 2024

I have this:

["@semantic-release/release-notes-generator", {
      "preset": "conventionalcommits",
      "presetConfig": {
        "types": [
          {
            "type": "breaking",
            "section": "Breaking Changes",
            "hidden": false
          },
          {
            "type": "feat",
            "section": "New Features",
            "hidden": false
          },
          {
            "type": "fix",
            "section": "Bug Fixes",
            "hidden": false
          }
        ],
        "writerOpts": {
          "commitGroupsSort": (a, b) => {
            console.log("called", [a, b])
            throw new Error([a, b])
          }
        }
      }
    }],

and the output is:

## 1.11.1-beta.1 (2023-04-17)

### New Features

    * Some change (bdd860a)

and without the writerOpts, the output is also the same.

So I think that the custom writerOpts aren't getting replaced. Looking at the code too, it doesn't seem like it overrides the vars.

from release-notes-generator.

subhamagr avatar subhamagr commented on August 15, 2024

ah, I am sorry. I tried again with the fix, It worked as expected! Thank you ๐ŸŒŸ ๐ŸŽ‰

from release-notes-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.