Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8503ef5

Browse files
authored
Merge pull request #41 from aj3sh/verbose
feat: add verbose option
2 parents 3c63a5f + 4ec08c1 commit 8503ef5

File tree

13 files changed

+464
-116
lines changed

13 files changed

+464
-116
lines changed

‎.github/workflows/test_action.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ jobs:
1616
uses: ./ # Uses an action in the root directory
1717
# or use a released GitHub Action
1818
# uses: opensource-nepal/commitlint@v0.2.1
19+
with:
20+
verbose: true

‎README.md‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ jobs:
102102
| # | Name | Type | Default | Description |
103103
| --- | ----------------- | ------- | ------- | --------------------------------------------------------------------- |
104104
| 1 | **fail_on_error** | Boolean | true | Determines whether the GitHub Action should fail if commitlint fails. |
105+
| 2 | **verbose** | Boolean | true | Verbose output. |
105106
106107
#### GitHub Action Outputs
107108
@@ -121,23 +122,23 @@ pip install commitlint
121122
### Usage
122123

123124
```
124-
$ commitlint --help
125-
usage: commitlint [-h] [-V] [--file FILE] [--hash HASH] [--from-hash FROM_HASH] [--to-hash TO_HASH] [--skip-detail] [commit_message]
126-
127-
Check if a commit message follows the conventional commit format.
125+
commitlint [-h] [-V] [--file FILE] [--hash HASH] [--from-hash FROM_HASH] [--to-hash TO_HASH] [--skip-detail] [-q | -v]
126+
[commit_message]
128127
129128
positional arguments:
130-
commit_message The commit message to be checked.
129+
commit_message The commit message to be checked
131130
132131
optional arguments:
133132
-h, --help show this help message and exit
134133
-V, --version show program's version number and exit
135-
--file FILE Path to a file containing the commit message.
134+
--file FILE Path to a file containing the commit message
136135
--hash HASH Commit hash
137136
--from-hash FROM_HASH
138137
From commit hash
139138
--to-hash TO_HASH To commit hash
140139
--skip-detail Skip the detailed error message check
140+
-q, --quiet Ignore stdout and stderr
141+
-v, --verbose Verbose output
141142
```
142143

143144
### Examples
@@ -174,6 +175,18 @@ $ commitlint --skip-detail "chore: my commit message"
174175
$ commitlint --skip-detail --hash 9a8c08173
175176
```
176177

178+
Run commitlint in quiet mode:
179+
180+
```shell
181+
$ commitlint --quiet "chore: my commit message"
182+
```
183+
184+
Run commitlint in verbose mode:
185+
186+
```shell
187+
$ commitlint --verbose "chore: my commit message"
188+
```
189+
177190
Version check:
178191

179192
```shell

‎action.yml‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
name: "Conventional Commitlint"
2-
description: "A GitHub Action to check conventional commit message"
1+
name: 'Conventional Commitlint'
2+
description: 'A GitHub Action to check conventional commit message'
33
inputs:
44
fail_on_error:
55
description: Whether to fail the workflow if commit messages don't follow conventions.
66
default: 'true'
77
required: false
8+
verbose:
9+
description: Verbose output.
10+
default: 'false'
11+
required: false
812
outputs:
9-
status:
10-
description: Status
11-
value: ${{ steps.commitlint.outputs.status }}
12-
exit_code:
13-
description: Exit Code
14-
value: ${{ steps.commitlint.outputs.exit_code }}
13+
status:
14+
description: Status
15+
value: ${{ steps.commitlint.outputs.status }}
16+
exit_code:
17+
description: Exit Code
18+
value: ${{ steps.commitlint.outputs.exit_code }}
1519
branding:
16-
color: "red"
17-
icon: "git-commit"
20+
color: 'red'
21+
icon: 'git-commit'
1822
runs:
19-
using: "composite"
23+
using: 'composite'
2024
steps:
2125
- name: Install Python
2226
uses: actions/setup-python@v5
2327
with:
24-
python-version: "3.8"
28+
python-version: '3.8'
2529

