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

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

Open
isabr85 opened this issue Dec 7, 2021 · 2 comments
Open

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

isabr85 opened this issue Dec 7, 2021 · 2 comments

Comments

@isabr85
Copy link

isabr85 commented Dec 7, 2021

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
Copy link

DavidNorena commented Dec 21, 2023

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
Copy link

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

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

No branches or pull requests

3 participants