mikepenz / release-changelog-builder-action

A GitHub action that builds your release notes / changelog fast, easy and exactly the way you want.
https://blog.mikepenz.dev
Apache License 2.0
702 stars 102 forks source link

[Question] Extract labels from PR title #1344

Closed sebastianrosch closed 1 month ago

sebastianrosch commented 2 months ago

I have tried lots of different configurations but can't figure out how to extract the label from the PR title. No matter what I do, I always get only uncategorized PRs.

Screenshot 2024-07-06 at 17 04 57 Screenshot 2024-07-06 at 17 10 26

The latest config I'm using looks like this:

 - name: Generate release changelog
        id: generate_release_changelog
        uses: mikepenz/release-changelog-builder-action@v4
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          configurationJson: |
            {
              "label_extractor": [
                {
                  "pattern": ".*(\\[Feature\\]|\\[Fix\\]|\\[Chore\\]|\\[Test\\]).*",
                  "on_property": "title",
                  "target": "$1"
                }
              ]
            }

I've looked at similar issues and found a reference to the test here: https://github.com/mikepenz/release-changelog-builder-action/blob/dffddcd83353ebb1c64f53b6cacfd6e24b707728/__tests__/transform.test.ts#L145

However, that's not working for me either.

Can you point out what I'm doing wrong?

mikepenz commented 2 months ago

Good day.

So before looking into the extractor itself, I'd like to point out that in your case you use the default configuration for categories, so chore would not be recognized as a category. The default categories are: https://github.com/mikepenz/release-changelog-builder-action/blob/v4.2.2/src/configuration.ts#L76-L93

That said you can probably use something like this instead: https://github.com/mikepenz/release-changelog-builder-action/issues/1172#issuecomment-1650493613 whichever is first in [] to be the label. that also scales better for the future.

Unless you already used that too?

sebastianrosch commented 1 month ago

Hi @mikepenz, thanks for the quick response.

I tried lots of different configurations and also defined them explicitly.

For example:

{
  "template": "#{{CHANGELOG}}\n\n<details>\n<summary>Uncategorized</summary>\n\n#{{UNCATEGORIZED}}\n</details>",
  "categories": [
    {
      "title": "## 🚀 Features",
      "labels": [
        "feat",
        "feature"
      ]
    },
    {
      "title": "## 🐛 Fixes",
      "labels": [
        "fix",
        "bug"
      ]
    },
    {
      "title": "## 🛠️ Chores",
      "labels": [
        "chore"
      ]
    },
    {
      "title": "## 🛠️ Dependencies",
      "labels": [
        "deps"
      ]
    }
  ],
  "label_extractor": [
    {
      "method": "match",
      "pattern": "^\\[(\\w*)\\]",
      "target": "$1",
      "on_property": "title"
    }
  ]
}

In my understanding of the regex, it should match Chore in the case of a PR titled [Chore] ..... I also tried defining the labels in the categories as title case. I've played around with the regex in regex tools to see if that works, then escaped the \. I still get all PRs as uncategorized. It seems like it reads the JSON correctly as the template looks different.

Am I not getting something regarding the escaping of the regex or the definition of the labels?

Screenshot 2024-07-08 at 11 24 23

Thanks!

mikepenz commented 1 month ago

Is the repo you run those on open source? Would allow me to reproduce it more quickly

sebastianrosch commented 1 month ago

The repos I'm trying to do this for are not, but I was able to reproduce it in this public repo: https://github.com/sebastianrosch/changelog-test

mikepenz commented 1 month ago

That is super helpful. Using your demo repo. Here's an adjusted config which should handle your use-case:

{
    "template": "#Changes: #{{CHANGELOG}}\n\n<details>\n<summary>Uncategorized</summary>\n\n#{{UNCATEGORIZED}}\n</details>",
    "categories": [
        {
            "title": "## 🚀 Features",
            "labels": [
                "[feat]",
                "[feature]",
                "[Feature"
            ]
        },
        {
            "title": "## 🐛 Fixes",
            "labels": [
                "[fix]",
                "[bug]",
                "[Fix]"
            ]
        },
        {
            "title": "## 🛠️ Chores",
            "labels": [
                "[chore]",
                "[Chore]"
            ]
        },
        {
            "title": "## 🛠️ Dependencies",
            "labels": [
                "[deps]"
            ]
        }
    ],
    "label_extractor": [
        {
            "method": "match",
            "pattern": "\\[.+?\\]",
            "on_property": "title"
        }
    ]
}

Key details:

That said, v5 of the action actually contains a feature that makes such regex's much simpler, by offering a regex match logic similar to what regexr does.

So for @v5 the following will work:

{
    "template": "#Changes: #{{CHANGELOG}}\n\n<details>\n<summary>Uncategorized</summary>\n\n#{{UNCATEGORIZED}}\n</details>",
    "categories": [
        {
            "title": "## 🚀 Features",
            "labels": [
                "feat",
                "feature",
                "Feature"
            ]
        },
        {
            "title": "## 🐛 Fixes",
            "labels": [
                "fix",
                "bug",
                "Fix"
            ]
        },
        {
            "title": "## 🛠️ Chores",
            "labels": [
                "chore",
                "Chore"
            ]
        },
        {
            "title": "## 🛠️ Dependencies",
            "labels": [
                "deps"
            ]
        }
    ],
    "label_extractor": [
        {
            "method": "regexr",
            "pattern": "\\[(.+?)\\].+",
            "target": "$1",
            "on_property": "title"
        }
    ]
}
sebastianrosch commented 1 month ago

Awesome, thank you, that worked! I've tried the regexr method before but didn't see that this was only supported in v5. Thanks for the help!