blob: 5c4a150cbd3eec9f1f246ea7e4dab1ee2db03685 (
plain)
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
|
import re
from pathlib import Path
from typing import Tuple
from gitlint.rules import CommitRule, RuleViolation
class AllowedAuthors(CommitRule):
"""Enforce that only authors listed in the AUTHORS file are allowed to commit."""
authors_file_names = ("AUTHORS", "AUTHORS.txt", "AUTHORS.md")
parse_authors = re.compile(r"^(?P<name>.*) <(?P<email>.*)>$", re.MULTILINE)
name = "contrib-allowed-authors"
id = "CC3"
@classmethod
def _read_authors_from_file(cls, git_ctx) -> Tuple[str, str]:
for file_name in cls.authors_file_names:
path = Path(git_ctx.repository_path) / file_name
if path.exists():
authors_file = path
break
else:
raise FileNotFoundError("No AUTHORS file found!")
authors_file_content = authors_file.read_text("utf-8")
authors = re.findall(cls.parse_authors, authors_file_content)
return set(authors), authors_file.name
def validate(self, commit):
registered_authors, authors_file_name = AllowedAuthors._read_authors_from_file(commit.message.context)
author = (commit.author_name, commit.author_email.lower())
if author not in registered_authors:
return [
RuleViolation(
self.id,
f"Author not in '{authors_file_name}' file: " f'"{commit.author_name} <{commit.author_email}>"',
)
]
return []
|