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