3
3
import sys
4
4
import subprocess
5
5
import re
6
- from github import Github
6
+ from github import Github # type: ignore
7
7
8
8
9
9
# Constants for message titles
@@ -52,7 +52,8 @@ def run_commit_check() -> int:
52
52
args = [
53
53
arg
54
54
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 ],
56
57
)
57
58
if value == "true"
58
59
]
@@ -104,7 +105,12 @@ def add_pr_comments() -> int:
104
105
try :
105
106
token = os .getenv ("GITHUB_TOKEN" )
106
107
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" )
108
114
109
115
# Initialize GitHub client
110
116
g = Github (token )
@@ -157,6 +163,23 @@ def add_pr_comments() -> int:
157
163
return 1
158
164
159
165
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
+
160
183
def main ():
161
184
"""Main function to run commit-check, add job summary and post PR comments."""
162
185
log_env_vars ()
@@ -169,7 +192,8 @@ def main():
169
192
if DRY_RUN == "true" :
170
193
ret_code = 0
171
194
172
- sys .exit (ret_code )
195
+ result_text = read_result_file ()
196
+ log_error_and_exit (FAILURE_TITLE , result_text , ret_code )
173
197
174
198
175
199
if __name__ == "__main__" :
0 commit comments