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

main
ts 202 lines 6.45 KB
Raw
1 import * as firebaseappdistribution from "@distilled.cloud/gcp/firebaseappdistribution_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9 import {
10 getGroup,
11 groupIdOf,
12 groupName,
13 hasOwnershipMarker,
14 listGroups,
15 parseDisplayName,
16 parseGroupName,
17 projectParent,
18 replaceOnIdentity,
19 ResourceNotResolved,
20 retryTransient,
21 sameText,
22 toDisplayName,
23 toGroupId,
24 } from "./internal.ts";
25
26 export type GroupProps = {
27 /**
28 * Group alias (the `{group}` segment of
29 * `projects/{project}/groups/{group}`). 4-63 characters: lowercase
30 * letters, digits, and hyphens. If omitted, a unique id is generated
31 * from the stack, stage, and logical id. Immutable — changing it
32 * replaces the group.
33 */
34 groupId?: string;
35 /**
36 * Display name shown to testers. Defaults to a generated name.
37 */
38 displayName?: string;
39 };
40
41 export type Group = Resource<
42 "GCP.FirebaseAppDistribution.Group",
43 GroupProps,
44 {
45 /** Full resource name `projects/{project}/groups/{group}`. */
46 name: string;
47 /** Group alias (last path segment). */
48 groupId: string;
49 /** Parent project id. */
50 project: string;
51 /** Parent resource `projects/{project}`. */
52 parent: string;
53 /** Display name (a legacy Alchemy ownership prefix is stripped). */
54 displayName: string | undefined;
55 /** Number of testers who are members of this group. */
56 testerCount: number | undefined;
57 /** Number of invite links for this group. */
58 inviteLinkCount: number | undefined;
59 /** Number of releases this group is permitted to access. */
60 releaseCount: number | undefined;
61 },
62 never,
63 Providers
64 >;
65
66 /**
67 * A Firebase App Distribution tester group.
68 *
69 * Groups have no labels field, and the display name is shown to testers,
70 * so Alchemy writes no ownership marker: a generated alias identifies the
71 * group, and `read` reports an explicit alias it has no state for as
72 * unowned. `groupId` is identity — changing it
73 * replaces the group. Display name updates in place.
74 *
75 * ### Creating a Group
76 * **Example:** Generated alias
77 * ```typescript
78 * const group = yield* GCP.FirebaseAppDistribution.Group("Qa", {
79 * displayName: "qa",
80 * });
81 * ```
82 *
83 * **Example:** Explicit alias
84 * ```typescript
85 * const group = yield* GCP.FirebaseAppDistribution.Group("Qa", {
86 * groupId: "qa-testers",
87 * displayName: "qa",
88 * });
89 * ```
90 *
91 * ### Updating a Group
92 * **Example:** Rename
93 * ```typescript
94 * const group = yield* GCP.FirebaseAppDistribution.Group("Qa", {
95 * displayName: "qa-prod",
96 * });
97 * ```
98 *
99 * @resource
100 * @category FirebaseAppDistribution
101 */
102 export const Group = Resource<Group>("GCP.FirebaseAppDistribution.Group");
103
104 const toAttrs = (
105 group: firebaseappdistribution.GoogleFirebaseAppdistroV1Group,
106 project: string,
107 ): Group["Attributes"] => {
108 const name = group.name ?? "";
109 const parsed = parseGroupName(name, project);
110 return {
111 name,
112 groupId: parsed.groupId,
113 project: parsed.project || project,
114 parent: parsed.parent || projectParent(project),
115 displayName: parseDisplayName(group.displayName).displayName,
116 testerCount: group.testerCount,
117 inviteLinkCount: group.inviteLinkCount,
118 releaseCount: group.releaseCount,
119 };
120 };
121
122 export const GroupProvider = () =>
123 Provider.succeed(Group, {
124 stables: ["name", "groupId", "project", "parent"],
125
126 diff: Effect.fn(function* ({ news, olds, output }) {
127 if (!isResolved(news)) return undefined;
128 const previous = groupIdOf(olds?.groupId ?? output?.groupId);
129 const next = groupIdOf(news.groupId);
130 return replaceOnIdentity(previous, next);
131 }),
132
133 read: Effect.fn(function* ({ id, olds, output }) {
134 const env = yield* GcpEnvironment.current;
135 const groupId = yield* toGroupId(id, olds?.groupId ?? output?.groupId, output?.name);
136 const existing = yield* getGroup(output?.name ?? groupName(env.project, groupId));
137 if (existing === undefined) return undefined;
138 const attrs = toAttrs(existing, env.project);
139 // No labels, and the display name is shown to testers: a generated
140 // alias derives from this stack, stage, logical id and instance; an
141 // explicit alias is only ours when state has it.
142 return output !== undefined || olds?.groupId === undefined ? attrs : Unowned(attrs);
143 }),
144
145 list: () =>
146 Effect.gen(function* () {
147 const env = yield* GcpEnvironment.current;
148 const groups = yield* listGroups(projectParent(env.project));
149 return groups
150 .filter((group) => hasOwnershipMarker(group.displayName))
151 .map((group) => toAttrs(group, env.project));
152 }),
153
154 reconcile: Effect.fn(function* ({ id, news, output }) {
155 const env = yield* GcpEnvironment.current;
156 const parent = projectParent(env.project);
157 const groupId = yield* toGroupId(id, news.groupId, output?.groupId ?? output?.name);
158 const name = groupName(env.project, groupId);
159 const desiredDisplay = yield* toDisplayName(id, news.displayName, output?.displayName);
160
161 let current = yield* getGroup(output?.name ?? name);
162
163 if (current === undefined) {
164 const created = yield* retryTransient(
165 firebaseappdistribution.createProjectsGroups({
166 parent,
167 groupId,
168 body: { displayName: desiredDisplay },
169 }),
170 ).pipe(Effect.catchTag("Conflict", () => getGroup(name)));
171 current = created ?? undefined;
172 }
173
174 if (current === undefined) {
175 return yield* new ResourceNotResolved({
176 resourceName: output?.name ?? name,
177 });
178 }
179
180 const currentName = current.name ?? name;
181 if (!sameText(current.displayName, desiredDisplay) && currentName.length > 0) {
182 current = yield* retryTransient(
183 firebaseappdistribution.patchProjectsGroups({
184 name: currentName,
185 updateMask: "display_name",
186 body: { displayName: desiredDisplay },
187 }),
188 );
189 }
190
191 return toAttrs(current, env.project);
192 }),
193
194 delete: Effect.fn(function* ({ output }) {
195 if (!output.name) return;
196 yield* retryTransient(
197 firebaseappdistribution.deleteProjectsGroups({
198 name: output.name,
199 }),
200 ).pipe(Effect.catchTag("NotFound", () => Effect.void));
201 }),
202 });