-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestApi.py
141 lines (97 loc) · 3.6 KB
/
RestApi.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
132
133
134
135
136
137
138
139
140
import json
import logging
from dataclasses import dataclass, field
import requests
from secret.config import this_repo, token
API_URL = 'https://api.github.com'
headers = {'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28',
'Authorization': token}
def values():
return repo_values(this_repo)
def update_values(name, value):
return update_repo_value(this_repo, name, value)
def create_value(name):
return create_repo_value(this_repo, name)
def assert200(response: requests.Response):
if response.status_code != 200:
logging.error(response.content)
return True
return False
# https://docs.github.com/rest/actions/variables?apiVersion=2022-11-28#list-repository-variables
def repo_value(repo, name):
r = requests.get(f'{API_URL}/repos/{repo}/actions/variables/{name}', headers=headers)
assert200(r)
json_data = json.loads(r.content)
return json_data['value']
def repo_values(repo) -> list:
r = requests.get(f'{API_URL}/repos/{repo}/actions/variables', headers=headers)
assert200(r)
json_data = json.loads(r.content)
logging.info(f'variables of {repo}:\n{[x["name"] for x in json_data["variables"]]}')
return json_data['variables']
def create_repo_value(repo, name):
r = requests.post(f'{API_URL}/repos/{repo}/actions/variables', headers=headers,
data=json.dumps({'name': name, 'value': 'null'}))
assert200(r)
logging.info(f'create value for {repo}: {name}#{r.status_code}')
def update_repo_value(repo, name, value):
r = requests.patch(f'{API_URL}/repos/{repo}/actions/variables/{name}', headers=headers,
data=json.dumps({'name': name, 'value': value}))
assert200(r)
logging.info(f'update value for {repo}: {name}#{r.status_code}')
# https://docs.github.com/rest/releases/releases?apiVersion=2022-11-28#list-releases
def list_releases(repo):
r = requests.get(f'{API_URL}/repos/{repo}/releases', headers=headers)
if assert200(r):
return None
json_data = json.loads(r.content)
logging.info(f'{repo} has {len(json_data)} releases at least')
return json_data
def get_release_by_tag(repo, tag):
r = requests.get(f'{API_URL}/repos/{repo}/releases/tags/{tag}', headers=headers)
if assert200(r):
return
json_data = json.loads(r.content)
return json_data
def list_tags(repo):
r = requests.get(f'{API_URL}/repos/{repo}/tags', headers=headers)
# assert r.status_code == 200, r.content
if assert200(r):
return None
json_data = json.loads(r.content)
return json_data
# https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions
class StdCommand:
@staticmethod
def simple(name, value):
print(f'::{name}::{value}')
@staticmethod
def debug(content):
StdCommand.simple('debug', content)
@staticmethod
def notice(content):
StdCommand.simple('notice', content)
@staticmethod
def warning(content):
StdCommand.simple('warning', content)
@staticmethod
def error(content):
StdCommand.simple('error', content)
StdCmd = StdCommand
@dataclass
class ResultData:
result: bool
data: dict = field(default_factory=dict)
class AttrDict(dict):
def __init__(self, **kwargs):
super().__init__(kwargs)
@staticmethod
def from_dict(v: dict) -> 'AttrDict':
r = AttrDict()
r.update(v)
return r
def __getattr__(self, item):
return self[item] if item in self.keys() else None
def __setattr__(self, key, value):
self[key] = value
RData = ResultData