-
Notifications
You must be signed in to change notification settings - Fork 0
/
components_crawler.py
66 lines (58 loc) · 2.72 KB
/
components_crawler.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
import base64
import yaml
import requests
from global_settings import GITHUB_API_PARAMETER
class ComponentsCrawler:
def __init__(self, repo, access_token):
self.repo = repo
self.access_token = access_token
self.headers = {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"Authorization": f"token {access_token}",
}
self.app_components_dict = {}
def scan_components(self):
print("\nstart to scan components.\n")
url = f"https://api.github.com/repos/{self.repo}/contents/tests/config/"
response = requests.get(url, headers=self.headers, params=GITHUB_API_PARAMETER)
content = response.json()
for file in content:
print("\nscanning " + file["name"] + "...")
try:
file_url = url + file["name"]
response = requests.get(
file_url, headers=self.headers, params=GITHUB_API_PARAMETER
)
content = response.json()["content"]
yaml_string = base64.b64decode(content).decode("utf-8")
yaml_split_string = yaml_string.split("---")
for s in yaml_split_string:
if len(s):
data = yaml.safe_load(s)
if (data is not None) and ("kind" in data):
if data["kind"] != "Component":
print("it is not about component, skip")
continue
components_name = data["spec"]["type"]
if "scopes" in data:
for name in data["scopes"]:
if name not in self.app_components_dict:
self.app_components_dict[name] = []
self.app_components_dict[name].append(
components_name
)
print("app " + name + " is added.")
else:
print(
"No scope is specified for component "
+ components_name
+ ", skip"
)
else:
print("it is not a k8s api yaml, skip.")
except:
print("Fail to parse " + file["name"] + ", skip.")
for key in self.app_components_dict:
self.app_components_dict[key] = set(self.app_components_dict[key])
return self.app_components_dict