Skip to content

Commit 9a8c081

Browse files
aj3shsugat009
andcommitted
feat: enhanced error message
- Removed old `commitlint.py`. - Added new module `linter` for linting and validation. - Added detailed validation on `linter` for message enhancement. - Added new error messages. - Added option `--skip-detail` for simple linting. - Moved old validation to simple linting. - Added `pytest.ini` for adding src as a python path. - Updated test for both detailed and simple with same test data. Co-authored-by: Sugat Bajracharya <[email protected]>
1 parent 11040b0 commit 9a8c081

23 files changed

+767
-286
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ jobs:
6868
uses: liskin/gh-problem-matcher-wrap@v3
6969
with:
7070
linters: isort
71-
run: isort --line-length=88 --check src/
71+
run: isort --line-length=88 --check --profile black src/

github_actions/event.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
This module relies on the presence of specific environment variables
66
set by GitHub Actions.
77
"""
8+
89
import json
910
import os
1011
from typing import Any, Dict

github_actions/run.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
This script contains actions to be taken based on GitHub events,
33
specifically for push and pull_request events.
44
"""
5+
56
import os
67
import subprocess
78
import sys

pytest.ini

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
pythonpath = src
3+
python_files = test_*.py
4+
addopts = -vvv

src/commitlint/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Main module for commitlint"""
1+
"""Main module for commitlint."""
22

3-
from .commitlint import check_commit_message
3+
from .linter import lint_commit_message
44

5-
__all__ = ["check_commit_message"]
5+
__all__ = ["lint_commit_message"]

src/commitlint/cli.py

+43-19
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
from typing import List
2020

2121
from .__version__ import __version__
22-
from .commitlint import check_commit_message, remove_comments
2322
from .exceptions import CommitlintException
2423
from .git_helpers import get_commit_message_of_hash, get_commit_messages_of_hash_range
25-
from .messages import VALIDATION_SUCCESSFUL
24+
from .linter import lint_commit_message
25+
from .linter.utils import remove_comments
26+
from .messages import VALIDATION_FAILED, VALIDATION_SUCCESSFUL
2627

2728

2829
def get_args() -> argparse.Namespace:
@@ -57,28 +58,45 @@ def get_args() -> argparse.Namespace:
5758
# --to-hash is optional
5859
parser.add_argument("--to-hash", type=str, help="To commit hash", default="HEAD")
5960

61+
# feature options
62+
parser.add_argument(
63+
"--skip-detail",
64+
action="store_true",
65+
help="Skip the detailed error message check",
66+
)
67+
6068
# parsing args
6169
args = parser.parse_args()
6270

6371
return args
6472

6573

66-
def _show_errors(commit_message: str, errors: List[str]) -> None:
74+
def _show_errors(
75+
commit_message: str,
76+
errors: List[str],
77+
skip_detail: bool = False,
78+
) -> None:
6779
"""
6880
Display a formatted error message for a list of errors.
6981
7082
Args:
83+
commit_message (str): The commit message to display.
7184
errors (List[str]): A list of error messages to be displayed.
85+
skip_detail (bool): Whether to skip the detailed error message.
86+
7287
"""
7388
error_count = len(errors)
7489
commit_message = remove_comments(commit_message)
7590

76-
sys.stderr.write(
77-
f"⧗ Input:\n{commit_message}\n\n✖ Found {error_count} error(s).\n\n"
78-
)
79-
for index, error in enumerate(errors):
80-
end_char = "" if index == error_count - 1 else "\n"
81-
sys.stderr.write(f"- {error}\n{end_char}")
91+
sys.stderr.write(f"⧗ Input:\n{commit_message}\n\n")
92+
93+
if skip_detail:
94+
sys.stderr.write(f"{VALIDATION_FAILED}\n")
95+
return
96+
97+
sys.stderr.write(f"✖ Found {error_count} error(s).\n")
98+
for error in errors:
99+
sys.stderr.write(f"- {error}\n")
82100

83101

84102
def _get_commit_message_from_file(filepath: str) -> str:
@@ -101,41 +119,45 @@ def _get_commit_message_from_file(filepath: str) -> str:
101119
return commit_message
102120

103121

104-
def _handle_commit_message(commit_message: str) -> None:
122+
def _handle_commit_message(commit_message: str, skip_detail: bool) -> None:
105123
"""
106124
Handles a single commit message, checks its validity, and prints the result.
107125
108126
Args:
109127
commit_message (str): The commit message to be handled.
128+
skip_detail (bool): Whether to skip the detailed error linting.
110129
111130
Raises:
112131
SystemExit: If the commit message is invalid.
113132
"""
114-
success, errors = check_commit_message(commit_message)
133+
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
115134

116135
if success:
117136
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
118137
else:
119-
_show_errors(commit_message, errors)
138+
_show_errors(commit_message, errors, skip_detail=skip_detail)
120139
sys.exit(1)
121140

122141

123-
def _handle_multiple_commit_messages(commit_messages: List[str]) -> None:
142+
def _handle_multiple_commit_messages(
143+
commit_messages: List[str], skip_detail: bool
144+
) -> None:
124145
"""
125146
Handles multiple commit messages, checks their validity, and prints the result.
126147
127148
Args:
128149
commit_messages (List[str]): List of commit messages to be handled.
150+
skip_detail (bool): Whether to skip the detailed error linting.
129151
130152
Raises:
131153
SystemExit: If any of the commit messages is invalid.
132154
"""
133155
has_error = False
134156
for commit_message in commit_messages:
135-
success, errors = check_commit_message(commit_message)
157+
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
136158
if not success:
137159
has_error = True
138-
_show_errors(commit_message, errors)
160+
_show_errors(commit_message, errors, skip_detail=skip_detail)
139161
sys.stderr.write("\n")
140162

141163
if has_error:
@@ -153,18 +175,20 @@ def main() -> None:
153175
try:
154176
if args.file:
155177
commit_message = _get_commit_message_from_file(args.file)
156-
_handle_commit_message(commit_message)
178+
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
157179
elif args.hash:
158180
commit_message = get_commit_message_of_hash(args.hash)
159-
_handle_commit_message(commit_message)
181+
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
160182
elif args.from_hash:
161183
commit_messages = get_commit_messages_of_hash_range(
162184
args.from_hash, args.to_hash
163185
)
164-
_handle_multiple_commit_messages(commit_messages)
186+
_handle_multiple_commit_messages(
187+
commit_messages, skip_detail=args.skip_detail
188+
)
165189
else:
166190
commit_message = args.commit_message.strip()
167-
_handle_commit_message(commit_message)
191+
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
168192
except CommitlintException as ex:
169193
sys.stderr.write(f"{ex}\n")
170194
sys.exit(1)

src/commitlint/commitlint.py

-124
This file was deleted.

src/commitlint/constants.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
"""This module defines constants used throughout the application."""
22

33
COMMIT_HEADER_MAX_LENGTH = 72
4+
5+
COMMIT_TYPES = (
6+
"build",
7+
"ci",
8+
"docs",
9+
"feat",
10+
"fix",
11+
"perf",
12+
"refactor",
13+
"style",
14+
"test",
15+
"chore",
16+
"revert",
17+
"bump",
18+
)
19+
20+
IGNORE_COMMIT_PATTERNS = (
21+
r"^((Merge pull request)|(Merge (.*?) into (.*?)|(Merge branch (.*?)))(?:\r?\n)*$)|"
22+
r"^(Merge tag (.*?))(?:\r?\n)*$|"
23+
r"^(R|r)evert (.*)|"
24+
r"^(Merged (.*?)(in|into) (.*)|Merged PR (.*): (.*))$|"
25+
r"^Merge remote-tracking branch(\s*)(.*)$|"
26+
r"^Automatic merge(.*)$|"
27+
r"^Auto-merged (.*?) into (.*)$"
28+
)

src/commitlint/git_helpers.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
This module contains the git related helper functions.
33
"""
4+
45
import subprocess
56
from typing import List
67

src/commitlint/linter/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Main module for commit linters and validators"""
2+
3+
from ._linter import lint_commit_message
4+
5+
__all__ = [
6+
"lint_commit_message",
7+
]

0 commit comments

Comments
 (0)