- Route git workflows through source control providers - Replace git status broadcaster with VCS status service - Update orchestration, server, and web contracts for provider-aware git
Showing the changes to apps/server/src/sourceControl/GitHubSourceControlProvider.ts (1 of 40 files). Show all
Unified · Split
| @@ -0,0 +1,141 @@ | |||
| 1 | import { Effect, Layer, Result, Schema } from "effect"; | ||
| 2 | import { | ||
| 3 | SourceControlProviderError, | ||
| 4 | type ChangeRequest, | ||
| 5 | type ChangeRequestState, | ||
| 6 | type GitHubCliError, | ||
| 7 | } from "@t3tools/contracts"; | ||
| 8 | |||
| 9 | import { GitHubCli, type GitHubPullRequestSummary } from "../git/Services/GitHubCli.ts"; | ||
| 10 | import { decodeGitHubPullRequestListJson } from "../git/githubPullRequests.ts"; | ||
| 11 | import { SourceControlProvider, type SourceControlProviderShape } from "./SourceControlProvider.ts"; | ||
| 12 | |||
| 13 | function providerError(operation: string, cause: GitHubCliError): SourceControlProviderError { | ||
| 14 | return new SourceControlProviderError({ | ||
| 15 | provider: "github", | ||
| 16 | operation, | ||
| 17 | detail: cause.detail, | ||
| 18 | cause, | ||
| 19 | }); | ||
| 20 | } | ||
| 21 | |||
| 22 | function toChangeRequest(summary: GitHubPullRequestSummary): ChangeRequest { | ||
| 23 | return { | ||
| 24 | provider: "github", | ||
| 25 | number: summary.number, | ||
| 26 | title: summary.title, | ||
| 27 | url: summary.url, | ||
| 28 | baseRefName: summary.baseRefName, | ||
| 29 | headRefName: summary.headRefName, | ||
| 30 | state: summary.state ?? "open", | ||
| 31 | updatedAt: null, | ||
| 32 | ...(summary.isCrossRepository !== undefined | ||
| 33 | ? { isCrossRepository: summary.isCrossRepository } | ||
| 34 | : {}), | ||
| 35 | ...(summary.headRepositoryNameWithOwner !== undefined | ||
| 36 | ? { headRepositoryNameWithOwner: summary.headRepositoryNameWithOwner } | ||
| 37 | : {}), | ||
| 38 | ...(summary.headRepositoryOwnerLogin !== undefined | ||
| 39 | ? { headRepositoryOwnerLogin: summary.headRepositoryOwnerLogin } | ||
| 40 | : {}), | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | |||
| 44 | export const make = Effect.fn("makeGitHubSourceControlProvider")(function* () { | ||
| 45 | const github = yield* GitHubCli; | ||
| 46 | |||
| 47 | const listChangeRequests: SourceControlProviderShape["listChangeRequests"] = (input) => { | ||
| 48 | if (input.state === "open") { | ||
| 49 | return github | ||
| 50 | .listOpenPullRequests({ | ||
| 51 | cwd: input.cwd, | ||
| 52 | headSelector: input.headSelector, | ||
| 53 | ...(input.limit !== undefined ? { limit: input.limit } : {}), | ||
| 54 | }) | ||
| 55 | .pipe( | ||
| 56 | Effect.map((items) => items.map(toChangeRequest)), | ||
| 57 | Effect.mapError((error) => providerError("listChangeRequests", error)), | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | const stateArg: ChangeRequestState | "all" = input.state; | ||
| 62 | return github | ||
| 63 | .execute({ | ||
| 64 | cwd: input.cwd, | ||
| 65 | args: [ | ||
| 66 | "pr", | ||
| 67 | "list", | ||
| 68 | "--head", | ||
| 69 | input.headSelector, | ||
| 70 | "--state", | ||
| 71 | stateArg, | ||
| 72 | "--limit", | ||
| 73 | String(input.limit ?? 20), | ||
| 74 | "--json", | ||
| 75 | "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner", | ||
| 76 | ], | ||
| 77 | }) | ||
| 78 | .pipe( | ||
| 79 | Effect.flatMap((result) => | ||
| 80 | Effect.sync(() => decodeGitHubPullRequestListJson(result.stdout.trim())).pipe( | ||
| 81 | Effect.flatMap((decoded) => | ||
| 82 | Result.isSuccess(decoded) | ||
| 83 | ? Effect.succeed( | ||
| 84 | decoded.success.map((item) => ({ | ||
| 85 | ...toChangeRequest(item), | ||
| 86 | updatedAt: item.updatedAt, | ||
| 87 | })), | ||
| 88 | ) | ||
| 89 | : Effect.fail( | ||
| 90 | new SourceControlProviderError({ | ||
| 91 | provider: "github", | ||
| 92 | operation: "listChangeRequests", | ||
| 93 | detail: "GitHub CLI returned invalid change request JSON.", | ||
| 94 | cause: decoded.failure, | ||
| 95 | }), | ||
| 96 | ), | ||
| 97 | ), | ||
| 98 | ), | ||
| 99 | ), | ||
| 100 | Effect.mapError((error) => | ||
| 101 | Schema.is(SourceControlProviderError)(error) | ||
| 102 | ? error | ||
| 103 | : providerError("listChangeRequests", error), | ||
| 104 | ), | ||
| 105 | ); | ||
| 106 | }; | ||
| 107 | |||
| 108 | return SourceControlProvider.of({ | ||
| 109 | kind: "github", | ||
| 110 | listChangeRequests, | ||
| 111 | getChangeRequest: (input) => | ||
| 112 | github.getPullRequest(input).pipe( | ||
| 113 | Effect.map(toChangeRequest), | ||
| 114 | Effect.mapError((error) => providerError("getChangeRequest", error)), | ||
| 115 | ), | ||
| 116 | createChangeRequest: (input) => | ||
| 117 | github | ||
| 118 | .createPullRequest({ | ||
| 119 | cwd: input.cwd, | ||
| 120 | baseBranch: input.baseRefName, | ||
| 121 | headSelector: input.headSelector, | ||
| 122 | title: input.title, | ||
| 123 | bodyFile: input.bodyFile, | ||
| 124 | }) | ||
| 125 | .pipe(Effect.mapError((error) => providerError("createChangeRequest", error))), | ||
| 126 | getRepositoryCloneUrls: (input) => | ||
| 127 | github | ||
| 128 | .getRepositoryCloneUrls(input) | ||
| 129 | .pipe(Effect.mapError((error) => providerError("getRepositoryCloneUrls", error))), | ||
| 130 | getDefaultBranch: (input) => | ||
| 131 | github | ||
| 132 | .getDefaultBranch(input) | ||
| 133 | .pipe(Effect.mapError((error) => providerError("getDefaultBranch", error))), | ||
| 134 | checkoutChangeRequest: (input) => | ||
| 135 | github | ||
| 136 | .checkoutPullRequest(input) | ||
| 137 | .pipe(Effect.mapError((error) => providerError("checkoutChangeRequest", error))), | ||
| 138 | }); | ||
| 139 | }); | ||
| 140 | |||
| 141 | export const layer = Layer.effect(SourceControlProvider, make()); | ||