-
Notifications
You must be signed in to change notification settings - Fork 61
ci(lint): forbid misleading type+scope combinations in commit messages #2799
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
Merged
+152
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """Gitlint rule that forbids misleading type+scope combinations. | ||
|
|
||
| Types like ``feat`` and ``fix`` appear in user-facing release notes. | ||
| Scopes like ``ci`` and ``e2e`` describe infrastructure, not user-visible | ||
| changes. Using them together pollutes the release notes with entries | ||
| that mean nothing to end users. | ||
| """ | ||
|
|
||
| import re | ||
|
|
||
| from gitlint.rules import CommitRule, RuleViolation | ||
|
|
||
| # Pattern: type(scope): description | ||
| _CONVENTIONAL = re.compile(r"^(?P<type>\w+)\((?P<scope>[^)]+)\)") | ||
|
|
||
|
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Regex misses colon delimiter ForbiddenTypeScope’s regex matches type(scope) without requiring the documented : delimiter, so UC1 can emit its “pollutes release notes” message on malformed/non-conventional titles that happen to start with fix(ci)/feat(ci) etc. Agent Prompt
|
||
| # Map of (type, scope) -> suggested replacement. | ||
| # Scope matches are case-insensitive. | ||
| _FORBIDDEN = { | ||
| ("feat", "ci"): "ci(<subsystem>)", | ||
| ("fix", "ci"): "ci(<subsystem>)", | ||
| ("feat", "e2e"): "ci(e2e)", | ||
| ("fix", "e2e"): "ci(e2e)", | ||
| } | ||
|
|
||
|
|
||
| class ForbiddenTypeScope(CommitRule): | ||
| name = "forbidden-type-scope" | ||
| id = "UC1" | ||
|
|
||
| def validate(self, commit): | ||
| title = commit.message.title | ||
| m = _CONVENTIONAL.match(title) | ||
| if not m: | ||
| return [] | ||
|
|
||
| ctype = m.group("type").lower() | ||
| scope = m.group("scope").lower() | ||
| key = (ctype, scope) | ||
|
|
||
| if key not in _FORBIDDEN: | ||
| return [] | ||
|
|
||
| suggestion = _FORBIDDEN[key] | ||
| return [ | ||
| RuleViolation( | ||
| self.id, | ||
| f'"{ctype}({scope})" pollutes release notes with non-user-facing changes. ' | ||
| f'Use "{suggestion}: ..." instead.', | ||
| line_nr=1, | ||
| ) | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """Tests for the ForbiddenTypeScope gitlint rule.""" | ||
|
|
||
| import pytest | ||
| from gitlint.rules import RuleViolation | ||
|
|
||
| from gitlint_rules.forbidden_type_scope import ForbiddenTypeScope | ||
|
|
||
|
|
||
| class FakeCommit: | ||
| """Minimal stand-in for a gitlint commit object.""" | ||
|
|
||
| def __init__(self, title): | ||
| self.message = type("msg", (), {"title": title})() | ||
|
|
||
|
|
||
| def run_rule(title): | ||
| """Run ForbiddenTypeScope against a commit title and return violations.""" | ||
| rule = ForbiddenTypeScope() | ||
| commit = FakeCommit(title) | ||
| return rule.validate(commit) | ||
|
|
||
|
|
||
| # --- should be rejected --- | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "title", | ||
| [ | ||
| "fix(ci): update workflow", | ||
| "feat(ci): add new job", | ||
| "fix(e2e): repair test", | ||
| "feat(e2e): add new test", | ||
| ], | ||
| ) | ||
| def test_rejects_forbidden_combinations(title): | ||
| violations = run_rule(title) | ||
| assert violations, f"Expected violation for {title!r}" | ||
| assert len(violations) == 1 | ||
| assert isinstance(violations[0], RuleViolation) | ||
|
|
||
|
|
||
| def test_fix_ci_suggests_ci_subsystem(): | ||
| violations = run_rule("fix(ci): update workflow") | ||
| assert "ci(<subsystem>)" in violations[0].message.lower() | ||
|
|
||
|
|
||
| def test_feat_e2e_suggests_ci_e2e(): | ||
| violations = run_rule("feat(e2e): add new test") | ||
| assert "ci(e2e)" in violations[0].message.lower() | ||
|
|
||
|
|
||
| # --- should be allowed --- | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "title", | ||
| [ | ||
| "ci(lint): update linter config", | ||
| "ci(e2e): fix flaky test", | ||
| "fix(mint): correct token refresh", | ||
| "feat(review-agent): add outcome labels", | ||
| "chore(ci): bump action version", | ||
| "test(e2e): add new scenario", | ||
| "refactor(ci): simplify matrix", | ||
| "docs: update readme", | ||
| "fix(#123): handle nil pointer", | ||
| ], | ||
| ) | ||
| def test_allows_valid_combinations(title): | ||
| violations = run_rule(title) | ||
| assert not violations, f"Unexpected violation for {title!r}: {violations}" | ||
|
|
||
|
|
||
| # --- should not crash on non-conventional titles --- | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "title", | ||
| [ | ||
| "just a plain message", | ||
| "WIP", | ||
| "", | ||
| ], | ||
| ) | ||
| def test_ignores_non_conventional(title): | ||
| violations = run_rule(title) | ||
| assert not violations |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[high] protected-path
This PR modifies .github/workflows/lint.yml, which is under the .github/ protected path. The PR has no linked issue providing authorization for modifying governance/infrastructure files. Human approval is always required for protected-path changes.
Suggested fix: File an issue documenting the need to add pytest and gitlint-core to the CI lint workflow, and link it to this PR.