Coverage for ghtc/overrides.py: 93%
46 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-10-08 10:59 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-10-08 10:59 +0000
1from typing import Dict, Optional
2import os
3import re
4from ghtc.models import ConventionalCommitMessage
5from ghtc.parser import parse
6import mflog
8GIT_COMMIT_DELIMITER_REGEX = r"^\[([0-9a-f]{5,40})\]$"
9GIT_COMMIT_DELIMITER_COMPILED_REGEX = re.compile(GIT_COMMIT_DELIMITER_REGEX)
10LOGGER = mflog.get_logger("ghtc.overrides")
13class Overrides:
14 def __init__(self, path):
15 self.path = path
16 self.commits: Dict[str, Optional[ConventionalCommitMessage]] = {}
18 def parse(self):
19 if not os.path.isfile(self.path):
20 return
21 with open(self.path, "r") as f:
22 commit: Optional[str] = None
23 commit_message: Optional[str] = None
24 for tmp in f.readlines():
25 line = tmp.strip()
26 if commit is None and len(line) == 0:
27 continue
28 match = GIT_COMMIT_DELIMITER_COMPILED_REGEX.match(line)
29 if match is None:
30 if commit is None:
31 LOGGER.warning("badly formatted overrides file => ignoring")
32 return False
33 if commit_message is None:
34 if len(line) > 0:
35 commit_message = line
36 else:
37 commit_message = commit_message + "\n" + line
38 else:
39 if commit is not None:
40 self.commits[commit] = self._parse(commit, commit_message)
41 commit = match[1]
42 commit_message = None
43 if commit is not None:
44 self.commits[commit] = self._parse(commit, commit_message)
45 return True
47 def _parse(self, commit, commit_message) -> Optional[ConventionalCommitMessage]:
48 res: Optional[ConventionalCommitMessage] = None
49 if commit_message is not None:
50 res = parse(commit_message)
51 if res is None:
52 LOGGER.warning(
53 f"can't parse overriden commit "
54 f"message for commit: {commit} => ignoring"
55 )
56 return res