Skip to content

Commit

Permalink
Change: Much more flexible Conventional Commits support (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself authored Mar 3, 2023
1 parent a188538 commit 7176fa3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 25 deletions.
10 changes: 4 additions & 6 deletions changelog.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
commit_types = [
{ message = "^add", group = "Added"},
{ message = "^remove", group = "Removed"},
{ message = "^change", group = "Changed"},
{ message = "^fix", group = "Bug Fixes"},
{ message = "add", group = "Added"},
{ message = "remove", group = "Removed"},
{ message = "change", group = "Changed"},
{ message = "fix", group = "Bug Fixes"},
]

changelog_dir = "changelog"
30 changes: 23 additions & 7 deletions pontos/changelog/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
ADDRESS = "https://github.com/"

DEFAULT_CHANGELOG_CONFIG = """commit_types = [
{ message = "^add", group = "Added"},
{ message = "^remove", group = "Removed"},
{ message = "^change", group = "Changed"},
{ message = "^fix", group = "Bug Fixes"},
{ message = "add", group = "Added"},
{ message = "remove", group = "Removed"},
{ message = "change", group = "Changed"},
{ message = "fix", group = "Bug Fixes"},
]
"""

JIRA_ID = r"(\s?/?\[?[A-Z0-9]+[\- ][0-9]{1,5}\]?\s?/?)?"


class ChangelogBuilder:
"""
Expand Down Expand Up @@ -162,14 +164,15 @@ def _sort_commits(self, commits: List[str]) -> Dict[str, List[str]]:
commit_link = f"{ADDRESS}{self.space}/{self.project}/commit/"

commit_dict = {}

self._prepare_regexs([t["message"] for t in commit_types])
if commits and len(commits) > 0:
for commit in commits:
commit = commit.split(" ", maxsplit=1)
for commit_type in commit_types:
reg = re.compile(
rf'{commit_type["message"]}\s?[:|-]', flags=re.I
match = self._check_for_conventional_commit(
commit_type["message"], commit[1]
)
match = reg.match(commit[1])
if match:
if commit_type["group"] not in commit_dict:
commit_dict[commit_type["group"]] = []
Expand All @@ -185,6 +188,19 @@ def _sort_commits(self, commits: List[str]) -> Dict[str, List[str]]:

return commit_dict

def _prepare_regexs(self, commit_types: List[str]) -> None:
# only compile these regexes once
self.cc_regexes: Dict[str, re.Pattern] = {}
for commit_type in commit_types:
self.cc_regexes[commit_type] = re.compile(
rf"{JIRA_ID}{commit_type}{JIRA_ID}\s??[:|-]?\s", flags=re.I
)

def _check_for_conventional_commit(
self, commit_type: str, commit_msg: str
) -> Optional[re.Match]:
return self.cc_regexes[commit_type].match(commit_msg)

def _build_changelog(
self,
last_version: Optional[str],
Expand Down
16 changes: 7 additions & 9 deletions tests/changelog/changelog.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
commit_types = [
{ message = "^add", group = "Added"},
{ message = "^remove", group = "Removed"},
{ message = "^change", group = "Changed"},
{ message = "^fix", group = "Bug Fixes"},
{ message = "^doc", group = "Documentation"},
{ message = "^refactor", group = "Refactor"},
{ message = "^test", group = "Testing"},
{ message = "add", group = "Added"},
{ message = "remove", group = "Removed"},
{ message = "change", group = "Changed"},
{ message = "fix", group = "Bug Fixes"},
{ message = "doc", group = "Documentation"},
{ message = "refactor", group = "Refactor"},
{ message = "test", group = "Testing"},
]

changelog_dir = "changelog"
56 changes: 53 additions & 3 deletions tests/changelog/test_conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=protected-access

import re
import unittest
from dataclasses import dataclass
from datetime import datetime
Expand Down Expand Up @@ -63,6 +66,7 @@ def test_changelog_builder(self, git_mock: MagicMock):
## Added
* foo bar [1234567](https://github.com/foo/bar/commit/1234567)
* bar baz [8abcdef](https://github.com/foo/bar/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/foo/bar/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/foo/bar/commit/42a42a4)
Expand Down Expand Up @@ -152,6 +156,7 @@ def test_changelog_builder_no_last_version(self, git_mock: MagicMock):
## Added
* foo bar [1234567](https://github.com/foo/bar/commit/1234567)
* bar baz [8abcdef](https://github.com/foo/bar/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/foo/bar/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/foo/bar/commit/42a42a4)
Expand Down Expand Up @@ -208,6 +213,7 @@ def test_changelog_builder_no_next_version(self, git_mock: MagicMock):
## Added
* foo bar [1234567](https://github.com/foo/bar/commit/1234567)
* bar baz [8abcdef](https://github.com/foo/bar/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/foo/bar/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/foo/bar/commit/42a42a4)
Expand Down Expand Up @@ -268,6 +274,7 @@ def test_changelog_builder_no_next_and_last_version(
## Added
* foo bar [1234567](https://github.com/foo/bar/commit/1234567)
* bar baz [8abcdef](https://github.com/foo/bar/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/foo/bar/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/foo/bar/commit/42a42a4)
Expand Down Expand Up @@ -371,6 +378,7 @@ def test_changelog_builder_with_default_changelog_config(
## Added
* foo bar [1234567](https://github.com/foo/bar/commit/1234567)
* bar baz [8abcdef](https://github.com/foo/bar/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/foo/bar/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/foo/bar/commit/42a42a4)
Expand Down Expand Up @@ -421,6 +429,7 @@ def test_changelog_builder_with_empty_git_tag_prefix(
## Added
* foo bar [1234567](https://github.com/foo/bar/commit/1234567)
* bar baz [8abcdef](https://github.com/foo/bar/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/foo/bar/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/foo/bar/commit/42a42a4)
Expand Down Expand Up @@ -451,9 +460,9 @@ def test_write_changelog_to_file(self, git_mock: MagicMock):

git_mock.return_value.list_tags.return_value = ["v0.0.1"]
git_mock.return_value.log.return_value = [
"1234567 Add: foo bar",
"8abcdef Add: bar baz",
"8abcd3f Add bar baz",
"1234567 TXT-123 Add: foo bar",
"8abcdef [XC-33] Add: bar baz",
"8abcd3f Add/TT 55 bar baz",
"8abcd3d Adding bar baz",
"1337abc Change: bar to baz",
"42a42a4 Remove: foo bar again",
Expand All @@ -468,6 +477,7 @@ def test_write_changelog_to_file(self, git_mock: MagicMock):
## Added
* foo bar [1234567](https://github.com/foo/bar/commit/1234567)
* bar baz [8abcdef](https://github.com/foo/bar/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/foo/bar/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/foo/bar/commit/42a42a4)
Expand Down Expand Up @@ -505,3 +515,43 @@ def test_write_changelog_to_file(self, git_mock: MagicMock):
git_mock.return_value.log.assert_called_once_with(
"v0.0.1..HEAD", oneline=True
)

def test_regex(self):
own_path = Path(__file__).absolute().parent
config_toml = own_path / "changelog.toml"

changelog_builder = ChangelogBuilder(
space="foo", project="bar", config=config_toml
)
commit_types = changelog_builder.config.get("commit_types")
changelog_builder._prepare_regexs([t["message"] for t in commit_types])

commit_messages = [
"TEST-3 Add: blah",
"BLUBB-123 Change: blah",
"TEST 123 Add: blah",
"Add/TEST-123: blah",
"TEST 123 Fix blah",
"TE5T-23 Fix blah",
"Fix - blah",
"Fix : blah",
"Fix: blah",
"Fix| blah",
"[TEST-123] Add: blah",
"TEST-123/Add blah",
]
matches = len(commit_messages)
found = 0

for commit_message in commit_messages:
for commit_type in ["add", "fix", "change"]:
match = changelog_builder._check_for_conventional_commit(
commit_type=commit_type, commit_msg=commit_message
)
if match:
self.assertIsInstance(match, re.Match)
matched = True
found = found + 1
self.assertTrue(matched)
matched = False
self.assertEqual(matches, found)
1 change: 1 addition & 0 deletions tests/release/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ def test_release_with_changelog(
## Added
* foo bar [1234567](https://github.com/greenbone/foo/commit/1234567)
* bar baz [8abcdef](https://github.com/greenbone/foo/commit/8abcdef)
* bar baz [8abcd3f](https://github.com/greenbone/foo/commit/8abcd3f)
## Removed
* foo bar again [42a42a4](https://github.com/greenbone/foo/commit/42a42a4)
Expand Down

0 comments on commit 7176fa3

Please sign in to comment.