Edits history of script submission #22717 for ' List Issues (wiz)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Issues
     * List Wiz issues (risks) ordered by severity, with optional filters by severity, status and project. Returns a cursor-paginated page.
     */
    export async function main(
      auth: RT.Wiz,
      severity:
        | ("INFORMATIONAL" | "LOW" | "MEDIUM" | "HIGH" | "CRITICAL")[]
        | undefined,
      status: ("OPEN" | "IN_PROGRESS" | "RESOLVED" | "REJECTED")[] | undefined,
      first: number | undefined,
      after: string | undefined
    ) {
      const tokenResponse = await fetch(
        auth.auth_url || "https://auth.app.wiz.io/oauth/token",
        {
          method: "POST",
          headers: { "Content-Type": "application/x-www-form-urlencoded" },
          body: new URLSearchParams({
            grant_type: "client_credentials",
            audience: auth.audience || "wiz-api",
            client_id: auth.client_id,
            client_secret: auth.client_secret,
          }),
        }
      )
      if (!tokenResponse.ok) {
        throw new Error(`${tokenResponse.status} ${await tokenResponse.text()}`)
      }
      const { access_token } = (await tokenResponse.json()) as {
        access_token: string
      }
    
      const filterBy: { [key: string]: any } = {}
      if (severity && severity.length > 0) filterBy.severity = severity
      if (status && status.length > 0) filterBy.status = status
    
      const query = `
    query ListIssues($first: Int, $after: String, $filterBy: IssueFilters, $orderBy: IssueOrder) {
      issues: issuesV2(first: $first, after: $after, filterBy: $filterBy, orderBy: $orderBy) {
        totalCount
        pageInfo { hasNextPage endCursor }
        nodes {
          id
          type
          severity
          status
          createdAt
          updatedAt
          dueAt
          resolvedAt
          statusChangedAt
          resolutionReason
          sourceRule {
            __typename
            ... on Control { id name description }
            ... on CloudConfigurationRule { id name description serviceType }
            ... on CloudEventRule { id name description }
          }
          entitySnapshot {
            id
            name
            type
            nativeType
            status
            cloudPlatform
            region
            subscriptionName
            subscriptionExternalId
            providerId
            tags
          }
          projects { id name slug businessUnit }
          notes { id text createdAt updatedAt user { name email } }
          serviceTickets { externalId name url }
        }
      }
    }`
    
      const response = await fetch(auth.api_endpoint, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${access_token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          query,
          variables: {
            first: first ?? 50,
            after: after || null,
            filterBy,
            orderBy: { field: "SEVERITY", direction: "DESC" },
          },
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const result = (await response.json()) as { data?: any; errors?: any }
      if (result.errors) {
        throw new Error(JSON.stringify(result.errors))
      }
      return result.data.issues
    }
    

    Submitted by hugo989 5 days ago