List instances of a code scanning alert

Lists all instances of the specified code scanning alert. You must use an access token with the `security_events` scope to use this endpoint with private repos, the `public_repo` scope also grants permission to read security events on public repos only. GitHub Apps must have the `security_events` 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 instances of a code scanning alert
6
 * Lists all instances of the specified code scanning alert.
7
You must use an access token with the `security_events` scope to use this endpoint with private repos,
8
the `public_repo` scope also grants permission to read security events on public repos only.
9
GitHub Apps must have the `security_events` read permission to use this endpoint.
10
 */
11
export async function main(
12
  auth: Github,
13
  owner: string,
14
  repo: string,
15
  alert_number: string,
16
  page: string | undefined,
17
  per_page: string | undefined,
18
  ref: string | undefined
19
) {
20
  const url = new URL(
21
    `https://api.github.com/repos/${owner}/${repo}/code-scanning/alerts/${alert_number}/instances`
22
  );
23
  for (const [k, v] of [
24
    ["page", page],
25
    ["per_page", per_page],
26
    ["ref", ref],
27
  ]) {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45