Skip to content

Commit

Permalink
feat: add verbose option
Browse files Browse the repository at this point in the history
- Added verbose option on cli.
- Added verbose option on github actions.
- Updated doc for verbose and quiet.
  • Loading branch information
aj3sh committed May 23, 2024
1 parent 3c63a5f commit 4ec08c1
Show file tree
Hide file tree
Showing 13 changed files with 464 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test_action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ jobs:
uses: ./ # Uses an action in the root directory
# or use a released GitHub Action
# uses: opensource-nepal/commitlint@v0.2.1
with:
verbose: true
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ jobs:
| # | Name | Type | Default | Description |
| --- | ----------------- | ------- | ------- | --------------------------------------------------------------------- |
| 1 | **fail_on_error** | Boolean | true | Determines whether the GitHub Action should fail if commitlint fails. |
| 2 | **verbose** | Boolean | true | Verbose output. |

#### GitHub Action Outputs

Expand All @@ -121,23 +122,23 @@ pip install commitlint
### Usage

```
$ commitlint --help
usage: commitlint [-h] [-V] [--file FILE] [--hash HASH] [--from-hash FROM_HASH] [--to-hash TO_HASH] [--skip-detail] [commit_message]

Check if a commit message follows the conventional commit format.
commitlint [-h] [-V] [--file FILE] [--hash HASH] [--from-hash FROM_HASH] [--to-hash TO_HASH] [--skip-detail] [-q | -v]
[commit_message]

positional arguments:
commit_message The commit message to be checked.
commit_message The commit message to be checked

optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
--file FILE Path to a file containing the commit message.
--file FILE Path to a file containing the commit message
--hash HASH Commit hash
--from-hash FROM_HASH
From commit hash
--to-hash TO_HASH To commit hash
--skip-detail Skip the detailed error message check
-q, --quiet Ignore stdout and stderr
-v, --verbose Verbose output
```
### Examples
Expand Down Expand Up @@ -174,6 +175,18 @@ $ commitlint --skip-detail "chore: my commit message"
$ commitlint --skip-detail --hash 9a8c08173
```

Run commitlint in quiet mode:

```shell
$ commitlint --quiet "chore: my commit message"
```

Run commitlint in verbose mode:

```shell
$ commitlint --verbose "chore: my commit message"
```

Version check:

```shell
Expand Down
29 changes: 17 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
name: "Conventional Commitlint"
description: "A GitHub Action to check conventional commit message"
name: 'Conventional Commitlint'
description: 'A GitHub Action to check conventional commit message'
inputs:
fail_on_error:
description: Whether to fail the workflow if commit messages don't follow conventions.
default: 'true'
required: false
verbose:
description: Verbose output.
default: 'false'
required: false
outputs:
status:
description: Status
value: ${{ steps.commitlint.outputs.status }}
exit_code:
description: Exit Code
value: ${{ steps.commitlint.outputs.exit_code }}
status:
description: Status
value: ${{ steps.commitlint.outputs.status }}
exit_code:
description: Exit Code
value: ${{ steps.commitlint.outputs.exit_code }}
branding:
color: "red"
icon: "git-commit"
color: 'red'
icon: 'git-commit'
runs:
using: "composite"
using: 'composite'
steps:
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.8"
python-version: '3.8'

- name: Install Commitlint
run: python -m pip install -e ${{ github.action_path }}
Expand Down Expand Up @@ -59,3 +63,4 @@ runs:
shell: bash
env:
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
INPUT_VERBOSE: ${{ inputs.verbose }}
21 changes: 14 additions & 7 deletions github_actions/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Inputs
INPUT_FAIL_ON_ERROR = "INPUT_FAIL_ON_ERROR"
INPUT_VERBOSE = "INPUT_VERBOSE"

