0

List Incidents

by
Published 4 days ago

List incidents, optionally filtered by status, service, urgency, and time range. Returns the incidents array plus pagination fields (limit, offset, more, total).

Script pagerduty
  • Submitted by hugo989 Typescript (fetch-only)
    Created 5 days ago
    1
    //native
    2
    
    
    3
    /**
    4
     * List Incidents
    5
     * List incidents, optionally filtered by status, service, urgency, and time range. Returns the incidents array plus pagination fields (limit, offset, more, total).
    6
     */
    7
    export async function main(
    8
      auth: RT.Pagerduty,
    9
      statuses: ("triggered" | "acknowledged" | "resolved")[] | undefined,
    10
      service_ids: string[] | undefined,
    11
      urgencies: ("high" | "low")[] | undefined,
    12
      since: string | undefined,
    13
      until: string | undefined,
    14
      sort_by: string | undefined,
    15
      limit: number | undefined,
    16
      offset: number | undefined,
    17
    ) {
    18
      const url = new URL("https://api.pagerduty.com/incidents")
    19
      if (statuses) for (const s of statuses) url.searchParams.append("statuses[]", s)
    20
      if (service_ids)
    21
        for (const s of service_ids) url.searchParams.append("service_ids[]", s)
    22
      if (urgencies) for (const u of urgencies) url.searchParams.append("urgencies[]", u)
    23
      if (since !== undefined && since !== "") url.searchParams.append("since", since)
    24
      if (until !== undefined && until !== "") url.searchParams.append("until", until)
    25
      if (sort_by !== undefined && sort_by !== "")
    26
        url.searchParams.append("sort_by", sort_by)
    27
      if (limit !== undefined) url.searchParams.append("limit", String(limit))
    28
      if (offset !== undefined) url.searchParams.append("offset", String(offset))
    29
    
    
    30
      const response = await fetch(url, {
    31
        method: "GET",
    32
        headers: {
    33
          Authorization: `Token token=${auth.token}`,
    34
          Accept: "application/vnd.pagerduty+json;version=2",
    35
        },
    36
      })
    37
    
    
    38
      if (!response.ok) {
    39
        throw new Error(`${response.status} ${await response.text()}`)
    40
      }
    41
    
    
    42
      return await response.json()
    43
    }
    44