Skip to content

Commit 4f33d46

Browse files
authored
Merge pull request innogames#34 from seqizz/g_regexpcheck
Add optional branch name regexp check
2 parents 70b769a + 60d77f3 commit 4f33d46

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

‎README.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Features
4444
* Accept commits tagged as ``[HOTFIX]``, ``[MESS]``, ``[TEMP]``, or ``[WIP]``
4545
with issues
4646
* Check executable bits and shebangs
47+
* Check for allowed branch names
4748
* Check symlinks
4849
* Check CSS files with ``csslint``
4950
* Check Go files with ``golint``
@@ -210,6 +211,9 @@ Version 3.1
210211
Version 3.2
211212
* Reduce severity of length for merge commit summary to warning
212213

214+
Version 3.3
215+
* Add optional branch name check via regular expressions
216+
213217

214218
License
215219
-------

‎igcommit/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
Portions Copyright (c) 2021 Emre Hasegeli
55
"""
66

7-
VERSION= (3, 2)
7+
VERSION= (3, 3)

‎igcommit/commit_list_checks.py‎

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
Portions Copyright (c) 2021 Emre Hasegeli
55
"""
66

7+
importre
78
fromtimeimporttime
9+
fromos.pathimportexists
10+
fromosimportremove
811

912
fromigcommit.base_checkimportBaseCheck, Severity
10-
fromigcommit.gitimportCommit, CommitList
13+
fromigcommit.gitimportCommit, CommitList, CommittedFile
1114

1215

1316
classCommitListCheck(BaseCheck):
@@ -253,3 +256,97 @@ def check_contributor(self, contributor, commit):
253256
'the same name'
254257
.format(commit, contributor.email, other.email),
255258
)
259+
260+
261+
classCheckBranchNameRegexp(CommitListCheck):
262+
"""Check branch names against regular expressions
263+
264+
The configuration file can contain a list of regular expressions
265+
(one per line). The branch names are checked against those,
266+
and the commit is rejected if none of them match.
267+
"""
268+
269+
config_files= []
270+
271+
defprepare(self, obj):
272+
new=super(CheckBranchNameRegexp, self).prepare(obj)
273+
config_exists=new._prepare_configs(obj)
274+
ifconfig_exists:
275+
returnnew
276+
277+
def_prepare_configs(self, commitlist):
278+
"""Update used configuration files, return true if any exist
279+
280+
This is copied from file_checks.py and slightly modified.
281+
Please check the original file for explanation and
282+
shortcomings.
283+
"""
284+
config_exists=False
285+
forconfig_fileinself.config_files:
286+
prev_commit=config_file.commit
287+
# We will check if the config file exists on the latest commit
288+
config_file.commit=commitlist[-1]
289+
290+
ifconfig_file.exists():
291+
config_exists=True
292+
293+
# If the file is not changed on this commit, we can skip
294+
# downloading.
295+
ifnotprev_commitorconfig_file.changed():
296+
withopen(config_file.path, 'wb') asfd:
297+
fd.write(config_file.get_content())
298+
elifexists(config_file.path):
299+
# Not found on the latest commit, but it exists on the
300+
# workspace. Remove it.
301+
remove(config_file.path)
302+
303+
returnconfig_exists
304+
305+
defget_allowed_regexes(self):
306+
forconfig_fileinself.config_files:
307+
withopen(config_file.path) asf:
308+
allowed_regexes=f.read().splitlines()
309+
valid_list= []
310+
failed_list= []
311+
forregexlineinallowed_regexes:
312+
try:
313+
ifnotregexline.strip():
314+
continue
315+
re.compile(regexline)
316+
valid_list.append(regexline)
317+
exceptre.error:
318+
failed_list.append(regexline)
319+
returnvalid_list, failed_list
320+
321+
defcheck_branch_name(self, regexp_list, branch_name):
322+
"""Fails if the branch name does not match any regexp
323+
324+
It's enough to fit only one of the allowed patterns
325+
"""
326+
trimmed_branch_name=re.sub('refs/heads/', '', branch_name)
327+
forpatterninregexp_list:
328+
ifre.match(pattern, trimmed_branch_name):
329+
returnTrue
330+
returnFalse
331+
332+
defget_problems(self):
333+
branch_name=self.commit_list.branch_name
334+
valid_regexes, broken_regexes=self.get_allowed_regexes()
335+
ifbroken_regexes:
336+
yield (
337+
Severity.WARNING,
338+
'following string is not a valid pattern '
339+
'for branch name comparison:\n{}'
340+
.format("\n".join(broken_regexes))
341+
)
342+
ifnotself.check_branch_name(valid_regexes, branch_name):
343+
yield (
344+
Severity.ERROR,
345+
'{} is not an allowed branch name.\n'
346+
'Branch names must match any of the following '
347+
'list of regular expressions:\n{}'
348+
.format(
349+
branch_name,
350+
'\n'.join(valid_regexes)
351+
)
352+
)

‎igcommit/config.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
CheckDuplicateCommitSummaries,
2020
CheckMisleadingMergeCommit,
2121
CheckTimestamps,
22+
CheckBranchNameRegexp,
2223
)
2324
fromigcommit.file_checksimport (
2425
CheckCommand,
@@ -35,6 +36,9 @@
3536
checks.append(CheckMisleadingMergeCommit())
3637
checks.append(CheckTimestamps())
3738
checks.append(CheckContributors())
39+
checks.append(CheckBranchNameRegexp(
40+
config_files=[CommittedFile('.igcommit-branch-name.conf')]
41+
))
3842

3943
# Commit checks
4044
checks.append(CheckCommitMessage())

0 commit comments

Comments
(0)