forked from ProverCoderAI/docker-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api-helpers.ts
More file actions
62 lines (58 loc) · 2.05 KB
/
github-api-helpers.ts
File metadata and controls
62 lines (58 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type * as CommandExecutor from "@effect/platform/CommandExecutor"
import type { PlatformError } from "@effect/platform/Error"
import { Effect } from "effect"
import { runDockerAuthCapture } from "../shell/docker-auth.js"
import { CommandFailedError } from "../shell/errors.js"
import { buildDockerAuthSpec } from "./auth-helpers.js"
import { ghAuthDir, ghImageName } from "./github-auth-image.js"
// CHANGE: extract shared gh-API Docker helpers used by github-fork and state-repo-github
// WHY: avoid code duplication flagged by the duplicate-detection linter
// REF: issue-141
// PURITY: SHELL
// INVARIANT: helpers are stateless and composable
/**
* Run `gh api <args>` inside the auth Docker container and return trimmed stdout.
*
* @pure false
* @effect CommandExecutor (Docker)
* @invariant exits with CommandFailedError on non-zero exit code
* @complexity O(1)
*/
export const runGhApiCapture = (
cwd: string,
hostPath: string,
token: string,
args: ReadonlyArray<string>
): Effect.Effect<string, CommandFailedError | PlatformError, CommandExecutor.CommandExecutor> =>
runDockerAuthCapture(
buildDockerAuthSpec({
cwd,
image: ghImageName,
hostPath,
containerPath: ghAuthDir,
env: `GH_TOKEN=${token}`,
args: ["api", ...args],
interactive: false
}),
[0],
(exitCode) => new CommandFailedError({ command: `gh api ${args.join(" ")}`, exitCode })
).pipe(Effect.map((raw) => raw.trim()))
/**
* Like `runGhApiCapture` but returns `null` instead of failing on API errors
* (e.g. HTTP 404 / non-zero exit code).
*
* @pure false
* @effect CommandExecutor (Docker)
* @invariant never fails — errors become null
* @complexity O(1)
*/
export const runGhApiNullable = (
cwd: string,
hostPath: string,
token: string,
args: ReadonlyArray<string>
): Effect.Effect<string | null, PlatformError, CommandExecutor.CommandExecutor> =>
runGhApiCapture(cwd, hostPath, token, args).pipe(
Effect.catchTag("CommandFailedError", () => Effect.succeed("")),
Effect.map((raw) => (raw.length === 0 ? null : raw))
)