0

List tickets

by
Published today

List SecureChange tickets, optionally filtered by status, one page at a time.

Script tufin_securechange Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * List tickets
5
 * List SecureChange tickets, optionally filtered by status. Returns one page; pass next_start as start to get the next one.
6
 */
7
export async function main(
8
  auth: RT.TufinSecurechange,
9
  status:
10
    | (
11
        | "In Progress"
12
        | "Ticket Closed"
13
        | "Ticket Cancelled"
14
        | "Ticket Rejected"
15
        | "Ticket Resolved"
16
      )[]
17
    | undefined,
18
  start: number | undefined,
19
  descending: boolean | undefined
20
) {
21
  const base = auth.base_url.replace(/\/+$/, "")
22
  const url = new URL(`${base}/securechangeworkflow/api/securechange/tickets`)
23
  if (status?.length) url.searchParams.append("status", status.join(","))
24
  if (start != null) url.searchParams.append("start", String(start))
25
  if (descending) url.searchParams.append("desc", "true")
26

27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
31
      Accept: "application/json",
32
    },
33
  })
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  const data = await response.json()
40
  // The API links the next page as tickets.next["@href"]; it is absent on the last page.
41
  const nextUrl: string | null = data?.tickets?.next?.["@href"] ?? null
42
  const nextStart = nextUrl
43
    ? new URL(nextUrl, base).searchParams.get("start")
44
    : null
45
  return {
46
    ...data,
47
    next_url: nextUrl,
48
    next_start: nextStart != null ? Number(nextStart) : null,
49
  }
50
}
51