Skip to content

feat: print error log in github action #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 8, 2024
Merged
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: 10 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 4 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:

> [!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`
Expand Down
32 changes: 28 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import subprocess
import re
from github import Github
from github import Github # type: ignore


# Constants for message titles
Expand Down Expand Up @@ -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"
]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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__":
Expand Down
Loading