Skip to content

Commit 0699c41

Browse files
authored
CI: Chores/fix semantic release (#7)
* MINOR: test * Fix the semantic release * fix build command.
1 parent 17a3396 commit 0699c41

5 files changed

Lines changed: 152 additions & 40 deletions

File tree

.github/workflows/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ jobs:
4444
run: |
4545
git config --global user.email "infra@bimdata.io"
4646
git config --global user.name "GA"
47-
PYTHONPATH=$PWD python setup.py sdist
4847
PYTHONPATH=$PWD semantic-release version
4948
PYTHONPATH=$PWD twine upload --non-interactive dist/*
5049
PYTHONPATH=$PWD semantic-release publish

ci-requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-semantic-release==9.8.0
2+
twine==5.1.0

pyproject.toml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[semantic_release]
2+
assets = []
3+
build_command = "python setup.py sdist"
4+
build_command_env = []
5+
commit_message = "{version}\n\nAutomatically generated by python-semantic-release"
6+
commit_parser = "semantic_release_config.parser:BimdataCommitParser"
7+
logging_use_named_masks = false
8+
major_on_zero = true
9+
allow_zero_version = true
10+
no_git_verify = false
11+
tag_format = "v{version}"
12+
version_variables = ["setup.py:VERSION"]
13+
14+
[semantic_release.branches.main]
15+
match = "(main|master)"
16+
prerelease_token = "rc"
17+
prerelease = false
18+
19+
[semantic_release.changelog]
20+
template_dir = "templates"
21+
changelog_file = "CHANGELOG.md"
22+
exclude_commit_patterns = []
23+
24+
[semantic_release.changelog.environment]
25+
block_start_string = "{%"
26+
block_end_string = "%}"
27+
variable_start_string = "{{"
28+
variable_end_string = "}}"
29+
comment_start_string = "{#"
30+
comment_end_string = "#}"
31+
trim_blocks = false
32+
lstrip_blocks = false
33+
newline_sequence = "\n"
34+
keep_trailing_newline = false
35+
extensions = []
36+
autoescape = true
37+
38+
[semantic_release.commit_author]
39+
env = "GIT_COMMIT_AUTHOR"
40+
default = "semantic-release <semantic-release>"
41+
42+
[semantic_release.commit_parser_options]
43+
allowed_tags = ["PATCH", "MINOR", "MAJOR"]
44+
major_tags = ["MAJOR"]
45+
minor_tags = ["MINOR"]
46+
patch_tags = ["PATCH"]
47+
48+
[semantic_release.remote]
49+
name = "origin"
50+
token = { env = "GH_TOKEN" }
51+
type = "github"
52+
ignore_token_for_push = false
53+
insecure = false
54+
55+
[semantic_release.publish]
56+
dist_glob_patterns = ["dist/*"]
57+
upload_to_vcs_release = true

semantic_release_config/parser.py

Lines changed: 93 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,99 @@
1-
from semantic_release import UnknownCommitMessageStyleError
2-
from semantic_release.settings import config
3-
from semantic_release.history.parser_helpers import ParsedCommit
1+
"""
2+
BIMData.io commit style parser
3+
"""
44

5+
from __future__ import annotations
56

6-
def parse_commit_message(message):
7+
import logging
8+
import re
9+
from typing import TYPE_CHECKING, Tuple
10+
11+
from pydantic.dataclasses import dataclass
12+
13+
from semantic_release.commit_parser._base import CommitParser, ParserOptions
14+
from semantic_release.commit_parser.token import ParsedCommit, ParseError, ParseResult
15+
from semantic_release.enums import LevelBump
16+
from semantic_release.commit_parser.util import parse_paragraphs
17+
18+
if TYPE_CHECKING:
19+
from git.objects.commit import Commit
20+
21+
log = logging.getLogger(__name__)
22+
23+
24+
def _logged_parse_error(commit: Commit, error: str) -> ParseError:
25+
log.debug(error)
26+
return ParseError(commit, error=error)
27+
28+
29+
@dataclass
30+
class BimdataParserOptions(ParserOptions):
31+
"""Options dataclass for AngularCommitParser"""
32+
33+
allowed_tags: Tuple[str, ...] = (
34+
"MAJOR",
35+
"MINOR",
36+
"PATCH",
37+
)
38+
major_tags: Tuple[str, ...] = ("MAJOR",)
39+
minor_tags: Tuple[str, ...] = ("MINOR",)
40+
patch_tags: Tuple[str, ...] = ("PATCH",)
41+
default_bump_level: LevelBump = LevelBump.NO_RELEASE
42+
43+
44+
class BimdataCommitParser(CommitParser[ParseResult, BimdataParserOptions]):
745
"""
8-
Parses a commit message according to the 1.0 version of python-semantic-release. It expects
9-
a tag of some sort in the commit message and will use the rest of the first line as changelog
10-
content.
11-
:param message: A string of a commit message.
12-
:raises semantic_release.UnknownCommitMessageStyle: If it does not recognise the commit style
13-
:return: A tuple of (level to bump, type of change, scope of change, a tuple with descriptions)
46+
BIMData.io parser
1447
"""
15-
if config.get('minor_tag') in message:
16-
level = 'feature'
17-
level_bump = 2
18-
subject = message.replace(config.get('minor_tag'), '')
19-
20-
elif config.get('fix_tag') in message:
21-
level = 'fix'
22-
level_bump = 1
23-
subject = message.replace(config.get('fix_tag'), '')
24-
25-
elif config.get('major_tag') in message:
26-
level = 'breaking'
27-
level_bump = 3
28-
subject = message.replace(config.get('major_tag'), '')
29-
30-
else:
31-
raise UnknownCommitMessageStyleError(
32-
'Unable to parse the given commit message: {0}'.format(message)
33-
)
3448

35-
body = message
36-
footer = message
49+
parser_options = BimdataParserOptions
50+
51+
def __init__(self, options: BimdataParserOptions | None = None) -> None:
52+
super().__init__(options)
53+
self.re_parser = re.compile(r"^(?P<type>\w+)?:?\s*(?P<subject>.+)")
54+
55+
# The problem is the cache likely won't be present in CI environments
56+
def parse(self, commit: Commit) -> ParseResult:
57+
"""
58+
Attempt to parse the commit message with a regular expression into a
59+
ParseResult
60+
"""
61+
message = str(commit.message)
62+
subject = message.split("\n")[0]
3763

38-
return ParsedCommit(level_bump, level, None, (subject.strip(), body.strip(), footer.strip()), None)
64+
parsed = self.re_parser.match(subject)
65+
if not parsed:
66+
return _logged_parse_error(
67+
commit, f"Unable to parse the given commit message: {message}"
68+
)
69+
parsed_type = parsed.group("type")
70+
71+
level_bump = self.options.default_bump_level
72+
level = "unknown"
73+
if parsed_type in self.options.major_tags:
74+
level_bump = LevelBump.MAJOR
75+
level = "breaking"
76+
elif parsed_type in self.options.minor_tags:
77+
level_bump = LevelBump.MINOR
78+
level = "feature"
79+
elif parsed_type in self.options.patch_tags:
80+
level_bump = LevelBump.PATCH
81+
level = "fix"
82+
else:
83+
log.debug(
84+
f"commit {commit.hexsha} didn't match any tag, use default {level_bump} level_bump"
85+
)
86+
log.debug(f"commit {commit.hexsha} introduces a {level_bump} level_bump")
87+
88+
descriptions = parse_paragraphs(message)
89+
90+
return ParsedCommit(
91+
bump=level_bump,
92+
type=level,
93+
scope="",
94+
descriptions=descriptions,
95+
breaking_descriptions=(
96+
descriptions[1:] if level_bump is LevelBump.MAJOR else []
97+
),
98+
commit=commit,
99+
)

setup.cfg

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
[flake8]
22
max-line-length=99
3-
4-
[semantic_release]
5-
version_variable = setup.py:VERSION
6-
commit_parser=semantic_release_config.parser.parse_commit_message
7-
minor_tag=MINOR:
8-
fix_tag=PATCH:
9-
major_tag=MAJOR:

0 commit comments

Comments
 (0)