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

feat: Customizing code convention #60

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,33 @@ template = "keepachangelog"
version-regex = "^## \\\\[(?P<version>v?[^\\\\]]+)"
```

## Custom types

Configuration files offer greater flexibility compared to using CLI arguments.
You can overwrite default types with the `rewrite-convention` parameter.

In that case `sections` is required and `minor-types` is strongly recommended.

This can be useful for custom conventions or translating sections in your changelog.

```toml
[tool.git-changelog]
convention = "conventional"
sections = "build,chore,doc,n,feat"
minor-types = "feat,n"
...

[tool.git-changelog.rewrite-convention]
build = "Build"
chore = "Chore"
ci = "Continuous Integration"
deps = "Dependencies"
doc = "Documentation"
feat = "Features"
n = "Notes"

```

[keepachangelog]: https://keepachangelog.com/en/1.0.0/
[conventional-commit]: https://www.conventionalcommits.org/en/v1.0.0-beta.4/
[jinja]: https://jinja.palletsprojects.com/en/3.1.x/
Expand Down
9 changes: 9 additions & 0 deletions src/git_changelog/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def __init__(
sections: list[str] | None = None,
bump_latest: bool = False,
bump: str | None = None,
rewrite_convention: dict[str, str] | None = None,
minor_types: str | None = None,
):
"""Initialization method.

Expand All @@ -182,6 +184,9 @@ def __init__(
sections: The sections to render (features, bug fixes, etc.).
bump_latest: Deprecated, use `bump="auto"` instead. Whether to try and bump latest version to guess new one.
bump: Whether to try and bump to a given version.
rewrite_convention: A dictionary mapping type to section, intended to modify the default convention.TYPES.
If provided, the 'sections' argument becomes mandatory.
minor_types: Types signifying a minor version change. String separated by commas.
"""
self.repository: str | Path = repository
self.parse_provider_refs: bool = parse_provider_refs
Expand Down Expand Up @@ -209,6 +214,10 @@ def __init__(
# set convention
if isinstance(convention, str):
try:
if rewrite_convention:
self.CONVENTION[convention].replace_types(rewrite_convention)
if minor_types:
self.CONVENTION[convention].update_minor_list(minor_types)
convention = self.CONVENTION[convention]()
except KeyError:
print( # noqa: T201
Expand Down
13 changes: 13 additions & 0 deletions src/git_changelog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"sections": None,
"template": "keepachangelog",
"version_regex": DEFAULT_VERSION_REGEX,
"rewrite_convention": None,
"minor_types": None,
}


Expand Down Expand Up @@ -440,6 +442,8 @@ def build_and_render(
omit_empty_versions: bool = False, # noqa: FBT001,FBT002
provider: str | None = None,
bump: str | None = None,
rewrite_convention: dict | None = None,
minor_types: str | None = None,
) -> tuple[Changelog, str]:
"""Build a changelog and render it.

Expand All @@ -462,6 +466,9 @@ def build_and_render(
omit_empty_versions: Whether to omit empty versions from the output.
provider: Provider class used by this repository.
bump: Whether to try and bump to a given version.
rewrite_convention: A dictionary mapping type to section, intended to modify the default convention.TYPES.
If provided, the 'sections' argument becomes mandatory.
minor_types: Types signifying a minor version change. String separated by commas.

Raises:
ValueError: When some arguments are incompatible or missing.
Expand All @@ -486,6 +493,10 @@ def build_and_render(
if in_place and output is sys.stdout:
raise ValueError("Cannot write in-place to stdout")

if rewrite_convention and not sections:
raise ValueError("When using 'rewrite-convention', please specify the "
"sections you wish to render, e.g., sections='feat,docs'.")

# get provider
provider_class = providers[provider] if provider else None

Expand All @@ -504,6 +515,8 @@ def build_and_render(
parse_trailers=parse_trailers,
sections=sections,
bump=bump,
rewrite_convention=rewrite_convention,
minor_types=minor_types,
)

# remove empty versions from changelog data
Expand Down
87 changes: 55 additions & 32 deletions src/git_changelog/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,38 @@ def _format_sections_help(cls) -> str:
""",
)

def is_minor(self, commit_type: str) -> bool:
"""Tell if this commit is worth a minor bump.

Arguments:
commit_type: The commit type.

Returns:
Whether it's a minor commit.
"""
return commit_type in [self.TYPES[t] for t in self.MINOR_TYPES if t in self.TYPES]

@classmethod
def replace_types(cls, types: dict[str, str]) -> None:
"""Replace default TYPES with dict.

Arguments:
types: Dict with custom types.
"""
cls.TYPES = types

@classmethod
def update_minor_list(cls, commit_type: str) -> None:
"""Updates the MINOR_TYPES class variable with the provided comma-separated commit types.

Arguments:
commit_type (str): A comma-separated string of commit types to be considered as minor.

Example:
CommitConvention.update_minor_list("feat,fix,update")
"""
cls.MINOR_TYPES = commit_type.split(",")


class BasicConvention(CommitConvention):
"""Basic commit message convention."""
Expand All @@ -210,8 +242,7 @@ class BasicConvention(CommitConvention):
"merge": "Merged",
"doc": "Documented",
}

TYPE_REGEX: ClassVar[Pattern] = re.compile(r"^(?P<type>(%s))" % "|".join(TYPES.keys()), re.I)
MINOR_TYPES: ClassVar[list] = ["add"]
BREAK_REGEX: ClassVar[Pattern] = re.compile(
r"^break(s|ing changes?)?[ :].+$",
re.I | re.MULTILINE,
Expand All @@ -223,6 +254,11 @@ class BasicConvention(CommitConvention):
TYPES["remove"],
]

@property
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the CommitConvention classes utilize class attributes (rather than instance attributes), the process becomes slightly intricate. When updating self.TYPES, it's crucial for the regex to be updated as well. Consequently, we're presented with two options: either use a property or relocate it under __init__. I'm not sure which choice is superior.

def type_regex(self) -> re.Pattern:
"""Type regex."""
return re.compile(r"^(?P<type>(%s))" % "|".join(self.TYPES.keys()), re.I)

def parse_commit(self, commit: Commit) -> dict[str, str | bool]: # noqa: D102
commit_type = self.parse_type(commit.subject)
message = "\n".join([commit.subject, *commit.body])
Expand All @@ -246,22 +282,11 @@ def parse_type(self, commit_subject: str) -> str:
Returns:
The commit type.
"""
type_match = self.TYPE_REGEX.match(commit_subject)
type_match = self.type_regex.match(commit_subject)
if type_match:
return self.TYPES.get(type_match.groupdict()["type"].lower(), "")
return ""

def is_minor(self, commit_type: str) -> bool:
"""Tell if this commit is worth a minor bump.

Arguments:
commit_type: The commit type.

Returns:
Whether it's a minor commit.
"""
return commit_type == self.TYPES["add"]

def is_major(self, commit_message: str) -> bool:
"""Tell if this commit is worth a major bump.

Expand Down Expand Up @@ -294,9 +319,7 @@ class AngularConvention(CommitConvention):
"test": "Tests",
"tests": "Tests",
}
SUBJECT_REGEX: ClassVar[Pattern] = re.compile(
r"^(?P<type>(%s))(?:\((?P<scope>.+)\))?: (?P<subject>.+)$" % ("|".join(TYPES.keys())), # (%)
)
MINOR_TYPES: ClassVar[list] = ["feat"]
BREAK_REGEX: ClassVar[Pattern] = re.compile(
r"^break(s|ing changes?)?[ :].+$",
re.I | re.MULTILINE,
Expand All @@ -309,6 +332,13 @@ class AngularConvention(CommitConvention):
TYPES["perf"],
]

@property
def subject_regex(self) -> re.Pattern:
"""Subject regex."""
return re.compile(
r"^(?P<type>(%s))(?:\((?P<scope>.+)\))?: (?P<subject>.+)$" % ("|".join(self.TYPES.keys())), # (%)
)

def parse_commit(self, commit: Commit) -> dict[str, str | bool]: # noqa: D102
subject = self.parse_subject(commit.subject)
message = "\n".join([commit.subject, *commit.body])
Expand All @@ -334,24 +364,13 @@ def parse_subject(self, commit_subject: str) -> dict[str, str]:
Returns:
The parsed data.
"""
subject_match = self.SUBJECT_REGEX.match(commit_subject)
subject_match = self.subject_regex.match(commit_subject)
if subject_match:
dct = subject_match.groupdict()
dct["type"] = self.TYPES[dct["type"]]
return dct
return {"type": "", "scope": "", "subject": commit_subject}

def is_minor(self, commit_type: str) -> bool:
"""Tell if this commit is worth a minor bump.

Arguments:
commit_type: The commit type.

Returns:
Whether it's a minor commit.
"""
return commit_type == self.TYPES["feat"]

def is_major(self, commit_message: str) -> bool:
"""Tell if this commit is worth a major bump.

Expand All @@ -369,9 +388,13 @@ class ConventionalCommitConvention(AngularConvention):

TYPES: ClassVar[dict[str, str]] = AngularConvention.TYPES
DEFAULT_RENDER: ClassVar[list[str]] = AngularConvention.DEFAULT_RENDER
SUBJECT_REGEX: ClassVar[Pattern] = re.compile(
r"^(?P<type>(%s))(?:\((?P<scope>.+)\))?(?P<breaking>!)?: (?P<subject>.+)$" % ("|".join(TYPES.keys())), # (%)
)

@property
def subject_regex(self) -> re.Pattern:
"""Subject regex."""
return re.compile(
r"^(?P<type>(%s))(?:\((?P<scope>.+)\))?(?P<breaking>!)?: (?P<subject>.+)$" % ("|".join(self.TYPES.keys())), # (%)
)

def parse_commit(self, commit: Commit) -> dict[str, str | bool]: # noqa: D102
subject = self.parse_subject(commit.subject)
Expand Down
56 changes: 55 additions & 1 deletion tests/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

import pytest

from git_changelog.commit import Commit
from git_changelog.commit import (
AngularConvention,
BasicConvention,
Commit,
ConventionalCommitConvention,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -35,3 +40,52 @@ def test_parsing_trailers(body: str, expected_trailers: dict[str, str]) -> None:
parse_trailers=True,
)
assert commit.trailers == expected_trailers


@pytest.fixture()
def _reserve_types() -> None:
"""Fixture to preserve the conventional types."""
original_types = {
AngularConvention: [dict(AngularConvention.TYPES), list(AngularConvention.MINOR_TYPES)],
BasicConvention: [dict(BasicConvention.TYPES), list(BasicConvention.MINOR_TYPES)],
ConventionalCommitConvention: [dict(ConventionalCommitConvention.TYPES), list(ConventionalCommitConvention.MINOR_TYPES)],
}
yield
AngularConvention.TYPES = original_types[AngularConvention][0]
BasicConvention.TYPES = original_types[BasicConvention][0]
ConventionalCommitConvention.TYPES = original_types[ConventionalCommitConvention][0]
AngularConvention.MINOR_TYPES = original_types[AngularConvention][1]
BasicConvention.MINOR_TYPES = original_types[BasicConvention][1]
ConventionalCommitConvention.MINOR_TYPES = original_types[ConventionalCommitConvention][1]


@pytest.mark.usefixtures("_reserve_types")
def test_replace_types() -> None:
"""Test that the TYPES attribute is replaced correctly in various conventions."""
_new_types = {"n": "Notes", "o": "Other", "d": "Draft"}
for convention in [AngularConvention, BasicConvention, ConventionalCommitConvention]:
assert _new_types != convention.TYPES
convention.replace_types(_new_types)
assert _new_types == convention.TYPES


@pytest.mark.usefixtures("_reserve_types")
def test_is_minor_works_with_custom_minor_types() -> None:
"""Test that custom minor types are correctly recognized as minor changes."""
_new_types = {"n": "Notes", "o": "Other", "d": "Draft"}
_minor_types = "n,o"
for convention in [AngularConvention, BasicConvention, ConventionalCommitConvention]:
subject = "n: Added a new feature"
commit = Commit(
commit_hash="aaaaaaa",
subject=subject,
body=[""],
author_date="1574340645",
committer_date="1574340645",
)
convention.replace_types(_new_types)
convention.update_minor_list(_minor_types)
commit_dict = convention().parse_commit(commit)
assert not commit_dict["is_major"]
assert commit_dict["is_minor"]
assert not commit_dict["is_patch"]
Loading