@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 107 lines 3.96 KB
Raw
1 import * as integrations from "@distilled.cloud/gcp/integrations_v1";
2 import * as Effect from "effect/Effect";
3 import * as Stream from "effect/Stream";
4 import { isDeleted, lastSegment, locationParent } from "./ownership.ts";
5
6 export const parseResourceName = (name: string, collection: string, defaultLocation: string) => {
7 const parts = name.split("/").filter((part) => part.length > 0);
8 const collectionAt = parts.lastIndexOf(collection);
9 const locationsAt = parts.lastIndexOf("locations");
10 const projectsAt = parts.lastIndexOf("projects");
11 const instancesAt = parts.lastIndexOf("sfdcInstances");
12 return {
13 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
14 location:
15 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation,
16 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
17 sfdcInstanceId: instancesAt >= 0 && parts[instancesAt + 1] ? parts[instancesAt + 1]! : "",
18 sfdcInstance:
19 instancesAt >= 0
20 ? parts.slice(0, instancesAt + 2).join("/")
21 : parts.slice(0, Math.max(0, parts.length - 2)).join("/"),
22 parent:
23 collectionAt > 0
24 ? parts.slice(0, collectionAt).join("/")
25 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
26 };
27 };
28
29 const collectPages = <Page, A, E, R>(
30 pages: Stream.Stream<Page, E, R>,
31 items: (page: Page) => readonly A[] | undefined,
32 ) =>
33 pages.pipe(
34 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
35 Stream.runCollect,
36 Effect.map((chunk) => Array.from(chunk)),
37 );
38
39 export const listSfdcInstances = (parent: string) =>
40 collectPages(
41 integrations.listProjectsLocationsSfdcInstances.pages({
42 parent,
43 pageSize: 1000,
44 }),
45 (page) => page.sfdcInstances,
46 ).pipe(
47 Effect.catchTag("NotFound", () =>
48 Effect.succeed([] as integrations.GoogleCloudIntegrationsV1alphaSfdcInstance[]),
49 ),
50 Effect.map((items) => items.filter((item) => !isDeleted(item))),
51 );
52
53 export const listSfdcChannels = (parent: string) =>
54 collectPages(
55 integrations.listProjectsLocationsSfdcInstancesSfdcChannels.pages({
56 parent,
57 pageSize: 1000,
58 }),
59 (page) => page.sfdcChannels,
60 ).pipe(
61 Effect.catchTag("NotFound", () =>
62 Effect.succeed([] as integrations.GoogleCloudIntegrationsV1alphaSfdcChannel[]),
63 ),
64 Effect.map((items) => items.filter((item) => !isDeleted(item))),
65 );
66
67 export const listTemplates = (parent: string) =>
68 collectPages(
69 integrations.listProjectsLocationsTemplates.pages({
70 parent,
71 // A page of 200+ makes Integrations scan a template the project can't
72 // see and fail the whole list with 400 "Template ... is not visible to
73 // the current project"; 100 lists normally.
74 pageSize: 100,
75 }),
76 (page) => page.templates,
77 ).pipe(
78 Effect.catchTag("NotFound", () =>
79 Effect.succeed([] as integrations.GoogleCloudIntegrationsV1alphaTemplate[]),
80 ),
81 );
82
83 export const listOwnedSfdcChannels = (project: string, location: string) =>
84 Effect.gen(function* () {
85 const instances = yield* listSfdcInstances(locationParent(project, location));
86 const groups = yield* Effect.forEach(
87 instances.filter((instance) => (instance.name ?? "").length > 0),
88 (instance) => listSfdcChannels(instance.name!),
89 { concurrency: 4 },
90 );
91 return groups.flat();
92 });
93
94 export const findSfdcInstanceByDescription = (parent: string, description: string) =>
95 listSfdcInstances(parent).pipe(
96 Effect.map((items) => items.find((item) => item.description === description)),
97 );
98
99 export const findSfdcChannelByDescription = (parent: string, description: string) =>
100 listSfdcChannels(parent).pipe(
101 Effect.map((items) => items.find((item) => item.description === description)),
102 );
103
104 export const findTemplateByDescription = (parent: string, description: string) =>
105 listTemplates(parent).pipe(
106 Effect.map((items) => items.find((item) => item.description === description)),
107 );