Edits history of script submission #22644 for ' Update Incident (pagerduty)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Incident
     * Update an incident's status, title, urgency, priority, escalation policy, or assignments. Set status to "acknowledged" to acknowledge or "resolved" to resolve. Pass assignee_ids to replace the current assignees. Requires the resource's from_email when the API key is account-scoped.
     */
    export async function main(
      auth: RT.Pagerduty,
      incident_id: string,
      status: "acknowledged" | "resolved" | undefined,
      title: string | undefined,
      urgency: "high" | "low" | undefined,
      priority_id: string | undefined,
      escalation_policy_id: string | undefined,
      assignee_ids: string[] | undefined,
    ) {
      const incident: { [key: string]: any } = { type: "incident" }
      if (status !== undefined) incident.status = status
      if (title !== undefined && title !== "") incident.title = title
      if (urgency !== undefined) incident.urgency = urgency
      if (priority_id !== undefined && priority_id !== "")
        incident.priority = { id: priority_id, type: "priority_reference" }
      if (escalation_policy_id !== undefined && escalation_policy_id !== "")
        incident.escalation_policy = {
          id: escalation_policy_id,
          type: "escalation_policy_reference",
        }
      if (assignee_ids !== undefined)
        incident.assignments = assignee_ids.map((id) => ({
          assignee: { id, type: "user_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/${incident_id}`,
        {
          method: "PUT",
          headers,
          body: JSON.stringify({ incident }),
        },
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 days ago