import { context } from '@actions/github' ; import { octokit , octokitUsingPAT } from './octokit' ; import * as core from '@actions/core' ; export async function reRunLastWorkFlowIfRequired ( ) { if ( context . eventName === "pull_request" ) { core . debug ( `rerun not required for event - pull_request` ) ; return ; } const branch = await getBranchOfPullRequest ( ) ; const workflowId = await getSelfWorkflowId ( ) ; const runs = await listWorkflowRunsInBranch ( branch , workflowId ) ; if ( runs . data . total_count > 0 ) { const run = runs . data . workflow_runs [ 0 ] . id ; const isLastWorkFlowFailed = await checkIfLastWorkFlowFailed ( run ) ; if ( isLastWorkFlowFailed ) { core . debug ( `Rerunning build run ${ run }` ) ; await reRunWorkflow ( run ) . catch ( error => core . error ( `Error occurred when re-running the workflow: ${ error }` ) ) ; } } } async function getBranchOfPullRequest ( ) { const pullRequest = await octokit . pulls . get ( { owner : context . repo . owner , repo : context . repo . repo , pull_number : context . issue . number } ) ; return pullRequest . data . head . ref ; } async function getSelfWorkflowId ( ) { const workflowList = await octokit . actions . listRepoWorkflows ( { owner : context . repo . owner , repo : context . repo . repo , } ) ; const workflow = workflowList . data . workflows . find ( w => w . name == context . workflow ) ; if ( ! workflow ) { throw new Error ( `Unable to locate this workflow's ID in this repository, can't retrigger job..` ) ; } return workflow . id ; } async function listWorkflowRunsInBranch ( branch , workflowId ) { console . debug ( branch ) ; const runs = await octokit . actions . listWorkflowRuns ( { owner : context . repo . owner , repo : context . repo . repo , branch , workflow_id : workflowId , event : 'pull_request_target' } ) ; return runs ; } async function reRunWorkflow ( run ) { await octokitUsingPAT . actions . reRunWorkflow ( { owner : context . repo . owner , repo : context . repo . repo , run_id : run } ) ; } async function checkIfLastWorkFlowFailed ( run ) { const response = await octokit . actions . getWorkflowRun ( { owner : context . repo . owner , repo : context . repo . repo , run_id : run } ) ; return response . data . conclusion == 'failure' ; }