List GitHub Actions caches for a repository

Lists the GitHub Actions caches for a repository. You must authenticate using an access token with the `repo` scope to use this endpoint. GitHub Apps must have the `actions:read` permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List GitHub Actions caches for a repository
6
 * Lists the GitHub Actions caches for a repository.
7
You must authenticate using an access token with the `repo` scope to use this endpoint.
8
GitHub Apps must have the `actions:read` permission to use this endpoint.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  per_page: string | undefined,
15
  page: string | undefined,
16
  ref: string | undefined,
17
  key: string | undefined,
18
  sort: "created_at" | "last_accessed_at" | "size_in_bytes" | undefined,
19
  direction: "asc" | "desc" | undefined
20
) {
21
  const url = new URL(
22
    `https://api.github.com/repos/${owner}/${repo}/actions/caches`
23
  );
24
  for (const [k, v] of [
25
    ["per_page", per_page],
26
    ["page", page],
27
    ["ref", ref],
28
    ["key", key],
29
    ["sort", sort],
30
    ["direction", direction],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "GET",
38
    headers: {
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: undefined,
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49