22Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
33exports . execute = void 0 ;
44const core_1 = require ( "@actions/core" ) ;
5+ const github_action_helper_1 = require ( "@technote-space/github-action-helper" ) ;
56const misc_1 = require ( "./utils/misc" ) ;
67const workflow_1 = require ( "./utils/workflow" ) ;
78const execute = async ( logger , octokit , context ) => {
@@ -10,9 +11,9 @@ const execute = async (logger, octokit, context) => {
1011 core_1 . setOutput ( 'ids' , '' ) ;
1112 return ;
1213 }
13- const runId = misc_1 . getRunId ( ) ;
14+ const runId = misc_1 . getTargetRunId ( context ) ;
1415 logger . info ( 'run id: %d' , runId ) ;
15- const run = await workflow_1 . getWorkflowRun ( octokit , context ) ;
16+ const run = await workflow_1 . getWorkflowRun ( runId , octokit , context ) ;
1617 logger . startProcess ( 'run:' ) ;
1718 console . log ( misc_1 . getFilteredRun ( run ) ) ;
1819 logger . endProcess ( ) ;
@@ -22,22 +23,25 @@ const execute = async (logger, octokit, context) => {
2223 logger . startProcess ( 'workflow runs:' ) ;
2324 console . log ( runs . map ( run => misc_1 . getFilteredRun ( run ) ) ) ;
2425 logger . endProcess ( ) ;
26+ // cancel all workflows except latest one (consider re-run)
27+ const runsWithUpdatedAt = runs . map ( run => ( { ...run , updatedAt : workflow_1 . getWorkflowRunUpdatedAt ( run ) } ) ) ;
28+ const latestRunNumber = Math . max ( ...runsWithUpdatedAt . map ( run => workflow_1 . getWorkflowRunNumber ( run ) ) ) ;
29+ const latestUpdatedAt = Math . max ( ...runsWithUpdatedAt . map ( run => run . updatedAt ) ) ;
30+ const targetRuns = runsWithUpdatedAt . filter ( run => workflow_1 . getWorkflowRunNumber ( run ) < latestRunNumber && // not latest run
31+ ( ! misc_1 . isConsiderReRun ( ) || run . updatedAt < latestUpdatedAt ) ) ;
2532 logger . log ( ) ;
26- const runsWithCreatedAtTime = runs . filter ( run => run . id !== runId ) . map ( run => ( { ...run , createdAt : Date . parse ( workflow_1 . getWorkflowRunCreatedAt ( run ) ) } ) ) ;
27- const createdAt = Date . parse ( workflow_1 . getWorkflowRunCreatedAt ( run ) ) ;
28- const runNumber = workflow_1 . getWorkflowRunNumber ( run ) ;
29- if ( runsWithCreatedAtTime . find ( run => run . createdAt > createdAt || ( run . createdAt === createdAt && workflow_1 . getWorkflowRunNumber ( run ) > runNumber ) ) ) {
30- logger . info ( logger . c ( 'newer job exists' , { color : 'yellow' } ) ) ;
31- core_1 . setOutput ( 'ids' , '' ) ;
32- return ;
33- }
3433 logger . startProcess ( 'Cancelling...' ) ;
35- await Promise . all ( runsWithCreatedAtTime . map ( run => {
34+ const interval = misc_1 . getIntervalMs ( ) ;
35+ await targetRuns . reduce ( async ( prev , run ) => {
36+ await prev ;
3637 logger . log ( 'cancel: %d' , run . id ) ;
37- return workflow_1 . cancelWorkflowRun ( run . id , octokit , context ) ;
38- } ) ) ;
39- logger . info ( 'total: %d' , runsWithCreatedAtTime . length ) ;
40- core_1 . setOutput ( 'ids' , runsWithCreatedAtTime . map ( run => run . id ) . join ( ',' ) ) ;
38+ await workflow_1 . cancelWorkflowRun ( run . id , octokit , context ) ;
39+ if ( interval ) {
40+ await github_action_helper_1 . Utils . sleep ( interval ) ;
41+ }
42+ } , Promise . resolve ( ) ) ;
43+ logger . info ( 'total: %d' , targetRuns . length ) ;
44+ core_1 . setOutput ( 'ids' , targetRuns . map ( run => run . id ) . join ( ',' ) ) ;
4145 logger . endProcess ( ) ;
4246} ;
4347exports . execute = execute ;
0 commit comments