-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add authors-commit contrib rule #358
Add authors-commit contrib rule #358
Conversation
Adds the ability to validate committers from an AUTHORS file when committing.
|
||
|
||
@pytest.fixture(name="git_ctx") | ||
def gen_git_ctx(tmpdir): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the existing self.get_sample()
fixture method. Example usage:
gitcontext = self.gitcontext(self.get_sample("commit_message/sample1")) |
|
||
@pytest.mark.parametrize("author", [AUTHOR_1, AUTHOR_2]) | ||
def test_authors_succeeds(author, git_ctx): | ||
assert not validate_authors_rule(author.name, author.email, git_ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should match existing test styling. While correct, we should stay consistent in the project and not use pytest style tests.
This includes not using @pytest.mark.parametrize
, and not using assert
statements.
from collections import namedtuple | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Easiest is to not import pytest at all, that way we don't rely on pytest specific methods.
@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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git_ctx.repository_path
is still a string today. I agree it would be better if it were a Path
, but that's legacy code that would need updating (different discussion).
File "/workspaces/gitlint/gitlint-core/gitlint/contrib/rules/authors_commit.py", line 37, in validate
registered_authors, authors_file_name = Authors._read_authors_from_file(commit.message.context)
File "/workspaces/gitlint/gitlint-core/gitlint/contrib/rules/authors_commit.py", line 24, in _read_authors_from_file
path = Path(git_ctx.repository_path / file_name)
TypeError: unsupported operand type(s) for /: 'str' and 'str'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should now be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR, I think this makes sense to add as a contrib rule!
Notes:
- Hit an exception when trying this out, see inline, should be easily fixed
- Testing style would need updating to match existing project test style (i.e. don't import pytest), more details inline
- Missing docs, would need to be added to https://github.com/jorisroovers/gitlint/blob/main/docs/contrib_rules.md
Appreciate your contribution!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good!
2 nitpicks (also see inline):
- Shall we call this rule
contrib-allowed-authors
? I think that's more descriptive and similar to other gitlint rule names. If so, gentle reminder to also update the docs (easily forgotten 😄 ) - 1 remaining case of using
assert
overassertEquals
Should be straight merge after this.
# A rule MUST have a human friendly name | ||
name = "contrib-authors-commit" | ||
|
||
# A rule MUST have a *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This copy-paste comment from the examples is slightly confusing here since it references a User-defined rule. We should just remove it.
# A rule MUST have a *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
authors_file_names = ("AUTHORS", "AUTHORS.txt", "AUTHORS.md") | ||
parse_authors = re.compile(r"^(?P<name>.*) <(?P<email>.*)>$", re.MULTILINE) | ||
|
||
# A rule MUST have a human friendly name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bit extraneous inside the gitlint codebase - suggest to remove
# A rule MUST have a human friendly name |
from gitlint.rules import CommitRule, RuleViolation | ||
|
||
|
||
class Authors(CommitRule): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: AllowedAuthors
is probably more descriptive.
class Authors(CommitRule): | |
class AllowedAuthors(CommitRule): |
parse_authors = re.compile(r"^(?P<name>.*) <(?P<email>.*)>$", re.MULTILINE) | ||
|
||
# A rule MUST have a human friendly name | ||
name = "contrib-authors-commit" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to class name, I think contrib-allowed-authors
is probably more descriptive. Docs would need quick update too.
name = "contrib-authors-commit" | |
name = "contrib-allowed-authors" |
assert authors_file_name == "AUTHORS" | ||
assert len(authors) == 1 | ||
assert authors == {self.author_1} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert authors_file_name == "AUTHORS" | |
assert len(authors) == 1 | |
assert authors == {self.author_1} | |
self.assertEqual(authors_file_name, "AUTHORS") | |
self.assertEqual(len(authors), 1) | |
self.assertEqual(authors, {self.author_1}) |
Thanks for the note regarding the |
Merged, super thanks! |
- Python 3.11 support - Last release to support Python 3.6 - Behavior Change: In a future release, gitlint will be switching to use `re.search` instead of `re.match` semantics for all rules. (#254) - gitlint no longer uses the `sh` library by default in an attempt to reduce external dependencies. - `--commits` now also accepts a comma-separated list of commit hashes, making it possible to lint a list of non-contiguous commits without invoking gitlint multiple times (#283) - Improved handling of branches that have no commits (#188) - Support for `GITLINT_CONFIG` env variable (#189) - Added a new `gitlint-ci` pre-commit hook, making it easier to run gitlint through pre-commit in CI (#191) - Contrib Rules: - New `contrib-disallow-cleanup-commits` rule (#312) - New `contrib-allowed-authors` rule (#358) - User Defined rules: - Gitlint now recognizes `fixup=amend` commits, available as `commit.is_fixup_amend_commit=True` - Gitlint now parses diff **stat** information, available in `commit.changed_files_stats` (#314) - Bugfixes: - Use correct encoding when using `--msg-filename` parameter (#310) - Various documentation fixes (#244) (#263) (#266) (#294) (#295) (#347) (#364) - Under-the-hood: - Dependencies updated - Moved to blacked for formatting - Fixed nasty CI issue (#298) - Unit tests fix (#256) - Vagrant box removed in favor of github dev containers (#348) - Removed a few lingering references to the `master` branch in favor of `main` - Moved roadmap and project planning to github projects Full Release details in CHANGELOG.md.
It is possible to manage a so-called AUTHORS file in the repository. In this file, all committers for the respective project are listed.
With this
gitlint
rule, it can be ensured that only people who are listed in the AUTHORS file can commit.