Skip to content

Commit 08d9b61

Browse files
authored
Fallback token and other fixes (#33)
Fallback token and other fixes Add Github fallback token to avoid exceeding API calls rate limit Fix Squad and Service Name titles issues for github-related PRs Fix failed_zuul.py hanging during process empty repo Reviewed-by: Vladimir Vshivkov
1 parent 0eef04a commit 08d9b61

6 files changed

+261
-173
lines changed

failed_zuul.py

+86-75
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
db_host = os.getenv("DB_HOST")
2020
db_port = os.getenv("DB_PORT")
2121
db_name = os.getenv("DB_ZUUL") # here we're using dedicated postgres db 'zuul' since Failed Zuul PRs panel should be placed on a same dashboard such as Open PRs
22-
db_csv = os.getenv("DB_CSV") # here rtc service table is located
2322
db_user = os.getenv("DB_USER")
2423
db_password = os.getenv("DB_PASSWORD")
2524

@@ -72,7 +71,28 @@ def create_prs_table(conn_zuul, cur_zuul, table_name):
7271
print(f"Create table: an error occurred while trying to create a table {table_name} in the database: {e}")
7372

7473

74+
def is_repo_empty(org, repo, gitea_token):
75+
try:
76+
commits_resp = session.get(f"{gitea_api_endpoint}/repos/{org}/{repo}/commits?token={gitea_token}")
77+
commits_resp.raise_for_status()
78+
79+
commits_data = json.loads(commits_resp.content.decode())
80+
if not commits_data:
81+
return True
82+
return False
83+
except requests.exceptions.HTTPError as e:
84+
if e.response.status_code == 409: # Conflict error which might mean empty repo, skip this repo to avoid script hangs
85+
print(f"Repo {repo} is empty, skipping")
86+
return True
87+
print(f"Check repo: an error occurred while trying to get commits for repo {repo}: {e}")
88+
return False
89+
except requests.exceptions.RequestException as e:
90+
print(f"Check repo: an error occurred while trying to get commits for repo {repo}: {e}")
91+
return False
92+
93+
7594
def get_repos(org, gitea_token):
95+
print("Gathering repos...")
7696
repos = []
7797
page = 1
7898
while True:
@@ -90,7 +110,8 @@ def get_repos(org, gitea_token):
90110
break
91111

92112
for repo in repos_dict:
93-
repos.append(repo["name"])
113+
if not is_repo_empty(org, repo["name"], gitea_token): # Skipping empty repos
114+
repos.append(repo["name"])
94115

95116
link_header = repos_resp.headers.get("Link")
96117
if link_header is None or "rel=\"next\"" not in link_header:
@@ -121,14 +142,16 @@ def get_f_pr_commits(org, repo, f_pr_number, gitea_token):
121142
created_at = None
122143
days_passed = None
123144

124-
pull_request_resp = session.get(f"{gitea_api_endpoint}/repos/{org}/{repo}/pulls/{f_pr_number}/commits?token={gitea_token}")
145+
pull_request_resp = session.get(
146+
f"{gitea_api_endpoint}/repos/{org}/{repo}/pulls/{f_pr_number}/commits?token={gitea_token}")
125147
pull_request_resp.raise_for_status()
126148

127149
f_pr_info = json.loads(pull_request_resp.content.decode("utf-8"))
128150

129151
if len(f_pr_info) > 0:
130152
f_commit_sha = f_pr_info[0]["sha"]
131-
commit_status_resp = session.get(f"{gitea_api_endpoint}/repos/{org}/{repo}/statuses/{f_commit_sha}?token={gitea_token}")
153+
commit_status_resp = session.get(
154+
f"{gitea_api_endpoint}/repos/{org}/{repo}/statuses/{f_commit_sha}?token={gitea_token}")
132155
commit_status_resp.raise_for_status()
133156

134157
commit_info = json.loads(commit_status_resp.content.decode("utf-8"))
@@ -143,113 +166,114 @@ def get_f_pr_commits(org, repo, f_pr_number, gitea_token):
143166
return zuul_url, status, created_at, days_passed
144167

145168
except requests.exceptions.RequestException as e:
146-
print(f"Get failed PR commits: an error occurred while trying to get pull requests of {repo} repo for {org} org: {e}")
169+
print(
170+
f"Get failed PR commits: an error occurred while trying to get pull requests of {repo} repo for {org} org: {e}")
147171

148172

149173
def get_failed_prs(org, repo, gitea_token, conn_zuul, cur_zuul, table_name):
150-
174+
# print(f"Processing {repo}...") # Debug print, uncomment in case of script hangs
151175
try:
152-
if repo != "doc-exports" and repo != "dsf":
176+
if repo != "doc-exports":
153177
page = 1
154178
while True:
155-
repo_resp = session.get(f"{gitea_api_endpoint}/repos/{org}/{repo}/pulls?state=open&page={page}&limit=1000&token={gitea_token}")
156-
pull_requests = []
157-
if repo_resp.status_code == 200:
158-
try:
159-
pull_requests = json.loads(repo_resp.content.decode("utf-8"))
160-
except json.JSONDecodeError as e:
161-
print(f"Get parent PR: an error occurred while decoding JSON: {e}")
162-
if not pull_requests:
163-
break
164-
165-
for pull_req in pull_requests:
166-
body = pull_req["body"]
167-
if body.startswith("This is an automatically created Pull Request"):
168-
if pull_req["merged"] is True:
169-
continue
170-
else:
171-
f_par_pr_num = extract_number_from_body(body)
172-
f_pr_number = pull_req["number"]
173-
service_name = repo
174-
squad = ""
175-
title = pull_req["title"]
176-
f_pr_url = pull_req["url"]
177-
f_pr_state = pull_req["state"]
178-
zuul_url, status, created_at, days_passed = get_f_pr_commits(org, repo, f_pr_number, gitea_token)
179-
try:
180-
if all(item is not None for item in [zuul_url, status, created_at, days_passed]):
181-
cur_zuul.execute(f"""
182-
183-
INSERT INTO public.{table_name}
184-
("Service Name", "Failed PR Title", "Failed PR URL", "Squad", "Failed PR State", "Zuul URL", "Zuul Check Status", "Days Passed", "Parent PR Number")
185-
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
186-
""",
187-
(service_name, title, f_pr_url, squad, f_pr_state, zuul_url, status, days_passed, f_par_pr_num)
188-
)
189-
conn_zuul.commit()
190-
except Exception as e:
191-
print(f"Failed PRs: an error occurred while inserting into {table_name} table: {e}")
192-
else:
193-
continue
194-
elif org == "docs-swiss" and repo_resp.status_code != 200:
179+
# print(f"Fetching PRs for {repo}, page {page}...") # Debug print, uncomment in case of script hangs
180+
repo_resp = session.get(
181+
f"{gitea_api_endpoint}/repos/{org}/{repo}/pulls?state=open&page={page}&limit=1000&token={gitea_token}")
182+
pull_requests = []
183+
if repo_resp.status_code == 200:
184+
try:
185+
pull_requests = json.loads(repo_resp.content.decode("utf-8"))
186+
except json.JSONDecodeError as e:
187+
print(f"Get parent PR: an error occurred while decoding JSON: {e}")
188+
if not pull_requests:
195189
break
196-
page += 1
190+
191+
for pull_req in pull_requests:
192+
body = pull_req["body"]
193+
if body.startswith("This is an automatically created Pull Request"):
194+
if pull_req["merged"] is True:
195+
continue
196+
else:
197+
f_par_pr_num = extract_number_from_body(body)
198+
f_pr_number = pull_req["number"]
199+
service_name = repo
200+
squad = ""
201+
title = pull_req["title"]
202+
f_pr_url = pull_req["url"]
203+
f_pr_state = pull_req["state"]
204+
zuul_url, status, created_at, days_passed = get_f_pr_commits(org, repo, f_pr_number,
205+
gitea_token)
206+
try:
207+
if all(item is not None for item in [zuul_url, status, created_at, days_passed]):
208+
cur_zuul.execute(f"""
209+
INSERT INTO public.{table_name}
210+
("Service Name", "Failed PR Title", "Failed PR URL", "Squad", "Failed PR State", "Zuul URL", "Zuul Check Status", "Days Passed", "Parent PR Number")
211+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
212+
""",
213+
(service_name, title, f_pr_url, squad, f_pr_state, zuul_url, status, days_passed, f_par_pr_num)
214+
)
215+
conn_zuul.commit()
216+
except Exception as e:
217+
print(f"Failed PRs: an error occurred while inserting into {table_name} table: {e}")
218+
else:
219+
continue
220+
elif org == "docs-swiss" and repo_resp.status_code != 200:
221+
break
222+
page += 1
197223

198224
except Exception as e:
199225
print('Failed PRs: an error occurred:', e)
200226

201227

202-
def update_squad_and_title(cur_dict, conn_table, cur_table, rtctable, opentable):
228+
def update_squad_and_title(conn_zuul, cur_zuul, rtctable, opentable):
203229
print(f"Updating squads and titles in {opentable}...")
204230
try:
205-
cur_table.execute(f"SELECT * FROM {opentable};")
206-
failed_prs_rows = cur_table.fetchall()
231+
cur_zuul.execute(f"SELECT * FROM {opentable};")
232+
failed_prs_rows = cur_zuul.fetchall()
207233

208234
for row in failed_prs_rows:
209235
service_name_index = 1
210236
id_index = 0
211237

212-
cur_dict.execute(
238+
cur_zuul.execute(
213239
f"""SELECT "Title", "Category"
214240
FROM {rtctable}
215241
WHERE "Repository" = %s;""",
216242
(row[service_name_index],)
217243
)
218-
rtc_row = cur_dict.fetchone()
244+
rtc_row = cur_zuul.fetchone()
219245

220246
if rtc_row:
221-
cur_table.execute(
247+
cur_zuul.execute(
222248
f"""UPDATE {opentable}
223249
SET "Service Name" = %s, "Squad" = %s
224250
WHERE id = %s;""",
225251
(rtc_row[0], rtc_row[1], row[id_index])
226252
)
227253

228254
if row[service_name_index] in ('doc-exports', 'docs_on_docs', 'docsportal'):
229-
cur_table.execute(
255+
cur_zuul.execute(
230256
f"""UPDATE {opentable}
231257
SET "Squad" = 'Other'
232258
WHERE id = %s;""",
233259
(row[id_index],)
234260
)
235261

236-
conn_table.commit()
262+
conn_zuul.commit()
237263

238264
except Exception as e:
239265
print(f"Error updating squad and title: {e}")
240-
conn_table.rollback()
266+
conn_zuul.rollback()
241267

242268

243269
def main(org, table_name, rtc):
244270
check_env_variables()
245271

246272
conn_zuul = connect_to_db(db_name)
247273
cur_zuul = conn_zuul.cursor()
248-
conn_csv = connect_to_db(db_csv)
249-
cur_csv = conn_csv.cursor()
250274

251-
cur_csv.execute(f"DROP TABLE IF EXISTS {table_name}")
252-
conn_csv.commit()
275+
cur_zuul.execute(f"DROP TABLE IF EXISTS {table_name}")
276+
conn_zuul.commit()
253277

254278
create_prs_table(conn_zuul, cur_zuul, table_name)
255279

@@ -259,10 +283,8 @@ def main(org, table_name, rtc):
259283
for repo in repos:
260284
get_failed_prs(org, repo, gitea_token, conn_zuul, cur_zuul, table_name)
261285

262-
update_squad_and_title(cur_csv, conn_zuul, cur_zuul, rtc, table_name)
286+
update_squad_and_title(conn_zuul, cur_zuul, rtc, failed_table)
263287

264-
cur_csv.close()
265-
conn_csv.close()
266288
cur_zuul.close()
267289
conn_zuul.close()
268290

@@ -275,18 +297,7 @@ def main(org, table_name, rtc):
275297
main(org_string, failed_table, rtc_table)
276298
main(f"{org_string}-swiss", f"{failed_table}_swiss", f"{rtc_table}_swiss")
277299

278-
279-
if __name__ == "__main__":
280-
org_string = "docs"
281-
failed_table = "failed_zuul_prs"
282-
rtc_table = "repo_title_category"
283-
284-
main(org_string, failed_table, rtc_table)
285-
main(f"{org_string}-swiss", f"{failed_table}_swiss", f"{rtc_table}_swiss")
286-
287-
288300
end_time = time.time()
289301
execution_time = end_time - start_time
290302
minutes, seconds = divmod(execution_time, 60)
291303
print(f"Script failed_zuul.py executed in {int(minutes)} minutes {int(seconds)} seconds! Let's go drink some beer :)")
292-

0 commit comments

Comments
 (0)