13
13
gitea_api_endpoint = "https://gitea.eco.tsi-dev.otc-service.com/api/v1"
14
14
session = requests .Session ()
15
15
session .debug = False
16
- org = "docs"
17
16
gitea_token = os .getenv ("GITEA_TOKEN" )
18
17
github_token = os .getenv ("GITHUB_TOKEN" )
19
18
20
19
db_host = os .getenv ("DB_HOST" )
21
20
db_port = os .getenv ("DB_PORT" )
22
- db_name = os .getenv ("DB_NAME" )
21
+ db_name = os .getenv ("DB_CSV" ) # here we're using main postgres db since we don't need orphan PRs
23
22
db_user = os .getenv ("DB_USER" )
24
23
db_password = os .getenv ("DB_PASSWORD" )
25
24
@@ -34,8 +33,8 @@ def check_env_variables():
34
33
raise Exception (f"Missing environment variable: { var } " )
35
34
36
35
37
- def connect_to_db ():
38
- print ("Connecting to Postgres..." )
36
+ def connect_to_db (db_name ):
37
+ print (f "Connecting to Postgres ( { db_name } ) ..." )
39
38
try :
40
39
return psycopg2 .connect (
41
40
host = db_host ,
@@ -45,7 +44,7 @@ def connect_to_db():
45
44
password = db_password
46
45
)
47
46
except psycopg2 .Error as e :
48
- print (f"Connecting to Postgres: an error occurred while trying to connect to the database: { e } " )
47
+ print (f"Connecting to Postgres: an error occurred while trying to connect to the database { db_name } : { e } " )
49
48
return None
50
49
51
50
@@ -114,21 +113,21 @@ def extract_number_from_body(text):
114
113
return None
115
114
116
115
117
- def get_f_pr_commits (repo , f_pr_number , gitea_token ):
116
+ def get_f_pr_commits (org , repo , f_pr_number , gitea_token ):
118
117
try :
119
118
zuul_url = None
120
119
status = None
121
120
created_at = None
122
121
days_passed = None
123
122
124
- pull_request_resp = session .get (f"{ gitea_api_endpoint } /repos/docs /{ repo } /pulls/{ f_pr_number } /commits?token={ gitea_token } " )
125
- pull_request_resp .raise_for_status () # Raise an exception if the response contains an HTTP error status.
123
+ pull_request_resp = session .get (f"{ gitea_api_endpoint } /repos/{ org } /{ repo } /pulls/{ f_pr_number } /commits?token={ gitea_token } " )
124
+ pull_request_resp .raise_for_status ()
126
125
127
126
f_pr_info = json .loads (pull_request_resp .content .decode ("utf-8" ))
128
127
129
128
if len (f_pr_info ) > 0 :
130
129
f_commit_sha = f_pr_info [0 ]["sha" ]
131
- commit_status_resp = session .get (f"{ gitea_api_endpoint } /repos/docs /{ repo } /statuses/{ f_commit_sha } ?token={ gitea_token } " )
130
+ commit_status_resp = session .get (f"{ gitea_api_endpoint } /repos/{ org } /{ repo } /statuses/{ f_commit_sha } ?token={ gitea_token } " )
132
131
commit_status_resp .raise_for_status ()
133
132
134
133
commit_info = json .loads (commit_status_resp .content .decode ("utf-8" ))
@@ -143,10 +142,10 @@ def get_f_pr_commits(repo, f_pr_number, gitea_token):
143
142
return zuul_url , status , created_at , days_passed
144
143
145
144
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: { e } " )
145
+ print (f"Get failed PR commits: an error occurred while trying to get pull requests of { repo } repo for { org } org : { e } " )
147
146
148
147
149
- def get_failed_prs (repo , gitea_token , conn , cur ):
148
+ def get_failed_prs (org , repo , gitea_token , conn , cur , table_name ):
150
149
try :
151
150
if repo != "doc-exports" and repo != "dsf" :
152
151
page = 1
@@ -174,47 +173,49 @@ def get_failed_prs(repo, gitea_token, conn, cur):
174
173
title = pull_req ["title" ]
175
174
f_pr_url = pull_req ["url" ]
176
175
f_pr_state = pull_req ["state" ]
177
- zuul_url , status , created_at , days_passed = get_f_pr_commits (repo , f_pr_number , gitea_token )
176
+ zuul_url , status , created_at , days_passed = get_f_pr_commits (org , repo , f_pr_number , gitea_token )
178
177
try :
179
178
if all (item is not None for item in [zuul_url , status , created_at , days_passed ]):
180
- cur .execute ("""
181
- INSERT INTO public.failed_zuul_prs
179
+ cur .execute (f """
180
+ INSERT INTO public.{ table_name }
182
181
("Service Name", "Failed PR Title", "Failed PR URL", "Squad", "Failed PR State", "Zuul URL", "Zuul Check Status", "Days Passed", "Parent PR Number")
183
182
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
184
183
""" ,
185
184
(service_name , title , f_pr_url , squad , f_pr_state , zuul_url , status , days_passed , f_par_pr_num )
186
185
)
187
186
conn .commit ()
188
187
except Exception as e :
189
- print (f"Failed PRs: an error occurred while inserting into the failed_zuul_prs table: { e } " )
188
+ print (f"Failed PRs: an error occurred while inserting into { table_name } table: { e } " )
190
189
else :
191
190
continue
191
+ elif org == "docs-swiss" and repo_resp .status_code != 200 :
192
+ break
192
193
page += 1
193
194
194
195
except Exception as e :
195
196
print ('Failed PRs: an error occurred:' , e )
196
197
197
198
198
- def update_squad_and_title (conn , cur ):
199
- print ("Updating squads and titles..." )
199
+ def update_squad_and_title (conn , cur , table_name , rtc ):
200
+ print (f "Updating squads and titles with { rtc } ..." )
200
201
try :
201
- cur .execute ("SELECT * FROM failed_zuul_prs ;" )
202
+ cur .execute (f "SELECT * FROM { table_name } ;" )
202
203
failed_prs_rows = cur .fetchall ()
203
204
204
205
for row in failed_prs_rows :
205
206
cur .execute (
206
- """UPDATE failed_zuul_prs
207
+ f """UPDATE { table_name }
207
208
SET "Service Name" = rtc."Title", "Squad" = rtc."Category"
208
- FROM repo_title_category AS rtc
209
- WHERE failed_zuul_prs ."Service Name" = rtc."Repository"
210
- AND failed_zuul_prs .id = %s;""" ,
209
+ FROM { rtc } AS rtc
210
+ WHERE { table_name } ."Service Name" = rtc."Repository"
211
+ AND { table_name } .id = %s;""" ,
211
212
(row [0 ],)
212
213
)
213
214
cur .execute (
214
- """UPDATE failed_zuul_prs
215
+ f """UPDATE { table_name }
215
216
SET "Squad" = 'Other'
216
- WHERE failed_zuul_prs ."Service Name" IN ('doc-exports', 'docs_on_docs', 'docsportal')
217
- AND failed_zuul_prs .id = %s;""" ,
217
+ WHERE { table_name } ."Service Name" IN ('doc-exports', 'docs_on_docs', 'docsportal')
218
+ AND { table_name } .id = %s;""" ,
218
219
(row [0 ],)
219
220
)
220
221
conn .commit ()
@@ -224,33 +225,40 @@ def update_squad_and_title(conn, cur):
224
225
conn .rollback ()
225
226
226
227
227
- def main ():
228
+ def main (org , table_name , rtc ):
228
229
check_env_variables ()
229
230
230
- conn = connect_to_db ()
231
+ conn = connect_to_db (db_name )
231
232
cur = conn .cursor ()
232
233
233
- cur .execute ("DROP TABLE IF EXISTS failed_zuul_prs " )
234
+ cur .execute (f "DROP TABLE IF EXISTS { table_name } " )
234
235
conn .commit ()
235
236
236
- create_prs_table (conn , cur , "failed_zuul_prs" )
237
+ create_prs_table (conn , cur , table_name )
237
238
238
239
repos = get_repos (org , gitea_token )
239
240
240
241
print ("Gathering PRs info..." )
241
242
for repo in repos :
242
- get_failed_prs (repo , gitea_token , conn , cur )
243
+ get_failed_prs (org , repo , gitea_token , conn , cur , table_name )
243
244
244
- update_squad_and_title (conn , cur )
245
+ update_squad_and_title (conn , cur , table_name , rtc )
245
246
246
247
cur .close ()
247
248
conn .close ()
248
249
250
+
251
+ if __name__ == "__main__" :
252
+ org_string = "docs"
253
+ failed_table = "failed_zuul_prs"
254
+ rtc_table = "repo_title_category"
255
+
256
+ main (org_string , failed_table , rtc_table )
257
+ main (f"{ org_string } -swiss" , f"{ failed_table } _swiss" , f"{ rtc_table } _swiss" )
258
+
259
+
249
260
end_time = time .time ()
250
261
execution_time = end_time - start_time
251
262
minutes , seconds = divmod (execution_time , 60 )
252
263
print (f"Script failed_zuul.py executed in { int (minutes )} minutes { int (seconds )} seconds! Let's go drink some beer :)" )
253
264
254
-
255
- if __name__ == "__main__" :
256
- main ()
0 commit comments