@narumitw/pi-starship 0.49.3 → 0.49.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -4
- package/package.json +4 -8
- package/src/command-contract.ts +13 -0
- package/src/command-inspector.ts +198 -0
- package/src/command-preview.ts +206 -0
- package/src/commands.ts +575 -0
- package/src/config.ts +794 -0
- package/src/format/formatter.ts +275 -0
- package/src/format/style.ts +278 -0
- package/src/index.ts +1 -1
- package/src/modules/activity.ts +38 -0
- package/src/modules/brand.ts +13 -0
- package/src/modules/cache.ts +23 -0
- package/src/modules/catalog.ts +124 -0
- package/src/modules/cloud.ts +74 -0
- package/src/modules/context.ts +33 -0
- package/src/modules/cost.ts +31 -0
- package/src/modules/deployment.ts +61 -0
- package/src/modules/development.ts +112 -0
- package/src/modules/directory.ts +151 -0
- package/src/modules/display.ts +14 -0
- package/src/modules/execution.ts +84 -0
- package/src/modules/extension-status.ts +93 -0
- package/src/modules/fill.ts +14 -0
- package/src/modules/git/branch.ts +31 -0
- package/src/modules/git/commit.ts +29 -0
- package/src/modules/git/metrics.ts +26 -0
- package/src/modules/git/runtime.ts +415 -0
- package/src/modules/git/state.ts +21 -0
- package/src/modules/git/status.ts +87 -0
- package/src/modules/git/worktree.ts +16 -0
- package/src/modules/github-pr.ts +24 -0
- package/src/modules/helpers.ts +5 -0
- package/src/modules/index.ts +31 -0
- package/src/modules/inspection.ts +138 -0
- package/src/modules/languages.ts +87 -0
- package/src/modules/model.ts +85 -0
- package/src/modules/package.ts +17 -0
- package/src/modules/provider.ts +13 -0
- package/src/modules/render.ts +166 -0
- package/src/modules/terminal.ts +60 -0
- package/src/modules/thinking.ts +13 -0
- package/src/modules/time.ts +20 -0
- package/src/modules/tokens.ts +18 -0
- package/src/modules/truncation.ts +37 -0
- package/src/modules/turn.ts +13 -0
- package/src/modules/types.ts +189 -0
- package/src/modules/workspace-helpers.ts +10 -0
- package/src/pi-starship.ts +609 -0
- package/src/runtime/cloud.ts +166 -0
- package/src/runtime/command.ts +123 -0
- package/src/runtime/deployment.ts +201 -0
- package/src/runtime/development.ts +153 -0
- package/src/runtime/execution.ts +151 -0
- package/src/runtime/github-pr.ts +332 -0
- package/src/runtime/helpers.ts +274 -0
- package/src/runtime/languages.ts +205 -0
- package/src/runtime/package.ts +112 -0
- package/src/runtime/refresh-controller.ts +107 -0
- package/src/runtime/types.ts +63 -0
- package/src/runtime/workspace.ts +147 -0
- package/src/usage.ts +53 -0
- package/dist/chunks/chunk-CKNSOLSW.ts +0 -10
- package/dist/chunks/chunk-CKNSOLSW.ts.map +0 -7
- package/dist/chunks/chunk-G2GG2V6Q.ts +0 -218
- package/dist/chunks/chunk-G2GG2V6Q.ts.map +0 -7
- package/dist/chunks/chunk-Q6DLIM7H.ts +0 -2492
- package/dist/chunks/chunk-Q6DLIM7H.ts.map +0 -7
- package/dist/chunks/cloud-JBSZWTB6.ts +0 -142
- package/dist/chunks/cloud-JBSZWTB6.ts.map +0 -7
- package/dist/chunks/commands-BSQZ2R4M.ts +0 -757
- package/dist/chunks/commands-BSQZ2R4M.ts.map +0 -7
- package/dist/chunks/deployment-ZNYM4B6W.ts +0 -172
- package/dist/chunks/deployment-ZNYM4B6W.ts.map +0 -7
- package/dist/chunks/development-TNWZ5SRC.ts +0 -132
- package/dist/chunks/development-TNWZ5SRC.ts.map +0 -7
- package/dist/chunks/execution-QHAP4U33.ts +0 -132
- package/dist/chunks/execution-QHAP4U33.ts.map +0 -7
- package/dist/chunks/languages-DPWDCH6Q.ts +0 -184
- package/dist/chunks/languages-DPWDCH6Q.ts.map +0 -7
- package/dist/chunks/package-MXN2YFLX.ts +0 -104
- package/dist/chunks/package-MXN2YFLX.ts.map +0 -7
- package/dist/pi-starship.ts +0 -1350
- package/dist/pi-starship.ts.map +0 -7
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {
|
|
2
|
+
exactAlias,
|
|
3
|
+
optionBoolean,
|
|
4
|
+
optionMap,
|
|
5
|
+
optionString,
|
|
6
|
+
optionStrings,
|
|
7
|
+
safeMetadata,
|
|
8
|
+
} from "./helpers.js";
|
|
9
|
+
import {
|
|
10
|
+
type CollectorContext,
|
|
11
|
+
type MutableModuleSnapshot,
|
|
12
|
+
PRIVATE_STYLE_SELECTOR,
|
|
13
|
+
} from "./types.js";
|
|
14
|
+
|
|
15
|
+
export async function collectExecution(context: CollectorContext): Promise<MutableModuleSnapshot> {
|
|
16
|
+
if (context.input.reason === "periodic" && context.input.previous) {
|
|
17
|
+
const retained: MutableModuleSnapshot = {};
|
|
18
|
+
for (const name of ["os", "container", "hostname", "username"] as const) {
|
|
19
|
+
const values = context.input.previous.modules[name];
|
|
20
|
+
if (!context.needs(name) || !values) continue;
|
|
21
|
+
const selector = context.input.previous.styleSelectors?.[name];
|
|
22
|
+
retained[name] = {
|
|
23
|
+
...values,
|
|
24
|
+
...(selector === undefined ? {} : { [PRIVATE_STYLE_SELECTOR]: selector }),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return retained;
|
|
28
|
+
}
|
|
29
|
+
const result: MutableModuleSnapshot = {};
|
|
30
|
+
if (context.needs("os")) result.os = await osValues(context);
|
|
31
|
+
const container = context.needs("container") ? await containerValues(context) : undefined;
|
|
32
|
+
if (container) result.container = container;
|
|
33
|
+
const hostname = context.needs("hostname") ? hostnameValues(context) : undefined;
|
|
34
|
+
if (hostname) result.hostname = hostname;
|
|
35
|
+
const username = context.needs("username") ? usernameValues(context) : undefined;
|
|
36
|
+
if (username) result.username = username;
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function osValues(context: CollectorContext): Promise<Record<string, string>> {
|
|
41
|
+
const wsl = Boolean(safeMetadata(context.input.environment.WSL_DISTRO_NAME));
|
|
42
|
+
const type = wsl ? "wsl" : platformType(context.input.platform);
|
|
43
|
+
const details = context.input.platform === "linux" ? await linuxRelease(context) : {};
|
|
44
|
+
const symbols = optionMap(context, "os", "symbols");
|
|
45
|
+
return {
|
|
46
|
+
type,
|
|
47
|
+
name: details.name ?? platformName(context.input.platform, wsl),
|
|
48
|
+
...(details.version ? { version: details.version } : {}),
|
|
49
|
+
...(details.edition ? { edition: details.edition } : {}),
|
|
50
|
+
...(details.codename ? { codename: details.codename } : {}),
|
|
51
|
+
...(Object.hasOwn(symbols, type) && safeMetadata(symbols[type])
|
|
52
|
+
? { symbol: safeMetadata(symbols[type]) as string }
|
|
53
|
+
: {}),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function linuxRelease(context: CollectorContext): Promise<{
|
|
58
|
+
name?: string;
|
|
59
|
+
version?: string;
|
|
60
|
+
edition?: string;
|
|
61
|
+
codename?: string;
|
|
62
|
+
}> {
|
|
63
|
+
const source = await context.fs.readFile("/etc/os-release", 16 * 1024);
|
|
64
|
+
if (!source) return {};
|
|
65
|
+
const values: Record<string, string> = {};
|
|
66
|
+
for (const line of source.split(/\r?\n/u)) {
|
|
67
|
+
const match = /^([A-Z_]+)=(.*)$/u.exec(line);
|
|
68
|
+
if (!match?.[1] || match[2] === undefined) continue;
|
|
69
|
+
const value = safeMetadata(match[2].replace(/^['"]|['"]$/gu, ""));
|
|
70
|
+
if (value) values[match[1]] = value;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
name: values.NAME,
|
|
74
|
+
version: values.VERSION_ID,
|
|
75
|
+
edition: values.ID,
|
|
76
|
+
codename: values.VERSION_CODENAME,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function containerValues(
|
|
81
|
+
context: CollectorContext,
|
|
82
|
+
): Promise<Record<string, string> | undefined> {
|
|
83
|
+
const env = context.input.environment;
|
|
84
|
+
if (safeMetadata(env.REMOTE_CONTAINERS) || safeMetadata(env.CODESPACES)) {
|
|
85
|
+
return { name: "Dev Container", type: "devcontainer" };
|
|
86
|
+
}
|
|
87
|
+
const wsl = safeMetadata(env.WSL_DISTRO_NAME);
|
|
88
|
+
if (wsl) return { name: `WSL ${wsl}`, type: "wsl" };
|
|
89
|
+
if (await context.fs.fileExists("/.dockerenv")) return { name: "Docker", type: "docker" };
|
|
90
|
+
if (await context.fs.fileExists("/run/.containerenv")) return { name: "Podman", type: "podman" };
|
|
91
|
+
const systemd = safeMetadata(await context.fs.readFile("/run/systemd/container", 256));
|
|
92
|
+
return systemd ? { name: systemd, type: "container" } : undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function hostnameValues(context: CollectorContext): Record<string, string> | undefined {
|
|
96
|
+
const ssh = isSsh(context);
|
|
97
|
+
if (optionBoolean(context, "hostname", "ssh_only") && !ssh) return undefined;
|
|
98
|
+
const raw = safeMetadata(context.input.hostname);
|
|
99
|
+
if (!raw) return undefined;
|
|
100
|
+
const aliases = optionMap(context, "hostname", "aliases");
|
|
101
|
+
const trimAt = optionString(context, "hostname", "trim_at");
|
|
102
|
+
const index = trimAt ? raw.indexOf(trimAt) : -1;
|
|
103
|
+
const trimmed = index >= 0 ? raw.slice(0, index) : raw;
|
|
104
|
+
const hostname = exactAlias(trimmed, aliases) ?? trimmed;
|
|
105
|
+
return { hostname, ssh_symbol: ssh ? "🌐 " : "" };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function usernameValues(context: CollectorContext): Record<string, string> | undefined {
|
|
109
|
+
const user = safeMetadata(context.input.username);
|
|
110
|
+
if (!user) return undefined;
|
|
111
|
+
const env = context.input.environment;
|
|
112
|
+
const login = safeMetadata(env.LOGNAME ?? env.USER ?? env.USERNAME);
|
|
113
|
+
const privileged = /^(?:root|administrator)$/iu.test(user);
|
|
114
|
+
const detected = optionStrings(context, "username", "detect_env_vars").some(
|
|
115
|
+
(name) => safeMetadata(env[name]) !== undefined,
|
|
116
|
+
);
|
|
117
|
+
if (
|
|
118
|
+
!optionBoolean(context, "username", "show_always") &&
|
|
119
|
+
!isSsh(context) &&
|
|
120
|
+
!privileged &&
|
|
121
|
+
!(login && login !== user) &&
|
|
122
|
+
!detected
|
|
123
|
+
) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
user: exactAlias(user, optionMap(context, "username", "aliases")) ?? user,
|
|
128
|
+
[PRIVATE_STYLE_SELECTOR]: privileged ? "root" : "user",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isSsh(context: CollectorContext): boolean {
|
|
133
|
+
return Boolean(
|
|
134
|
+
safeMetadata(context.input.environment.SSH_CONNECTION) ||
|
|
135
|
+
safeMetadata(context.input.environment.SSH_TTY),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function platformType(platform: NodeJS.Platform): string {
|
|
140
|
+
if (platform === "darwin") return "macos";
|
|
141
|
+
if (platform === "win32") return "windows";
|
|
142
|
+
return platform;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function platformName(platform: NodeJS.Platform, wsl: boolean): string {
|
|
146
|
+
if (wsl) return "WSL";
|
|
147
|
+
if (platform === "darwin") return "macOS";
|
|
148
|
+
if (platform === "win32") return "Windows";
|
|
149
|
+
if (platform === "linux") return "Linux";
|
|
150
|
+
return platform;
|
|
151
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import type { GithubPrSnapshot, GithubPrState } from "../modules/types.js";
|
|
2
|
+
import type { WorkspaceExec } from "./types.js";
|
|
3
|
+
|
|
4
|
+
const GH_TIMEOUT_MS = 10_000;
|
|
5
|
+
const MAX_GH_OUTPUT_BYTES = 128 * 1024;
|
|
6
|
+
const MAX_URL_LENGTH = 4_096;
|
|
7
|
+
const MAX_CHECKS = 1_000;
|
|
8
|
+
const MAX_FIELD_LENGTH = 128;
|
|
9
|
+
const RFC3339_TIMESTAMP =
|
|
10
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(?:Z|[+-](\d{2}):(\d{2}))$/u;
|
|
11
|
+
export const TERMINAL_PR_LIFETIME_MS = 24 * 60 * 60 * 1_000;
|
|
12
|
+
|
|
13
|
+
const GH_PR_FIELDS = [
|
|
14
|
+
"number",
|
|
15
|
+
"isDraft",
|
|
16
|
+
"url",
|
|
17
|
+
"state",
|
|
18
|
+
"closedAt",
|
|
19
|
+
"mergedAt",
|
|
20
|
+
"reviewDecision",
|
|
21
|
+
"statusCheckRollup",
|
|
22
|
+
] as const;
|
|
23
|
+
|
|
24
|
+
interface GithubPrQueryOptions {
|
|
25
|
+
platform?: NodeJS.Platform;
|
|
26
|
+
environment?: Readonly<Record<string, string | undefined>>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface CheckSummary {
|
|
30
|
+
failed: number;
|
|
31
|
+
pending: number;
|
|
32
|
+
total: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type PullRequestState = "OPEN" | "CLOSED" | "MERGED";
|
|
36
|
+
type ReviewDecision = "APPROVED" | "CHANGES_REQUESTED" | "REVIEW_REQUIRED" | "";
|
|
37
|
+
|
|
38
|
+
export function ghPrViewInvocation(platform: NodeJS.Platform = process.platform): {
|
|
39
|
+
command: string;
|
|
40
|
+
args: string[];
|
|
41
|
+
} {
|
|
42
|
+
return {
|
|
43
|
+
command: platform === "win32" ? "gh.exe" : "gh",
|
|
44
|
+
args: ["pr", "view", "--json", GH_PR_FIELDS.join(",")],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function githubPrEnvironment(
|
|
49
|
+
source: Readonly<Record<string, string | undefined>> = process.env,
|
|
50
|
+
): Record<string, string | undefined> {
|
|
51
|
+
const environment: Record<string, string | undefined> = Object.create(null);
|
|
52
|
+
for (const [name, value] of Object.entries(source)) {
|
|
53
|
+
const normalized = name.toUpperCase();
|
|
54
|
+
if (normalized === "GH_HOST" || normalized === "GH_REPO") continue;
|
|
55
|
+
Object.defineProperty(environment, name, {
|
|
56
|
+
value,
|
|
57
|
+
writable: true,
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return environment;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function queryGithubPr(
|
|
66
|
+
exec: WorkspaceExec,
|
|
67
|
+
cwd: string,
|
|
68
|
+
signal?: AbortSignal,
|
|
69
|
+
now = Date.now(),
|
|
70
|
+
options: GithubPrQueryOptions = {},
|
|
71
|
+
): Promise<GithubPrSnapshot | undefined> {
|
|
72
|
+
const invocation = ghPrViewInvocation(options.platform);
|
|
73
|
+
try {
|
|
74
|
+
const result = await exec(invocation.command, invocation.args, {
|
|
75
|
+
cwd,
|
|
76
|
+
signal,
|
|
77
|
+
timeout: GH_TIMEOUT_MS,
|
|
78
|
+
maxOutputBytes: MAX_GH_OUTPUT_BYTES,
|
|
79
|
+
environment: githubPrEnvironment(options.environment),
|
|
80
|
+
});
|
|
81
|
+
if (result.killed || result.code !== 0) return undefined;
|
|
82
|
+
if (Buffer.byteLength(result.stdout) > MAX_GH_OUTPUT_BYTES) return undefined;
|
|
83
|
+
return buildGithubPrSnapshot(JSON.parse(result.stdout), now);
|
|
84
|
+
} catch {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function buildGithubPrSnapshot(
|
|
90
|
+
value: unknown,
|
|
91
|
+
now = Date.now(),
|
|
92
|
+
): GithubPrSnapshot | undefined {
|
|
93
|
+
try {
|
|
94
|
+
const pr = record(value);
|
|
95
|
+
const number = positiveInteger(pr.number);
|
|
96
|
+
const isDraft = requiredBoolean(pr.isDraft);
|
|
97
|
+
const url = requiredBoundedString(pr.url, MAX_URL_LENGTH, true);
|
|
98
|
+
const state = pullRequestState(pr.state);
|
|
99
|
+
const closedAt = optionalTimestamp(pr.closedAt);
|
|
100
|
+
const mergedAt = optionalTimestamp(pr.mergedAt);
|
|
101
|
+
const reviewDecision = reviewDecisionValue(pr.reviewDecision);
|
|
102
|
+
const checks = summarizeChecks(pr.statusCheckRollup);
|
|
103
|
+
const expiresAt = terminalExpiry(state, closedAt, mergedAt);
|
|
104
|
+
if (state !== "OPEN" && (expiresAt === undefined || now >= expiresAt)) return undefined;
|
|
105
|
+
|
|
106
|
+
const presentationState: GithubPrState =
|
|
107
|
+
state === "MERGED" ? "merged" : state === "CLOSED" ? "closed" : isDraft ? "draft" : "open";
|
|
108
|
+
const checksText = formatChecks(checks);
|
|
109
|
+
const review = formatReview(reviewDecision);
|
|
110
|
+
const status = compactStatus(presentationState, checks, checksText, review);
|
|
111
|
+
const snapshot: GithubPrSnapshot = {
|
|
112
|
+
number: String(number),
|
|
113
|
+
link: osc8Link(url, `#${number}`),
|
|
114
|
+
state: presentationState,
|
|
115
|
+
checks: checksText,
|
|
116
|
+
review,
|
|
117
|
+
status,
|
|
118
|
+
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
119
|
+
};
|
|
120
|
+
return Object.freeze(snapshot);
|
|
121
|
+
} catch {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function githubPrSnapshotEqual(
|
|
127
|
+
left: GithubPrSnapshot | undefined,
|
|
128
|
+
right: GithubPrSnapshot | undefined,
|
|
129
|
+
): boolean {
|
|
130
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function terminalExpiry(
|
|
134
|
+
state: PullRequestState,
|
|
135
|
+
closedAt: number | undefined,
|
|
136
|
+
mergedAt: number | undefined,
|
|
137
|
+
): number | undefined {
|
|
138
|
+
if (state === "OPEN") return undefined;
|
|
139
|
+
const terminalAt = state === "MERGED" ? mergedAt : closedAt;
|
|
140
|
+
return terminalAt === undefined ? undefined : terminalAt + TERMINAL_PR_LIFETIME_MS;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function summarizeChecks(value: unknown): CheckSummary {
|
|
144
|
+
if (!Array.isArray(value) || value.length > MAX_CHECKS) throw new Error("Invalid PR checks");
|
|
145
|
+
const summary: CheckSummary = { failed: 0, pending: 0, total: value.length };
|
|
146
|
+
for (const item of value) {
|
|
147
|
+
const check = record(item);
|
|
148
|
+
const state = optionalUppercase(check.state);
|
|
149
|
+
const status = optionalUppercase(check.status);
|
|
150
|
+
const conclusion = optionalUppercase(check.conclusion);
|
|
151
|
+
if (state === "SUCCESS") continue;
|
|
152
|
+
if (state === "FAILURE" || state === "ERROR") {
|
|
153
|
+
summary.failed += 1;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (state === "PENDING" || state === "EXPECTED") {
|
|
157
|
+
summary.pending += 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (status && status !== "COMPLETED") {
|
|
161
|
+
summary.pending += 1;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (conclusion === "SUCCESS" || conclusion === "SKIPPED" || conclusion === "NEUTRAL") {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (
|
|
168
|
+
conclusion === "FAILURE" ||
|
|
169
|
+
conclusion === "CANCELLED" ||
|
|
170
|
+
conclusion === "TIMED_OUT" ||
|
|
171
|
+
conclusion === "ACTION_REQUIRED" ||
|
|
172
|
+
conclusion === "STARTUP_FAILURE"
|
|
173
|
+
) {
|
|
174
|
+
summary.failed += 1;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
summary.pending += 1;
|
|
178
|
+
}
|
|
179
|
+
return summary;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function formatChecks(checks: CheckSummary): string {
|
|
183
|
+
if (checks.total === 0) return "no checks";
|
|
184
|
+
if (checks.failed > 0) return `${checks.failed} failing`;
|
|
185
|
+
if (checks.pending > 0) return `${checks.pending} pending`;
|
|
186
|
+
return "checks passing";
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function formatReview(review: ReviewDecision): string {
|
|
190
|
+
if (review === "APPROVED") return "approved";
|
|
191
|
+
if (review === "CHANGES_REQUESTED") return "changes requested";
|
|
192
|
+
if (review === "REVIEW_REQUIRED") return "review required";
|
|
193
|
+
return "";
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function compactStatus(
|
|
197
|
+
state: GithubPrState,
|
|
198
|
+
checks: CheckSummary,
|
|
199
|
+
checksText: string,
|
|
200
|
+
review: string,
|
|
201
|
+
): string {
|
|
202
|
+
if (state === "merged" || state === "closed" || state === "draft") return state;
|
|
203
|
+
if (checks.failed > 0) return checksText;
|
|
204
|
+
if (review === "changes requested") return review;
|
|
205
|
+
if (checks.pending > 0) return checksText;
|
|
206
|
+
if (review === "approved" || review === "review required") return review;
|
|
207
|
+
return checksText;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function osc8Link(value: string, label: string): string {
|
|
211
|
+
if (hasTerminalControls(value)) return label;
|
|
212
|
+
try {
|
|
213
|
+
const url = new URL(value);
|
|
214
|
+
if ((url.protocol !== "http:" && url.protocol !== "https:") || url.username || url.password) {
|
|
215
|
+
return label;
|
|
216
|
+
}
|
|
217
|
+
const normalized = url.toString();
|
|
218
|
+
if (normalized.length > MAX_URL_LENGTH || hasTerminalControls(normalized)) return label;
|
|
219
|
+
return `\u001b]8;;${normalized}\u0007${label}\u001b]8;;\u0007`;
|
|
220
|
+
} catch {
|
|
221
|
+
return label;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function hasTerminalControls(value: string): boolean {
|
|
226
|
+
return Array.from(value).some((character) => {
|
|
227
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
228
|
+
return (
|
|
229
|
+
codePoint <= 0x1f ||
|
|
230
|
+
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
231
|
+
(codePoint >= 0xd800 && codePoint <= 0xdfff)
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function record(value: unknown): Record<string, unknown> {
|
|
237
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
238
|
+
throw new Error("Expected PR object");
|
|
239
|
+
}
|
|
240
|
+
return value as Record<string, unknown>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function positiveInteger(value: unknown): number {
|
|
244
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
|
|
245
|
+
throw new Error("Invalid PR number");
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function requiredBoolean(value: unknown): boolean {
|
|
251
|
+
if (typeof value !== "boolean") throw new Error("Invalid PR boolean");
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function requiredBoundedString(value: unknown, maxLength: number, allowEmpty = false): string {
|
|
256
|
+
if (
|
|
257
|
+
typeof value !== "string" ||
|
|
258
|
+
value.length > maxLength ||
|
|
259
|
+
(!allowEmpty && value.length === 0)
|
|
260
|
+
) {
|
|
261
|
+
throw new Error("Invalid PR string");
|
|
262
|
+
}
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function optionalUppercase(value: unknown): string | undefined {
|
|
267
|
+
if (value === null || value === undefined || value === "") return undefined;
|
|
268
|
+
return requiredBoundedString(value, MAX_FIELD_LENGTH).toUpperCase();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function optionalTimestamp(value: unknown): number | undefined {
|
|
272
|
+
if (value === null || value === undefined || value === "") return undefined;
|
|
273
|
+
const text = requiredBoundedString(value, MAX_FIELD_LENGTH);
|
|
274
|
+
const match = RFC3339_TIMESTAMP.exec(text);
|
|
275
|
+
if (!match) throw new Error("Invalid PR timestamp");
|
|
276
|
+
const [
|
|
277
|
+
,
|
|
278
|
+
yearText,
|
|
279
|
+
monthText,
|
|
280
|
+
dayText,
|
|
281
|
+
hourText,
|
|
282
|
+
minuteText,
|
|
283
|
+
secondText,
|
|
284
|
+
offsetHourText,
|
|
285
|
+
offsetMinuteText,
|
|
286
|
+
] = match;
|
|
287
|
+
const year = Number(yearText);
|
|
288
|
+
const month = Number(monthText);
|
|
289
|
+
const day = Number(dayText);
|
|
290
|
+
const hour = Number(hourText);
|
|
291
|
+
const minute = Number(minuteText);
|
|
292
|
+
const second = Number(secondText);
|
|
293
|
+
const offsetHour = Number(offsetHourText ?? 0);
|
|
294
|
+
const offsetMinute = Number(offsetMinuteText ?? 0);
|
|
295
|
+
if (
|
|
296
|
+
month < 1 ||
|
|
297
|
+
month > 12 ||
|
|
298
|
+
day < 1 ||
|
|
299
|
+
day > daysInMonth(year, month) ||
|
|
300
|
+
hour > 23 ||
|
|
301
|
+
minute > 59 ||
|
|
302
|
+
second > 59 ||
|
|
303
|
+
offsetHour > 23 ||
|
|
304
|
+
offsetMinute > 59
|
|
305
|
+
) {
|
|
306
|
+
throw new Error("Invalid PR timestamp");
|
|
307
|
+
}
|
|
308
|
+
const timestamp = Date.parse(text);
|
|
309
|
+
if (!Number.isFinite(timestamp)) throw new Error("Invalid PR timestamp");
|
|
310
|
+
return timestamp;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function daysInMonth(year: number, month: number): number {
|
|
314
|
+
if (month === 2) {
|
|
315
|
+
const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
316
|
+
return leapYear ? 29 : 28;
|
|
317
|
+
}
|
|
318
|
+
return month === 4 || month === 6 || month === 9 || month === 11 ? 30 : 31;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function pullRequestState(value: unknown): PullRequestState {
|
|
322
|
+
if (value === "OPEN" || value === "CLOSED" || value === "MERGED") return value;
|
|
323
|
+
throw new Error("Invalid PR state");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function reviewDecisionValue(value: unknown): ReviewDecision {
|
|
327
|
+
if (value === null || value === undefined || value === "" || value === "UNKNOWN") return "";
|
|
328
|
+
if (value === "APPROVED" || value === "CHANGES_REQUESTED" || value === "REVIEW_REQUIRED") {
|
|
329
|
+
return value;
|
|
330
|
+
}
|
|
331
|
+
throw new Error("Invalid PR review decision");
|
|
332
|
+
}
|