Skip to content

Commit d7df5dc

Browse files
committed
better document
1 parent 5b1e6e2 commit d7df5dc

File tree

1 file changed

+57
-56
lines changed

1 file changed

+57
-56
lines changed

dev-packages/clear-cache-gh-action/index.mjs

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -56,66 +56,67 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend
5656
// There are two fundamental paths here:
5757
// If the cache belongs to a PR, we need to check if the PR has any pending workflows.
5858
// Else, we assume the cache belongs to a branch, where we do not check for pending workflows
59-
const pull_number = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1];
60-
if (pull_number) {
61-
if (!clearPending) {
62-
const pr =
63-
cachedPrs.get(pull_number) ||
64-
(await octokit.rest.pulls.get({
65-
owner,
66-
repo,
67-
pull_number,
68-
}));
69-
cachedPrs.set(pull_number, pr);
70-
71-
const prBranch = pr.data.head.ref;
72-
73-
// Check if PR has any pending workflows
74-
const workflowRuns =
75-
cachedWorkflows.get(prBranch) ||
76-
(await octokit.rest.actions.listWorkflowRunsForRepo({
77-
repo,
78-
owner,
79-
branch: prBranch,
80-
}));
81-
cachedWorkflows.set(prBranch, workflowRuns);
82-
83-
// We only care about the relevant workflow
84-
const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(
85-
workflow => workflow.name === workflowName,
86-
);
87-
88-
const latestWorkflowRun = relevantWorkflowRuns[0];
89-
90-
core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`);
91-
92-
// No relevant workflow? Clear caches!
93-
if (!latestWorkflowRun) {
94-
core.info('> Clearing cache because no relevant workflow was found.');
95-
continue;
96-
}
97-
98-
// If the latest run was not successful, keep caches
99-
// as either the run may be in progress,
100-
// or failed - in which case we may want to re-run the workflow
101-
if (latestWorkflowRun.conclusion !== 'success') {
102-
core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`);
103-
continue;
104-
}
105-
106-
core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`);
107-
} else {
108-
core.info('> Keeping cache of every PR workflow run.');
59+
const pullNumber = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1];
60+
const isPr = !!pullNumber;
61+
62+
// Case 1: This is a PR, and we do not want to clear pending PRs
63+
// In this case, we need to fetch all PRs and workflow runs to check them
64+
if (isPr && !clearPending) {
65+
const pr =
66+
cachedPrs.get(pullNumber) ||
67+
(await octokit.rest.pulls.get({
68+
owner,
69+
repo,
70+
pull_number: pullNumber,
71+
}));
72+
cachedPrs.set(pullNumber, pr);
73+
74+
const prBranch = pr.data.head.ref;
75+
76+
// Check if PR has any pending workflows
77+
const workflowRuns =
78+
cachedWorkflows.get(prBranch) ||
79+
(await octokit.rest.actions.listWorkflowRunsForRepo({
80+
repo,
81+
owner,
82+
branch: prBranch,
83+
}));
84+
cachedWorkflows.set(prBranch, workflowRuns);
85+
86+
// We only care about the relevant workflow
87+
const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(workflow => workflow.name === workflowName);
88+
89+
const latestWorkflowRun = relevantWorkflowRuns[0];
90+
91+
core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`);
92+
93+
// No relevant workflow? Clear caches!
94+
if (!latestWorkflowRun) {
95+
core.info('> Clearing cache because no relevant workflow was found.');
10996
continue;
11097
}
111-
} else {
112-
// This means this is not a pull request, so check clearBranches
113-
if (clearBranches) {
114-
core.info('> Clearing cache because it is not a PR.');
115-
} else {
116-
core.info('> Keeping cache for non-PR workflow run.');
98+
99+
// If the latest run was not successful, keep caches
100+
// as either the run may be in progress,
101+
// or failed - in which case we may want to re-run the workflow
102+
if (latestWorkflowRun.conclusion !== 'success') {
103+
core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`);
117104
continue;
118105
}
106+
107+
core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`);
108+
} else if (isPr) {
109+
// Case 2: This is a PR, but we do not want to clear pending PRs
110+
// In this case, this cache should never be cleared
111+
core.info('> Keeping cache of every PR workflow run.');
112+
continue;
113+
} else if (clearBranches) {
114+
// Case 3: This is not a PR, and we want to clean branches
115+
core.info('> Clearing cache because it is not a PR.');
116+
} else {
117+
// Case 4: This is not a PR, and we do not want to clean branches
118+
core.info('> Keeping cache for non-PR workflow run.');
119+
continue;
119120
}
120121

121122
core.info(`> Clearing cache ${id}...`);

0 commit comments

Comments
 (0)