2630
- name: Install Commitlint
2731
run: python -m pip install -e ${{ github.action_path }}
@@ -59,3 +63,4 @@ runs:
5963
shell: bash
6064
env:
6165
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
66+
INPUT_VERBOSE: ${{ inputs.verbose }}

‎github_actions/run.py‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# Inputs
1818
INPUT_FAIL_ON_ERROR = "INPUT_FAIL_ON_ERROR"
19+
INPUT_VERBOSE = "INPUT_VERBOSE"
1920

2021
# Status
2122
STATUS_SUCCESS = "success"
@@ -136,14 +137,20 @@ def _check_commits(from_hash: str, to_hash: str) -> None:
136137
"""
137138
sys.stdout.write(f"Commit from {from_hash} to {to_hash}\n")
138139
try:
140+
commands = [
141+
"commitlint",
142+
"--from-hash",
143+
from_hash,
144+
"--to-hash",
145+
to_hash,
146+
]
147+
148+
verbose = _parse_boolean_input(_get_input(INPUT_VERBOSE))
149+
if verbose:
150+
commands.append("--verbose")
151+
139152
output = subprocess.check_output(
140-
[
141-
"commitlint",
142-
"--from-hash",
143-
from_hash,
144-
"--to-hash",
145-
to_hash,
146-
],
153+
commands,
147154
text=True,
148155
).strip()
149156
sys.stdout.write(f"{output}\n")

‎src/commitlint/cli.py‎

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import sys
1919
from typing import List
2020

21+
from . import console
2122
from .__version__ import __version__
23+
from .config import config
2224
from .exceptions import CommitlintException
2325
from .git_helpers import get_commit_message_of_hash, get_commit_messages_of_hash_range
2426
from .linter import lint_commit_message
@@ -48,10 +50,10 @@ def get_args() -> argparse.Namespace:
4850
# for commit message check
4951
group = parser.add_mutually_exclusive_group(required=True)
5052
group.add_argument(
51-
"commit_message", nargs="?", type=str, help="The commit message to be checked."
53+
"commit_message", nargs="?", type=str, help="The commit message to be checked"
5254
)
5355
group.add_argument(
54-
"--file", type=str, help="Path to a file containing the commit message."
56+
"--file", type=str, help="Path to a file containing the commit message"
5557
)
5658
group.add_argument("--hash", type=str, help="Commit hash")
5759
group.add_argument("--from-hash", type=str, help="From commit hash")
@@ -64,14 +66,26 @@ def get_args() -> argparse.Namespace:
6466
action="store_true",
6567
help="Skip the detailed error message check",
6668
)
69+
70+
output_group = parser.add_mutually_exclusive_group(required=False)
6771
# --quiet option is optional
68-
parser.add_argument(
72+
output_group.add_argument(
6973
"-q",
7074
"--quiet",
7175
action="store_true",
7276
help="Ignore stdout and stderr",
7377
default=False,
7478
)
79+
80+
# --verbose option is optional
81+
output_group.add_argument(
82+
"-v",
83+
"--verbose",
84+
action="store_true",
85+
help="Verbose output",
86+
default=False,
87+
)
88+
7589
# parsing args
7690
args = parser.parse_args()
7791

@@ -95,15 +109,15 @@ def _show_errors(
95109
error_count = len(errors)
96110
commit_message = remove_comments(commit_message)
97111

98-
sys.stderr.write(f"⧗ Input:\n{commit_message}\n\n")
112+
console.error(f"⧗ Input:\n{commit_message}\n")
99113

100114
if skip_detail:
101-
sys.stderr.write(f"{VALIDATION_FAILED}\n")
115+
console.error(VALIDATION_FAILED)
102116
return
103117

104-
sys.stderr.write(f"✖ Found {error_count} error(s).\n")
118+
console.error(f"✖ Found {error_count} error(s).")
105119
for error in errors:
106-
sys.stderr.write(f"- {error}\n")
120+
console.error(f"- {error}")
107121

108122

109123
def _get_commit_message_from_file(filepath: str) -> str:
@@ -121,50 +135,42 @@ def _get_commit_message_from_file(filepath: str) -> str:
121135
IOError: If there is an issue reading the file.
122136
"""
123137
abs_filepath = os.path.abspath(filepath)
138+
console.verbose(f"reading commit message from file {abs_filepath}")
124139
with open(abs_filepath, encoding="utf-8") as commit_message_file:
125140
commit_message = commit_message_file.read().strip()
126141
return commit_message
127142

