|
1 | 1 | import { runGitHubStackAction, type GitHubStackActionError } from "./githubStackActions.ts"; |
2 | 2 | import * as Context from "effect/Context"; |
| 3 | +import * as Clock from "effect/Clock"; |
| 4 | +import * as NodeCrypto from "node:crypto"; |
3 | 5 | import * as Effect from "effect/Effect"; |
4 | 6 | import * as Layer from "effect/Layer"; |
5 | 7 | import * as Result from "effect/Result"; |
6 | 8 | import * as Schema from "effect/Schema"; |
7 | 9 | import { |
8 | 10 | resolvePullRequestAuthorFilter, |
| 11 | + PositiveInt, |
| 12 | + TrimmedNonEmptyString, |
9 | 13 | type PullRequestAction, |
10 | 14 | type PullRequestStackHead, |
11 | 15 | type PullRequestActor, |
@@ -413,8 +417,16 @@ export interface GitHubPullRequestDiffSlice { |
413 | 417 | export class GitHubPullRequestCli extends Context.Service< |
414 | 418 | GitHubPullRequestCli, |
415 | 419 | { |
| 420 | + readonly getRoutingIdentity: (input: { |
| 421 | + readonly cwd: string; |
| 422 | + readonly host: string; |
| 423 | + }) => Effect.Effect< |
| 424 | + { readonly accountId: string; readonly viewer: string }, |
| 425 | + GitHubPullRequestCliError |
| 426 | + >; |
416 | 427 | readonly getViewerLogin: (input: { |
417 | 428 | readonly cwd: string; |
| 429 | + readonly host: string; |
418 | 430 | }) => Effect.Effect<string, GitHubPullRequestCliError>; |
419 | 431 |
|
420 | 432 | readonly listPullRequests: (input: { |
@@ -991,6 +1003,63 @@ function actionArgs( |
991 | 1003 | export const make = Effect.gen(function* () { |
992 | 1004 | const github = yield* GitHubCli.GitHubCli; |
993 | 1005 | const graphQlBudget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget; |
| 1006 | + const routingIdentities = new Map< |
| 1007 | + string, |
| 1008 | + { |
| 1009 | + at: number; |
| 1010 | + value: { accountId: string; viewer: string }; |
| 1011 | + } |
| 1012 | + >(); |
| 1013 | + const decodeRoutingIdentity = Schema.decodeUnknownEffect( |
| 1014 | + Schema.fromJsonString( |
| 1015 | + Schema.Struct({ |
| 1016 | + id: PositiveInt, |
| 1017 | + login: TrimmedNonEmptyString, |
| 1018 | + }), |
| 1019 | + ), |
| 1020 | + ); |
| 1021 | + const getRoutingIdentity = Effect.fn("GitHubPullRequestCli.getRoutingIdentity")( |
| 1022 | + function* (input: { readonly cwd: string; readonly host: string }) { |
| 1023 | + const unavailable = () => |
| 1024 | + new GitHubViewerLoginUnavailableError({ command: "gh", cwd: input.cwd }); |
| 1025 | + const host = input.host.toLowerCase(); |
| 1026 | + // Only the digest is retained. Never attach credential lookup output to an error. |
| 1027 | + const token = (yield* github |
| 1028 | + .execute({ |
| 1029 | + cwd: input.cwd, |
| 1030 | + args: ["auth", "token", "--hostname", host], |
| 1031 | + env: { GH_DEBUG: "" }, |
| 1032 | + }) |
| 1033 | + .pipe(Effect.mapError(unavailable))).stdout.trim(); |
| 1034 | + if (!token) return yield* unavailable(); |
| 1035 | + const key = `${host}:${NodeCrypto.createHash("sha256").update(token).digest("hex")}`; |
| 1036 | + const now = yield* Clock.currentTimeMillis; |
| 1037 | + const cached = routingIdentities.get(key); |
| 1038 | + if (cached !== undefined && now - cached.at < 10 * 60_000) return cached.value; |
| 1039 | + // Pin this read to the captured credential so an auth switch cannot poison its cache entry. |
| 1040 | + const response = yield* github |
| 1041 | + .execute({ |
| 1042 | + cwd: input.cwd, |
| 1043 | + args: ["api", "user", "--hostname", host], |
| 1044 | + env: { |
| 1045 | + GH_TOKEN: token, |
| 1046 | + GITHUB_TOKEN: token, |
| 1047 | + GH_ENTERPRISE_TOKEN: token, |
| 1048 | + GITHUB_ENTERPRISE_TOKEN: token, |
| 1049 | + GH_DEBUG: "", |
| 1050 | + }, |
| 1051 | + }) |
| 1052 | + .pipe(Effect.mapError(unavailable)); |
| 1053 | + const identity = yield* decodeRoutingIdentity(response.stdout).pipe( |
| 1054 | + Effect.mapError(unavailable), |
| 1055 | + ); |
| 1056 | + const value = { accountId: String(identity.id), viewer: identity.login }; |
| 1057 | + if (routingIdentities.size >= 128) |
| 1058 | + routingIdentities.delete(routingIdentities.keys().next().value!); |
| 1059 | + routingIdentities.set(key, { at: now, value }); |
| 1060 | + return value; |
| 1061 | + }, |
| 1062 | + ); |
994 | 1063 |
|
995 | 1064 | /** |
996 | 1065 | * The pull request's own node id, which is what a mutation against the pull request itself is |
@@ -1468,15 +1537,9 @@ export const make = Effect.gen(function* () { |
1468 | 1537 | ); |
1469 | 1538 |
|
1470 | 1539 | return GitHubPullRequestCli.of({ |
| 1540 | + getRoutingIdentity, |
1471 | 1541 | getViewerLogin: (input) => |
1472 | | - github.execute({ cwd: input.cwd, args: ["api", "user", "--jq", ".login"] }).pipe( |
1473 | | - Effect.flatMap((result) => { |
1474 | | - const login = result.stdout.trim(); |
1475 | | - return login.length > 0 |
1476 | | - ? Effect.succeed(login) |
1477 | | - : Effect.fail(new GitHubViewerLoginUnavailableError({ command: "gh", cwd: input.cwd })); |
1478 | | - }), |
1479 | | - ), |
| 1542 | + getRoutingIdentity(input).pipe(Effect.map((identity) => identity.viewer)), |
1480 | 1543 |
|
1481 | 1544 | listPullRequests: (input) => { |
1482 | 1545 | const fallbackMaxRows = Math.max(input.limit + 1, PULL_REQUEST_FALLBACK_MAX_ROWS); |
|
0 commit comments