Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Redacted from "effect/Redacted";
6import { HttpClient, HttpClientResponse } from "effect/http";
7import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
8import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
9
10import * as GitHubCredentials from "./GitHubCredentials.ts";
11import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
12import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts";
13import * as GitHubQuota from "./GitHubQuota.ts";
14import * as GitHubRest from "./GitHubRest.ts";
15
16it.effect("keeps branch deletion on the credential that passed source repository permissions", () =>
17 Effect.gen(function* () {
18 let activeToken = "account-a";
19 const requests: Array<{ operation: string; authorization: string | undefined }> = [];
20 const credentials = Layer.succeed(
21 GitHubCredentials.GitHubCredentials,
22 GitHubCredentials.GitHubCredentials.of({
23 get: (host) =>
24 Effect.sync(() => ({
25 host,
26 token: Redacted.make(activeToken),
27 source: "gh" as const,
28 fingerprint: `${host}:${activeToken}`,
29 })),
30 invalidate: () => Effect.void,
31 }),
32 );
33 const http = Layer.succeed(
34 HttpClient.HttpClient,
35 HttpClient.make((request) =>
36 Effect.sync(() => {
37 const operation = request.url.endsWith("/user")
38 ? "user"
39 : request.body._tag === "Uint8Array"
40 ? new TextDecoder().decode(request.body.body)
41 : "";
42 requests.push({ operation, authorization: request.headers["authorization"] });
43 let body;
44 if (operation === "user") {
45 body = { id: activeToken === "account-a" ? 101 : 202, login: activeToken };
46 } else if (operation.includes("query PullRequestBranchToDelete")) {
47 body = {
48 data: {
49 repository: {
50 pullRequest: {
51 state: "MERGED",
52 headRefName: "feature",
53 baseRefName: "main",
54 headRefOid: "abc123",
55 headRepository: { id: "FORK_REPO" },
56 },
57 },
58 },
59 };
60 } else if (operation.includes("query PullRequestSourceBranch")) {
61 body = {
62 data: {
63 node: {
64 __typename: "Repository",
65 defaultBranchRef: { name: "main" },
66 ref: { target: { __typename: "Commit", oid: "abc123" } },
67 },
68 },
69 };
70 } else if (operation.includes("updateRefs(")) {
71 body = { data: { updateRefs: { clientMutationId: null } } };
72 } else {
73 // The credential rotates the moment the permission read is answered.
74 activeToken = "account-b";
75 body = {
76 data: {
77 repository: {
78 mergeCommitAllowed: true,
79 squashMergeAllowed: true,
80 rebaseMergeAllowed: true,
81 autoMergeAllowed: true,
82 viewerPermission: "READ",
83 pullRequest: {
84 viewerCanUpdate: false,
85 viewerDidAuthor: false,
86 headRepository: { viewerPermission: "WRITE" },
87 },
88 },
89 },
90 };
91 }
92 return HttpClientResponse.fromWeb(
93 request,
94 new Response(JSON.stringify(body), {
95 headers: { "content-type": "application/json" },
96 }),
97 );
98 }),
99 ),
100 );
101 const provider = yield* GitHubPullRequestProvider.make.pipe(
102 Effect.provide(
103 GitHubPullRequestApi.layer.pipe(
104 // GraphQL reads go over `http` too, through the same credential and quota as REST.
105 Layer.provide(
106 GitHubRest.layer.pipe(
107 Layer.provideMerge(Layer.mergeAll(credentials, http)),
108 Layer.provideMerge(GitHubQuota.layer),
109 Layer.provideMerge(SourceControlRateLimit.layer),
110 ),
111 ),
112 Layer.provide(TestSourceControlHost.layer()),
113 Layer.provide(NodeServices.layer),
114 ),
115 ),
116 );
117 const reference = { cwd: "/a", repository: "acme/web", host: "github.com", number: 1 };
118 assert.isDefined(provider.withVerifiedCredential);
119 yield* provider.withVerifiedCredential!({ cwd: "/a", host: "github.com" }, () =>
120 Effect.gen(function* () {
121 const permissions = yield* provider.getViewerPermissions({
122 ...reference,
123 includeUpdateBranch: false,
124 });
125 assert.include(permissions.actions, "delete-branch");
126 yield* provider.runAction({ ...reference, action: "delete-branch" });
127 }),
128 );
129
130 assert.strictEqual(activeToken, "account-b");
131 assert.strictEqual(
132 requests.filter(({ operation }) => operation.includes("updateRefs(")).length,
133 1,
134 );
135 assert.deepStrictEqual(
136 requests.map(({ authorization }) => authorization),
137 [
138 "Bearer account-a",
139 "Bearer account-a",
140 "Bearer account-a",
141 "Bearer account-a",
142 "Bearer account-a",
143 ],
144 );
145 }),
146);