-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathdeploy_util.py
More file actions
154 lines (133 loc) · 4.17 KB
/
deploy_util.py
File metadata and controls
154 lines (133 loc) · 4.17 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import functools
import http.cookiejar
import os
import pathlib
import subprocess
import time
import requests
__all__ = [
"DEPLOY_TRIGGER",
"HEADER",
"SLEEP_DURATION",
"deploy_file_to_wiki",
"get_git_deploy_reason",
"get_wiki_api_url",
"get_wikis",
"read_cookie_jar",
"read_file_from_path",
"write_to_github_summary_file",
]
DEPLOY_TRIGGER = os.getenv("DEPLOY_TRIGGER")
DRY_RUN = bool(int(os.getenv("DRY_RUN", 0)))
GITHUB_STEP_SUMMARY_FILE = os.getenv("GITHUB_STEP_SUMMARY")
USER_AGENT = f"GitHub Autodeploy Bot/2.0.0 ({os.getenv('WIKI_UA_EMAIL')})"
WIKI_BASE_URL = os.getenv("WIKI_BASE_URL")
HEADER = {
"User-Agent": USER_AGENT,
"accept": "application/json",
"Accept-Encoding": "gzip",
}
SLEEP_DURATION = 4
def get_wikis() -> set[str]:
response = requests.get(
"https://liquipedia.net/api.php",
headers=HEADER,
)
wikis = response.json()
time.sleep(SLEEP_DURATION)
return set(wikis["allwikis"].keys())
@functools.cache
def get_wiki_api_url(wiki: str) -> str:
return f"{WIKI_BASE_URL}/{wiki}/api.php"
def get_git_deploy_reason():
return (
subprocess.check_output(["git", "log", "-1", "--pretty='%h %s'"])
.decode()
.strip()
)
def deploy_file_to_wiki(
session: requests.Session,
file_path: pathlib.Path,
file_content: str,
wiki: str,
target_page: str,
token: str,
deploy_reason: str,
) -> tuple[bool, bool]:
payload = {
"title": target_page,
"text": file_content,
"summary": f"Git: {deploy_reason}",
"bot": "true",
"recreate": "true",
"token": token,
}
if DRY_RUN:
print(f"HEADER: {HEADER}")
print(f"PARAM: { {'format': 'json', 'action': 'edit'} }")
print(f"DATA: {payload}")
return True, False
change_made = False
deployed = True
response = session.post(
get_wiki_api_url(wiki),
headers=HEADER,
params={"format": "json", "action": "edit"},
data=payload,
).json()
edit_info = response.get("edit")
error_info = response.get("error")
# Handle API errors or unexpected response structure
if error_info is not None or edit_info is None:
print(f"::warning file={str(file_path)}::failed to deploy (API error)")
details = ""
if isinstance(error_info, dict):
code = error_info.get("code")
info = error_info.get("info")
detail_parts = []
if code:
detail_parts.append(f"code={code}")
if info:
detail_parts.append(f"info={info}")
if detail_parts:
details = " (" + ", ".join(detail_parts) + ")"
write_to_github_summary_file(
f":warning: {str(file_path)} failed to deploy due to API error{details}"
)
deployed = False
time.sleep(SLEEP_DURATION)
return deployed, change_made
result = edit_info.get("result")
new_rev_id = edit_info.get("newrevid")
if result == "Success":
if new_rev_id is not None:
change_made = True
if DEPLOY_TRIGGER != "push":
print(f"::warning file={str(file_path)}::File changed")
print(f"...{result}")
print("...done")
write_to_github_summary_file(
f":information_source: {str(file_path)} successfully deployed"
)
else:
print(f"::warning file={str(file_path)}::failed to deploy")
write_to_github_summary_file(f":warning: {str(file_path)} failed to deploy")
deployed = False
time.sleep(SLEEP_DURATION)
return deployed, change_made
def read_cookie_jar(wiki: str) -> http.cookiejar.FileCookieJar:
ckf = f"cookie_{wiki}.ck"
cookie_jar = http.cookiejar.LWPCookieJar(filename=ckf)
try:
cookie_jar.load(ignore_discard=True)
except OSError:
pass
return cookie_jar
def read_file_from_path(file_path: pathlib.Path) -> str:
with file_path.open("r") as file:
return file.read()
def write_to_github_summary_file(text: str):
if not GITHUB_STEP_SUMMARY_FILE:
return
with open(GITHUB_STEP_SUMMARY_FILE, "a") as summary:
summary.write(f"{text}\n")