List package versions for a package owned by an organization

Lists package versions for a package owned by an organization. If the `package_type` belongs to a GitHub Packages registry that only supports repository-scoped permissions, your token must also include the `repo` scope. For the list of GitHub Packages registries that only support repository-scoped permissions, see "[About permissions for GitHub Packages](https://docs.github.com/packages/learn-github-packages/about-permissions-for-github-packages#permissions-for-repository-scoped-packages)."

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List package versions for a package owned by an organization
6
 * Lists package versions for a package owned by an organization.
7

8
If the `package_type` belongs to a GitHub Packages registry that only supports repository-scoped permissions, your token must also include the `repo` scope. For the list of GitHub Packages registries that only support repository-scoped permissions, see "[About permissions for GitHub Packages](https://docs.github.com/packages/learn-github-packages/about-permissions-for-github-packages#permissions-for-repository-scoped-packages)."
9
 */
10
export async function main(
11
  auth: Github,
12
  package_type: "npm" | "maven" | "rubygems" | "docker" | "nuget" | "container",
13
  package_name: string,
14
  org: string,
15
  page: string | undefined,
16
  per_page: string | undefined,
17
  state: "active" | "deleted" | undefined
18
) {
19
  const url = new URL(
20
    `https://api.github.com/orgs/${org}/packages/${package_type}/${package_name}/versions`
21
  );
22
  for (const [k, v] of [
23
    ["page", page],
24
    ["per_page", per_page],
25
    ["state", state],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44