|
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 | +""" |
4 | 4 |
|
| 5 | +from __future__ import annotations |
5 | 6 |
|
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]): |
7 | 45 | """ |
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 |
14 | 47 | """ |
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 | | - ) |
34 | 48 |
|
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] |
37 | 63 |
|
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 | + ) |
0 commit comments