Edits history of script submission #22718 for ' List Projects (wiz)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Projects
     * List Wiz projects (logical groupings of cloud resources used to scope issues and access), with their security score and resource counts.
     */
    export async function main(
      auth: RT.Wiz,
      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 query = `
    query ListProjects($first: Int, $after: String) {
      projects(first: $first, after: $after) {
        totalCount
        pageInfo { hasNextPage endCursor }
        nodes {
          id
          name
          slug
          description
          businessUnit
          archived
          securityScore
          riskProfile { businessImpact }
          cloudAccountCount
          repositoryCount
          kubernetesClusterCount
          workloadCount
          entityCount
          teamMemberCount
          projectOwners { id }
        }
      }
    }`
    
      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 },
        }),
      })
    
      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.projects
    }
    

    Submitted by hugo989 5 days ago