2
2
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3
3
exports . execute = void 0 ;
4
4
const core_1 = require ( "@actions/core" ) ;
5
+ const github_action_helper_1 = require ( "@technote-space/github-action-helper" ) ;
5
6
const misc_1 = require ( "./utils/misc" ) ;
6
7
const workflow_1 = require ( "./utils/workflow" ) ;
7
8
const execute = async ( logger , octokit , context ) => {
@@ -10,9 +11,9 @@ const execute = async (logger, octokit, context) => {
10
11
core_1 . setOutput ( 'ids' , '' ) ;
11
12
return ;
12
13
}
13
- const runId = misc_1 . getRunId ( ) ;
14
+ const runId = misc_1 . getTargetRunId ( context ) ;
14
15
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 ) ;
16
17
logger . startProcess ( 'run:' ) ;
17
18
console . log ( misc_1 . getFilteredRun ( run ) ) ;
18
19
logger . endProcess ( ) ;
@@ -22,22 +23,25 @@ const execute = async (logger, octokit, context) => {
22
23
logger . startProcess ( 'workflow runs:' ) ;
23
24
console . log ( runs . map ( run => misc_1 . getFilteredRun ( run ) ) ) ;
24
25
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 ) ) ;
25
32
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
- }
34
33
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 ;
36
37
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 ( ',' ) ) ;
41
45
logger . endProcess ( ) ;
42
46
} ;
43
47
exports . execute = execute ;
0 commit comments