128143

129-
def _handle_commit_message(
130-
commit_message: str, skip_detail: bool, quiet: bool = False
131-
) -> None:
144+
def _handle_commit_message(commit_message: str, skip_detail: bool) -> None:
132145
"""
133146
Handles a single commit message, checks its validity, and prints the result.
134147
135148
Args:
136149
commit_message (str): The commit message to be handled.
137150
skip_detail (bool): Whether to skip the detailed error linting.
138-
quiet (bool): Whether to ignore stout and stderr
139151
140152
Raises:
141153
SystemExit: If the commit message is invalid.
142154
"""
143155
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
144156

145-
if success and quiet:
146-
return
147-
148157
if success:
149-
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
158+
console.success(VALIDATION_SUCCESSFUL)
150159
return
151160

152-
if not quiet:
153-
_show_errors(commit_message, errors, skip_detail=skip_detail)
154-
161+
_show_errors(commit_message, errors, skip_detail=skip_detail)
155162
sys.exit(1)
156163

157164

158165
def _handle_multiple_commit_messages(
159-
commit_messages: List[str], skip_detail: bool, quiet: bool=False
166+
commit_messages: List[str], skip_detail: bool
160167
) -> None:
161168
"""
162169
Handles multiple commit messages, checks their validity, and prints the result.
163170
164171
Args:
165172
commit_messages (List[str]): List of commit messages to be handled.
166173
skip_detail (bool): Whether to skip the detailed error linting.
167-
quiet (bool): Whether to show the error and messages in console
168174
Raises:
169175
SystemExit: If any of the commit messages is invalid.
170176
"""
@@ -173,18 +179,17 @@ def _handle_multiple_commit_messages(
173179
for commit_message in commit_messages:
174180
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
175181
if success:
182+
console.verbose("lint success")
176183
continue
177184

178185
has_error = True
179-
if not quiet:
180-
_show_errors(commit_message, errors, skip_detail=skip_detail)
181-
sys.stderr.write("\n")
186+
_show_errors(commit_message, errors, skip_detail=skip_detail)
187+
console.error("")
182188

183189
if has_error:
184190
sys.exit(1)
185191

186-
if not quiet:
187-
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
192+
console.success(VALIDATION_SUCCESSFUL)
188193

189194

190195
def main() -> None:
@@ -193,31 +198,34 @@ def main() -> None:
193198
"""
194199
args = get_args()
195200

201+
# setting config based on args
202+
config.quiet = args.quiet
203+
config.verbose = args.verbose
204+
205+
console.verbose("starting commitlint")
196206
try:
197207
if args.file:
208+
console.verbose("checking commit from file")
198209
commit_message = _get_commit_message_from_file(args.file)
199-
_handle_commit_message(
200-
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
201-
)
210+
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
202211
elif args.hash:
212+
console.verbose("checking commit from hash")
203213
commit_message = get_commit_message_of_hash(args.hash)
204-
_handle_commit_message(
205-
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
206-
)
214+
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
207215
elif args.from_hash:
216+
console.verbose("checking commit from hash range")
208217
commit_messages = get_commit_messages_of_hash_range(
209218
args.from_hash, args.to_hash
210219
)
211220
_handle_multiple_commit_messages(
212-
commit_messages, skip_detail=args.skip_detail, quiet=args.quiet
221+
commit_messages, skip_detail=args.skip_detail
213222
)
214223
else:
224+
console.verbose("checking commit message")
215225
commit_message = args.commit_message.strip()
216-
_handle_commit_message(
217-
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
218-
)
226+
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
219227
except CommitlintException as ex:
220-
sys.stderr.write(f"{ex}\n")
228+
console.error(f"{ex}")
221229
sys.exit(1)
222230

223231

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /