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
| 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 { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy"; |
| 6 | import * as Output from "alchemy/Output"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import { destroy } from "alchemy/RemovalPolicy"; |
| 9 | import { State } from "alchemy/State"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { gh, scopedToOwner } from "./scope.ts"; |
| 13 | |
| 14 | const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test"; |
| 15 | if (owner !== "alchemy-run-test" && owner !== "alchemy-run-test-2") { |
| 16 | throw new Error(`Refusing GitHub Ruleset tests for unauthorized owner: ${owner}`); |
| 17 | } |
| 18 | |
| 19 | const { test } = Test.make({ providers: GitHub.providers({ baseUrl: "github.com" }) }); |
| 20 | |
| 21 | const fixtureNames = [ |
| 22 | "alchemy-pr-1570-ruleset-lifecycle", |
| 23 | "alchemy-pr-1570-ruleset-list", |
| 24 | "alchemy-pr-1570-ruleset-replace-a", |
| 25 | "alchemy-pr-1570-ruleset-replace-b", |
| 26 | "alchemy-pr-1570-ruleset-adopt", |
| 27 | "alchemy-pr-1570-ruleset-legacy-rules", |
| 28 | ]; |
| 29 | |
| 30 | // Retain public fixtures: the gh token lacks delete_repo, and private rulesets are plan-gated. |
| 31 | const repository = (name: string, id = "Repo") => |
| 32 | GitHub.Repository(id, { owner, name, visibility: "public", autoInit: true }); |
| 33 | |
| 34 | const repoName = (repo: GitHub.Repository) => |
| 35 | Output.map(repo.fullName, (fullName) => fullName.split("/")[1]!); |
| 36 | |
| 37 | const getRuleset = (repo: string, rulesetId: number) => |
| 38 | Effect.gen(function* () { |
| 39 | return yield* gh(Repos.getRepoRuleset({ owner, repo, ruleset_id: rulesetId })).pipe( |
| 40 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 41 | ); |
| 42 | }); |
| 43 | |
| 44 | const getRulesets = (repo: string) => |
| 45 | Effect.gen(function* () { |
| 46 | return yield* gh( |
| 47 | Stream.runCollect( |
| 48 | Repos.getRepoRulesets.items({ |
| 49 | owner, |
| 50 | repo, |
| 51 | includes_parents: false, |
| 52 | per_page: 100, |
| 53 | }), |
| 54 | ), |
| 55 | ); |
| 56 | }); |
| 57 | |
| 58 | test.provider( |
| 59 | "create, update, clear, and delete a ruleset", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | yield* stack.destroy(); |
| 63 | const repo = fixtureNames[0]!; |
| 64 | const deploy = (props: Omit<GitHub.RulesetProps, "owner" | "repository">) => |
| 65 | stack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | const fixture = yield* repository(repo); |
| 68 | return yield* GitHub.Ruleset("MainProtection", { |
| 69 | owner, |
| 70 | repository: repoName(fixture), |
| 71 | ...props, |
| 72 | }).pipe(destroy()); |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | const created = yield* deploy({ |
| 77 | name: "main protection", |
| 78 | conditions: { include: ["refs/heads/main"] }, |
| 79 | bypassActors: [{ actorType: "RepositoryRole", actorId: 5 }], |
| 80 | nonFastForward: true, |
| 81 | deletion: true, |
| 82 | update: true, |
| 83 | pullRequest: { |
| 84 | allowedMergeMethods: ["squash"], |
| 85 | requireExtraApprovalForUnattributedChanges: false, |
| 86 | }, |
| 87 | requiredStatusChecks: { checks: [{ context: "ci/test" }] }, |
| 88 | mergeQueue: { |
| 89 | mergeMethod: "SQUASH", |
| 90 | groupingStrategy: "HEADGREEN", |
| 91 | maxEntriesToBuild: 2, |
| 92 | minEntriesToMerge: 1, |
| 93 | maxEntriesToMerge: 1, |
| 94 | minEntriesToMergeWaitMinutes: 1, |
| 95 | checkResponseTimeoutMinutes: 30, |
| 96 | }, |
| 97 | }); |
| 98 | expect(created.rulesetId).toBeGreaterThan(0); |
| 99 | expect(created.name).toBe("main protection"); |
| 100 | const fetched = yield* getRuleset(repo, created.rulesetId); |
| 101 | expect(fetched?.name).toBe("main protection"); |
| 102 | expect(fetched?.enforcement).toBe("active"); |
| 103 | expect(fetched?.rules?.map((rule) => rule.type).sort()).toEqual([ |
| 104 | "deletion", |
| 105 | "merge_queue", |
| 106 | "non_fast_forward", |
| 107 | "pull_request", |
| 108 | "required_status_checks", |
| 109 | "update", |
| 110 | ]); |
| 111 | expect( |
| 112 | fetched?.rules?.find((rule) => rule.type === "pull_request")?.parameters, |
| 113 | ).toMatchObject({ |
| 114 | allowed_merge_methods: ["squash"], |
| 115 | require_extra_approval_for_unattributed_changes: false, |
| 116 | }); |
| 117 | expect(fetched?.rules?.find((rule) => rule.type === "merge_queue")?.parameters).toEqual({ |
| 118 | merge_method: "SQUASH", |
| 119 | grouping_strategy: "HEADGREEN", |
| 120 | max_entries_to_build: 2, |
| 121 | min_entries_to_merge: 1, |
| 122 | max_entries_to_merge: 1, |
| 123 | min_entries_to_merge_wait_minutes: 1, |
| 124 | check_response_timeout_minutes: 30, |
| 125 | }); |
| 126 | expect(fetched?.bypass_actors).toEqual([ |
| 127 | { actor_type: "RepositoryRole", actor_id: 5, bypass_mode: "always" }, |
| 128 | ]); |
| 129 | expect(created.createdAt).toBe(fetched?.created_at); |
| 130 | |
| 131 | // Dropping both props resets them to GitHub's defaults: every merge |
| 132 | // method is allowed again and the extra approval is required again. |
| 133 | const relaxed = yield* deploy({ |
| 134 | name: "main protection", |
| 135 | conditions: { include: ["refs/heads/main"] }, |
| 136 | bypassActors: [{ actorType: "RepositoryRole", actorId: 5 }], |
| 137 | nonFastForward: true, |
| 138 | deletion: true, |
| 139 | update: true, |
| 140 | pullRequest: {}, |
| 141 | requiredStatusChecks: { checks: [{ context: "ci/test" }] }, |
| 142 | mergeQueue: {}, |
| 143 | }); |
| 144 | expect(relaxed.rulesetId).toBe(created.rulesetId); |
| 145 | const afterRelax = yield* getRuleset(repo, created.rulesetId); |
| 146 | expect( |
| 147 | afterRelax?.rules?.find((rule) => rule.type === "pull_request")?.parameters, |
| 148 | ).toMatchObject({ |
| 149 | allowed_merge_methods: ["merge", "squash", "rebase"], |
| 150 | require_extra_approval_for_unattributed_changes: true, |
| 151 | }); |
| 152 | // An empty merge queue converges to GitHub's defaults. |
| 153 | expect(afterRelax?.rules?.find((rule) => rule.type === "merge_queue")?.parameters).toEqual({ |
| 154 | merge_method: "MERGE", |
| 155 | grouping_strategy: "ALLGREEN", |
| 156 | max_entries_to_build: 5, |
| 157 | min_entries_to_merge: 1, |
| 158 | max_entries_to_merge: 5, |
| 159 | min_entries_to_merge_wait_minutes: 5, |
| 160 | check_response_timeout_minutes: 60, |
| 161 | }); |
| 162 | |
| 163 | const updated = yield* deploy({ |
| 164 | name: "updated main protection", |
| 165 | enforcement: "disabled", |
| 166 | conditions: { include: ["refs/heads/main", "refs/heads/release/*"] }, |
| 167 | nonFastForward: true, |
| 168 | requiredLinearHistory: true, |
| 169 | update: false, |
| 170 | }); |
| 171 | expect(updated.rulesetId).toBe(created.rulesetId); |
| 172 | expect(updated.nodeId).toBe(created.nodeId); |
| 173 | expect(updated.name).toBe("updated main protection"); |
| 174 | const afterUpdate = yield* getRuleset(repo, updated.rulesetId); |
| 175 | expect(afterUpdate?.name).toBe("updated main protection"); |
| 176 | expect(afterUpdate?.enforcement).toBe("disabled"); |
| 177 | expect(afterUpdate?.conditions?.ref_name?.include).toEqual([ |
| 178 | "refs/heads/main", |
| 179 | "refs/heads/release/*", |
| 180 | ]); |
| 181 | expect(afterUpdate?.rules?.map((rule) => rule.type).sort()).toEqual([ |
| 182 | "non_fast_forward", |
| 183 | "required_linear_history", |
| 184 | ]); |
| 185 | expect(afterUpdate?.bypass_actors ?? []).toEqual([]); |
| 186 | |
| 187 | const cleared = yield* deploy({ name: "all branches" }); |
| 188 | expect(cleared.rulesetId).toBe(created.rulesetId); |
| 189 | const afterClear = yield* getRuleset(repo, cleared.rulesetId); |
| 190 | expect(afterClear?.conditions?.ref_name).toEqual({ include: ["~ALL"], exclude: [] }); |
| 191 | expect(afterClear?.rules ?? []).toEqual([]); |
| 192 | expect(afterClear?.enforcement).toBe("active"); |
| 193 | |
| 194 | // Delete the ruleset while its repository is still managed, then release the fixture. |
| 195 | yield* stack.deploy(repository(repo)); |
| 196 | expect(yield* getRuleset(repo, created.rulesetId)).toBeUndefined(); |
| 197 | expect(yield* getRulesets(repo)).toEqual([]); |
| 198 | yield* stack.destroy(); |
| 199 | }), |
| 200 | { tags: ["provider:github", "provider:github:repository", "provider:github:ruleset", "live"] }, |
| 201 | ); |
| 202 | |
| 203 | test.provider( |
| 204 | "state persisted with nested rules diffs and reconciles against top-level rules", |
| 205 | (stack) => |
| 206 | Effect.gen(function* () { |
| 207 | yield* stack.destroy(); |
| 208 | const repo = fixtureNames[5]!; |
| 209 | const ruleset = (props: Omit<GitHub.RulesetProps, "owner" | "repository" | "name">) => |
| 210 | Effect.gen(function* () { |
| 211 | const fixture = yield* repository(repo); |
| 212 | return yield* GitHub.Ruleset("LegacyRules", { |
| 213 | owner, |
| 214 | repository: repoName(fixture), |
| 215 | name: "legacy rules", |
| 216 | conditions: { include: ["refs/heads/main"] }, |
| 217 | ...props, |
| 218 | }).pipe(destroy()); |
| 219 | }); |
| 220 | const ruleTypes = (rulesetId: number) => |
| 221 | Effect.map(getRuleset(repo, rulesetId), (ruleset) => |
| 222 | ruleset?.rules?.map((rule) => rule.type).sort(), |
| 223 | ); |
| 224 | |
| 225 | // Rewrites the persisted props into the shape written before the rules |
| 226 | // moved to the top level: `rules: { nonFastForward, deletion, ... }`. |
| 227 | const persistNestedRules = Effect.gen(function* () { |
| 228 | const state = yield* yield* State; |
| 229 | const scope = { stack: stack.name, stage: stack.stage, fqn: "LegacyRules" }; |
| 230 | const row = yield* state.get(scope); |
| 231 | if (row === undefined || !("props" in row)) { |
| 232 | return yield* Effect.die("LegacyRules has no persisted props"); |
| 233 | } |
| 234 | const { nonFastForward, deletion, requiredLinearHistory, ...props } = |
| 235 | row.props as GitHub.RulesetProps; |
| 236 | yield* state.set({ |
| 237 | ...scope, |
| 238 | value: { |
| 239 | ...row, |
| 240 | props: { ...props, rules: { nonFastForward, deletion, requiredLinearHistory } }, |
| 241 | }, |
| 242 | }); |
| 243 | }); |
| 244 | |
| 245 | const created = yield* stack.deploy(ruleset({ nonFastForward: true, deletion: true })); |
| 246 | expect(yield* ruleTypes(created.rulesetId)).toEqual(["deletion", "non_fast_forward"]); |
| 247 | |
| 248 | // The same rules plan a noop against the nested state and leave GitHub untouched. |
| 249 | yield* persistNestedRules; |
| 250 | const plan = yield* stack.plan(ruleset({ nonFastForward: true, deletion: true })); |
| 251 | expect(plan.resources["LegacyRules"]).toMatchObject({ action: "noop" }); |
| 252 | const unchanged = yield* stack.deploy(ruleset({ nonFastForward: true, deletion: true })); |
| 253 | expect(unchanged.rulesetId).toBe(created.rulesetId); |
| 254 | expect((yield* getRuleset(repo, created.rulesetId))?.updated_at).toBe(created.updatedAt); |
| 255 | |
| 256 | // Changed rules update the ruleset in place from the nested state. |
| 257 | yield* persistNestedRules; |
| 258 | const changed = ruleset({ deletion: true, requiredLinearHistory: true }); |
| 259 | expect((yield* stack.plan(changed)).resources["LegacyRules"]).toMatchObject({ |
| 260 | action: "update", |
| 261 | }); |
| 262 | const updated = yield* stack.deploy(changed); |
| 263 | expect(updated.rulesetId).toBe(created.rulesetId); |
| 264 | expect(yield* ruleTypes(created.rulesetId)).toEqual(["deletion", "required_linear_history"]); |
| 265 | |
| 266 | yield* stack.deploy(repository(repo)); |
| 267 | expect(yield* getRulesets(repo)).toEqual([]); |
| 268 | yield* stack.destroy(); |
| 269 | }), |
| 270 | { tags: ["provider:github", "provider:github:repository", "provider:github:ruleset", "live"] }, |
| 271 | ); |
| 272 | |
| 273 | test.provider( |
| 274 | "adopts an existing ruleset by name", |
| 275 | (stack) => |
| 276 | Effect.gen(function* () { |
| 277 | yield* stack.destroy(); |
| 278 | const repo = fixtureNames[4]!; |
| 279 | yield* stack.deploy(repository(repo)); |
| 280 | const existing = yield* gh( |
| 281 | Repos.createRepoRuleset({ |
| 282 | owner, |
| 283 | repo, |
| 284 | name: "adopted protection", |
| 285 | target: "tag", |
| 286 | enforcement: "active", |
| 287 | rules: [{ type: "deletion" }], |
| 288 | }), |
| 289 | ); |
| 290 | yield* Effect.addFinalizer(() => |
| 291 | gh(Repos.deleteRepoRuleset({ owner, repo, ruleset_id: existing.id })).pipe(Effect.ignore), |
| 292 | ); |
| 293 | |
| 294 | const deploy = (enabled: boolean) => |
| 295 | stack.deploy( |
| 296 | Effect.gen(function* () { |
| 297 | const fixture = yield* repository(repo); |
| 298 | return yield* GitHub.Ruleset("AdoptedRuleset", { |
| 299 | owner, |
| 300 | repository: repoName(fixture), |
| 301 | name: "adopted protection", |
| 302 | conditions: { include: ["~DEFAULT_BRANCH"] }, |
| 303 | deletion: true, |
| 304 | nonFastForward: true, |
| 305 | }).pipe(adopt(enabled), destroy()); |
| 306 | }), |
| 307 | ); |
| 308 | |
| 309 | const refused = yield* deploy(false).pipe(Effect.flip); |
| 310 | expect(refused).toBeInstanceOf(OwnedBySomeoneElse); |
| 311 | |
| 312 | const adopted = yield* deploy(true); |
| 313 | expect(adopted.rulesetId).toBe(existing.id); |
| 314 | const observed = yield* getRuleset(repo, existing.id); |
| 315 | expect(observed?.target).toBe("branch"); |
| 316 | expect(observed?.conditions?.ref_name).toEqual({ |
| 317 | include: ["~DEFAULT_BRANCH"], |
| 318 | exclude: [], |
| 319 | }); |
| 320 | expect(observed?.rules?.map((rule) => rule.type).sort()).toEqual([ |
| 321 | "deletion", |
| 322 | "non_fast_forward", |
| 323 | ]); |
| 324 | expect((yield* getRulesets(repo)).map((ruleset) => ruleset.id)).toEqual([existing.id]); |
| 325 | |
| 326 | yield* stack.deploy(repository(repo)); |
| 327 | expect(yield* getRulesets(repo)).toEqual([]); |
| 328 | yield* stack.destroy(); |
| 329 | }), |
| 330 | { tags: ["provider:github", "provider:github:repository", "provider:github:ruleset", "live"] }, |
| 331 | ); |
| 332 | |
| 333 | test.provider( |
| 334 | "list rulesets across test repositories", |
| 335 | (stack) => |
| 336 | Effect.gen(function* () { |
| 337 | yield* stack.destroy(); |
| 338 | const repo = fixtureNames[1]!; |
| 339 | const created = yield* stack.deploy( |
| 340 | Effect.gen(function* () { |
| 341 | const fixture = yield* repository(repo); |
| 342 | return yield* GitHub.Ruleset("ListedRuleset", { |
| 343 | owner, |
| 344 | repository: repoName(fixture), |
| 345 | name: "listed tag protection", |
| 346 | target: "tag", |
| 347 | deletion: true, |
| 348 | }).pipe(destroy()); |
| 349 | }), |
| 350 | ); |
| 351 | const provider = yield* Provider.findProvider(GitHub.Ruleset); |
| 352 | // list() enumerates /user/repos; confine it to this suite's fixture. |
| 353 | const listed = yield* provider |
| 354 | .list() |
| 355 | .pipe(scopedToOwner(owner, { repository: repo, path: "rulesets" })); |
| 356 | const found = listed.find((ruleset) => ruleset.rulesetId === created.rulesetId); |
| 357 | expect(found?.name).toBe("listed tag protection"); |
| 358 | expect(found?.nodeId).toBe(created.nodeId); |
| 359 | expect((yield* getRuleset(repo, created.rulesetId))?.target).toBe("tag"); |
| 360 | |
| 361 | yield* stack.deploy(repository(repo)); |
| 362 | expect(yield* getRuleset(repo, created.rulesetId)).toBeUndefined(); |
| 363 | expect(yield* getRulesets(repo)).toEqual([]); |
| 364 | yield* stack.destroy(); |
| 365 | }), |
| 366 | { tags: ["provider:github", "provider:github:repository", "provider:github:ruleset", "live"] }, |
| 367 | ); |
| 368 | |
| 369 | test.provider( |
| 370 | "changing the repository replaces the ruleset", |
| 371 | (stack) => |
| 372 | Effect.gen(function* () { |
| 373 | yield* stack.destroy(); |
| 374 | const repoA = fixtureNames[2]!; |
| 375 | const repoB = fixtureNames[3]!; |
| 376 | const fixtures = Effect.gen(function* () { |
| 377 | const a = yield* repository(repoA, "RepoA"); |
| 378 | const b = yield* repository(repoB, "RepoB"); |
| 379 | return { a, b }; |
| 380 | }); |
| 381 | const deploy = (destination: "a" | "b") => |
| 382 | stack.deploy( |
| 383 | Effect.gen(function* () { |
| 384 | const repos = yield* fixtures; |
| 385 | return yield* GitHub.Ruleset("Protection", { |
| 386 | owner, |
| 387 | repository: repoName(repos[destination]), |
| 388 | name: "replacement protection", |
| 389 | nonFastForward: true, |
| 390 | }).pipe(destroy()); |
| 391 | }), |
| 392 | ); |
| 393 | const created = yield* deploy("a"); |
| 394 | expect((yield* getRuleset(repoA, created.rulesetId))?.name).toBe("replacement protection"); |
| 395 | const replaced = yield* deploy("b"); |
| 396 | expect(replaced.rulesetId).not.toBe(created.rulesetId); |
| 397 | expect(yield* getRuleset(repoA, created.rulesetId)).toBeUndefined(); |
| 398 | expect((yield* getRuleset(repoB, replaced.rulesetId))?.name).toBe("replacement protection"); |
| 399 | |
| 400 | yield* stack.deploy(fixtures); |
| 401 | expect(yield* getRuleset(repoB, replaced.rulesetId)).toBeUndefined(); |
| 402 | expect(yield* getRulesets(repoA)).toEqual([]); |
| 403 | expect(yield* getRulesets(repoB)).toEqual([]); |
| 404 | yield* stack.destroy(); |
| 405 | }), |
| 406 | { tags: ["provider:github", "provider:github:repository", "provider:github:ruleset", "live"] }, |
| 407 | ); |