Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitHubPullRequestProvider.credential.test.ts

157 lines5.9 KB

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