1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as developerconnect from "@distilled.cloud/gcp/developerconnect_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12// Git links need a connection whose GitHub App install is COMPLETE; a fresh
13// connection stays PENDING_USER_OAUTH and link create fails with BadRequest
14// ("connection must have installation_state COMPLETE"). Set
15// GCP_TEST_DEVELOPERCONNECT_GITHUB_TOKEN_SECRET (OAuth token secret version)
16// and GCP_TEST_DEVELOPERCONNECT_GITHUB_INSTALLATION_ID to run.
17const githubTokenSecret =
18 process.env.GCP_TEST_DEVELOPERCONNECT_GITHUB_TOKEN_SECRET;
19const githubInstallationId =
20 process.env.GCP_TEST_DEVELOPERCONNECT_GITHUB_INSTALLATION_ID;
21const runLinkLifecycle = !!githubTokenSecret && !!githubInstallationId;
22const githubConfig = {
23 githubApp: "DEVELOPER_CONNECT",
24 authorizerCredential: { oauthTokenSecretVersion: githubTokenSecret },
25 appInstallationId: githubInstallationId,
26};
27
28const logLevel = Effect.provideService(
29 MinimumLogLevel,
30 process.env.DEBUG ? "Debug" : "Info",
31);
32
33const cloneUri =
34 process.env.GCP_DEVELOPERCONNECT_CLONE_URI ??
35 "https://github.com/octocat/Hello-World.git";
36
37const waitUntilGone = (name: string) =>
38 developerconnect
39 .getProjectsLocationsConnectionsGitRepositoryLinks({ name })
40 .pipe(
41 Effect.as("found" as const),
42 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
43 Effect.repeat({
44 schedule: Schedule.spaced("1 second"),
45 until: (status) => status === "gone",
46 times: 10,
47 }),
48 );
49
50const waitUntilConnectionGone = (name: string) =>
51 developerconnect.getProjectsLocationsConnections({ name }).pipe(
52 Effect.as("found" as const),
53 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
54 Effect.repeat({
55 schedule: Schedule.spaced("1 second"),
56 until: (status) => status === "gone",
57 times: 10,
58 }),
59 );
60
61test.provider(
62 "getProjectsLocationsConnectionsGitRepositoryLinks on a missing link fails with a typed tag",
63 (stack) =>
64 Effect.gen(function* () {
65 const { project } = yield* GcpEnvironment.current;
66 yield* stack.destroy();
67
68 const error = yield* Effect.flip(
69 developerconnect.getProjectsLocationsConnectionsGitRepositoryLinks({
70 name: `projects/${project}/locations/us-central1/connections/alchemy-missing-connection/gitRepositoryLinks/alchemy-missing-link`,
71 }),
72 );
73 expect(error._tag).toEqual("NotFound");
74
75 yield* stack.destroy();
76 }).pipe(logLevel),
77 {
78 tags: ["provider:gcp", "provider:gcp:developerconnect", "live"],
79 timeout: 90_000,
80 },
81);
82
83test.provider.skipIf(!runLinkLifecycle)(
84 "create, update, replace, and delete a git repository link",
85 (stack) =>
86 Effect.gen(function* () {
87 yield* stack.destroy();
88
89 const created = yield* stack.deploy(
90 Effect.gen(function* () {
91 const github = yield* GCP.DeveloperConnect.Connection("Github", {
92 location: "us-central1",
93 githubConfig,
94 labels: { env: "test" },
95 });
96 const source =
97 yield* GCP.DeveloperConnect.ConnectionsGitRepositoryLink("Source", {
98 connection: github.name,
99 cloneUri,
100 labels: { env: "test" },
101 });
102 return { github, source };
103 }),
104 );
105
106 expect(created.source.gitRepositoryLinkId).toEqual(expect.any(String));
107 expect(created.source.name).toContain("/gitRepositoryLinks/");
108 expect(created.source.connection).toEqual(created.github.name);
109 expect(created.source.cloneUri).toEqual(cloneUri);
110 expect(created.source.labels).toMatchObject({ env: "test" });
111
112 const fetched =
113 yield* developerconnect.getProjectsLocationsConnectionsGitRepositoryLinks(
114 {
115 name: created.source.name,
116 },
117 );
118 expect(fetched.name).toEqual(created.source.name);
119 expect(fetched.cloneUri).toEqual(cloneUri);
120 expect(fetched.labels?.env).toEqual("test");
121 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
122
123 const updated = yield* stack.deploy(
124 Effect.gen(function* () {
125 const github = yield* GCP.DeveloperConnect.Connection("Github", {
126 connectionId: created.github.connectionId,
127 location: "us-central1",
128 githubConfig,
129 labels: { env: "test" },
130 });
131 const source =
132 yield* GCP.DeveloperConnect.ConnectionsGitRepositoryLink("Source", {
133 connection: github.name,
134 gitRepositoryLinkId: created.source.gitRepositoryLinkId,
135 cloneUri,
136 labels: { env: "prod", role: "source" },
137 });
138 return { github, source };
139 }),
140 );
141
142 expect(updated.source.connection).toEqual(created.github.name);
143 expect(updated.source.labels).toMatchObject({
144 env: "prod",
145 role: "source",
146 });
147
148 const fetchedUpdate =
149 yield* developerconnect.getProjectsLocationsConnectionsGitRepositoryLinks(
150 {
151 name: updated.source.name,
152 },
153 );
154 expect(fetchedUpdate.labels?.env).toEqual("prod");
155 expect(fetchedUpdate.labels?.role).toEqual("source");
156
157 yield* stack.destroy();
158
159 const gone = yield* waitUntilGone(updated.source.name);
160 expect(gone).toEqual("gone");
161 const connectionGone = yield* waitUntilConnectionGone(
162 created.github.name,
163 );
164 expect(connectionGone).toEqual("gone");
165 }).pipe(logLevel),
166 {
167 tags: ["provider:gcp", "provider:gcp:developerconnect", "live"],
168 timeout: 90_000,
169 },
170);