List Dependabot alerts for a repository

You must use an access token with the `security_events` scope to use this endpoint with private repositories. You can also use tokens with the `public_repo` scope for public repositories only. GitHub Apps must have **Dependabot alerts** 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 Dependabot alerts for a repository
6
 * You must use an access token with the `security_events` scope to use this endpoint with private repositories.
7
You can also use tokens with the `public_repo` scope for public repositories only.
8
GitHub Apps must have **Dependabot alerts** read permission to use this endpoint.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  state: string | undefined,
15
  severity: string | undefined,
16
  ecosystem: string | undefined,
17
  package: string | undefined,
18
  manifest: string | undefined,
19
  scope: "development" | "runtime" | undefined,
20
  sort: "created" | "updated" | undefined,
21
  direction: "asc" | "desc" | undefined,
22
  page: string | undefined,
23
  per_page: string | undefined,
24
  before: string | undefined,
25
  after: string | undefined,
26
  first: string | undefined,
27
  last: string | undefined
28
) {
29
  const url = new URL(
30
    `https://api.github.com/repos/${owner}/${repo}/dependabot/alerts`
31
  );
32
  for (const [k, v] of [
33
    ["state", state],
34
    ["severity", severity],
35
    ["ecosystem", ecosystem],
36
    ["package", package],
37
    ["manifest", manifest],
38
    ["scope", scope],
39
    ["sort", sort],
40
    ["direction", direction],
41
    ["page", page],
42
    ["per_page", per_page],
43
    ["before", before],
44
    ["after", after],
45
    ["first", first],
46
    ["last", last],
47
  ]) {
48
    if (v !== undefined && v !== "") {
49
      url.searchParams.append(k, v);
50
    }
51
  }
52
  const response = await fetch(url, {
53
    method: "GET",
54
    headers: {
55
      Authorization: "Bearer " + auth.token,
56
    },
57
    body: undefined,
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65