Skip to content

feat(parser-conventional-monorepo): add new conventional-commits standard parser for monorepos #1143

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 50 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,22 @@ For more information see :ref:`commit-parsing`.
``commit_parser_options``
"""""""""""""""""""""""""

This section defines configuration options that modify commit parser options.

.. note::
**pyproject.toml:** ``[tool.semantic_release.commit_parser_options]``

**releaserc.toml:** ``[semantic_release.commit_parser_options]``

**releaserc.json:** ``{ "semantic_release": { "commit_parser_options": {} } }``

----

.. _config-commit_parser_options-types:

``allowed_types``/``minor_types``/``patch_types``
*************************************************

**Type:** ``dict[str, Any]``

This set of options are passed directly to the commit parser class specified in
Expand All @@ -822,6 +838,40 @@ For more information (to include defaults), see

----

.. _config-commit_parser_options-path_filters:

``path_filters``
***************************

**Type:** ``list[str]``

A set of relative paths to filter commits by. Only commits with file changes that
match these file paths or its subdirectories will be considered valid commits.

Syntax is similar to .gitignore with file path globs and inverse file match globs
via ``!`` prefix. Paths should be relative to the current working directory.

**Default:** ``["."]``

----

.. _config-commit_parser_options-scope_prefix:

``scope_prefix``
***************************

**Type:** ``str``

A prefix that will be striped from the scope when parsing commit messages.

If set, it will cause unscoped commits to be ignored. Use this in tandem with
the :ref:`config-commit_parser_options-path_filters` option to filter commits by
directory and scope.

**Default:** ``""``

----

.. _config-logging_use_named_masks:

``logging_use_named_masks``
Expand Down
4 changes: 3 additions & 1 deletion src/semantic_release/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from typing_extensions import Annotated, Self
from urllib3.util.url import parse_url

from semantic_release.commit_parser.conventional_monorepo import ConventionalCommitMonorepoParser
import semantic_release.hvcs as hvcs
from semantic_release.changelog.context import ChangelogMode
from semantic_release.changelog.template import environment
Expand Down Expand Up @@ -72,8 +73,9 @@ class HvcsClient(str, Enum):


_known_commit_parsers: Dict[str, type[CommitParser]] = {
"conventional": ConventionalCommitParser,
"angular": AngularCommitParser,
"conventional": ConventionalCommitParser,
"conventional-monorepo": ConventionalCommitMonorepoParser,
"emoji": EmojiCommitParser,
"scipy": ScipyCommitParser,
"tag": TagCommitParser,
Expand Down
11 changes: 11 additions & 0 deletions src/semantic_release/commit_parser/conventional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .options import ConventionalCommitParserOptions
from .options_monorepo import ConventionalMonorepoParserOptions
from .parser import ConventionalCommitParser
from .parser_monorepo import ConventionalCommitMonorepoParser

__all__ = [
"ConventionalCommitParser",
"ConventionalCommitParserOptions",
"ConventionalCommitMonorepoParser",
"ConventionalMonorepoParserOptions",
]
10 changes: 10 additions & 0 deletions src/semantic_release/commit_parser/conventional/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

from pydantic.dataclasses import dataclass

from semantic_release.commit_parser.angular import AngularParserOptions


@dataclass
class ConventionalCommitParserOptions(AngularParserOptions):
"""Options dataclass for the ConventionalCommitParser."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterable

from pydantic import Field, field_validator
from pydantic.dataclasses import dataclass

# typing_extensions is for Python 3.8, 3.9, 3.10 compatibility
from typing_extensions import Annotated

from .options import ConventionalCommitParserOptions

if TYPE_CHECKING: # pragma: no cover
pass


@dataclass
class ConventionalMonorepoParserOptions(ConventionalCommitParserOptions):
"""Options dataclass for ConventionalCommitMonorepoParser."""

path_filters: Annotated[tuple[Path, ...], Field(validate_default=True)] = (
Path("."),
)
"""
A set of relative paths to filter commits by. Only commits with file changes that
match these file paths or its subdirectories will be considered valid commits.

Syntax is similar to .gitignore with file path globs and inverse file match globs
via `!` prefix. Paths should be relative to the current working directory.
"""

scope_prefix: str = ""
"""
A prefix that will be striped from the scope when parsing commit messages.

If set, it will cause unscoped commits to be ignored. Use this in tandem with
the `path_filters` option to filter commits by directory and scope.
"""

@field_validator("path_filters", mode="before")
@classmethod
def convert_strs_to_paths(cls, value: Any) -> tuple[Path]:
values = value if isinstance(value, Iterable) else [value]
results = []

for val in values:
if isinstance(val, (str, Path)):
results.append(Path(val))
continue

raise TypeError(f"Invalid type: {type(val)}, expected str or Path.")

return tuple(results)

@field_validator("path_filters", mode="after")
@classmethod
def resolve_path(cls, dir_paths: tuple[Path, ...]) -> tuple[Path, ...]:
return tuple(
(
Path(f"!{Path(str_path[1:]).expanduser().absolute().resolve()}")
# maintains the negation prefix if it exists
if (str_path := str(path)).startswith("!")
# otherwise, resolve the path normally
else path.expanduser().absolute().resolve()
)
for path in dir_paths
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
from __future__ import annotations

from pydantic.dataclasses import dataclass

from semantic_release.commit_parser.angular import (
AngularCommitParser,
AngularParserOptions,
)


@dataclass
class ConventionalCommitParserOptions(AngularParserOptions):
"""Options dataclass for the ConventionalCommitParser."""
from semantic_release.commit_parser.angular import AngularCommitParser
from .options import ConventionalCommitParserOptions


class ConventionalCommitParser(AngularCommitParser):
Expand Down
Loading