Skip to content

Commit cd614c1

Browse files
authored
feat: print error log in github action (#91)
* feat: print error log in github action * feat: add pre-commit.yml * chore: add type to function
1 parent 4a509ea commit cd614c1

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

.github/workflows/pre-commit.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Run pre-commit
2+
3+
on:
4+
push:
5+
pull_request:
6+
types: opened
7+
8+
jobs:
9+
pre-commit:
10+
uses: commit-check/.github/.github/workflows/pre-commit.yml@main

.pre-commit-config.yaml

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ repos:
1717
rev: 24.10.0
1818
hooks:
1919
- id: black
20-
# FIXME: main.py:109: error: Item "None" of "str | None" has no attribute "split" [union-attr]
21-
# - repo: https://github.com/pre-commit/mirrors-mypy
22-
# rev: v1.12.0
23-
# hooks:
24-
# - id: mypy
20+
- repo: https://github.com/pre-commit/mirrors-mypy
21+
rev: v1.12.0
22+
hooks:
23+
- id: mypy
2524
- repo: https://github.com/codespell-project/codespell
2625
rev: v2.3.0
2726
hooks:

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ jobs:
9090

9191
> [!IMPORTANT]
9292
> `merge-base` is an experimental feature. by default it's disable.
93-
>
93+
>
9494
> To use this feature, you need fetch all history for all branches by setting `fetch-depth: 0` in `actions/checkout`.
9595

9696
### `dry-run`

main.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import subprocess
55
import re
6-
from github import Github
6+
from github import Github # type: ignore
77

88

99
# Constants for message titles
@@ -52,7 +52,8 @@ def run_commit_check() -> int:
5252
args = [
5353
arg
5454
for arg, value in zip(
55-
args, [MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL, COMMIT_SIGNOFF, MERGE_BASE]
55+
args,
56+
[MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL, COMMIT_SIGNOFF, MERGE_BASE],
5657
)
5758
if value == "true"
5859
]
@@ -104,7 +105,12 @@ def add_pr_comments() -> int:
104105
try:
105106
token = os.getenv("GITHUB_TOKEN")
106107
repo_name = os.getenv("GITHUB_REPOSITORY")
107-
pr_number = os.getenv("GITHUB_REF").split("/")[-2]
108+
pr_number = os.getenv("GITHUB_REF")
109+
if pr_number is not None:
110+
pr_number = pr_number.split("/")[-2]
111+
else:
112+
# Handle the case where GITHUB_REF is not set
113+
raise ValueError("GITHUB_REF environment variable is not set")
108114

109115
# Initialize GitHub client
110116
g = Github(token)
@@ -157,6 +163,23 @@ def add_pr_comments() -> int:
157163
return 1
158164

159165

166+
def log_error_and_exit(
167+
failure_title: str, result_text: str | None, ret_code: int
168+
) -> None:
169+
"""
170+
Logs an error message to GitHub Actions and exits with the specified return code.
171+
172+
Args:
173+
failure_title (str): The title of the failure message.
174+
result_text (str): The detailed result text to include in the error message.
175+
ret_code (int): The return code to exit with.
176+
"""
177+
if result_text:
178+
error_message = f"{failure_title}\n```\n{result_text}\n```"
179+
print(f"::error::{error_message}")
180+
sys.exit(ret_code)
181+
182+
160183
def main():
161184
"""Main function to run commit-check, add job summary and post PR comments."""
162185
log_env_vars()
@@ -169,7 +192,8 @@ def main():
169192
if DRY_RUN == "true":
170193
ret_code = 0
171194

172-
sys.exit(ret_code)
195+
result_text = read_result_file()
196+
log_error_and_exit(FAILURE_TITLE, result_text, ret_code)
173197

174198

175199
if __name__ == "__main__":

0 commit comments

Comments
 (0)