Edits history of script submission #22630 for ' Create Incident (pagerduty)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_service_id = string
    
    // Live dropdown of the account's services, so the user picks instead of typing a service ID.
    export async function service_id(auth: RT.Pagerduty) {
      const response = await fetch(
        "https://api.pagerduty.com/services?limit=100",
        {
          headers: {
            Authorization: `Token token=${auth.token}`,
            Accept: "application/vnd.pagerduty+json;version=2",
          },
        },
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { services } = (await response.json()) as {
        services: { id: string; name: string }[]
      }
      return services.map((s) => ({ value: s.id, label: `${s.name} (${s.id})` }))
    }
    
    /**
     * Create Incident
     * Open a new incident on a service. Set incident_key to a stable value (e.g. a hash of the source event) to dedupe retries — PagerDuty rejects a second open incident with the same key on the same service. Requires the resource's from_email when the API key is account-scoped.
     */
    export async function main(
      auth: RT.Pagerduty,
      title: string,
      service_id: DynSelect_service_id,
      urgency: "high" | "low" | undefined,
      incident_key: string | undefined,
      details: string | undefined,
      escalation_policy_id: string | undefined,
      priority_id: string | undefined,
    ) {
      const incident: { [key: string]: any } = {
        type: "incident",
        title,
        service: { id: service_id, type: "service_reference" },
      }
      if (urgency !== undefined) incident.urgency = urgency
      if (incident_key !== undefined && incident_key !== "")
        incident.incident_key = incident_key
      if (details !== undefined && details !== "")
        incident.body = { type: "incident_body", details }
      if (escalation_policy_id !== undefined && escalation_policy_id !== "")
        incident.escalation_policy = {
          id: escalation_policy_id,
          type: "escalation_policy_reference",
        }
      if (priority_id !== undefined && priority_id !== "")
        incident.priority = { id: priority_id, type: "priority_reference" }
    
      const headers: { [key: string]: string } = {
        Authorization: `Token token=${auth.token}`,
        "Content-Type": "application/json",
        Accept: "application/vnd.pagerduty+json;version=2",
      }
      if (auth.from_email) headers["From"] = auth.from_email
    
      const response = await fetch("https://api.pagerduty.com/incidents", {
        method: "POST",
        headers,
        body: JSON.stringify({ incident }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago