-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
98 lines (91 loc) · 3.27 KB
/
utils.ts
File metadata and controls
98 lines (91 loc) · 3.27 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Shared utilities used across multiple MCP tool files.
*/
/** Format a past ISO8601 date as a human-readable relative string. */
export function timeAgo(dateStr: string): string {
const sec = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
if (sec < 60) return "now";
if (sec < 3600) return `${Math.floor(sec / 60)}m ago`;
if (sec < 86400) return `${Math.floor(sec / 3600)}h ago`;
if (sec < 604800) return `${Math.floor(sec / 86400)}d ago`;
return `${Math.floor(sec / 604800)}w ago`;
}
/**
* Parse a relative duration string ("48h", "7d") or an ISO8601 date/datetime
* into an ISO8601 timestamp suitable for GitHub API `since` parameters.
* Passthrough for anything that doesn't match: GitHub will reject it.
*/
export function parseSince(since: string): string {
if (/^\d{4}-\d{2}-\d{2}T/.test(since) || /^\d{4}-\d{2}-\d{2}$/.test(since)) {
return since;
}
const hoursMatch = /^(\d+(?:\.\d+)?)h$/i.exec(since);
if (hoursMatch?.[1]) {
const ms = Number.parseFloat(hoursMatch[1]) * 3_600_000;
return new Date(Date.now() - ms).toISOString();
}
const daysMatch = /^(\d+(?:\.\d+)?)d$/i.exec(since);
if (daysMatch?.[1]) {
const ms = Number.parseFloat(daysMatch[1]) * 86_400_000;
return new Date(Date.now() - ms).toISOString();
}
return since;
}
/**
* Extract PR numbers from commit message `(#NNN)` patterns.
* Returns all matches in order of appearance.
*/
export function extractPRNumbers(message: string): number[] {
const result: number[] = [];
for (const m of message.matchAll(/\(#(\d+)\)/g)) {
const raw = m[1];
if (!raw) continue;
const n = Number.parseInt(raw, 10);
if (!Number.isNaN(n)) result.push(n);
}
return result;
}
/** Extract the first PR number from a commit message `(#NNN)` pattern. */
export function extractFirstPR(message: string): number | undefined {
const m = /\(#(\d+)\)/.exec(message);
if (!m?.[1]) return undefined;
const n = Number.parseInt(m[1], 10);
return Number.isNaN(n) ? undefined : n;
}
/**
* Tail-truncate: keep the LAST `maxLines` of `text`.
* Used when fetching CI logs where failures appear at the bottom.
*/
export function tailTruncate(text: string, maxLines: number): string {
const lines = text.split("\n");
if (lines.length <= maxLines) return text;
return `... [${lines.length - maxLines} lines above truncated]\n${lines.slice(-maxLines).join("\n")}`;
}
/**
* Minimal shape for a GitHub status-check rollup context node.
* Covers both `CheckRun` (name + conclusion) and `StatusContext` (context + state).
*/
export interface CheckNode {
name?: string;
conclusion?: string;
context?: string;
state?: string;
}
/**
* Normalize a list of check-rollup context nodes into a flat array of
* `{ name, conclusion }` pairs for any check that isn't passing/skipped.
*
* Used by `repo_status` and `release_readiness` to surface failed CI checks.
*/
export function normalizeFailedChecks(nodes: CheckNode[]): { name: string; conclusion: string }[] {
return nodes
.filter((n) => {
if (n.conclusion) return !["SUCCESS", "SKIPPED"].includes(n.conclusion);
if (n.state) return n.state !== "SUCCESS";
return false;
})
.map((n) => ({
name: n.name ?? n.context ?? "unknown",
conclusion: n.conclusion ?? n.state ?? "unknown",
}));
}