# Status
STATUS_SUCCESS = "success"
Expand Down Expand Up @@ -136,14 +137,20 @@ def _check_commits(from_hash: str, to_hash: str) -> None:
"""
sys.stdout.write(f"Commit from {from_hash} to {to_hash}\n")
try:
commands = [
"commitlint",
"--from-hash",
from_hash,
"--to-hash",
to_hash,
]

verbose = _parse_boolean_input(_get_input(INPUT_VERBOSE))
if verbose:
commands.append("--verbose")

output = subprocess.check_output(
[
"commitlint",
"--from-hash",
from_hash,
"--to-hash",
to_hash,
],
commands,
text=True,
).strip()
sys.stdout.write(f"{output}\n")
Expand Down
80 changes: 44 additions & 36 deletions src/commitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import sys
from typing import List

from . import console
from .__version__ import __version__
from .config import config
from .exceptions import CommitlintException
from .git_helpers import get_commit_message_of_hash, get_commit_messages_of_hash_range
from .linter import lint_commit_message
Expand Down Expand Up @@ -48,10 +50,10 @@ def get_args() -> argparse.Namespace:
# for commit message check
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"commit_message", nargs="?", type=str, help="The commit message to be checked."
"commit_message", nargs="?", type=str, help="The commit message to be checked"
)
group.add_argument(
"--file", type=str, help="Path to a file containing the commit message."
"--file", type=str, help="Path to a file containing the commit message"
)
group.add_argument("--hash", type=str, help="Commit hash")
group.add_argument("--from-hash", type=str, help="From commit hash")
Expand All @@ -64,14 +66,26 @@ def get_args() -> argparse.Namespace:
action="store_true",
help="Skip the detailed error message check",
)

output_group = parser.add_mutually_exclusive_group(required=False)
# --quiet option is optional
parser.add_argument(
output_group.add_argument(
"-q",
"--quiet",
action="store_true",
help="Ignore stdout and stderr",
default=False,
)

# --verbose option is optional
output_group.add_argument(
"-v",
"--verbose",
action="store_true",
help="Verbose output",
default=False,
)

# parsing args
args = parser.parse_args()

Expand All @@ -95,15 +109,15 @@ def _show_errors(
error_count = len(errors)
commit_message = remove_comments(commit_message)

sys.stderr.write(f"⧗ Input:\n{commit_message}\n\n")
console.error(f"⧗ Input:\n{commit_message}\n")

if skip_detail:
sys.stderr.write(f"{VALIDATION_FAILED}\n")
console.error(VALIDATION_FAILED)
return

sys.stderr.write(f"✖ Found {error_count} error(s).\n")
console.error(f"✖ Found {error_count} error(s).")
for error in errors:
sys.stderr.write(f"- {error}\n")
console.error(f"- {error}")


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


def _handle_commit_message(
commit_message: str, skip_detail: bool, quiet: bool = False
) -> None:
def _handle_commit_message(commit_message: str, skip_detail: bool) -> None:
"""
Handles a single commit message, checks its validity, and prints the result.
Args:
commit_message (str): The commit message to be handled.
skip_detail (bool): Whether to skip the detailed error linting.
quiet (bool): Whether to ignore stout and stderr
Raises:
SystemExit: If the commit message is invalid.
"""
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)

if success and quiet:
return

if success:
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
console.success(VALIDATION_SUCCESSFUL)
return

if not quiet:
_show_errors(commit_message, errors, skip_detail=skip_detail)

_show_errors(commit_message, errors, skip_detail=skip_detail)
sys.exit(1)


def _handle_multiple_commit_messages(
commit_messages: List[str], skip_detail: bool, quiet: bool = False
commit_messages: List[str], skip_detail: bool
) -> None:
"""
Handles multiple commit messages, checks their validity, and prints the result.
Args:
commit_messages (List[str]): List of commit messages to be handled.
skip_detail (bool): Whether to skip the detailed error linting.
quiet (bool): Whether to show the error and messages in console
Raises:
SystemExit: If any of the commit messages is invalid.
"""
Expand All @@ -173,18 +179,17 @@ def _handle_multiple_commit_messages(
for commit_message in commit_messages:
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
if success:
console.verbose("lint success")
continue

has_error = True
if not quiet:
_show_errors(commit_message, errors, skip_detail=skip_detail)
sys.stderr.write("\n")
_show_errors(commit_message, errors, skip_detail=skip_detail)
console.error("")

if has_error:
sys.exit(1)

if not quiet:
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
console.success(VALIDATION_SUCCESSFUL)


def main() -> None:
Expand All @@ -193,31 +198,34 @@ def main() -> None:
"""
args = get_args()

# setting config based on args
config.quiet = args.quiet
config.verbose = args.verbose

console.verbose("starting commitlint")
try:
if args.file:
console.verbose("checking commit from file")
commit_message = _get_commit_message_from_file(args.file)
_handle_commit_message(
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
)
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
elif args.hash:
console.verbose("checking commit from hash")
commit_message = get_commit_message_of_hash(args.hash)
_handle_commit_message(
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
)
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
elif args.from_hash:
console.verbose("checking commit from hash range")
commit_messages = get_commit_messages_of_hash_range(
args.from_hash, args.to_hash
)
_handle_multiple_commit_messages(
commit_messages, skip_detail=args.skip_detail, quiet=args.quiet
commit_messages, skip_detail=args.skip_detail
)
else:
console.verbose("checking commit message")
commit_message = args.commit_message.strip()
_handle_commit_message(
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
)
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
except CommitlintException as ex:
sys.stderr.write(f"{ex}\n")
console.error(f"{ex}")
sys.exit(1)


Expand Down
Loading

0 comments on commit 4ec08c1

Please sign in to comment.