22This script contains actions to be taken based on GitHub events,
33specifically for push and pull_request events.
44"""
5+ import os
56import subprocess
67import sys
8+ from typing import Optional , Union
79
810from event import GithubEvent
911
12+ # Events
1013EVENT_PUSH = "push"
1114EVENT_PULL_REQUEST = "pull_request"
1215
16+ # Inputs
17+ INPUT_FAIL_ON_ERROR = "INPUT_FAIL_ON_ERROR"
18+
19+ # Status
20+ STATUS_SUCCESS = "success"
21+ STATUS_FAILURE = "failure"
22+
1323
1424def _handle_pr_event (event : GithubEvent ) -> None :
1525 """
@@ -55,6 +65,67 @@ def _handle_push_event(event: GithubEvent) -> None:
5565 raise EnvironmentError ("Unable to retrieve From hash and To hash" ) from None
5666
5767
68+ def _write_output (name : str , value : Union [str , int ]) -> None :
69+ """
70+ Writes an output to the GitHub Actions environment.
71+
72+ Args:
73+ name (str): The name of the output variable.
74+ value: The value to be assigned to the output variable.
75+
76+ Raises:
77+ OSError: If there is an issue opening or writing to the output file.
78+ """
79+ output_file_path = os .environ .get ("GITHUB_OUTPUT" , "" )
80+ with open (file = output_file_path , mode = "a" , encoding = "utf-8" ) as output_file :
81+ output_file .write (f"{ name } ={ value } \n " )
82+
83+
84+ def _get_input (key : str ) -> Optional [str ]:
85+ """
86+ Reads the github action input
87+
88+ Args:
89+ key (str): The environment variable to parse
90+
91+ Returns:
92+ str or None: The value of the input or None if it is not set
93+ """
94+ return os .environ .get (key )
95+
96+
97+ def _parse_boolean_input (val : Optional [str ]) -> bool :
98+ """
99+ Parses the input environment key of boolean type in the YAML 1.2
100+ "core schema" specification.
101+ Support boolean input list:
102+ `true | True | TRUE | false | False | FALSE` .
103+ ref: https://yaml.org/spec/1.2/spec.html#id2804923
104+
105+ Args:
106+ key (str, optional): The name of the environment variable to parse.
107+
108+ Returns:
109+ bool: The parsed boolean value.
110+
111+ Raises:
112+ TypeError: If the environment variable's value does not meet the
113+ YAML 1.2 "core schema" specification for booleans.
114+ """
115+
116+ if val in {"true" , "True" , "TRUE" }:
117+ return True
118+ if val in {"false" , "False" , "FALSE" }:
119+ return False
120+ raise TypeError (
121+ """
122+ Input does not meet YAML 1.2 "Core Schema" specification.\n '
123+ Support boolean input list:
124+ `true | True | TRUE | false | False | FALSE
125+ """
126+ )
127+
128+
58129def _check_commits (from_hash : str , to_hash : str ) -> None :
59130 """Check commits using commitlint.
60131
@@ -75,8 +146,17 @@ def _check_commits(from_hash: str, to_hash: str) -> None:
75146 text = True ,
76147 ).strip ()
77148 sys .stdout .write (f"{ output } \n " )
78- except subprocess .CalledProcessError :
79- sys .exit (1 )
149+
150+ _write_output ("status" , STATUS_SUCCESS )
151+ _write_output ("exit_code" , 0 )
152+
153+ except subprocess .CalledProcessError as error :
154+ _write_output ("status" , STATUS_FAILURE )
155+ _write_output ("exit_code" , error .returncode )
156+ val = _get_input (INPUT_FAIL_ON_ERROR )
157+ fail_on_error = _parse_boolean_input (val )
158+ if fail_on_error :
159+ sys .exit (1 )
80160
81161
82162def main () -> None :
0 commit comments