diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..7ee1b19 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,10 @@ +name: Run pre-commit + +on: + push: + pull_request: + types: opened + +jobs: + pre-commit: + uses: commit-check/.github/.github/workflows/pre-commit.yml@main diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b45ec09..571d5ee 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,11 +17,10 @@ repos: rev: 24.10.0 hooks: - id: black -# FIXME: main.py:109: error: Item "None" of "str | None" has no attribute "split" [union-attr] -# - repo: https://github.com/pre-commit/mirrors-mypy -# rev: v1.12.0 -# hooks: -# - id: mypy +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.12.0 + hooks: + - id: mypy - repo: https://github.com/codespell-project/codespell rev: v2.3.0 hooks: diff --git a/README.md b/README.md index a25d56f..5bd3919 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,15 @@ A Github Action for checking commit message formatting, branch naming, committer name, email, commit signoff and more. +## Table of Contents + +* [Usage](#usage) +* [Optional Inputs](#optional-inputs) +* [GitHub Action Job Summary](#github-action-job-summary) +* [GitHub Pull Request Comments](#github-pull-request-comments) +* [Badging Your Repository](#badging-your-repository) +* [Versioning](#versioning) + ## Usage Create a new GitHub Actions workflow in your project, e.g. at [.github/workflows/commit-check.yml](.github/workflows/commit-check.yml) @@ -51,53 +60,53 @@ jobs: - **Description**: check commit message formatting convention. - By default the rule follows [conventional commits](https://www.conventionalcommits.org/). -- Default: 'true' +- Default: `true` ### `branch` - **Description**: check git branch naming convention. - By default follow bitbucket [conventional branch](https://conventional-branch.github.io/). -- Default: 'true' +- Default: `true` ### `author-name` - **Description**: check committer author name. -- Default: 'true' +- Default: `true` ### `author-email` - **Description**: check committer author email. -- Default: 'true' +- Default: `true` ### `commit-signoff` - **Description**: check committer commit signature. -- Default: 'true' +- Default: `true` ### `merge-base` - **Description**: check current branch is rebased onto target branch. -- Default: 'false' +- Default: `false` > [!IMPORTANT] > `merge-base` is an experimental feature. by default it's disable. -> +> > To use this feature, you need fetch all history for all branches by setting `fetch-depth: 0` in `actions/checkout`. ### `dry-run` - **Description**: run checks without failing. exit code is 0 otherwise is 1. -- Default: 'false' +- Default: `false` ### `job-summary` - **Description**: display job summary to the workflow run. -- Default: 'true' +- Default: `true` ### `pr-comments` - **Description**: post results to the pull request comments. -- Default: 'false' +- Default: `false` > [!IMPORTANT] > `pr-comments` is an experimental feature. by default it's disable. To use it you need to set `GITHUB_TOKEN` in the GitHub Action. @@ -106,29 +115,29 @@ jobs: Note: the default rule of above inputs is following [this configuration](https://github.com/commit-check/commit-check/blob/main/.commit-check.yml), if you want to customize just add your `.commit-check.yml` config file under your repository root directory. -## GitHub Action job summary +## GitHub Action Job Summary By default, commit-check-action results are shown on the job summary page of the workflow. -### Success job summary +### Success Job Summary  -### Failure job summary +### Failure Job Summary  -## GitHub Pull Request comments +## GitHub Pull Request Comments -### Success pull request comment +### Success Pull Request Comment  -### Failure pull request comment +### Failure Pull Request Comment  -## Badging your repository +## Badging Your Repository You can add a badge to your repository to show your contributors/users that you use commit-check! diff --git a/main.py b/main.py index 91d2246..87434ec 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import sys import subprocess import re -from github import Github +from github import Github # type: ignore # Constants for message titles @@ -52,7 +52,8 @@ def run_commit_check() -> int: args = [ arg for arg, value in zip( - args, [MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL, COMMIT_SIGNOFF, MERGE_BASE] + args, + [MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL, COMMIT_SIGNOFF, MERGE_BASE], ) if value == "true" ] @@ -104,7 +105,12 @@ def add_pr_comments() -> int: try: token = os.getenv("GITHUB_TOKEN") repo_name = os.getenv("GITHUB_REPOSITORY") - pr_number = os.getenv("GITHUB_REF").split("/")[-2] + pr_number = os.getenv("GITHUB_REF") + if pr_number is not None: + pr_number = pr_number.split("/")[-2] + else: + # Handle the case where GITHUB_REF is not set + raise ValueError("GITHUB_REF environment variable is not set") # Initialize GitHub client g = Github(token) @@ -157,6 +163,23 @@ def add_pr_comments() -> int: return 1 +def log_error_and_exit( + failure_title: str, result_text: str | None, ret_code: int +) -> None: + """ + Logs an error message to GitHub Actions and exits with the specified return code. + + Args: + failure_title (str): The title of the failure message. + result_text (str): The detailed result text to include in the error message. + ret_code (int): The return code to exit with. + """ + if result_text: + error_message = f"{failure_title}\n```\n{result_text}\n```" + print(f"::error::{error_message}") + sys.exit(ret_code) + + def main(): """Main function to run commit-check, add job summary and post PR comments.""" log_env_vars() @@ -169,7 +192,8 @@ def main(): if DRY_RUN == "true": ret_code = 0 - sys.exit(ret_code) + result_text = read_result_file() + log_error_and_exit(FAILURE_TITLE, result_text, ret_code) if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index e13f4c3..fdcf08c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # Install commit-check CLI # For details please see: https://github.com/commit-check/commit-check -commit-check==0.9.1 +commit-check==0.9.2 # Interact with the GitHub API. PyGithub==2.5.0
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: