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

main
ts 259 lines 10.2 KB
Raw
1 import * as GitHub from "@alchemy.run/github";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Repos from "@distilled.cloud/github/repos";
5 import * as Output from "alchemy/Output";
6 import * as Provider from "alchemy/Provider";
7 import { destroy } from "alchemy/RemovalPolicy";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import { gh } from "./scope.ts";
11
12 const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test";
13 if (owner !== "alchemy-run-test" && owner !== "alchemy-run-test-2") {
14 throw new Error(`Refusing GitHub mutations outside the test organizations: ${owner}`);
15 }
16
17 const { test } = Test.make({
18 providers: GitHub.providers({ baseUrl: "github.com" }),
19 });
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 // Public fixtures avoid plan gates and are retained because gh lacks delete_repo.
24 const repo = "alchemy-effect-pr-1514-branch-protection";
25 const replacementRepo = "alchemy-effect-pr-1514-branch-protection-replacement";
26
27 // Derive the repository name from the `fullName` output — referencing an
28 // output (rather than the `repo` constant) makes the engine order dependent
29 // resources after the repository exists.
30 const repoName = (repository: GitHub.Repository) =>
31 Output.map(repository.fullName, (fullName) => fullName.split("/")[1]!);
32
33 const getProtection = (branch: string, repository: string = repo) =>
34 Effect.gen(function* () {
35 return yield* gh(
36 Repos.getBranchProtection({
37 owner,
38 repo: repository,
39 branch,
40 }),
41 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
42 });
43
44 const hostRepository = GitHub.Repository("Repo", {
45 owner,
46 name: repo,
47 description: "alchemy-effect branch protection test",
48 visibility: "public",
49 autoInit: true,
50 });
51
52 test.provider(
53 "create, update, replace, and delete a branch protection rule",
54 (stack) =>
55 Effect.gen(function* () {
56 // Clean up any leftovers from a previous run before deploying.
57 yield* stack.destroy();
58
59 // Create — protect the default branch with reviews, status checks, and
60 // the strict toggles. `Repository` defaults to `retain`, so the host
61 // repo is created once and reused across runs (reconcile is
62 // idempotent); `autoInit` guarantees the default branch exists.
63 const created = yield* stack.deploy(
64 Effect.gen(function* () {
65 const repository = yield* hostRepository;
66
67 return yield* GitHub.BranchProtection("Protection", {
68 owner,
69 repository: repoName(repository),
70 branch: repository.defaultBranch,
71 requiredStatusChecks: {
72 strict: true,
73 contexts: ["ci"],
74 },
75 requiredPullRequestReviews: {
76 requiredApprovingReviewCount: 1,
77 dismissStaleReviews: true,
78 },
79 enforceAdmins: true,
80 requiredLinearHistory: true,
81 requiredConversationResolution: true,
82 }).pipe(destroy());
83 }),
84 );
85
86 expect(created.url).toContain(`/repos/${owner}/${repo}/branches/`);
87 expect(created.branch.length).toBeGreaterThan(0);
88 expect(created.enforceAdmins).toBe(true);
89 expect(created.requiredLinearHistory).toBe(true);
90 expect(created.requiredConversationResolution).toBe(true);
91 expect(created.requiredSignatures).toBe(false);
92 expect(created.allowForcePushes).toBe(false);
93
94 const fetched = yield* getProtection(created.branch);
95 expect(fetched?.required_status_checks?.strict).toBe(true);
96 expect(fetched?.required_status_checks?.contexts).toEqual(["ci"]);
97 expect(fetched?.required_pull_request_reviews?.required_approving_review_count).toEqual(1);
98 expect(fetched?.required_pull_request_reviews?.dismiss_stale_reviews).toBe(true);
99 expect(fetched?.enforce_admins?.enabled).toBe(true);
100 expect(fetched?.required_linear_history?.enabled).toBe(true);
101 expect(fetched?.required_conversation_resolution?.enabled).toBe(true);
102
103 const provider = yield* Provider.findProvider(GitHub.BranchProtection);
104 const recovered = yield* provider.read!({
105 id: "Protection",
106 fqn: "Protection",
107 instanceId: "pr-1514",
108 olds: { owner, repository: repo, branch: created.branch },
109 output: undefined,
110 });
111 expect(recovered?.url).toEqual(created.url);
112 expect(recovered?.enforceAdmins).toBe(true);
113
114 // Both props are set. GitHub rejects a body that sends `contexts` and
115 // `checks` together, so this update must send `checks` alone. The
116 // read-back still fills `contexts` from the stored checks.
117 const withChecks = yield* stack.deploy(
118 Effect.gen(function* () {
119 const repository = yield* hostRepository;
120
121 return yield* GitHub.BranchProtection("Protection", {
122 owner,
123 repository: repoName(repository),
124 branch: repository.defaultBranch,
125 requiredStatusChecks: {
126 strict: false,
127 contexts: ["ci"],
128 checks: [{ context: "Verify" }, { context: "API tests" }],
129 },
130 requiredPullRequestReviews: {
131 requiredApprovingReviewCount: 1,
132 dismissStaleReviews: true,
133 },
134 enforceAdmins: true,
135 requiredLinearHistory: true,
136 requiredConversationResolution: true,
137 }).pipe(destroy());
138 }),
139 );
140
141 expect(withChecks.url).toEqual(created.url);
142 const afterChecks = yield* getProtection(created.branch);
143 const storedChecks = ["Verify", "API tests"];
144 expect(afterChecks?.required_status_checks?.strict).toBe(false);
145 expect(afterChecks?.required_status_checks?.contexts).toEqual(storedChecks);
146 expect(afterChecks?.required_status_checks?.checks?.map((check) => check.context)).toEqual(
147 storedChecks,
148 );
149
150 // Update — drop reviews and status checks, relax admin enforcement,
151 // allow force pushes, and require signed commits (the aspect that lives
152 // behind its own endpoint). Same logical ID → same rule URL (update in
153 // place, not replace).
154 const updated = yield* stack.deploy(
155 Effect.gen(function* () {
156 const repository = yield* hostRepository;
157
158 return yield* GitHub.BranchProtection("Protection", {
159 owner,
160 repository: repoName(repository),
161 branch: repository.defaultBranch,
162 allowForcePushes: true,
163 requiredSignatures: true,
164 requiredLinearHistory: true,
165 }).pipe(destroy());
166 }),
167 );
168
169 expect(updated.url).toEqual(created.url);
170 expect(updated.enforceAdmins).toBe(false);
171 expect(updated.allowForcePushes).toBe(true);
172 expect(updated.requiredSignatures).toBe(true);
173 expect(updated.requiredConversationResolution).toBe(false);
174
175 const afterUpdate = yield* getProtection(created.branch);
176 expect(afterUpdate?.required_status_checks).toBeUndefined();
177 expect(afterUpdate?.required_pull_request_reviews).toBeUndefined();
178 expect(afterUpdate?.enforce_admins?.enabled).toBe(false);
179 expect(afterUpdate?.allow_force_pushes?.enabled).toBe(true);
180 expect(afterUpdate?.required_signatures?.enabled).toBe(true);
181 expect(afterUpdate?.required_linear_history?.enabled).toBe(true);
182
183 // Re-deploying the same props is a no-op that still converges.
184 const unchanged = yield* stack.deploy(
185 Effect.gen(function* () {
186 const repository = yield* hostRepository;
187
188 return yield* GitHub.BranchProtection("Protection", {
189 owner,
190 repository: repoName(repository),
191 branch: repository.defaultBranch,
192 allowForcePushes: true,
193 requiredSignatures: true,
194 requiredLinearHistory: true,
195 }).pipe(destroy());
196 }),
197 );
198 expect(unchanged.url).toEqual(created.url);
199 expect(unchanged.requiredSignatures).toBe(true);
200
201 const reset = yield* stack.deploy(
202 Effect.gen(function* () {
203 const repository = yield* hostRepository;
204 return yield* GitHub.BranchProtection("Protection", {
205 owner,
206 repository: repoName(repository),
207 branch: repository.defaultBranch,
208 }).pipe(destroy());
209 }),
210 );
211 expect(reset.requiredSignatures).toBe(false);
212 expect(reset.allowForcePushes).toBe(false);
213 expect(reset.requiredLinearHistory).toBe(false);
214 const afterReset = yield* getProtection(created.branch);
215 expect(afterReset?.required_signatures?.enabled ?? false).toBe(false);
216 expect(afterReset?.allow_force_pushes?.enabled).toBe(false);
217 expect(afterReset?.required_linear_history?.enabled).toBe(false);
218
219 // Keep the old dependency deployed until its protection is replaced.
220 const replaced = yield* stack.deploy(
221 Effect.gen(function* () {
222 yield* hostRepository;
223 const repository = yield* GitHub.Repository("ReplacementRepo", {
224 owner,
225 name: replacementRepo,
226 description: "alchemy-effect PR 1514 replacement fixture",
227 visibility: "public",
228 autoInit: true,
229 });
230 return yield* GitHub.BranchProtection("Protection", {
231 owner,
232 repository: repoName(repository),
233 branch: repository.defaultBranch,
234 enforceAdmins: true,
235 }).pipe(destroy());
236 }),
237 );
238 expect(replaced.url).not.toEqual(created.url);
239 expect(replaced.url).toContain(`/repos/${owner}/${replacementRepo}/branches/`);
240 expect(yield* getProtection(created.branch)).toBeUndefined();
241 expect(
242 (yield* getProtection(replaced.branch, replacementRepo))?.enforce_admins?.enabled,
243 ).toBe(true);
244
245 // Both rules must be gone independently of the retained repositories.
246 yield* stack.destroy();
247 expect(yield* getProtection(created.branch)).toBeUndefined();
248 expect(yield* getProtection(replaced.branch, replacementRepo)).toBeUndefined();
249 }).pipe(logLevel),
250 {
251 tags: [
252 "provider:github",
253 "provider:github:branchprotection",
254 "provider:github:repository",
255 "live",
256 ],
257 timeout: 120_000,
258 },
259 );