Edits history of script submission #24128 for ' Create ticket (tufin_securechange)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create ticket
     * Open a SecureChange ticket in a workflow, filling the fields of its first step (e.g. a multi_access_request field for an access request). Returns the new ticket ID.
     */
    export async function main(
      auth: RT.TufinSecurechange,
      workflow_name: string,
      subject: string,
      first_step_name: string,
      fields: { [key: string]: any }[],
      priority: "Critical" | "High" | "Normal" | "Low" | undefined
    ) {
      const base = auth.base_url.replace(/\/+$/, "")
      const url = new URL(`${base}/securechangeworkflow/api/securechange/tickets`)
    
      const body = {
        ticket: {
          subject,
          priority: priority || "Normal",
          workflow: { name: workflow_name },
          steps: {
            step: [
              {
                name: first_step_name,
                tasks: { task: { fields: { field: fields } } },
              },
            ],
          },
        },
      }
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      // 201 Created with an empty body; the new ticket's URL is in the Location header.
      const location = response.headers.get("Location") ?? ""
      const id = location.split("/").filter(Boolean).pop()
      return { id: id ? Number(id) : null, location }
    }
    

    Submitted by hugo989 6 hours ago