3434 get_workflow_run_summary ,
3535 get_argocd_run_summary ,
3636 get_max_sandboxes_summary ,
37+ get_generic_error_summary ,
3738)
3839from app .helpers .utils import merge_dicts
3940from app .models .request_models import PullRequest , WorkflowRun
4344logger = logging .getLogger (__name__ )
4445
4546
47+ def _post_generic_error_message (check_run : CheckRun , db_session : DBSession ) -> None :
48+ summary = get_generic_error_summary ()
49+ checkrun_status = CheckRunStatus .COMPLETE
50+ checkrun_conclusion = CheckRunStatus .FAILURE
51+ update_checkrun (
52+ check_run ,
53+ summary ,
54+ db_session ,
55+ status = checkrun_status ,
56+ conclusion = checkrun_conclusion ,
57+ )
58+
59+
4660def _sandbox_audit_log (
4761 pull_request : PullRequest , sandbox_status : SandboxStatus , db_session : DBSession
4862) -> None :
@@ -166,32 +180,36 @@ def create_or_update_instance(
166180 db_session = db_session ,
167181 )
168182
169- sandbox = Sandbox (pull_request .sandbox_name )
170-
171- # Don't trigger any workflow if create instance workflow is already running
172- if not sandbox .create_workflow_is_running ():
173- # If sandbox already exists update it, or create a new one
174- if sandbox .exists :
175- # Cancel any existing workflow runs for this sandbox first to avoid merge conflicts
176- sandbox .cancel_any_existing_runs (db_session )
177- _update_instance (pull_request , sandbox )
178- elif is_max_instances_exceeded ():
179- logger .info (
180- "Sandbox %s cannot be created since max sandbox count exceeded" ,
181- sandbox .sandbox_name ,
182- )
183- summary = get_max_sandboxes_summary ()
184- checkrun_status = CheckRunStatus .COMPLETE
185- checkrun_conclusion = CheckRunStatus .FAILURE
186- update_checkrun (
187- check_run ,
188- summary ,
189- db_session ,
190- status = checkrun_status ,
191- conclusion = checkrun_conclusion ,
192- )
193- else :
194- _create_new_instance (pull_request , sandbox , db_session )
183+ try :
184+ sandbox = Sandbox (pull_request .sandbox_name )
185+
186+ # Don't trigger any workflow if create instance workflow is already running
187+ if not sandbox .create_workflow_is_running ():
188+ # If sandbox already exists update it, or create a new one
189+ if sandbox .exists :
190+ # Cancel any existing workflow runs for this sandbox first to avoid merge conflicts
191+ sandbox .cancel_any_existing_runs (db_session )
192+ _update_instance (pull_request , sandbox )
193+ elif is_max_instances_exceeded ():
194+ logger .info (
195+ "Sandbox %s cannot be created since max sandbox count exceeded" ,
196+ sandbox .sandbox_name ,
197+ )
198+ summary = get_max_sandboxes_summary ()
199+ checkrun_status = CheckRunStatus .COMPLETE
200+ checkrun_conclusion = CheckRunStatus .FAILURE
201+ update_checkrun (
202+ check_run ,
203+ summary ,
204+ db_session ,
205+ status = checkrun_status ,
206+ conclusion = checkrun_conclusion ,
207+ )
208+ else :
209+ _create_new_instance (pull_request , sandbox , db_session )
210+ except Exception as e :
211+ _post_generic_error_message (check_run , db_session )
212+ raise e
195213
196214
197215def delete_instance (
@@ -338,30 +356,49 @@ def handle_workflow_run(
338356 )
339357 return
340358
341- if github_action == GithubActionTypes .IN_PROGRESS :
342- post_checkrun_updates (
343- check_run , sandbox , workflow_run , workflow_type , db_session
344- )
345- return
359+ try :
360+ if github_action == GithubActionTypes .IN_PROGRESS :
361+ post_checkrun_updates (
362+ check_run , sandbox , workflow_run , workflow_type , db_session
363+ )
364+ return
365+
366+ if workflow_run .conclusion in WORKFLOW_SUCCESS_CONCLUSION :
367+ trigger_next_workflow (workflow_type , check_run , sandbox , db_session )
368+ post_checkrun_updates (
369+ check_run ,
370+ sandbox ,
371+ workflow_run ,
372+ workflow_type ,
373+ db_session ,
374+ in_progress = False ,
375+ )
376+ elif workflow_run .conclusion == WorkFlowConclusion .CANCELLED :
377+ # Check if the workflow run cancellation was triggered
378+ # intentionally by this app. If so, ignore it. If not,
379+ # then update checkrun as failed.
380+ try :
381+ query = select (CancelledRun ).where (
382+ CancelledRun .run_id == workflow_run .id
383+ )
384+ db_session .fetch_one (query )
385+ except DBOperationException :
386+ post_checkrun_updates (
387+ check_run ,
388+ sandbox ,
389+ workflow_run ,
390+ workflow_type ,
391+ db_session ,
392+ in_progress = False ,
393+ failed = True ,
394+ )
395+ else :
396+ rerun = False
397+ # Handle failed workflow runs
398+ if workflow_run .attempt < config .max_run_attempt :
399+ rerun = True
400+ sandbox .trigger_workflow_rerun (workflow_run .id , workflow_run .rerun_url )
346401
347- if workflow_run .conclusion in WORKFLOW_SUCCESS_CONCLUSION :
348- trigger_next_workflow (workflow_type , check_run , sandbox , db_session )
349- post_checkrun_updates (
350- check_run ,
351- sandbox ,
352- workflow_run ,
353- workflow_type ,
354- db_session ,
355- in_progress = False ,
356- )
357- elif workflow_run .conclusion == WorkFlowConclusion .CANCELLED :
358- # Check if the workflow run cancellation was triggered
359- # intentionally by this app. If so, ignore it. If not,
360- # then update checkrun as failed.
361- try :
362- query = select (CancelledRun ).where (CancelledRun .run_id == workflow_run .id )
363- db_session .fetch_one (query )
364- except DBOperationException :
365402 post_checkrun_updates (
366403 check_run ,
367404 sandbox ,
@@ -370,24 +407,11 @@ def handle_workflow_run(
370407 db_session ,
371408 in_progress = False ,
372409 failed = True ,
410+ rerun = rerun ,
373411 )
374- else :
375- rerun = False
376- # Handle failed workflow runs
377- if workflow_run .attempt < config .max_run_attempt :
378- rerun = True
379- sandbox .trigger_workflow_rerun (workflow_run .id , workflow_run .rerun_url )
380-
381- post_checkrun_updates (
382- check_run ,
383- sandbox ,
384- workflow_run ,
385- workflow_type ,
386- db_session ,
387- in_progress = False ,
388- failed = True ,
389- rerun = rerun ,
390- )
412+ except Exception as e :
413+ _post_generic_error_message (check_run , db_session )
414+ raise e
391415
392416
393417def handle_argocd (
0 commit comments