The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { SourceControlSignInError, type SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | defineSourceControlDriver, |
| 4 | withOwnServices, |
| 5 | type SourceControlSignIn, |
| 6 | } from "@t3tools/source-control-core/server/driver"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import { HttpClient } from "effect/http"; |
| 10 | |
| 11 | import { definition } from "../shared/definition.ts"; |
| 12 | |
| 13 | import * as GitHubAppAccess from "./GitHubAppAccess.ts"; |
| 14 | import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; |
| 15 | import * as GitHubMediaFetch from "./gitHubMediaFetch.ts"; |
| 16 | import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts"; |
| 17 | import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts"; |
| 18 | import * as GitHubRest from "./GitHubRest.ts"; |
| 19 | import * as GitHubHttpClient from "./GitHubHttpClient.ts"; |
| 20 | import * as GitHubIssueTracker from "./GitHubIssueTracker.ts"; |
| 21 | import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; |
| 22 | |
| 23 | // `GitHubRest.layerWithDependencies` carries the quota reserve every GitHub reader shares, so it |
| 24 | // must be built once; the driver is. |
| 25 | const services = GitHubPullRequestApi.layer.pipe( |
| 26 | Layer.provideMerge(GitHubAppAccess.layer), |
| 27 | Layer.provideMerge(GitHubRest.layerWithDependencies), |
| 28 | Layer.provideMerge(GitHubHttpClient.layer), |
| 29 | ); |
| 30 | |
| 31 | const makeSignIn = Effect.gen(function* () { |
| 32 | const appSignIn = yield* GitHubAppSignIn.GitHubAppSignIn; |
| 33 | const appAccess = yield* GitHubAppAccess.GitHubAppAccess; |
| 34 | const kind: SourceControlProviderKind = definition.kind; |
| 35 | return { |
| 36 | start: appSignIn.start, |
| 37 | wait: (host) => |
| 38 | appSignIn.wait(host).pipe( |
| 39 | Effect.andThen( |
| 40 | appAccess.viewer(host).pipe( |
| 41 | Effect.mapError( |
| 42 | (cause) => |
| 43 | new SourceControlSignInError({ |
| 44 | provider: kind, |
| 45 | host, |
| 46 | reason: "failed", |
| 47 | detail: "Signed in, but GitHub would not say which account it belongs to.", |
| 48 | cause, |
| 49 | }), |
| 50 | ), |
| 51 | ), |
| 52 | ), |
| 53 | Effect.map((account) => ({ account })), |
| 54 | ), |
| 55 | cancel: appSignIn.cancel, |
| 56 | access: appAccess.reach, |
| 57 | } satisfies SourceControlSignIn; |
| 58 | }); |
| 59 | |
| 60 | const starredPath = (repository: string) => { |
| 61 | const segments = repository.split("/"); |
| 62 | // "." and ".." survive encoding and would normalize the URL out of user/starred. |
| 63 | return segments.length === 2 && segments.every((segment) => !["", ".", ".."].includes(segment)) |
| 64 | ? `user/starred/${segments.map(encodeURIComponent).join("/")}` |
| 65 | : undefined; |
| 66 | }; |
| 67 | |
| 68 | // Only 204 means starred. Tokens without star access, a missing login, and 404 all read as false. |
| 69 | export const makeIsRepositoryStarred = Effect.gen(function* () { |
| 70 | const api = yield* GitHubRest.GitHubRest; |
| 71 | return Effect.fn("GitHubDriver.isRepositoryStarred")( |
| 72 | function* (host: string, repository: string) { |
| 73 | const path = starredPath(repository); |
| 74 | if (path === undefined) return false; |
| 75 | const response = yield* api.rest({ |
| 76 | host, |
| 77 | operation: "isRepositoryStarred", |
| 78 | path, |
| 79 | allowReserve: false, |
| 80 | }); |
| 81 | return response.status === 204; |
| 82 | }, |
| 83 | Effect.orElseSucceed(() => false), |
| 84 | ); |
| 85 | }); |
| 86 | |
| 87 | // False on any failure, such as a token or app sign-in without permission to star. |
| 88 | export const makeStarRepository = Effect.gen(function* () { |
| 89 | const api = yield* GitHubRest.GitHubRest; |
| 90 | return Effect.fn("GitHubDriver.starRepository")( |
| 91 | function* (host: string, repository: string) { |
| 92 | const path = starredPath(repository); |
| 93 | if (path === undefined) return false; |
| 94 | const response = yield* api.rest({ host, operation: "starRepository", method: "PUT", path }); |
| 95 | return response.status === 204; |
| 96 | }, |
| 97 | Effect.orElseSucceed(() => false), |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | const makeInstance = Effect.gen(function* () { |
| 102 | const mediaHttpClient = yield* HttpClient.HttpClient; |
| 103 | return yield* withOwnServices( |
| 104 | Effect.all({ |
| 105 | sourceControl: GitHubSourceControlProvider.make, |
| 106 | discovery: GitHubSourceControlProvider.makeDiscovery, |
| 107 | pullRequests: GitHubPullRequestProvider.make, |
| 108 | issues: GitHubIssueTracker.make, |
| 109 | fetchMedia: GitHubMediaFetch.make.pipe( |
| 110 | Effect.provideService(HttpClient.HttpClient, mediaHttpClient), |
| 111 | ), |
| 112 | signIn: makeSignIn, |
| 113 | isRepositoryStarred: makeIsRepositoryStarred, |
| 114 | starRepository: makeStarRepository, |
| 115 | }), |
| 116 | services, |
| 117 | ); |
| 118 | }); |
| 119 | |
| 120 | export const driver = defineSourceControlDriver({ |
| 121 | kind: definition.kind, |
| 122 | definition, |
| 123 | make: makeInstance, |
| 124 | }); |