Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Github CI

on:
push:
branches: ['main']
branches: ["main"]
pull_request:

jobs:
Expand All @@ -11,13 +11,13 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.10']
python-version: ["3.10"]

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -38,7 +38,7 @@ jobs:
poetry run pytest --cov=src/ --cov-report=xml --no-cov-on-fail

- name: Send coverage to CodeCov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: Commitlint

on:
push:
branches: ['main']
branches: ["main"]
pull_request:

jobs:
commitlint:
runs-on: ubuntu-latest
name: Commitlint
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Run commitlint
# uses: opensource-nepal/commitlint@v1
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
steps:
- name: Release
id: release
uses: googleapis/release-please-action@7987652d64b4581673a76e33ad5e98e3dd56832f # v4.1.3
uses: googleapis/release-please-action@v4

- uses: actions/checkout@v4
- uses: actions/checkout@v6
if: ${{ steps.release.outputs.release_created }}

- name: tag major and minor versions
Expand All @@ -32,10 +32,10 @@ jobs:
git push origin v${{ steps.release.outputs.major }} -f

- name: Set up Python
uses: actions/setup-python@v5
uses: actions/setup-python@v6
if: ${{ steps.release.outputs.release_created }}
with:
python-version: '3.x'
python-version: '3.10'

- name: Install dependencies
if: ${{ steps.release.outputs.release_created }}
Expand All @@ -49,4 +49,4 @@ jobs:

- name: Publish package
if: ${{ steps.release.outputs.release_created }}
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
uses: pypa/gh-action-pypi-publish@v1.13.0
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ We welcome and appreciate pull requests from the community. To contribute:

- Participate in the code review process and address any feedback promptly.

## Release

