From 317a81c98e284308c07fdc48fb806f1bc2b07052 Mon Sep 17 00:00:00 2001 From: RomainMou <58464216+RomainMou@users.noreply.github.com> Date: Tue, 28 May 2024 21:42:47 +0200 Subject: [PATCH] Fix the semantic release --- ci-requirements.txt | 2 +- pyproject.toml | 56 +++++++++++++ semantic_release_config/parser.py | 125 ++++++++++++++++++++++-------- setup.cfg | 7 -- 4 files changed, 150 insertions(+), 40 deletions(-) create mode 100644 pyproject.toml diff --git a/ci-requirements.txt b/ci-requirements.txt index 156656e3..b25f8010 100644 --- a/ci-requirements.txt +++ b/ci-requirements.txt @@ -1,2 +1,2 @@ -python-semantic-release==8.3.0 +python-semantic-release==9.8.0 twine==5.1.0 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..33633195 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,56 @@ +[semantic_release] +assets = [] +build_command_env = [] +commit_message = "{version}\n\nAutomatically generated by python-semantic-release" +commit_parser = "semantic_release_config.parser:BimdataCommitParser" +logging_use_named_masks = false +major_on_zero = true +allow_zero_version = true +no_git_verify = false +tag_format = "v{version}" +version_variables = ["setup.py:VERSION"] + +[semantic_release.branches.main] +match = "(main|master)" +prerelease_token = "rc" +prerelease = false + +[semantic_release.changelog] +template_dir = "templates" +changelog_file = "CHANGELOG.md" +exclude_commit_patterns = [] + +[semantic_release.changelog.environment] +block_start_string = "{%" +block_end_string = "%}" +variable_start_string = "{{" +variable_end_string = "}}" +comment_start_string = "{#" +comment_end_string = "#}" +trim_blocks = false +lstrip_blocks = false +newline_sequence = "\n" +keep_trailing_newline = false +extensions = [] +autoescape = true + +[semantic_release.commit_author] +env = "GIT_COMMIT_AUTHOR" +default = "semantic-release " + +[semantic_release.commit_parser_options] +allowed_tags = ["PATCH", "MINOR", "MAJOR"] +major_tags = ["MAJOR"] +minor_tags = ["MINOR"] +patch_tags = ["PATCH"] + +[semantic_release.remote] +name = "origin" +token = { env = "GH_TOKEN" } +type = "github" +ignore_token_for_push = false +insecure = false + +[semantic_release.publish] +dist_glob_patterns = ["dist/*"] +upload_to_vcs_release = true diff --git a/semantic_release_config/parser.py b/semantic_release_config/parser.py index 7a7f879b..df74f800 100644 --- a/semantic_release_config/parser.py +++ b/semantic_release_config/parser.py @@ -1,38 +1,99 @@ -from semantic_release import UnknownCommitMessageStyleError -from semantic_release.settings import config -from semantic_release.history.parser_helpers import ParsedCommit +""" +BIMData.io commit style parser +""" +from __future__ import annotations -def parse_commit_message(message): +import logging +import re +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, ParseError, ParseResult +from semantic_release.enums import LevelBump +from semantic_release.commit_parser.util import parse_paragraphs + +if TYPE_CHECKING: + from git.objects.commit import Commit + +log = logging.getLogger(__name__) + + +def _logged_parse_error(commit: Commit, error: str) -> ParseError: + log.debug(error) + return ParseError(commit, error=error) + + +@dataclass +class BimdataParserOptions(ParserOptions): + """Options dataclass for AngularCommitParser""" + + allowed_tags: Tuple[str, ...] = ( + "MAJOR", + "MINOR", + "PATCH", + ) + major_tags: Tuple[str, ...] = ("MAJOR",) + minor_tags: Tuple[str, ...] = ("MINOR",) + patch_tags: Tuple[str, ...] = ("PATCH",) + default_bump_level: LevelBump = LevelBump.NO_RELEASE + + +class BimdataCommitParser(CommitParser[ParseResult, BimdataParserOptions]): """ - Parses a commit message according to the 1.0 version of python-semantic-release. It expects - a tag of some sort in the commit message and will use the rest of the first line as changelog - content. - :param message: A string of a commit message. - :raises semantic_release.UnknownCommitMessageStyle: If it does not recognise the commit style - :return: A tuple of (level to bump, type of change, scope of change, a tuple with descriptions) + BIMData.io parser """ - if config.get('minor_tag') in message: - level = 'feature' - level_bump = 2 - subject = message.replace(config.get('minor_tag'), '') - - elif config.get('fix_tag') in message: - level = 'fix' - level_bump = 1 - subject = message.replace(config.get('fix_tag'), '') - - elif config.get('major_tag') in message: - level = 'breaking' - level_bump = 3 - subject = message.replace(config.get('major_tag'), '') - - else: - raise UnknownCommitMessageStyleError( - 'Unable to parse the given commit message: {0}'.format(message) - ) - body = message - footer = message + parser_options = BimdataParserOptions + + def __init__(self, options: BimdataParserOptions | None = None) -> None: + super().__init__(options) + self.re_parser = re.compile(r"^(?P\w+)?:?\s*(?P.+)") + + # The problem is the cache likely won't be present in CI environments + def parse(self, commit: Commit) -> ParseResult: + """ + Attempt to parse the commit message with a regular expression into a + ParseResult + """ + message = str(commit.message) + subject = message.split("\n")[0] - return ParsedCommit(level_bump, level, None, (subject.strip(), body.strip(), footer.strip()), None) + parsed = self.re_parser.match(subject) + if not parsed: + return _logged_parse_error( + commit, f"Unable to parse the given commit message: {message}" + ) + parsed_type = parsed.group("type") + + level_bump = self.options.default_bump_level + level = "unknown" + if parsed_type in self.options.major_tags: + level_bump = LevelBump.MAJOR + level = "breaking" + elif parsed_type in self.options.minor_tags: + level_bump = LevelBump.MINOR + level = "feature" + elif parsed_type in self.options.patch_tags: + level_bump = LevelBump.PATCH + level = "fix" + else: + log.debug( + f"commit {commit.hexsha} didn't match any tag, use default {level_bump} level_bump" + ) + log.debug(f"commit {commit.hexsha} introduces a {level_bump} level_bump") + + descriptions = parse_paragraphs(message) + + return ParsedCommit( + bump=level_bump, + type=level, + scope="", + descriptions=descriptions, + breaking_descriptions=( + descriptions[1:] if level_bump is LevelBump.MAJOR else [] + ), + commit=commit, + ) diff --git a/setup.cfg b/setup.cfg index f545fcf3..11433ee8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,9 +1,2 @@ [flake8] max-line-length=99 - -[semantic_release] -version_variable = setup.py:VERSION -commit_parser=semantic_release_config.parser.parse_commit_message -minor_tag=MINOR: -fix_tag=PATCH: -major_tag=MAJOR: \ No newline at end of file