@@ -590,4 +590,74 @@ export class GithubActionsService {
590590 throw new GithubApiError ( `Error finding workflow run for commit ${ sha . substring ( 0 , 7 ) } ` , error . status , error ) ;
591591 }
592592 }
593+
594+ /**
595+ * Cancel a workflow run
596+ *
597+ * @param owner Repository owner
598+ * @param repo Repository name
599+ * @param runId Workflow run ID to cancel
600+ * @returns Promise resolving when the workflow run is cancelled
601+ * @throws GithubNotFoundError if the workflow run is not found
602+ * @throws GithubApiError for other API errors
603+ */
604+ async cancelWorkflowRun (
605+ owner : string ,
606+ repo : string ,
607+ runId : number
608+ ) : Promise < void > {
609+ try {
610+ await retry (
611+ async ( ) => {
612+ await this . octokit . actions . cancelWorkflowRun ( {
613+ owner,
614+ repo,
615+ run_id : runId ,
616+ } ) ;
617+ } ,
618+ {
619+ retries : this . maxRetries ,
620+ minTimeout : this . minTimeout ,
621+ maxTimeout : this . maxTimeout ,
622+ factor : this . factor ,
623+ onRetry : ( error : Error , attempt : number ) => {
624+ console . log (
625+ `[GitHub Actions] Retry ${ attempt } /${ this . maxRetries } - Cancelling workflow run ${ runId } : ${ error . message } `
626+ ) ;
627+ } ,
628+ }
629+ ) ;
630+
631+ console . log ( `[GitHub Actions] Successfully cancelled workflow run ${ runId } ` ) ;
632+ } catch ( error : any ) {
633+ // Handle specific error cases
634+ if ( error . status === 404 ) {
635+ throw new GithubNotFoundError (
636+ 'workflow run' ,
637+ `${ runId } in repository ${ owner } /${ repo } `
638+ ) ;
639+ }
640+
641+ if ( error . status === 403 ) {
642+ throw new GithubApiError (
643+ `Insufficient permissions to cancel workflow run ${ runId } ` ,
644+ error . status
645+ ) ;
646+ }
647+
648+ if ( error . status === 409 ) {
649+ throw new GithubApiError (
650+ `Workflow run ${ runId } cannot be cancelled (already completed or not cancellable)` ,
651+ error . status
652+ ) ;
653+ }
654+
655+ // Re-throw with more context
656+ throw new GithubApiError (
657+ `Failed to cancel workflow run ${ runId } : ${ error . message } ` ,
658+ error . status || 500 ,
659+ error
660+ ) ;
661+ }
662+ }
593663}
0 commit comments