0

List code scanning alerts for an organization

by
Published Oct 25, 2023

Lists code scanning alerts for the default branch for all eligible repositories in an organization.

Script github Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List code scanning alerts for an organization
6
 * Lists code scanning alerts for the default branch for all eligible repositories in an organization.
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  tool_name: string | undefined,
12
  tool_guid: string | undefined,
13
  before: string | undefined,
14
  after: string | undefined,
15
  page: string | undefined,
16
  per_page: string | undefined,
17
  direction: "asc" | "desc" | undefined,
18
  state: "open" | "closed" | "dismissed" | "fixed" | undefined,
19
  sort: "created" | "updated" | undefined,
20
  severity:
21
    | "critical"
22
    | "high"
23
    | "medium"
24
    | "low"
25
    | "warning"
26
    | "note"
27
    | "error"
28
    | undefined
29
) {
30
  const url = new URL(
31
    `https://api.github.com/orgs/${org}/code-scanning/alerts`
32
  );
33
  for (const [k, v] of [
34
    ["tool_name", tool_name],
35
    ["tool_guid", tool_guid],
36
    ["before", before],
37
    ["after", after],
38
    ["page", page],
39
    ["per_page", per_page],
40
    ["direction", direction],
41
    ["state", state],
42
    ["sort", sort],
43
    ["severity", severity],
44
  ]) {
45
    if (v !== undefined && v !== "") {
46
      url.searchParams.append(k, v);
47
    }
48
  }
49
  const response = await fetch(url, {
50
    method: "GET",
51
    headers: {
52
      Authorization: "Bearer " + auth.token,
53
    },
54
    body: undefined,
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62