1import * as websecurityscanner from "@distilled.cloud/gcp/websecurityscanner_v1";
2import * as Effect from "effect/Effect";
3import { createPhysicalName } from "../../PhysicalName.ts";
4
5export const lastSegment = (value: string) => {
6 const trimmed = value.replace(/\/+$/, "");
7 const parts = trimmed.split("/");
8 return parts[parts.length - 1] || trimmed;
9};
10
11export const projectParent = (project: string) => `projects/${project}`;
12
13export const scanConfigNameOf = (project: string, scanConfigId: string) =>
14 `projects/${project}/scanConfigs/${scanConfigId}`;
15
16export const sameText = (left: string | undefined, right: string | undefined) =>
17 (left ?? "") === (right ?? "");
18
19export const jsonEqual = (left: unknown, right: unknown) =>
20 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
21
22export const sortedStrings = (values: readonly string[] | undefined) =>
23 [...(values ?? [])].slice().sort();
24
25export const stringList = (
26 values: readonly (string | undefined)[] | null | undefined,
27): string[] =>
28 (values ?? []).filter((value): value is string => typeof value === "string");
29
30export const unspecified = (value: string | undefined) =>
31 value === undefined || value.length === 0 || value.endsWith("_UNSPECIFIED")
32 ? ""
33 : value;
34
35export const updateMaskOf = (...fields: Array<string | undefined>) =>
36 fields.filter((field): field is string => field !== undefined).join(",");
37
38export 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
54export 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)));