19
19
db_host = os .getenv ("DB_HOST" )
20
20
db_port = os .getenv ("DB_PORT" )
21
21
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
23
22
db_user = os .getenv ("DB_USER" )
24
23
db_password = os .getenv ("DB_PASSWORD" )
25
24
@@ -72,7 +71,28 @@ def create_prs_table(conn_zuul, cur_zuul, table_name):
72
71
print (f"Create table: an error occurred while trying to create a table { table_name } in the database: { e } " )
73
72
74
73
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
+
75
94
def get_repos (org , gitea_token ):
95
+ print ("Gathering repos..." )
76
96
repos = []
77
97
page = 1
78
98
while True :
@@ -90,7 +110,8 @@ def get_repos(org, gitea_token):
90
110
break
91
111
92
112
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" ])
94
115
95
116
link_header = repos_resp .headers .get ("Link" )
96
117
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):
121
142
created_at = None
122
143
days_passed = None
123
144
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 } " )
125
147
pull_request_resp .raise_for_status ()
126
148
127
149
f_pr_info = json .loads (pull_request_resp .content .decode ("utf-8" ))
128
150
129
151
if len (f_pr_info ) > 0 :
130
152
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 } " )
132
155
commit_status_resp .raise_for_status ()
133
156
134
157
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):
143
166
return zuul_url , status , created_at , days_passed
144
167
145
168
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 } " )
147
171
148
172
149
173
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
151
175
try :
152
- if repo != "doc-exports" and repo != "dsf" :
176
+ if repo != "doc-exports" :
153
177
page = 1
154
178
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 :
195
189
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
197
223
198
224
except Exception as e :
199
225
print ('Failed PRs: an error occurred:' , e )
200
226
201
227
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 ):
203
229
print (f"Updating squads and titles in { opentable } ..." )
204
230
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 ()
207
233
208
234
for row in failed_prs_rows :
209
235
service_name_index = 1
210
236
id_index = 0
211
237
212
- cur_dict .execute (
238
+ cur_zuul .execute (
213
239
f"""SELECT "Title", "Category"
214
240
FROM { rtctable }
215
241
WHERE "Repository" = %s;""" ,
216
242
(row [service_name_index ],)
217
243
)
218
- rtc_row = cur_dict .fetchone ()
244
+ rtc_row = cur_zuul .fetchone ()
219
245
220
246
if rtc_row :
221
- cur_table .execute (
247
+ cur_zuul .execute (
222
248
f"""UPDATE { opentable }
223
249
SET "Service Name" = %s, "Squad" = %s
224
250
WHERE id = %s;""" ,
225
251
(rtc_row [0 ], rtc_row [1 ], row [id_index ])
226
252
)
227
253
228
254
if row [service_name_index ] in ('doc-exports' , 'docs_on_docs' , 'docsportal' ):
229
- cur_table .execute (
255
+ cur_zuul .execute (
230
256
f"""UPDATE { opentable }
231
257
SET "Squad" = 'Other'
232
258
WHERE id = %s;""" ,
233
259
(row [id_index ],)
234
260
)
235
261
236
- conn_table .commit ()
262
+ conn_zuul .commit ()
237
263
238
264
except Exception as e :
239
265
print (f"Error updating squad and title: { e } " )
240
- conn_table .rollback ()
266
+ conn_zuul .rollback ()
241
267
242
268
243
269
def main (org , table_name , rtc ):
244
270
check_env_variables ()
245
271
246
272
conn_zuul = connect_to_db (db_name )
247
273
cur_zuul = conn_zuul .cursor ()
248
- conn_csv = connect_to_db (db_csv )
249
- cur_csv = conn_csv .cursor ()
250
274
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 ()
253
277
254
278
create_prs_table (conn_zuul , cur_zuul , table_name )
255
279
@@ -259,10 +283,8 @@ def main(org, table_name, rtc):
259
283
for repo in repos :
260
284
get_failed_prs (org , repo , gitea_token , conn_zuul , cur_zuul , table_name )
261
285
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 )
263
287
264
- cur_csv .close ()
265
- conn_csv .close ()
266
288
cur_zuul .close ()
267
289
conn_zuul .close ()
268
290
@@ -275,18 +297,7 @@ def main(org, table_name, rtc):
275
297
main (org_string , failed_table , rtc_table )
276
298
main (f"{ org_string } -swiss" , f"{ failed_table } _swiss" , f"{ rtc_table } _swiss" )
277
299
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
-
288
300
end_time = time .time ()
289
301
execution_time = end_time - start_time
290
302
minutes , seconds = divmod (execution_time , 60 )
291
303
print (f"Script failed_zuul.py executed in { int (minutes )} minutes { int (seconds )} seconds! Let's go drink some beer :)" )
292
-
0 commit comments