| 1 | import * as websecurityscanner from "@distilled.cloud/gcp/websecurityscanner_v1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { createPhysicalName } from "../../PhysicalName.ts"; |
| 4 | |
| 5 | export const lastSegment = (value: string) => { |
| 6 | const trimmed = value.replace(/\/+$/, ""); |
| 7 | const parts = trimmed.split("/"); |
| 8 | return parts[parts.length - 1] || trimmed; |
| 9 | }; |
| 10 | |
| 11 | export const projectParent = (project: string) => `projects/${project}`; |
| 12 | |
| 13 | export const scanConfigNameOf = (project: string, scanConfigId: string) => |
| 14 | `projects/${project}/scanConfigs/${scanConfigId}`; |
| 15 | |
| 16 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 17 | (left ?? "") === (right ?? ""); |
| 18 | |
| 19 | export const jsonEqual = (left: unknown, right: unknown) => |
| 20 | JSON.stringify(left ?? null) === JSON.stringify(right ?? null); |
| 21 | |
| 22 | export const sortedStrings = (values: readonly string[] | undefined) => |
| 23 | [...(values ?? [])].slice().sort(); |
| 24 | |
| 25 | export const stringList = ( |
| 26 | values: readonly (string | undefined)[] | null | undefined, |
| 27 | ): string[] => |
| 28 | (values ?? []).filter((value): value is string => typeof value === "string"); |
| 29 | |
| 30 | export const unspecified = (value: string | undefined) => |
| 31 | value === undefined || value.length === 0 || value.endsWith("_UNSPECIFIED") |
| 32 | ? "" |
| 33 | : value; |
| 34 | |
| 35 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 36 | fields.filter((field): field is string => field !== undefined).join(","); |
| 37 | |
| 38 | export const toUserDisplayName = ( |
| 39 | id: string, |
| 40 | requested: string | undefined, |
| 41 | existing: string | undefined, |
| 42 | maxLength = 80, |
| 43 | ) => |
| 44 | Effect.gen(function* () { |
| 45 | if (requested !== undefined && requested.length > 0) return requested; |
| 46 | if (existing !== undefined && existing.length > 0) return existing; |
| 47 | return yield* createPhysicalName({ |
| 48 | id, |
| 49 | maxLength, |
| 50 | lowercase: true, |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | export const getScanConfig = (name: string) => |
| 55 | name.length === 0 |
| 56 | ? Effect.succeed(undefined) |
| 57 | : websecurityscanner |
| 58 | .getProjectsScanConfigs({ name }) |
| 59 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |