| 1 | import * as GitHub from "@/GitHub"; |
| 2 | import { Octokit } from "@/GitHub/Octokit.ts"; |
| 3 | import * as Output from "@/Output"; |
| 4 | import { destroy } from "@/RemovalPolicy"; |
| 5 | import * as Test from "@/Test/Alchemy"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | |
| 9 | const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test"; |
| 10 | if (owner !== "alchemy-run-test" && owner !== "alchemy-run-test-2") { |
| 11 | throw new Error(`Unsafe GITHUB_TEST_OWNER: ${owner}`); |
| 12 | } |
| 13 | |
| 14 | const { test } = Test.make({ |
| 15 | providers: GitHub.providers({ baseUrl: "github.com" }), |
| 16 | }); |
| 17 | |
| 18 | const repo = "alchemy-pr-1569-pull-request"; |
| 19 | |
| 20 | const repository = () => |
| 21 | GitHub.Repository("Repo", { |
| 22 | owner, |
| 23 | name: repo, |
| 24 | description: "Retained deterministic fixture for alchemy PR #1569", |
| 25 | visibility: "public", |
| 26 | autoInit: true, |
| 27 | }); |
| 28 | |
| 29 | const repoName = (repository: GitHub.Repository) => |
| 30 | Output.map(repository.fullName, (fullName) => fullName.split("/")[1]!); |
| 31 | |
| 32 | const request = <A>(run: () => Promise<A>) => |
| 33 | Effect.tryPromise({ |
| 34 | try: run, |
| 35 | catch: (error) => error as Error & { status?: number }, |
| 36 | }); |
| 37 | |
| 38 | const branches = ["alchemy-pr-1569-a", "alchemy-pr-1569-b"]; |
| 39 | |
| 40 | const prepareBranches = Effect.gen(function* () { |
| 41 | const client = yield* Octokit; |
| 42 | const scope = { owner, repo }; |
| 43 | const { data: repository } = yield* request(() => |
| 44 | client.rest.repos.get(scope), |
| 45 | ); |
| 46 | const base = repository.default_branch; |
| 47 | const { data: ref } = yield* request(() => |
| 48 | client.rest.git.getRef({ ...scope, ref: `heads/${base}` }), |
| 49 | ); |
| 50 | for (const branch of branches) { |
| 51 | const existing = yield* request(() => |
| 52 | client.rest.git.getRef({ ...scope, ref: `heads/${branch}` }), |
| 53 | ).pipe( |
| 54 | Effect.catchIf( |
| 55 | (error) => error.status === 404, |
| 56 | () => Effect.succeed(undefined), |
| 57 | ), |
| 58 | ); |
| 59 | if (existing === undefined) { |
| 60 | yield* request(() => |
| 61 | client.rest.git.createRef({ |
| 62 | ...scope, |
| 63 | ref: `refs/heads/${branch}`, |
| 64 | sha: ref.object.sha, |
| 65 | }), |
| 66 | ); |
| 67 | yield* request(() => |
| 68 | client.rest.repos.createOrUpdateFileContents({ |
| 69 | ...scope, |
| 70 | branch, |
| 71 | path: "alchemy-pr-1569.txt", |
| 72 | message: "Add deterministic PR test fixture", |
| 73 | content: "YWxjaGVteSBQUiBmaXh0dXJlCg==", |
| 74 | }), |
| 75 | ); |
| 76 | } |
| 77 | } |
| 78 | return base; |
| 79 | }); |
| 80 | |
| 81 | const deleteBranches = Effect.gen(function* () { |
| 82 | const client = yield* Octokit; |
| 83 | for (const branch of branches) { |
| 84 | yield* request(() => |
| 85 | client.rest.git.deleteRef({ owner, repo, ref: `heads/${branch}` }), |
| 86 | ); |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | test.provider( |
| 91 | "create, update, replace, and close a pull request", |
| 92 | (stack) => |
| 93 | Effect.gen(function* () { |
| 94 | yield* stack.destroy(); |
| 95 | yield* stack.deploy(repository()); |
| 96 | const base = yield* prepareBranches; |
| 97 | const client = yield* Octokit; |
| 98 | const { data: user } = yield* request(() => |
| 99 | client.rest.users.getAuthenticated(), |
| 100 | ); |
| 101 | const { data: milestone } = yield* request(() => |
| 102 | client.rest.issues.createMilestone({ |
| 103 | owner, |
| 104 | repo, |
| 105 | title: "alchemy-pr-1569-lifecycle", |
| 106 | }), |
| 107 | ); |
| 108 | const deploy = (props: Partial<GitHub.PullRequestProps> = {}) => |
| 109 | stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | const repo = yield* repository(); |
| 112 | return yield* GitHub.PullRequest("PR", { |
| 113 | owner, |
| 114 | repository: repoName(repo), |
| 115 | title: "Alchemy PR #1569 lifecycle", |
| 116 | head: "alchemy-pr-1569-a", |
| 117 | base, |
| 118 | ...props, |
| 119 | }).pipe(destroy()); |
| 120 | }), |
| 121 | ); |
| 122 | const get = (number: number) => |
| 123 | request(() => |
| 124 | client.rest.pulls.get({ owner, repo, pull_number: number }), |
| 125 | ); |
| 126 | const created = yield* deploy({ |
| 127 | body: "\n Initial body\n ", |
| 128 | draft: true, |
| 129 | labels: ["bug"], |
| 130 | assignees: [user.login], |
| 131 | milestone: milestone.number, |
| 132 | }); |
| 133 | expect(created.prNumber).toBeGreaterThan(0); |
| 134 | expect(created.htmlUrl).toBe( |
| 135 | `https://github.com/${owner}/${repo}/pull/${created.prNumber}`, |
| 136 | ); |
| 137 | expect(created.draft).toBe(true); |
| 138 | expect(created.merged).toBe(false); |
| 139 | const initial = (yield* get(created.prNumber)).data; |
| 140 | expect(initial.body).toBe("Initial body"); |
| 141 | expect(initial.assignees?.map((assignee) => assignee.login)).toEqual([ |
| 142 | user.login, |
| 143 | ]); |
| 144 | expect(initial.milestone?.number).toBe(milestone.number); |
| 145 | expect(initial.labels.map((label) => label.name)).toEqual(["bug"]); |
| 146 | |
| 147 | const updated = yield* deploy({ |
| 148 | title: "Updated PR", |
| 149 | body: "", |
| 150 | draft: false, |
| 151 | labels: ["enhancement"], |
| 152 | assignees: [], |
| 153 | milestone: null, |
| 154 | reviewers: [], |
| 155 | teamReviewers: [], |
| 156 | }); |
| 157 | expect(updated.prNumber).toBe(created.prNumber); |
| 158 | expect(updated.nodeId).toBe(created.nodeId); |
| 159 | expect(updated.draft).toBe(false); |
| 160 | const afterUpdate = (yield* get(created.prNumber)).data; |
| 161 | expect(afterUpdate.title).toBe("Updated PR"); |
| 162 | expect(afterUpdate.body ?? "").toBe(""); |
| 163 | expect(afterUpdate.draft).toBe(false); |
| 164 | expect(afterUpdate.assignees).toEqual([]); |
| 165 | expect(afterUpdate.milestone).toBeNull(); |
| 166 | expect(afterUpdate.labels.map((label) => label.name)).toEqual([ |
| 167 | "enhancement", |
| 168 | ]); |
| 169 | expect(afterUpdate.requested_reviewers).toEqual([]); |
| 170 | expect(afterUpdate.requested_teams).toEqual([]); |
| 171 | |
| 172 | const draft = yield* deploy({ draft: true, labels: [] }); |
| 173 | expect(draft.draft).toBe(true); |
| 174 | expect((yield* get(draft.prNumber)).data.draft).toBe(true); |
| 175 | expect((yield* get(draft.prNumber)).data.labels).toEqual([]); |
| 176 | const closed = yield* deploy({ state: "closed", draft: true }); |
| 177 | expect(closed.state).toBe("closed"); |
| 178 | expect((yield* get(closed.prNumber)).data.state).toBe("closed"); |
| 179 | const reopened = yield* deploy(); |
| 180 | expect(reopened.prNumber).toBe(created.prNumber); |
| 181 | expect(reopened.state).toBe("open"); |
| 182 | expect(reopened.draft).toBe(false); |
| 183 | |
| 184 | const replaced = yield* deploy({ |
| 185 | head: "alchemy-pr-1569-b", |
| 186 | state: "closed", |
| 187 | }); |
| 188 | expect(replaced.prNumber).not.toBe(created.prNumber); |
| 189 | expect(replaced.nodeId).not.toBe(created.nodeId); |
| 190 | expect(replaced.state).toBe("closed"); |
| 191 | expect((yield* get(created.prNumber)).data.state).toBe("closed"); |
| 192 | expect((yield* get(replaced.prNumber)).data.state).toBe("closed"); |
| 193 | const final = yield* deploy({ head: "alchemy-pr-1569-b" }); |
| 194 | expect(final.prNumber).toBe(replaced.prNumber); |
| 195 | expect(final.state).toBe("open"); |
| 196 | |
| 197 | // Verify closure before releasing the retained repository fixture. |
| 198 | yield* stack.deploy(repository()); |
| 199 | expect((yield* get(final.prNumber)).data.state).toBe("closed"); |
| 200 | const open = yield* request(() => |
| 201 | client.rest.pulls.list({ owner, repo, state: "open" }), |
| 202 | ); |
| 203 | expect(open.data).toEqual([]); |
| 204 | yield* deleteBranches; |
| 205 | yield* request(() => |
| 206 | client.rest.issues.deleteMilestone({ |
| 207 | owner, |
| 208 | repo, |
| 209 | milestone_number: milestone.number, |
| 210 | }), |
| 211 | ); |
| 212 | yield* stack.destroy(); |
| 213 | }), |
| 214 | { |
| 215 | tags: [ |
| 216 | "provider:github", |
| 217 | "provider:github:pullrequest", |
| 218 | "provider:github:repository", |
| 219 | "live", |
| 220 | ], |
| 221 | timeout: 120_000, |
| 222 | }, |
| 223 | ); |