|
4 | 4 | Portions Copyright (c) 2021 Emre Hasegeli |
5 | 5 | """ |
6 | 6 |
|
| 7 | +importre |
7 | 8 | fromtimeimporttime |
| 9 | +fromos.pathimportexists |
| 10 | +fromosimportremove |
8 | 11 |
|
9 | 12 | fromigcommit.base_checkimportBaseCheck, Severity |
10 | | -fromigcommit.gitimportCommit, CommitList |
| 13 | +fromigcommit.gitimportCommit, CommitList, CommittedFile |
11 | 14 |
|
12 | 15 |
|
13 | 16 | classCommitListCheck(BaseCheck): |
@@ -253,3 +256,97 @@ def check_contributor(self, contributor, commit): |
253 | 256 | 'the same name' |
254 | 257 | .format(commit, contributor.email, other.email), |
255 | 258 | ) |
| 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 | + ) |
0 commit comments