-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.py
executable file
·131 lines (101 loc) · 3.89 KB
/
entrypoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# Copyright 2023 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
""" Python utility which loads inputs based on GitHub action def in 'action.yml'. """
import json
import os
import sys
import uuid
from autolabeler.main import main_with_args
def load_target_from_env() -> str|None:
""" Attempts to load the labelling target from the os.environ by checking:
- $INPUT_TARGET: returned directly
- $INPUT_TARGET_FROM_ACTION_ENV: loads the target from '${{ github }}'
"""
target = os.getenv("INPUT_TARGET")
if target:
return target
github_json = os.getenv("INPUT_TARGET-FROM-ACTION-ENV")
if not github_json:
return None
load = json.loads(github_json)
repo = load.get("repository")
if not repo:
return None
event = load.get("event")
if event is None:
return None
pr_number = event.get("pull_request", {}).get("number", None)
if pr_number:
return f"{repo}/pull/{pr_number}"
issue_number = event.get("issue", {}).get("number", None)
if issue_number:
return f"{repo}/issue/{issue_number}"
return repo
def load_token_from_env() -> str|None:
token = os.getenv("INPUT_TOKEN")
if token:
return token
github_json = os.getenv("INPUT_TARGET-FROM-ACTION-ENV")
if not github_json:
return None
return json.loads(github_json).get("token", None)
def load_args_from_env():
""" Composes arguments for the labeler command from env variables.
All of the parameters defined in 'actions.yml' should be available
within the Docker container as uppercased "INPUT_"-prefixed env variables.
('example-param' -> '$INPUT_EXAMPLE_PARAM')
https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
"""
target = load_target_from_env()
if not target:
sys.exit(
f"No INPUT_TARGET or INPUT_TARGET-FROM-ACTION-ENV: {os.environ}")
vars = ["COMMAND", "CONFIG-PATH"]
vars_map = {v: os.getenv(f"INPUT_{v}") for v in vars}
if any(v is None for v in vars_map.values()):
real = {f"INPUT_{v}": vars_map[v] for v in vars}
sys.exit(
f"One or more required env vars are undefined: {real}.\n"
f"Env is: {os.environ}")
token = load_token_from_env()
if token is None:
sys.exit(
f"Failed to load INPUT_TOKEN or INPUT_TARGET-FROM-ACTION-ENV.token")
return [
"--github-token", token,
"--label-definitions-file", vars_map['CONFIG-PATH'],
target,
vars_map['COMMAND']]
def set_github_output_var(key: str, val: str):
outfile_path = os.getenv("GITHUB_OUTPUT")
if not outfile_path:
raise ValueError(f"No 'GITHUB_OUTPUT' defined in env: {os.environ}")
delim = f"cbsl_autolabels_{uuid.uuid4()}"
# NOTE: can add output variables to GITHUB_OUTPUT with bash heredoc syntax:
vardef = f"{key}<<{delim}{os.linesep}{val}{os.linesep}{delim}{os.linesep}"
with open(outfile_path, "a") as fout:
fout.write(vardef)
def main():
args = []
if len(sys.argv) > 1:
args = sys.argv[1:]
else:
args = load_args_from_env()
labels_dicts = main_with_args(args)
serialized = json.dumps(labels_dicts, indent=4)
print(serialized)
set_github_output_var("labels", serialized)
if __name__ == "__main__":
main()