diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-11-19 14:52:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-11-19 14:53:01 +0000 |
commit | f3b6c222fb11c96e2f8bbaa0622f46c8ec486874 (patch) | |
tree | 0f38497775e27d3e16b20573b36dd22aa5b24f3e /gitlint-core/gitlint/contrib/rules/authors_commit.py | |
parent | Releasing debian version 0.17.0-1. (diff) | |
download | gitlint-f3b6c222fb11c96e2f8bbaa0622f46c8ec486874.tar.xz gitlint-f3b6c222fb11c96e2f8bbaa0622f46c8ec486874.zip |
Merging upstream version 0.18.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint-core/gitlint/contrib/rules/authors_commit.py')
-rw-r--r-- | gitlint-core/gitlint/contrib/rules/authors_commit.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/gitlint-core/gitlint/contrib/rules/authors_commit.py b/gitlint-core/gitlint/contrib/rules/authors_commit.py new file mode 100644 index 0000000..ce11663 --- /dev/null +++ b/gitlint-core/gitlint/contrib/rules/authors_commit.py @@ -0,0 +1,46 @@ +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 [] |