Repository navigation
Expand file tree
/
Copy pathrules.py
More file actions
185 lines (168 loc) · 5.79 KB
/
Copy pathrules.py
File metadata and controls
185 lines (168 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import re
from collections.abc import Iterable, Mapping
from string import Template
from typing import Any
from commitizen import git
from commitizen.cz.base import BaseCommitizen
from commitizen.cz.utils import required_validator
from commitizen.question import CzQuestion
from cz_kpn.consts import (
APP_NAME,
BREAK,
BREAK_DESCR,
BUMP_PATTERN,
COMMIT_PARSER,
COMMIT_PARSER_STRICT,
COMMIT_URL,
FIX,
FIX_DESCR,
NEW,
NEW_DESCR,
OPT,
OPT_DESCR,
STRICT_CHECK,
)
def _parse_subject(text: str) -> str:
value = text.strip(".").strip()
msg = ""
if not value:
msg = "Subject is required."
elif len(value) < 3:
msg = "Subject must be at least 3 characters long."
elif len(value) > 79:
msg = "Subject must be at most 79 characters long."
return required_validator(value, msg=msg)
class KPNCz(BaseCommitizen):
bump_pattern = BUMP_PATTERN
bump_map = {
"CHANGE": "MAJOR",
"BREAK": "MAJOR",
"NEW": "MINOR",
"OPT": "PATCH",
"FIX": "PATCH",
}
bump_map_major_version_zero = {
"CHANGE": "MINOR",
"BREAK": "MINOR",
"NEW": "MINOR",
"OPT": "PATCH",
"FIX": "PATCH",
}
changelog_pattern = BUMP_PATTERN
commit_parser = COMMIT_PARSER
change_type_map = {
"CHANGE": "BREAKING CHANGES",
"BREAK": "BREAKING CHANGES",
"NEW": "Features",
"OPT": "Improvements",
"FIX": "Fixes",
}
def parse_issue(self, text: str) -> str:
"""Parse issue ID.
If valid it's converted to uppercase.
If strict enabled, it's required.
"""
kpn_strict = self.config.settings.get(STRICT_CHECK, False)
if kpn_strict:
return required_validator(text.upper(), msg="Issue ID is required")
return text.upper()
def questions(self) -> list[CzQuestion]:
questions: list[CzQuestion] = [
{
"type": "list",
"name": "prefix",
"message": "Commit prefix?",
"choices": [
{"value": FIX, "name": f"{FIX} - {FIX_DESCR}"},
{"value": NEW, "name": f"{NEW} - {NEW_DESCR}"},
{"value": OPT, "name": f"{OPT} - {OPT_DESCR}"},
{"value": BREAK, "name": f"{BREAK} - {BREAK_DESCR}"},
],
},
{
"type": "input",
"name": "scope",
"message": "Scope or App name:\n",
},
{
"type": "input",
"name": "title",
"message": "Short description:\n",
"filter": _parse_subject,
},
{
"type": "input",
"name": "issue",
"message": "Issue ID:\n",
"filter": self.parse_issue,
},
{"type": "input", "name": "description", "message": "Long description:\n"},
]
return questions
def message(self, answers: Mapping[str, Any]) -> str:
prefix = answers["prefix"]
scope = answers["scope"]
title = answers["title"]
issue = answers["issue"]
description = answers["description"]
message = ""
if prefix and not scope:
message += f"{prefix}:"
elif prefix and scope:
message += f"{prefix}({scope}):"
if title:
message += f" {title}"
if issue:
message += f" (#{issue})"
if description:
message += f"\n.\n{description}"
return message
def example(self) -> str:
return (
"BREAK: Change foobar into quux (#XXX-2053)\n"
"\n"
"We changed foobar into quux because the backend changed their implementation.\n"
"This change will make the code more robust for future changes.\n"
)
def filter_commits(self, commits: list[git.GitCommit]) -> list[git.GitCommit]:
"""We use this hook to filter the commits which have the given app as the scope."""
app_name: str | None = self.config.settings.get(APP_NAME) # type: ignore
if not app_name:
return commits
kpn_strict = self.config.settings.get(STRICT_CHECK, False)
if kpn_strict:
raise RuntimeError("`kpn_app_name` only works with `kpn_strict = False`")
regex_pattern = self.schema_pattern()
pattern = re.compile(regex_pattern)
filtered_commits = []
for commit in commits:
match_result = pattern.match(commit.message)
if match_result:
scope = match_result.group("scope")
if app_name == scope:
filtered_commits.append(commit)
return filtered_commits
def schema(self) -> str:
return "<CHANGE_TYPE>(<SCOPE>): <SUBJECT> (#<ISSUE_ID>)\n\n<LONG_DESCRIPTION>"
def info(self) -> str:
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "cz_kpn_info.txt")
with open(filepath, "r") as f:
return f.read()
def changelog_message_builder_hook(
self, message: dict[str, Any], commit: git.GitCommit
) -> dict[str, Any]:
commit_url: str | None = self.config.settings.get(COMMIT_URL) # type: ignore
if commit_url:
t = Template(commit_url)
url = t.safe_substitute(COMMIT_REV=commit.rev)
short_rev = commit.rev[:7]
msg = message["message"]
message["message"] = f"{msg} ([{short_rev}]({url}))"
return message
def schema_pattern(self) -> str:
kpn_strict = self.config.settings.get(STRICT_CHECK, False)
if kpn_strict:
return COMMIT_PARSER_STRICT
return COMMIT_PARSER