18
18
19
19
db_host = os .getenv ("DB_HOST" )
20
20
db_port = os .getenv ("DB_PORT" )
21
- db_name = os .getenv ("DB_CSV" ) # here we're using main postgres db since we don't need orphan PRs
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
22
23
db_user = os .getenv ("DB_USER" )
23
24
db_password = os .getenv ("DB_PASSWORD" )
24
25
@@ -48,9 +49,9 @@ def connect_to_db(db_name):
48
49
return None
49
50
50
51
51
- def create_prs_table (conn , cur , table_name ):
52
+ def create_prs_table (conn_zuul , cur_zuul , table_name ):
52
53
try :
53
- cur .execute (
54
+ cur_zuul .execute (
54
55
f'''CREATE TABLE IF NOT EXISTS { table_name } (
55
56
id SERIAL PRIMARY KEY,
56
57
"Service Name" VARCHAR(255),
@@ -65,7 +66,7 @@ def create_prs_table(conn, cur, table_name):
65
66
"Parent PR Number" INT
66
67
);'''
67
68
)
68
- conn .commit ()
69
+ conn_zuul .commit ()
69
70
print (f"Table { table_name } has been created successfully" )
70
71
except psycopg2 .Error as e :
71
72
print (f"Create table: an error occurred while trying to create a table { table_name } in the database: { e } " )
@@ -145,7 +146,8 @@ def get_f_pr_commits(org, repo, f_pr_number, gitea_token):
145
146
print (f"Get failed PR commits: an error occurred while trying to get pull requests of { repo } repo for { org } org: { e } " )
146
147
147
148
148
- def get_failed_prs (org , repo , gitea_token , conn , cur , table_name ):
149
+ def get_failed_prs (org , repo , gitea_token , conn_zuul , cur_zuul , table_name ):
150
+
149
151
try :
150
152
if repo != "doc-exports" and repo != "dsf" :
151
153
page = 1
@@ -176,14 +178,15 @@ def get_failed_prs(org, repo, gitea_token, conn, cur, table_name):
176
178
zuul_url , status , created_at , days_passed = get_f_pr_commits (org , repo , f_pr_number , gitea_token )
177
179
try :
178
180
if all (item is not None for item in [zuul_url , status , created_at , days_passed ]):
179
- cur .execute (f"""
181
+ cur_zuul .execute (f"""
182
+
180
183
INSERT INTO public.{ table_name }
181
184
("Service Name", "Failed PR Title", "Failed PR URL", "Squad", "Failed PR State", "Zuul URL", "Zuul Check Status", "Days Passed", "Parent PR Number")
182
185
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
183
186
""" ,
184
187
(service_name , title , f_pr_url , squad , f_pr_state , zuul_url , status , days_passed , f_par_pr_num )
185
188
)
186
- conn .commit ()
189
+ conn_zuul .commit ()
187
190
except Exception as e :
188
191
print (f"Failed PRs: an error occurred while inserting into { table_name } table: { e } " )
189
192
else :
@@ -196,56 +199,81 @@ def get_failed_prs(org, repo, gitea_token, conn, cur, table_name):
196
199
print ('Failed PRs: an error occurred:' , e )
197
200
198
201
199
- def update_squad_and_title (conn , cur , table_name , rtc ):
200
- print (f"Updating squads and titles with { rtc } ..." )
202
+ def update_squad_and_title (cur_dict , conn_table , cur_table , rtctable , opentable ):
203
+ print (f"Updating squads and titles in { opentable } ..." )
201
204
try :
202
- cur .execute (f"SELECT * FROM { table_name } ;" )
203
- failed_prs_rows = cur .fetchall ()
205
+ cur_table .execute (f"SELECT * FROM { opentable } ;" )
206
+ failed_prs_rows = cur_table .fetchall ()
204
207
205
208
for row in failed_prs_rows :
206
- cur .execute (
207
- f"""UPDATE { table_name }
208
- SET "Service Name" = rtc."Title", "Squad" = rtc."Category"
209
- FROM { rtc } AS rtc
210
- WHERE { table_name } ."Service Name" = rtc."Repository"
211
- AND { table_name } .id = %s;""" ,
212
- (row [0 ],)
213
- )
214
- cur .execute (
215
- f"""UPDATE { table_name }
216
- SET "Squad" = 'Other'
217
- WHERE { table_name } ."Service Name" IN ('doc-exports', 'docs_on_docs', 'docsportal')
218
- AND { table_name } .id = %s;""" ,
219
- (row [0 ],)
209
+ service_name_index = 1
210
+ id_index = 0
211
+
212
+ cur_dict .execute (
213
+ f"""SELECT "Title", "Category"
214
+ FROM { rtctable }
215
+ WHERE "Repository" = %s;""" ,
216
+ (row [service_name_index ],)
220
217
)
221
- conn .commit ()
218
+ rtc_row = cur_dict .fetchone ()
219
+
220
+ if rtc_row :
221
+ cur_table .execute (
222
+ f"""UPDATE { opentable }
223
+ SET "Service Name" = %s, "Squad" = %s
224
+ WHERE id = %s;""" ,
225
+ (rtc_row [0 ], rtc_row [1 ], row [id_index ])
226
+ )
227
+
228
+ if row [service_name_index ] in ('doc-exports' , 'docs_on_docs' , 'docsportal' ):
229
+ cur_table .execute (
230
+ f"""UPDATE { opentable }
231
+ SET "Squad" = 'Other'
232
+ WHERE id = %s;""" ,
233
+ (row [id_index ],)
234
+ )
235
+
236
+ conn_table .commit ()
222
237
223
238
except Exception as e :
224
239
print (f"Error updating squad and title: { e } " )
225
- conn .rollback ()
240
+ conn_table .rollback ()
226
241
227
242
228
243
def main (org , table_name , rtc ):
229
244
check_env_variables ()
230
245
231
- conn = connect_to_db (db_name )
232
- cur = conn .cursor ()
246
+ conn_zuul = connect_to_db (db_name )
247
+ cur_zuul = conn_zuul .cursor ()
248
+ conn_csv = connect_to_db (db_csv )
249
+ cur_csv = conn_csv .cursor ()
233
250
234
- cur .execute (f"DROP TABLE IF EXISTS { table_name } " )
235
- conn .commit ()
251
+ cur_csv .execute (f"DROP TABLE IF EXISTS { table_name } " )
252
+ conn_csv .commit ()
236
253
237
- create_prs_table (conn , cur , table_name )
254
+ create_prs_table (conn_zuul , cur_zuul , table_name )
238
255
239
256
repos = get_repos (org , gitea_token )
240
257
241
258
print ("Gathering PRs info..." )
242
259
for repo in repos :
243
- get_failed_prs (org , repo , gitea_token , conn , cur , table_name )
260
+ get_failed_prs (org , repo , gitea_token , conn_zuul , cur_zuul , table_name )
244
261
245
- update_squad_and_title (conn , cur , table_name , rtc )
262
+ update_squad_and_title (cur_csv , conn_zuul , cur_zuul , rtc , table_name )
246
263
247
- cur .close ()
248
- conn .close ()
264
+ cur_csv .close ()
265
+ conn_csv .close ()
266
+ cur_zuul .close ()
267
+ conn_zuul .close ()
268
+
269
+
270
+ if __name__ == "__main__" :
271
+ org_string = "docs"
272
+ failed_table = "open_prs"
273
+ rtc_table = "repo_title_category"
274
+
275
+ main (org_string , failed_table , rtc_table )
276
+ main (f"{ org_string } -swiss" , f"{ failed_table } _swiss" , f"{ rtc_table } _swiss" )
249
277
250
278
251
279
if __name__ == "__main__" :
0 commit comments