The release process, changelog, and versioning are managed by
[release-please](https://github.com/googleapis/release-please). Versions are automatically
determined based on Conventional Commit types, and the changelog is generated from commit
messages.

The [release-please-action](https://github.com/googleapis/release-please-action) creates
a release PR ([example PR](https://github.com/opensource-nepal/commitlint/pull/62)) for
bug fixes and features. A new release is published only after the release PR is merged.

## License

By contributing to this project, you agree that your contributions will be licensed under the **GPL-3.0 License**.
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ steps:
...
```

If you don't have any workflows, create a new GitHub workflow file, e.g., `.github/workflows/commitlint.yaml`:
If you don't have any workflows, create a new GitHub workflow file, e.g., `.github/workflows/commitlint.yml`:

```yaml
name: Conventional Commitlint
Expand Down Expand Up @@ -70,11 +70,12 @@ Github API failed with status code 403. Response: {'message': 'Resource not acce

#### GitHub Action Inputs

| # | Name | Type | Default | Description |
| --- | ----------------- | ------- | ---------------------- | --------------------------------------------------------------------- |
| 1 | **fail_on_error** | Boolean | `true` | Whether the GitHub Action should fail if commitlint detects an issue. |
| 2 | **verbose** | Boolean | `false` | Enables verbose output. |
| 3 | **token** | String | `secrets.GITHUB_TOKEN` | GitHub Token for fetching commits using the GitHub API. |
| # | Name | Type | Default | Description |
| --- | --------------------- | ------- | ---------------------- | ---------------------------------------------------------------------------- |
| 1 | **fail_on_error** | Boolean | `true` | Whether the GitHub Action should fail if commitlint detects an issue. |
| 2 | **verbose** | Boolean | `false` | Enables verbose output. |
| 3 | **max_header_length** | Number | | Optional. Maximum Header length to check. Skips header length check if empty |
| 4 | **token** | String | `secrets.GITHUB_TOKEN` | GitHub Token for fetching commits using the GitHub API. |

#### GitHub Action Outputs

Expand Down
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ inputs:
description: Verbose output.
default: 'false'
required: false
max_header_length:
description: Check maximum header length.
required: false
token:
description: Token for fetching commits using Github API.
default: ${{ github.token }}
Expand All @@ -31,7 +34,9 @@ runs:
using: 'composite'
steps:
- name: Install Python
uses: actions/setup-python@v5.1.0
# Use a specific version for action dependencies
# A commitlint action version should use fixed dependency versions (not mutable versions)
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
with:
python-version: '3.10'

Expand All @@ -46,3 +51,4 @@ runs:
INPUT_TOKEN: ${{ inputs.token }}
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
INPUT_VERBOSE: ${{ inputs.verbose }}
INPUT_MAX_HEADER_LENGTH: ${{ inputs.max_header_length }}
33 changes: 21 additions & 12 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@ pip install commitlint
## Usage

```
commitlint [-h] [-V] [--file FILE] [--hash HASH] [--from-hash FROM_HASH] [--to-hash TO_HASH] [--skip-detail] [--hide-input]
commitlint [-h] [-V] [--file FILE] [--hash HASH] [--from-hash FROM_HASH] [--to-hash TO_HASH]
[--skip-detail] [--hide-input]
[-q | -v]
[--max-header-length MAX_HEADER_LENGTH]
[commit_message]

Check if a commit message follows the Conventional Commits format.

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

Options:
-h, --help Show this help message and exit.
-V, --version Show the program's version number and exit.
--file FILE Path to a file containing the commit message.
--hash HASH Commit hash.
--from-hash FROM_HASH Commit hash to start checking from.
--to-hash TO_HASH Commit hash to check up to.
--skip-detail Skip detailed error messages.
--hide-input Hide input from stdout.
-q, --quiet Suppress stdout and stderr.
-v, --verbose Enable verbose output.
-h, --help Show this help message and exit.
--file FILE Path to a file containing the commit message.
-V, --version Show the program's version number and exit.
--hash HASH Commit hash.
--from-hash FROM_HASH Commit hash to start checking from.
--to-hash TO_HASH Commit hash to check up to.
--skip-detail Skip detailed error messages.
--hide-input Hide input from stdout.
-q, --quiet Suppress stdout and stderr.
-v, --verbose Enable verbose output.
--max-header-length LENGTH Maximum header length to check.
```

## Examples
Expand Down Expand Up @@ -79,6 +82,12 @@ Run `commitlint` in verbose mode:
$ commitlint --verbose "chore: my commit message"
```

Run `commitlint` with maximum header length check:

```shell
$ commitlint --max-header-length 72 "chore: my commit message"
```

Check the version:

```shell
Expand Down
6 changes: 6 additions & 0 deletions github_actions/action/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .utils import (
get_boolean_input,
get_input,
get_int_input,
request_github_api,
write_line_to_file,
write_output,
Expand All @@ -26,6 +27,7 @@
INPUT_TOKEN = "token"
INPUT_FAIL_ON_ERROR = "fail_on_error"
INPUT_VERBOSE = "verbose"
INPUT_MAX_HEADER_LENGTH = "max_header_length"

# Status
STATUS_SUCCESS = "success"
Expand Down Expand Up @@ -115,6 +117,10 @@ def run_commitlint(commit_message: str) -> Tuple[bool, Optional[str]]:
if verbose:
commands.append("--verbose")

max_header_length = get_int_input(INPUT_MAX_HEADER_LENGTH)
if max_header_length:
commands.extend(["--max-header-length", str(max_header_length)])

output = subprocess.check_output(commands, text=True, stderr=subprocess.PIPE)
if output:
sys.stdout.write(f"{output}")
Expand Down
21 changes: 21 additions & 0 deletions github_actions/action/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ def get_boolean_input(key: str) -> bool:
)


def get_int_input(key: str) -> int | None:
"""
Read the GitHub action integer input.

Args:
key: Input key.

Returns:
The integer value of the input. If not integer, returns None
"""
val = get_input(key)

if not val:
return None

try:
return int(val)
except ValueError:
return None


def write_line_to_file(filepath: str, line: str) -> None:
"""
Write line to a specified filepath.
Expand Down
9 changes: 9 additions & 0 deletions src/commitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def get_args() -> argparse.Namespace:
default=False,
)

# --max-header-length : enables header length check (optional)
parser.add_argument(
"--max-header-length",
type=int,
help="Maximum header length to check",
default=None,
)

# parsing args
args = parser.parse_args()

Expand Down Expand Up @@ -222,6 +230,7 @@ def main() -> None:
# setting config based on args
config.quiet = args.quiet
config.verbose = args.verbose
config.max_header_length = args.max_header_length

console.verbose("starting commitlint")
try:
Expand Down
1 change: 1 addition & 0 deletions src/commitlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class _CommitlintConfig:

_verbose: bool = False
_quiet: bool = False
max_header_length: int | None = None

def __new__(cls) -> "_CommitlintConfig":
"""
Expand Down
2 changes: 0 additions & 2 deletions src/commitlint/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""This module defines constants used throughout the application."""

COMMIT_HEADER_MAX_LENGTH = 72

COMMIT_TYPES = (
"build",
"ci",
Expand Down
13 changes: 10 additions & 3 deletions src/commitlint/linter/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from typing import List, Tuple, Type, Union

from .. import console
from ..constants import COMMIT_HEADER_MAX_LENGTH, COMMIT_TYPES
from ..config import config
from ..constants import COMMIT_TYPES
from ..messages import (
COMMIT_TYPE_INVALID_ERROR,
COMMIT_TYPE_MISSING_ERROR,
Expand Down Expand Up @@ -70,9 +71,15 @@ def validate(self) -> None:
Returns:
None
"""
max_header_length = config.max_header_length

if not max_header_length:
# skip header length check
return

header = self.commit_message.split("\n")[0]
if len(header) > COMMIT_HEADER_MAX_LENGTH:
self.add_error(HEADER_LENGTH_ERROR)
if len(header) > max_header_length:
self.add_error(HEADER_LENGTH_ERROR % max_header_length)


class SimplePatternValidator(CommitValidator):
Expand Down
6 changes: 2 additions & 4 deletions src/commitlint/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
This module provides constant messages used in the application for various scenarios.
"""

from .constants import COMMIT_HEADER_MAX_LENGTH, COMMIT_TYPES
from .constants import COMMIT_TYPES

VALIDATION_SUCCESSFUL = "Commit validation: successful!"
VALIDATION_FAILED = "Commit validation: failed!"

INCORRECT_FORMAT_ERROR = (
"Commit message does not follow the Conventional Commits format."
)
HEADER_LENGTH_ERROR = (
f"Header length cannot exceed {COMMIT_HEADER_MAX_LENGTH} characters."
)
HEADER_LENGTH_ERROR = "Header length cannot exceed %s characters."
COMMIT_TYPE_MISSING_ERROR = "Type is missing."
COMMIT_TYPE_INVALID_ERROR = (
f"Invalid type '%s'. Type must be one of: {', '.join(COMMIT_TYPES)}."
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/actions_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ def set_github_env_vars():
os.environ["INPUT_TOKEN"] = "token"
os.environ["INPUT_VERBOSE"] = "false"
os.environ["INPUT_FAIL_ON_ERROR"] = "true"
os.environ["INPUT_MAX_HEADER_LENGTH"] = ""
15 changes: 0 additions & 15 deletions tests/fixtures/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# pylint: disable=all
from typing import List, Tuple

from commitlint.constants import COMMIT_HEADER_MAX_LENGTH
from commitlint.messages import (
COMMIT_TYPE_INVALID_ERROR,
COMMIT_TYPE_MISSING_ERROR,
Expand All @@ -11,7 +10,6 @@
DESCRIPTION_MISSING_ERROR,
DESCRIPTION_MULTIPLE_SPACE_START_ERROR,
DESCRIPTION_NO_LEADING_SPACE_ERROR,
HEADER_LENGTH_ERROR,
INCORRECT_FORMAT_ERROR,
SCOPE_EMPTY_ERROR,
SCOPE_WHITESPACE_ERROR,
Expand All @@ -36,11 +34,6 @@
# success
("feat: add new feature", True, []),
("feat: add new feature\n\nthis is body", True, []),
(
"feat: add new feature\n\nthis is body" + "a" * COMMIT_HEADER_MAX_LENGTH,
True,
[],
),
("feat: add new feature\n\nthis is body\n\ntest", True, []),
("feat: add new feature\n", True, []),
("build(deps-dev): bump @babel/traverse from 7.22.17 to 7.24.0", True, []),
Expand All @@ -64,14 +57,6 @@
("initial Commit", True, []),
# incorrect format check
("feat add new feature", False, [INCORRECT_FORMAT_ERROR]),
# header length check
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
(
"Test " + "a" * (COMMIT_HEADER_MAX_LENGTH + 1),
False,
[HEADER_LENGTH_ERROR, INCORRECT_FORMAT_ERROR],
),
# commit type check
(": add new feature", False, [COMMIT_TYPE_MISSING_ERROR]),
("(invalid): add new feature", False, [COMMIT_TYPE_MISSING_ERROR]),
Expand Down
Loading
Loading