semantic-release / commit-analyzer

:bulb: semantic-release plugin to analyze commits with conventional-changelog
MIT License
371 stars 74 forks source link

Can Commit-Analyzer handle 2 different commit structure? #294

Open isabr85 opened 2 years ago

isabr85 commented 2 years ago

Let's say I have a commit which look like that: fix: this is an example Commit-analyzer is detecting this commit and triggers a patch version - which is the expected behaviour.

Then I complete a PR which creates a new commit: Merged PR 11111: fix: this is another example Commit-analyzer can't detect the commit fix: this is another example because of the prefix that Azure Devops adds to the commit it generates when completing a PR (Merged PR 1111:). So I have found the mergePattern property inside parserOpts and I added a regex that inform commit-analyzer about the prefix:

parserOpts: {
   mergePattern: /^Merged PR (\d+): (\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)((.|\n)*)/,  
   mergeCorrespondence: ['id', 'type', 'scope', 'subject'],
   noteKeywords: ["BREAKING CHANGE", "BREAKING CHANGES"]
}

Now commit-analyzer has been able to detect the relevant commit content within this commit structure.

My problem is that now, with the mergePattern attribute defined to look for specific structure - a regular commit such as the first example fix: this is an example won't be considered as a valid commit and no version will be triggered.

There is an option to support both types of commits? with and without Merged PR #####: prefix?

Thanks.

DavidNorena commented 10 months ago

Hi, for future developers / devops engineers trying to figure out this same situation here is what I found:

When you merge the Azure Devops PRs the commit message is as follows:

    Merged PR 257: feat: add terraform support

    # Description

    Added initial support for terraform

    # Checklist

    ... etc ...

If you analyze the code of the conventional-commits-parser and read the documentation about the mergePattern you will understand that even when you match the first line with the mergePattern, the code tries to parse the next line to get the header information, so the default logic is not friendly with this situation.

So one way to solve it is to use part of the regex you were using with the mergePattern but with a little modification so the Prefix (Merged PR xxx) is optional, and fixing the line breaks at the end of the line to be optional as well (and with support for windows or linux file formats):

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

But another way to think about it is just to modify a little bit the default headerPattern so both the standard commit messages and the ones with the Azure Devops prefix will work: (similar to the previous one but with less configuration)

"plugins": [
    [
      "@semantic-release/commit-analyzer",
      {
        "parserOpts": {
          "headerPattern": "^(?:Merged PR \\d+:\\s)?(\\w*)(?:\\(([\\w\\$\\.\\-\\* ]*)\\))?:(.*)(?:\\r?\\n|$)"
        }
      }
    ]
  ]

Hope it helps people out there with this issue.

@travi I believe this can be closed now, thanks.

SalahAdDin commented 7 months ago

@DavidNorena It didn't work for me in any case.