@@ -24,7 +24,7 @@ async function run() {
2424 const { throttling } = require ( "@octokit/plugin-throttling" ) ;
2525 const MyOctokit = Octokit . plugin ( throttling ) ;
2626 const octokit = new MyOctokit ( {
27- auth : token ,
27+ auth : token ,
2828 baseUrl : url ,
2929 // To avoid "API rate limit exceeded" errors
3030 throttle : {
@@ -45,13 +45,49 @@ async function run() {
4545 ) ;
4646 } ,
4747 } ,
48- } ) ;
48+ } ) ;
4949 let workflows = await octokit
5050 . paginate ( "GET /repos/:owner/:repo/actions/workflows" , {
5151 owner : repo_owner ,
5252 repo : repo_name ,
5353 } ) ;
5454
55+ let workflow_ids = workflows . map ( w => w . id ) ;
56+
57+ // Gets all workflow runs for the repository
58+ // see https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository
59+ let all_runs = await octokit
60+ . paginate ( 'GET /repos/:owner/:repo/actions/runs' , {
61+ owner : repo_owner ,
62+ repo : repo_name ,
63+ } ) ;
64+
65+ // Creates the delete runs array, and adds the runs that don't have a workflow associated with it
66+ let del_runs = new Array ( ) ;
67+ del_runs = all_runs . filter (
68+ ( { workflow_id } ) => {
69+ return workflow_ids . some ( w => w === workflow_id ) ;
70+ }
71+ ) ;
72+
73+ for ( const del of del_runs ) {
74+ core . debug ( `Deleting '${ del . name } ' workflow run ${ del . id } ` ) ;
75+ // Execute the API "Delete a workflow run", see 'https://octokit.github.io/rest.js/v18#actions-delete-workflow-run'
76+
77+ if ( dry_run ) {
78+ console . log ( `[dry-run] 🚀 Delete run ${ del . id } of '${ del . name } ' workflow` ) ;
79+ continue ;
80+ }
81+
82+ await octokit . actions . deleteWorkflowRun ( {
83+ owner : repo_owner ,
84+ repo : repo_name ,
85+ run_id : del . id
86+ } ) ;
87+
88+ console . log ( `🚀 Delete run ${ del . id } of '${ del . name } ' workflow` ) ;
89+ }
90+
5591 if ( delete_workflow_pattern ) {
5692 console . log ( `💬 workflows containing '${ delete_workflow_pattern } ' will be targeted` ) ;
5793 workflows = workflows . filter (
@@ -97,20 +133,20 @@ async function run() {
97133 console . log ( `👻 Skipped '${ workflow . name } ' workflow run ${ run . id } : it is in '${ run . status } ' state` ) ;
98134 continue ;
99135 }
100-
136+
101137 if ( check_pullrequest_exist && run . pull_requests . length > 0 ) {
102138 console . log ( ` Skipping '${ workflow . name } ' workflow run ${ run . id } because PR is attached.` ) ;
103139 continue ;
104140 }
105141
106- if ( check_branch_existence && branchNames . indexOf ( run . head_branch ) === 1 ) {
142+ if ( check_branch_existence && branchNames . indexOf ( run . head_branch ) === 1 ) {
107143 console . log ( ` Skipping '${ workflow . name } ' workflow run ${ run . id } because branch is still active.` ) ;
108144 continue ;
109145 }
110146
111147 if ( delete_run_by_conclusion_pattern
112- && ! delete_run_by_conclusion_pattern . split ( "," ) . map ( x => x . trim ( ) ) . includes ( run . conclusion )
113- && delete_run_by_conclusion_pattern . toUpperCase ( ) !== "ALL" ) {
148+ && ! delete_run_by_conclusion_pattern . split ( "," ) . map ( x => x . trim ( ) ) . includes ( run . conclusion )
149+ && delete_run_by_conclusion_pattern . toUpperCase ( ) !== "ALL" ) {
114150 core . debug ( ` Skipping '${ workflow . name } ' workflow run ${ run . id } because conclusion was ${ run . conclusion } ` ) ;
115151 continue ;
116152 }
0 commit comments