Skip to content
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

Merged
merged 4 commits into from
Nov 11, 2022

Conversation

stauchert
Copy link
Contributor

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.

Adds the ability to validate committers from an AUTHORS file when committing.


@pytest.fixture(name="git_ctx")
def gen_git_ctx(tmpdir):
Copy link
Owner

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)
Copy link
Owner

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
Copy link
Owner

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)
Copy link
Owner

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'

Copy link
Contributor Author

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.

Copy link
Owner

@jorisroovers jorisroovers left a 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:

Appreciate your contribution!

Copy link
Owner

@jorisroovers jorisroovers left a 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 over assertEquals

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).
Copy link
Owner

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.

Suggested change
# 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
Copy link
Owner

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

Suggested change
# A rule MUST have a human friendly name

from gitlint.rules import CommitRule, RuleViolation


class Authors(CommitRule):
Copy link
Owner

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.

Suggested change
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"
Copy link
Owner

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.

Suggested change
name = "contrib-authors-commit"
name = "contrib-allowed-authors"

Comment on lines 95 to 97
assert authors_file_name == "AUTHORS"
assert len(authors) == 1
assert authors == {self.author_1}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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})

@jorisroovers jorisroovers added this to the 0.18.0 milestone Nov 10, 2022
@stauchert
Copy link
Contributor Author

Thanks for the note regarding the assert statement and the name suggestion! I agree with you on that. 👍

@jorisroovers jorisroovers merged commit 3740e97 into jorisroovers:main Nov 11, 2022
@jorisroovers
Copy link
Owner

Merged, super thanks!

jorisroovers added a commit that referenced this pull request Nov 16, 2022
- 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 0.18.0
Development

Successfully merging this pull request may close these issues.

None yet

2 participants