@@ -51,14 +51,16 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend
5151 break ;
5252 }
5353
54- for ( const { id, ref, size_in_bytes } of response . data ) {
55- core . info ( `Checking cache ${ id } for ${ ref } ...` ) ;
54+ /**
55+ * Clear caches.
56+ *
57+ * @param {{ref: string} } options
58+ */
59+ const shouldClearCache = async ( { ref } ) => {
5660 // Do not clear develop caches if clearDevelop is false.
5761 if ( ! clearDevelop && ref === 'refs/heads/develop' ) {
5862 core . info ( '> Keeping cache because it is on develop.' ) ;
59- remainingCaches ++ ;
60- remainingSize += size_in_bytes ;
61- continue ;
63+ return false ;
6264 }
6365
6466 // There are two fundamental paths here:
@@ -101,46 +103,54 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend
101103 // No relevant workflow? Clear caches!
102104 if ( ! latestWorkflowRun ) {
103105 core . info ( '> Clearing cache because no relevant workflow was found.' ) ;
104- continue ;
106+ return true ;
105107 }
106108
107109 // If the latest run was not successful, keep caches
108110 // as either the run may be in progress,
109111 // or failed - in which case we may want to re-run the workflow
110112 if ( latestWorkflowRun . conclusion !== 'success' ) {
111113 core . info ( `> Keeping cache because latest workflow is ${ latestWorkflowRun . conclusion } .` ) ;
112- continue ;
114+ return false ;
113115 }
114116
115117 core . info ( `> Clearing cache because latest workflow run is ${ latestWorkflowRun . conclusion } .` ) ;
116118 } else if ( isPr ) {
117119 // Case 2: This is a PR, but we do not want to clear pending PRs
118120 // In this case, this cache should never be cleared
119121 core . info ( '> Keeping cache of every PR workflow run.' ) ;
120- remainingCaches ++ ;
121- remainingSize += size_in_bytes ;
122- continue ;
122+ return false ;
123123 } else if ( clearBranches ) {
124124 // Case 3: This is not a PR, and we want to clean branches
125125 core . info ( '> Clearing cache because it is not a PR.' ) ;
126+ return true ;
126127 } else {
127128 // Case 4: This is not a PR, and we do not want to clean branches
128129 core . info ( '> Keeping cache for non-PR workflow run.' ) ;
129- remainingCaches ++ ;
130- remainingSize += size_in_bytes ;
131- continue ;
130+ return false ;
132131 }
132+ } ;
133133
134- core . info ( `> Clearing cache ${ id } ...` ) ;
134+ for ( const { id, ref, size_in_bytes } of response . data ) {
135+ core . info ( `Checking cache ${ id } for ${ ref } ...` ) ;
136+
137+ const shouldDelete = await shouldClearCache ( { ref } ) ;
138+
139+ if ( shouldDelete ) {
140+ core . info ( `> Clearing cache ${ id } ...` ) ;
135141
136- deletedCaches ++ ;
137- deletedSize += size_in_bytes ;
142+ deletedCaches ++ ;
143+ deletedSize += size_in_bytes ;
138144
139- /* await octokit.rest.actions.deleteActionsCacheById({
145+ /* await octokit.rest.actions.deleteActionsCacheById({
140146 owner,
141147 repo,
142148 cache_id: id,
143149 }); */
150+ } else {
151+ remainingCaches ++ ;
152+ remainingSize += size_in_bytes ;
153+ }
144154 }
145155
146156 const format = new Intl . NumberFormat ( 'en-US' , {
0 commit comments