"""
Angular commit style parser
https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines
"""
from __future__ import annotations
import logging
import re
from functools import reduce
from itertools import zip_longest
from re import compile as regexp
from typing import TYPE_CHECKING, Tuple
from pydantic.dataclasses import dataclass
from semantic_release.commit_parser._base import CommitParser, ParserOptions
from semantic_release.commit_parser.token import (
ParsedCommit,
ParsedMessageResult,
ParseError,
ParseResult,
)
from semantic_release.commit_parser.util import (
breaking_re,
parse_paragraphs,
sort_numerically,
)
from semantic_release.enums import LevelBump
from semantic_release.errors import InvalidParserOptions
if TYPE_CHECKING: # pragma: no cover
from git.objects.commit import Commit
logger = logging.getLogger(__name__)
def _logged_parse_error(commit: Commit, error: str) -> ParseError:
logger.debug(error)
return ParseError(commit, error=error)
# TODO: Remove from here, allow for user customization instead via options
# types with long names in changelog
LONG_TYPE_NAMES = {
"build": "build system",
"ci": "continuous integration",
"chore": "chores",
"docs": "documentation",
"feat": "features",
"fix": "bug fixes",
"perf": "performance improvements",
"refactor": "refactoring",
"style": "code style",
"test": "testing",
}
[docs]
@dataclass
class AngularParserOptions(ParserOptions):
"""Options dataclass for AngularCommitParser"""
minor_tags: Tuple[str, ...] = ("feat",)
"""Commit-type prefixes that should result in a minor release bump."""
patch_tags: Tuple[str, ...] = ("fix", "perf")
"""Commit-type prefixes that should result in a patch release bump."""
other_allowed_tags: Tuple[str, ...] = (
"build",
"chore",
"ci",
"docs",
"style",
"refactor",
"test",
)
"""Commit-type prefixes that are allowed but do not result in a version bump."""
allowed_tags: Tuple[str, ...] = (
*minor_tags,
*patch_tags,
*other_allowed_tags,
)
"""
All commit-type prefixes that are allowed.
These are used to identify a valid commit message. If a commit message does not start with
one of these prefixes, it will not be considered a valid commit message.
"""
default_bump_level: LevelBump = LevelBump.NO_RELEASE
"""The minimum bump level to apply to valid commit message."""
@property
def tag_to_level(self) -> dict[str, LevelBump]:
"""A mapping of commit tags to the level bump they should result in."""
return self._tag_to_level
def __post_init__(self) -> None:
self._tag_to_level: dict[str, LevelBump] = {
str(tag): level
for tag, level in [
# we have to do a type ignore as zip_longest provides a type that is not specific enough
# for our expected output. Due to the empty second array, we know the first is always longest
# and that means no values in the first entry of the tuples will ever be a LevelBump. We
# apply a str() to make mypy happy although it will never happen.
*zip_longest(self.allowed_tags, (), fillvalue=self.default_bump_level),
*zip_longest(self.patch_tags, (), fillvalue=LevelBump.PATCH),
*zip_longest(self.minor_tags, (), fillvalue=LevelBump.MINOR),
]
if "|" not in str(tag)
}
[docs]
class AngularCommitParser(CommitParser[ParseResult, AngularParserOptions]):
"""
A commit parser for projects conforming to the angular style of conventional
commits. See https://www.conventionalcommits.org/en/v1.0.0-beta.4/
"""
# TODO: Deprecate in lieu of get_default_options()
parser_options = AngularParserOptions
def __init__(self, options: AngularParserOptions | None = None) -> None:
super().__init__(options)
try:
commit_type_pattern = regexp(
r"(?P<type>%s)" % str.join("|", self.options.allowed_tags)
)
except re.error as err:
raise InvalidParserOptions(
str.join(
"\n",
[
f"Invalid options for {self.__class__.__name__}",
"Unable to create regular expression from configured commit-types.",
"Please check the configured commit-types and remove or escape any regular expression characters.",
],
)
) from err
self.re_parser = regexp(
str.join(
"",
[
r"^" + commit_type_pattern.pattern,
r"(?:\((?P<scope>[^\n]+)\))?",
# TODO: remove ! support as it is not part of the angular commit spec (its part of conventional commits spec)
r"(?P<break>!)?:\s+",
r"(?P<subject>[^\n]+)",
r"(?:\n\n(?P<text>.+))?", # commit body
],
),
flags=re.DOTALL,
)
# GitHub & Gitea use (#123), GitLab uses (!123), and BitBucket uses (pull request #123)
self.mr_selector = regexp(
r"[\t ]+\((?:pull request )?(?P<mr_number>[#!]\d+)\)[\t ]*$"
)
self.issue_selector = regexp(
str.join(
"",
[
r"^(?:clos(?:e|es|ed|ing)|fix(?:es|ed|ing)?|resolv(?:e|es|ed|ing)|implement(?:s|ed|ing)?):",
r"[\t ]+(?P<issue_predicate>.+)[\t ]*$",
],
),
flags=re.MULTILINE | re.IGNORECASE,
)
[docs]
@staticmethod
def get_default_options() -> AngularParserOptions:
return AngularParserOptions()
[docs]
def commit_body_components_separator(
self, accumulator: dict[str, list[str]], text: str
) -> dict[str, list[str]]:
if match := breaking_re.match(text):
accumulator["breaking_descriptions"].append(match.group(1) or "")
# TODO: breaking change v10, removes breaking change footers from descriptions
# return accumulator
elif match := self.issue_selector.search(text):
# if match := self.issue_selector.search(text):
predicate = regexp(r",? and | *[,;/& ] *").sub(
",", match.group("issue_predicate") or ""
)
# Almost all issue trackers use a number to reference an issue so
# we use a simple regexp to validate the existence of a number which helps filter out
# any non-issue references that don't fit our expected format
has_number = regexp(r"\d+")
new_issue_refs: set[str] = set(
filter(
lambda issue_str, validator=has_number: validator.search(issue_str), # type: ignore[arg-type]
predicate.split(","),
)
)
accumulator["linked_issues"] = sort_numerically(
set(accumulator["linked_issues"]).union(new_issue_refs)
)
# TODO: breaking change v10, removes resolution footers from descriptions
# return accumulator
# Prevent appending duplicate descriptions
if text not in accumulator["descriptions"]:
accumulator["descriptions"].append(text)
return accumulator
[docs]
def parse_message(self, message: str) -> ParsedMessageResult | None:
if not (parsed := self.re_parser.match(message)):
return None
parsed_break = parsed.group("break")
parsed_scope = parsed.group("scope")
parsed_subject = parsed.group("subject")
parsed_text = parsed.group("text")
parsed_type = parsed.group("type")
linked_merge_request = ""
if mr_match := self.mr_selector.search(parsed_subject):
linked_merge_request = mr_match.group("mr_number")
# TODO: breaking change v10, removes PR number from subject/descriptions
# expects changelog template to format the line accordingly
# parsed_subject = self.pr_selector.sub("", parsed_subject).strip()
body_components: dict[str, list[str]] = reduce(
self.commit_body_components_separator,
[
# Insert the subject before the other paragraphs
parsed_subject,
*parse_paragraphs(parsed_text or ""),
],
{
"breaking_descriptions": [],
"descriptions": [],
"linked_issues": [],
},
)
level_bump = (
LevelBump.MAJOR
# TODO: remove parsed break support as it is not part of the angular commit spec (its part of conventional commits spec)
if body_components["breaking_descriptions"] or parsed_break
else self.options.tag_to_level.get(
parsed_type, self.options.default_bump_level
)
)
return ParsedMessageResult(
bump=level_bump,
type=parsed_type,
category=LONG_TYPE_NAMES.get(parsed_type, parsed_type),
scope=parsed_scope,
descriptions=tuple(body_components["descriptions"]),
breaking_descriptions=tuple(body_components["breaking_descriptions"]),
linked_issues=tuple(body_components["linked_issues"]),
linked_merge_request=linked_merge_request,
)
# Maybe this can be cached as an optimization, similar to how
# mypy/pytest use their own caching directories, for very large commit
# histories?
# The problem is the cache likely won't be present in CI environments
[docs]
def parse(self, commit: Commit) -> ParseResult:
"""
Attempt to parse the commit message with a regular expression into a
ParseResult
"""
if not (pmsg_result := self.parse_message(str(commit.message))):
return _logged_parse_error(
commit, f"Unable to parse commit message: {commit.message!r}"
)
logger.debug(
"commit %s introduces a %s level_bump",
commit.hexsha[:8],
pmsg_result.bump,
)
return ParsedCommit.from_parsed_message_result(commit, pmsg_result)