Skip to content

Commit

Permalink
CI: fix semantic release (#6)
Browse files Browse the repository at this point in the history
* MINOR: test

* Fix the semantic release
  • Loading branch information
RomainMou authored May 28, 2024
1 parent 17a3396 commit e65087c
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 39 deletions.
2 changes: 2 additions & 0 deletions ci-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-semantic-release==9.8.0
twine==5.1.0
56 changes: 56 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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>"

[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
125 changes: 93 additions & 32 deletions semantic_release_config/parser.py
Original file line number Diff line number Diff line change
@@ -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<type>\w+)?:?\s*(?P<subject>.+)")

# 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,
)
7 changes: 0 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -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:

0 comments on commit e65087c

Please sign in to comment.