Edits history of script submission #22636 for ' List On-Calls (pagerduty)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List On-Calls
     * List on-call entries — who is on call now or during a time window — filterable by schedule IDs, escalation policy IDs, and user IDs. Time params use ISO 8601 with an explicit UTC offset; the range cannot exceed 3 months.
     */
    export async function main(
      auth: RT.Pagerduty,
      schedule_ids: string[] | undefined,
      escalation_policy_ids: string[] | undefined,
      user_ids: string[] | undefined,
      since: string | undefined,
      until: string | undefined,
      earliest: boolean | undefined,
      limit: number | undefined,
      offset: number | undefined,
    ) {
      const url = new URL("https://api.pagerduty.com/oncalls")
      if (schedule_ids)
        for (const s of schedule_ids) url.searchParams.append("schedule_ids[]", s)
      if (escalation_policy_ids)
        for (const e of escalation_policy_ids)
          url.searchParams.append("escalation_policy_ids[]", e)
      if (user_ids) for (const u of user_ids) url.searchParams.append("user_ids[]", u)
      if (since !== undefined && since !== "") url.searchParams.append("since", since)
      if (until !== undefined && until !== "") url.searchParams.append("until", until)
      if (earliest !== undefined)
        url.searchParams.append("earliest", String(earliest))
      if (limit !== undefined) url.searchParams.append("limit", String(limit))
      if (offset !== undefined) url.searchParams.append("offset", String(offset))
    
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: `Token token=${auth.token}`,
          Accept: "application/vnd.pagerduty+json;version=2",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago