Edits history of script submission #22632 for ' Create Service (pagerduty)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Service
     * Create a service (a system or application that generates incidents) attached to an escalation policy. Set auto_resolve_timeout to 0 to disable auto-resolve and acknowledgement_timeout to 0 to disable re-triggering after acknowledgment.
     */
    export async function main(
      auth: RT.Pagerduty,
      name: string,
      escalation_policy_id: string,
      description: string | undefined,
      auto_resolve_timeout: number | undefined,
      acknowledgement_timeout: number | undefined,
    ) {
      const service: { [key: string]: any } = {
        type: "service",
        name,
        escalation_policy: {
          id: escalation_policy_id,
          type: "escalation_policy_reference",
        },
      }
      if (description !== undefined && description !== "")
        service.description = description
      if (auto_resolve_timeout !== undefined)
        service.auto_resolve_timeout = auto_resolve_timeout
      if (acknowledgement_timeout !== undefined)
        service.acknowledgement_timeout = acknowledgement_timeout
    
      const response = await fetch("https://api.pagerduty.com/services", {
        method: "POST",
        headers: {
          Authorization: `Token token=${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/vnd.pagerduty+json;version=2",
        },
        body: JSON.stringify({ service }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 days ago