generated from yumemi-inc/typescript-action-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
72 lines (61 loc) · 1.78 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { exit } from 'node:process';
import { error, getInput, warning } from '@actions/core';
import { context } from '@actions/github';
import type { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types';
import { Octokit } from '@octokit/rest';
import fetch from 'node-fetch';
const getInputRequired = (name: string) =>
getInput(name, {
required: true,
});
const getInputOptional = (name: string) => {
const i = getInput(name);
return i === '' ? undefined : i;
};
(async () => {
const ref = getInputOptional('ref');
const key = getInputOptional('key');
const octokit = new Octokit({
baseUrl: context.apiUrl,
auth: getInputRequired('token'),
request: {
fetch,
},
});
let page = 1;
let caches: RestEndpointMethodTypes['actions']['getActionsCacheList']['response'];
do {
console.log(
`🔍 Retrieving cache list from GitHub Actions... (Page ${page})`,
);
caches = await octokit.actions.getActionsCacheList({
...context.repo,
ref,
key,
page,
per_page: 100,
});
if (page++ === 1 && caches.data.actions_caches.length === 0) {
console.log('✨ No caches found, looks shine!');
break;
}
for (const cache of caches.data.actions_caches) {
if (!cache.id) {
warning('⚠️ Malformed response, skipping');
continue;
}
await octokit.actions.deleteActionsCacheById({
...context.repo,
cache_id: cache.id,
});
console.log(
`✅ Successfully deleted cache '${cache.key}' (${cache.size_in_bytes} bytes)`,
);
}
} while (caches.data.total_count > caches.data.actions_caches.length);
})()
.then()
.catch((e) => {
error(`❌ ${e}`);
exit(1);
});