-
-
Notifications
You must be signed in to change notification settings - Fork 25
chore(tasks): Script to clear GH cache #1848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||||||||||||||||||||||
| #!/usr/bin/env node | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import { execSync } from 'node:child_process' | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const REPO = 'cedarjs/cedar' | ||||||||||||||||||||||||||
| const API = 'https://api.github.com' | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| interface GitHubCache { | ||||||||||||||||||||||||||
| id: number | ||||||||||||||||||||||||||
| key: string | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| interface GitHubCacheResponse { | ||||||||||||||||||||||||||
| actions_caches: GitHubCache[] | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function getToken(): string { | ||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| return execSync('gh auth token', { encoding: 'utf-8' }).trim() | ||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||
| console.error('Failed to get GitHub token. Run `gh auth login` first.') | ||||||||||||||||||||||||||
| process.exit(1) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| async function listCaches(token: string): Promise<GitHubCache[]> { | ||||||||||||||||||||||||||
| const caches: GitHubCache[] = [] | ||||||||||||||||||||||||||
| let page = 1 | ||||||||||||||||||||||||||
| let hasNextPage = true | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| while (hasNextPage) { | ||||||||||||||||||||||||||
| const res = await fetch( | ||||||||||||||||||||||||||
| `${API}/repos/${REPO}/actions/caches?per_page=100&page=${page}`, | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||
| Authorization: `Bearer ${token}`, | ||||||||||||||||||||||||||
| Accept: 'application/vnd.github+json', | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (!res.ok) { | ||||||||||||||||||||||||||
| console.error(`Failed to list caches: ${res.status} ${await res.text()}`) | ||||||||||||||||||||||||||
| process.exit(1) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const data: GitHubCacheResponse = await res.json() | ||||||||||||||||||||||||||
| caches.push(...data.actions_caches) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (data.actions_caches.length < 100) { | ||||||||||||||||||||||||||
| hasNextPage = false | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| page++ | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return caches | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| async function deleteCache(token: string, id: number): Promise<boolean> { | ||||||||||||||||||||||||||
| const res = await fetch(`${API}/repos/${REPO}/actions/caches/${id}`, { | ||||||||||||||||||||||||||
| method: 'DELETE', | ||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||
| Authorization: `Bearer ${token}`, | ||||||||||||||||||||||||||
| Accept: 'application/vnd.github+json', | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| return res.ok | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| async function deleteCachesByKey(token: string, key: string): Promise<boolean> { | ||||||||||||||||||||||||||
| const res = await fetch( | ||||||||||||||||||||||||||
| `${API}/repos/${REPO}/actions/caches?key=${encodeURIComponent(key)}`, | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| method: 'DELETE', | ||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||
| Authorization: `Bearer ${token}`, | ||||||||||||||||||||||||||
| Accept: 'application/vnd.github+json', | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| return res.ok | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const token = getToken() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const keyFilter = process.argv[2] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (keyFilter) { | ||||||||||||||||||||||||||
| console.log(`Deleting all caches matching key "${keyFilter}"...`) | ||||||||||||||||||||||||||
| const ok = await deleteCachesByKey(token, keyFilter) | ||||||||||||||||||||||||||
| if (ok) { | ||||||||||||||||||||||||||
| console.log('Done.') | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| console.error('Failed to delete caches.') | ||||||||||||||||||||||||||
| process.exit(1) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| console.log('Listing all caches...') | ||||||||||||||||||||||||||
| const caches = await listCaches(token) | ||||||||||||||||||||||||||
| console.log(`Found ${caches.length} cache(s).`) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (caches.length === 0) { | ||||||||||||||||||||||||||
| console.log('Nothing to delete.') | ||||||||||||||||||||||||||
| process.exit(0) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| let deleted = 0 | ||||||||||||||||||||||||||
| for (const cache of caches) { | ||||||||||||||||||||||||||
| const ok = await deleteCache(token, cache.id) | ||||||||||||||||||||||||||
| if (ok) { | ||||||||||||||||||||||||||
| deleted++ | ||||||||||||||||||||||||||
| process.stdout.write('.') | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| process.stdout.write('!') | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+111
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| console.log(`\nDeleted ${deleted}/${caches.length} cache(s).`) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.