33import sys
44import subprocess
55import 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,21 @@ def add_pr_comments() -> int:
157163 return 1
158164
159165
166+ def log_error_and_exit (failure_title , result_text , ret_code ):
167+ """
168+ Logs an error message to GitHub Actions and exits with the specified return code.
169+
170+ Args:
171+ failure_title (str): The title of the failure message.
172+ result_text (str): The detailed result text to include in the error message.
173+ ret_code (int): The return code to exit with.
174+ """
175+ if result_text :
176+ error_message = f"{ failure_title } \n ```\n { result_text } \n ```"
177+ print (f"::error::{ error_message } " )
178+ sys .exit (ret_code )
179+
180+
160181def main ():
161182 """Main function to run commit-check, add job summary and post PR comments."""
162183 log_env_vars ()
@@ -169,7 +190,8 @@ def main():
169190 if DRY_RUN == "true" :
170191 ret_code = 0
171192
172- sys .exit (ret_code )
193+ result_text = read_result_file ()
194+ log_error_and_exit (FAILURE_TITLE , result_text , ret_code )
173195
174196
175197if __name__ == "__main__" :
0 commit comments