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
+ import os
5
6
import subprocess
6
7
import sys
8
+ from typing import Optional , Union
7
9
8
10
from event import GithubEvent
9
11
12
+ # Events
10
13
EVENT_PUSH = "push"
11
14
EVENT_PULL_REQUEST = "pull_request"
12
15
16
+ # Inputs
17
+ INPUT_FAIL_ON_ERROR = "INPUT_FAIL_ON_ERROR"
18
+
19
+ # Status
20
+ STATUS_SUCCESS = "success"
21
+ STATUS_FAILURE = "failure"
22
+
13
23
14
24
def _handle_pr_event (event : GithubEvent ) -> None :
15
25
"""
@@ -55,6 +65,67 @@ def _handle_push_event(event: GithubEvent) -> None:
55
65
raise EnvironmentError ("Unable to retrieve From hash and To hash" ) from None
56
66
57
67
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
+
58
129
def _check_commits (from_hash : str , to_hash : str ) -> None :
59
130
"""Check commits using commitlint.
60
131
@@ -75,8 +146,17 @@ def _check_commits(from_hash: str, to_hash: str) -> None:
75
146
text = True ,
76
147
).strip ()
77
148
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 )
80
160
81
161
82
162
def main () -> None :
0 commit comments