@@ -32,105 +32,105 @@ async function run() {
32
32
* @param {{repo: string, owner: string, clearDevelop: boolean, clearPending: boolean, clearBranches: boolean, workflowName: string} } options
33
33
*/
34
34
async function clearGithubCaches ( octokit , { repo, owner, clearDevelop, clearPending, clearBranches, workflowName } ) {
35
+ let deletedCaches = 0 ;
36
+ let remainingCaches = 0 ;
37
+
38
+ let deletedSize = 0 ;
39
+ let remainingSize = 0 ;
40
+
41
+ /** @type {Map<number, ReturnType<typeof octokit.rest.pulls.get>> } */
42
+ const cachedPrs = new Map ( ) ;
43
+ /** @type {Map<string, ReturnType<typeof octokit.rest.actions.listWorkflowRunsForRepo>> } */
44
+ const cachedWorkflows = new Map ( ) ;
45
+
46
+ /**
47
+ * Clear caches.
48
+ *
49
+ * @param {{ref: string} } options
50
+ */
51
+ const shouldClearCache = async ( { ref } ) => {
52
+ // Do not clear develop caches if clearDevelop is false.
53
+ if ( ! clearDevelop && ref === 'refs/heads/develop' ) {
54
+ core . info ( '> Keeping cache because it is on develop.' ) ;
55
+ return false ;
56
+ }
57
+
58
+ // There are two fundamental paths here:
59
+ // If the cache belongs to a PR, we need to check if the PR has any pending workflows.
60
+ // Else, we assume the cache belongs to a branch, where we do not check for pending workflows
61
+ const pullNumber = / ^ r e f s \/ p u l l \/ ( \d + ) \/ m e r g e $ / . exec ( ref ) ?. [ 1 ] ;
62
+ const isPr = ! ! pullNumber ;
63
+
64
+ // Case 1: This is a PR, and we do not want to clear pending PRs
65
+ // In this case, we need to fetch all PRs and workflow runs to check them
66
+ if ( isPr && ! clearPending ) {
67
+ const pr =
68
+ cachedPrs . get ( pullNumber ) ||
69
+ ( await octokit . rest . pulls . get ( {
70
+ owner,
71
+ repo,
72
+ pull_number : pullNumber ,
73
+ } ) ) ;
74
+ cachedPrs . set ( pullNumber , pr ) ;
75
+
76
+ const prBranch = pr . data . head . ref ;
77
+
78
+ // Check if PR has any pending workflows
79
+ const workflowRuns =
80
+ cachedWorkflows . get ( prBranch ) ||
81
+ ( await octokit . rest . actions . listWorkflowRunsForRepo ( {
82
+ repo,
83
+ owner,
84
+ branch : prBranch ,
85
+ } ) ) ;
86
+ cachedWorkflows . set ( prBranch , workflowRuns ) ;
87
+
88
+ // We only care about the relevant workflow
89
+ const relevantWorkflowRuns = workflowRuns . data . workflow_runs . filter ( workflow => workflow . name === workflowName ) ;
90
+
91
+ const latestWorkflowRun = relevantWorkflowRuns [ 0 ] ;
92
+
93
+ core . info ( `> Latest relevant workflow run: ${ latestWorkflowRun . html_url } ` ) ;
94
+
95
+ // No relevant workflow? Clear caches!
96
+ if ( ! latestWorkflowRun ) {
97
+ core . info ( '> Clearing cache because no relevant workflow was found.' ) ;
98
+ return true ;
99
+ }
100
+
101
+ // If the latest run was not successful, keep caches
102
+ // as either the run may be in progress,
103
+ // or failed - in which case we may want to re-run the workflow
104
+ if ( latestWorkflowRun . conclusion !== 'success' ) {
105
+ core . info ( `> Keeping cache because latest workflow is ${ latestWorkflowRun . conclusion } .` ) ;
106
+ return false ;
107
+ }
108
+
109
+ core . info ( `> Clearing cache because latest workflow run is ${ latestWorkflowRun . conclusion } .` ) ;
110
+ } else if ( isPr ) {
111
+ // Case 2: This is a PR, but we do not want to clear pending PRs
112
+ // In this case, this cache should never be cleared
113
+ core . info ( '> Keeping cache of every PR workflow run.' ) ;
114
+ return false ;
115
+ } else if ( clearBranches ) {
116
+ // Case 3: This is not a PR, and we want to clean branches
117
+ core . info ( '> Clearing cache because it is not a PR.' ) ;
118
+ return true ;
119
+ } else {
120
+ // Case 4: This is not a PR, and we do not want to clean branches
121
+ core . info ( '> Keeping cache for non-PR workflow run.' ) ;
122
+ return false ;
123
+ }
124
+ } ;
125
+
35
126
for await ( const response of octokit . paginate . iterator ( octokit . rest . actions . getActionsCacheList , {
36
127
owner,
37
128
repo,
38
129
} ) ) {
39
- /** @type {Map<number, ReturnType<typeof octokit.rest.pulls.get>> } */
40
- const cachedPrs = new Map ( ) ;
41
- /** @type {Map<string, ReturnType<typeof octokit.rest.actions.listWorkflowRunsForRepo>> } */
42
- const cachedWorkflows = new Map ( ) ;
43
-
44
- let deletedCaches = 0 ;
45
- let remainingCaches = 0 ;
46
-
47
- let deletedSize = 0 ;
48
- let remainingSize = 0 ;
49
-
50
130
if ( ! response . data . length ) {
51
131
break ;
52
132
}
53
133
54
- /**
55
- * Clear caches.
56
- *
57
- * @param {{ref: string} } options
58
- */
59
- const shouldClearCache = async ( { ref } ) => {
60
- // Do not clear develop caches if clearDevelop is false.
61
- if ( ! clearDevelop && ref === 'refs/heads/develop' ) {
62
- core . info ( '> Keeping cache because it is on develop.' ) ;
63
- return false ;
64
- }
65
-
66
- // There are two fundamental paths here:
67
- // If the cache belongs to a PR, we need to check if the PR has any pending workflows.
68
- // Else, we assume the cache belongs to a branch, where we do not check for pending workflows
69
- const pullNumber = / ^ r e f s \/ p u l l \/ ( \d + ) \/ m e r g e $ / . exec ( ref ) ?. [ 1 ] ;
70
- const isPr = ! ! pullNumber ;
71
-
72
- // Case 1: This is a PR, and we do not want to clear pending PRs
73
- // In this case, we need to fetch all PRs and workflow runs to check them
74
- if ( isPr && ! clearPending ) {
75
- const pr =
76
- cachedPrs . get ( pullNumber ) ||
77
- ( await octokit . rest . pulls . get ( {
78
- owner,
79
- repo,
80
- pull_number : pullNumber ,
81
- } ) ) ;
82
- cachedPrs . set ( pullNumber , pr ) ;
83
-
84
- const prBranch = pr . data . head . ref ;
85
-
86
- // Check if PR has any pending workflows
87
- const workflowRuns =
88
- cachedWorkflows . get ( prBranch ) ||
89
- ( await octokit . rest . actions . listWorkflowRunsForRepo ( {
90
- repo,
91
- owner,
92
- branch : prBranch ,
93
- } ) ) ;
94
- cachedWorkflows . set ( prBranch , workflowRuns ) ;
95
-
96
- // We only care about the relevant workflow
97
- const relevantWorkflowRuns = workflowRuns . data . workflow_runs . filter ( workflow => workflow . name === workflowName ) ;
98
-
99
- const latestWorkflowRun = relevantWorkflowRuns [ 0 ] ;
100
-
101
- core . info ( `> Latest relevant workflow run: ${ latestWorkflowRun . html_url } ` ) ;
102
-
103
- // No relevant workflow? Clear caches!
104
- if ( ! latestWorkflowRun ) {
105
- core . info ( '> Clearing cache because no relevant workflow was found.' ) ;
106
- return true ;
107
- }
108
-
109
- // If the latest run was not successful, keep caches
110
- // as either the run may be in progress,
111
- // or failed - in which case we may want to re-run the workflow
112
- if ( latestWorkflowRun . conclusion !== 'success' ) {
113
- core . info ( `> Keeping cache because latest workflow is ${ latestWorkflowRun . conclusion } .` ) ;
114
- return false ;
115
- }
116
-
117
- core . info ( `> Clearing cache because latest workflow run is ${ latestWorkflowRun . conclusion } .` ) ;
118
- } else if ( isPr ) {
119
- // Case 2: This is a PR, but we do not want to clear pending PRs
120
- // In this case, this cache should never be cleared
121
- core . info ( '> Keeping cache of every PR workflow run.' ) ;
122
- return false ;
123
- } else if ( clearBranches ) {
124
- // Case 3: This is not a PR, and we want to clean branches
125
- core . info ( '> Clearing cache because it is not a PR.' ) ;
126
- return true ;
127
- } else {
128
- // Case 4: This is not a PR, and we do not want to clean branches
129
- core . info ( '> Keeping cache for non-PR workflow run.' ) ;
130
- return false ;
131
- }
132
- } ;
133
-
134
134
for ( const { id, ref, size_in_bytes } of response . data ) {
135
135
core . info ( `Checking cache ${ id } for ${ ref } ...` ) ;
136
136
@@ -152,15 +152,15 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend
152
152
remainingSize += size_in_bytes ;
153
153
}
154
154
}
155
+ }
155
156
156
- const format = new Intl . NumberFormat ( 'en-US' , {
157
- style : 'decimal' ,
158
- } ) ;
157
+ const format = new Intl . NumberFormat ( 'en-US' , {
158
+ style : 'decimal' ,
159
+ } ) ;
159
160
160
- core . info ( 'Summary:' ) ;
161
- core . info ( `Deleted ${ deletedCaches } caches, freeing up ~${ format . format ( deletedSize / 1000 / 1000 ) } mb.` ) ;
162
- core . info ( `Remaining ${ remainingCaches } caches, using ~${ format . format ( remainingSize / 1000 / 1000 ) } mb.` ) ;
163
- }
161
+ core . info ( 'Summary:' ) ;
162
+ core . info ( `Deleted ${ deletedCaches } caches, freeing up ~${ format . format ( deletedSize / 1000 / 1000 ) } mb.` ) ;
163
+ core . info ( `Remaining ${ remainingCaches } caches, using ~${ format . format ( remainingSize / 1000 / 1000 ) } mb.` ) ;
164
164
}
165
165
166
166
run ( ) ;
0 commit comments