-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpin-drift-tool.ts
More file actions
557 lines (494 loc) · 18 KB
/
pin-drift-tool.ts
File metadata and controls
557 lines (494 loc) · 18 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import { execFileSync } from "node:child_process";
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import type { FastMCP } from "fastmcp";
import { z } from "zod";
import { countBehind, resolveRef } from "./compare-refs.js";
import { gateAuth } from "./github-auth.js";
import { classifyError, graphqlQuery, parallelApi, parseGitHubRemoteUrl } from "./github-client.js";
import { errorRespond, jsonRespond, type McpErrorEnvelope } from "./json.js";
import { FormatSchema } from "./schemas.js";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
type PinSource = "go.mod" | ".gitmodules" | "scripts/versions.env" | "package.json";
interface PinEntry {
source: PinSource;
owner: string;
repo: string;
pinnedRef: string;
pinnedDate?: string;
defaultBranch: string;
headSha: string;
behindBy: number;
grepMatches?: number;
commits: { sha7: string; message: string; author: string; date: string }[];
stale: boolean;
/** Populated when the pin could not be resolved; `behindBy` is -1 in that case. */
error?: McpErrorEnvelope;
}
interface SkippedEntry {
source: PinSource;
key: string;
value: string;
reason: string;
}
interface PinDriftResult {
localPath: string;
pins: PinEntry[];
skipped: SkippedEntry[];
summary: { totalPins: number; stale: number; upToDate: number };
}
interface HeadResult {
repository: {
defaultBranchRef: {
name: string;
target: { oid: string; committedDate: string };
} | null;
};
}
// ---------------------------------------------------------------------------
// Parsers
// ---------------------------------------------------------------------------
/** Extract 12-char SHA prefix from a Go pseudo-version: v0.0.0-YYYYMMDDHHMMSS-<sha12> */
export function pseudoVersionSha(version: string): string | undefined {
const m = /v\d+\.\d+\.\d+-\d{14}-([0-9a-f]{12})$/.exec(version);
return m?.[1];
}
interface RawPin {
source: PinSource;
owner: string;
repo: string;
pinnedRef: string; // SHA or branch/tag
}
/** Parse go.mod for replace directives and pseudo-version requires pointing at GitHub. */
function parseGoMod(localPath: string): { pins: RawPin[]; skipped: SkippedEntry[] } {
const goModPath = join(localPath, "go.mod");
if (!existsSync(goModPath)) return { pins: [], skipped: [] };
const pins: RawPin[] = [];
const skipped: SkippedEntry[] = [];
const text = readFileSync(goModPath, "utf8");
// Replace directives: `replace X => github.com/owner/repo vVersion`
for (const m of text.matchAll(/^[ \t]*replace\s+\S+\s+=>\s+(github\.com\/\S+)\s+(\S+)/gm)) {
const path = m[1];
const version = m[2];
if (!path || !version) continue;
const pathParts = /github\.com\/([^/]+)\/([^/]+)/.exec(path);
if (!pathParts?.[1] || !pathParts[2]) continue;
const owner = pathParts[1];
const repo = pathParts[2].replace(/\.git$/, "");
const sha12 = pseudoVersionSha(version);
if (sha12) {
pins.push({ source: "go.mod", owner, repo, pinnedRef: sha12 });
} else if (/^v\d+\.\d+\.\d+$/.test(version)) {
pins.push({ source: "go.mod", owner, repo, pinnedRef: version });
} else {
skipped.push({
source: "go.mod",
key: `replace ${path}`,
value: version,
reason: "ambiguous_ref",
});
}
}
// Require lines with pseudo-versions for github.com modules
for (const m of text.matchAll(/^[ \t]*(?:github\.com\/([^/\s]+)\/([^/\s]+))\s+(v\S+)/gm)) {
const owner = m[1];
const repo = m[2]?.replace(/\.git$/, "");
const version = m[3];
if (!owner || !repo || !version) continue;
// Only pseudo-versions (not a tagged release or branch)
const sha12 = pseudoVersionSha(version);
if (!sha12) continue;
// Avoid duplicates from replace block
const alreadyPinned = pins.some((p) => p.owner === owner && p.repo === repo);
if (alreadyPinned) continue;
pins.push({ source: "go.mod", owner, repo, pinnedRef: sha12 });
}
return { pins, skipped };
}
/** Parse .gitmodules for submodule URLs, then read pinned SHAs via git ls-tree. */
function parseGitModules(localPath: string): { pins: RawPin[]; skipped: SkippedEntry[] } {
const gmPath = join(localPath, ".gitmodules");
if (!existsSync(gmPath)) return { pins: [], skipped: [] };
const pins: RawPin[] = [];
const skipped: SkippedEntry[] = [];
const text = readFileSync(gmPath, "utf8");
// Collect submodule entries: each block has a path and url
const blocks = text.split(/^\[submodule /m).slice(1);
for (const block of blocks) {
const pathMatch = /^\s*path\s*=\s*(.+)$/m.exec(block);
const urlMatch = /^\s*url\s*=\s*(.+)$/m.exec(block);
if (!pathMatch?.[1] || !urlMatch?.[1]) continue;
const subPath = pathMatch[1].trim();
const url = urlMatch[1].trim();
const ownerRepo = parseGitHubRemoteUrl(url);
if (!ownerRepo) {
skipped.push({
source: ".gitmodules",
key: subPath,
value: url,
reason: "not_github",
});
continue;
}
// Read pinned SHA from git ls-tree
try {
const lsOut = execFileSync("git", ["ls-tree", "HEAD", subPath], {
cwd: localPath,
encoding: "utf8",
timeout: 5_000,
stdio: ["ignore", "pipe", "ignore"],
}).trim();
// Format: <mode> commit <sha>\t<path>
const shaMatch = /^\d+\s+commit\s+([0-9a-f]{40})\t/.exec(lsOut);
if (!shaMatch?.[1]) {
skipped.push({
source: ".gitmodules",
key: subPath,
value: url,
reason: "ls_tree_no_sha",
});
continue;
}
pins.push({
source: ".gitmodules",
owner: ownerRepo.owner,
repo: ownerRepo.repo,
pinnedRef: shaMatch[1],
});
} catch {
skipped.push({
source: ".gitmodules",
key: subPath,
value: url,
reason: "ls_tree_failed",
});
}
}
return { pins, skipped };
}
/** Parse scripts/versions.env for KEY=VALUE lines where value is a 40-char hex SHA. */
function parseVersionsEnv(localPath: string): { skipped: SkippedEntry[] } {
const envPath = join(localPath, "scripts", "versions.env");
if (!existsSync(envPath)) return { skipped: [] };
const skipped: SkippedEntry[] = [];
const text = readFileSync(envPath, "utf8");
for (const line of text.split("\n")) {
const m = /^([A-Z0-9_]+(?:_REF|_SHA|_VERSION))\s*=\s*([^\s#]+)/.exec(line);
if (!m?.[1] || !m[2]) continue;
const key = m[1];
const value = m[2];
if (/^[0-9a-f]{40}$/.test(value)) {
// Can't infer which repo this belongs to — mark as skipped
skipped.push({
source: "scripts/versions.env",
key,
value,
reason: "ambiguous_repo",
});
}
}
return { skipped };
}
/** Parse package.json for dependencies pinned to GitHub URLs. */
function parsePackageJson(localPath: string): { pins: RawPin[]; skipped: SkippedEntry[] } {
const pkgPath = join(localPath, "package.json");
if (!existsSync(pkgPath)) return { pins: [], skipped: [] };
const pins: RawPin[] = [];
const skipped: SkippedEntry[] = [];
let pkg: Record<string, unknown>;
try {
pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as Record<string, unknown>;
} catch {
return { pins, skipped };
}
const allDeps: Record<string, string> = {
...((pkg.dependencies as Record<string, string> | undefined) ?? {}),
...((pkg.devDependencies as Record<string, string> | undefined) ?? {}),
};
for (const [, version] of Object.entries(allDeps)) {
// GitHub shorthand: "owner/repo" or "owner/repo#sha/branch"
const shorthand = /^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)(?:#(.+))?$/.exec(version);
if (shorthand?.[1] && shorthand[2]) {
const owner = shorthand[1];
const repo = shorthand[2];
const ref = shorthand[3] ?? "HEAD";
pins.push({ source: "package.json", owner, repo, pinnedRef: ref });
continue;
}
// Full GitHub URL: https://github.com/owner/repo.git#sha or tarball URL
const ghUrl =
/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\.git)?(?:#(.+))?(?:$|\/)/.exec(
version,
);
if (ghUrl?.[1] && ghUrl[2]) {
const owner = ghUrl[1];
const repo = ghUrl[2];
const ref = ghUrl[3] ?? "HEAD";
pins.push({ source: "package.json", owner, repo, pinnedRef: ref });
}
// Not a GitHub dependency — skip silently
}
return { pins, skipped };
}
// ---------------------------------------------------------------------------
// Tool registration
// ---------------------------------------------------------------------------
const HEAD_QUERY = `query($owner:String!,$repo:String!){
repository(owner:$owner,name:$repo){
defaultBranchRef{ name target{ ...on Commit{ oid committedDate } } }
}
}`;
/**
* Expand a list of pin file patterns against files that actually exist in `localPath`.
* Patterns without a '*' are treated as literal relative paths.
* Patterns with '*' are matched as simple glob-style patterns against known file names.
*/
function expandPinFiles(patterns: string[], localPath: string): string[] {
const knownFiles = ["go.mod", ".gitmodules", "scripts/versions.env", "package.json"];
const expanded = new Set<string>();
for (const pattern of patterns) {
if (!pattern.includes("*")) {
// Literal path — accept as-is if it exists
if (existsSync(join(localPath, pattern))) expanded.add(pattern);
else expanded.add(pattern); // include anyway; parsers will early-return if absent
continue;
}
// Convert glob to regex: only support * and ** wildcards
const re = new RegExp(
"^" +
pattern
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
.replace(/\*\*/g, ".+")
.replace(/\*/g, "[^/]+") +
"$",
);
for (const known of knownFiles) {
if (re.test(known)) expanded.add(known);
}
// Also try matching against actual directory contents for unknown files
try {
const entries = readdirSync(localPath, { recursive: true }) as string[];
for (const entry of entries) {
if (re.test(String(entry))) expanded.add(String(entry));
}
} catch {
// directory not readable
}
}
return [...expanded];
}
export function registerPinDriftTool(server: FastMCP): void {
server.addTool({
name: "pin_drift",
description:
"Audit upstream pin drift in a local repo: checks go.mod (replace + pseudo-versions), " +
".gitmodules, scripts/versions.env, and package.json against upstream default branches.",
annotations: { readOnlyHint: true },
parameters: z.object({
localPath: z.string().describe("Absolute path to the local repo to audit."),
pinFiles: z
.array(z.string())
.optional()
.describe(
"Pin files to parse; supports glob patterns (e.g. '**/versions.env'). Defaults to auto-detect.",
),
ownerAllowlist: z
.array(z.string())
.optional()
.describe("Restrict to these GitHub owners (case-insensitive)."),
grep: z
.string()
.optional()
.describe("Regex; matching commits are tallied in grepMatches (behindBy is unaffected)."),
format: FormatSchema,
}),
execute: async (args) => {
const auth = gateAuth();
if (!auth.ok) return errorRespond(auth.envelope);
const { localPath, ownerAllowlist, grep } = args;
const grepRe = grep ? new RegExp(grep, "i") : undefined;
// Collect pins from each source
const allPins: RawPin[] = [];
const allSkipped: SkippedEntry[] = [];
const autoFiles = ["go.mod", ".gitmodules", "scripts/versions.env", "package.json"];
const filesToCheck = args.pinFiles ? expandPinFiles(args.pinFiles, localPath) : autoFiles;
if (filesToCheck.includes("go.mod")) {
const { pins, skipped } = parseGoMod(localPath);
allPins.push(...pins);
allSkipped.push(...skipped);
}
if (filesToCheck.includes(".gitmodules")) {
const { pins, skipped } = parseGitModules(localPath);
allPins.push(...pins);
allSkipped.push(...skipped);
}
if (filesToCheck.includes("scripts/versions.env")) {
const { skipped } = parseVersionsEnv(localPath);
allSkipped.push(...skipped);
}
if (filesToCheck.includes("package.json")) {
const { pins, skipped } = parsePackageJson(localPath);
allPins.push(...pins);
allSkipped.push(...skipped);
}
// Deduplicate by owner+repo (keep first occurrence)
const seen = new Set<string>();
const uniquePins: RawPin[] = [];
for (const pin of allPins) {
const key = `${pin.owner.toLowerCase()}/${pin.repo.toLowerCase()}`;
if (!seen.has(key)) {
seen.add(key);
uniquePins.push(pin);
}
}
// Apply owner allowlist filter
const filteredPins = ownerAllowlist
? uniquePins.filter((p) =>
ownerAllowlist.some((a) => a.toLowerCase() === p.owner.toLowerCase()),
)
: uniquePins;
// Fan out: resolve each pin against GitHub
const pinResults = await parallelApi(filteredPins, async (pin): Promise<PinEntry> => {
try {
// Resolve pinned SHA date
const pinResolved = await resolveRef(pin.owner, pin.repo, pin.pinnedRef);
const pinnedDate = pinResolved?.committedDate;
// Get default branch + head SHA
const headData = await graphqlQuery<HeadResult>(HEAD_QUERY, {
owner: pin.owner,
repo: pin.repo,
});
const dbRef = headData.repository.defaultBranchRef;
if (!dbRef) {
return {
source: pin.source,
owner: pin.owner,
repo: pin.repo,
pinnedRef: pin.pinnedRef,
defaultBranch: "unknown",
headSha: "",
behindBy: -1,
commits: [],
stale: false,
error: {
code: "NOT_FOUND",
message: `Upstream ${pin.owner}/${pin.repo} has no default branch or is inaccessible.`,
retryable: false,
},
};
}
const defaultBranch = dbRef.name;
const headSha = dbRef.target.oid;
// Already at head?
if (
headSha.startsWith(pin.pinnedRef) ||
pin.pinnedRef.startsWith(headSha.substring(0, 7))
) {
return {
source: pin.source,
owner: pin.owner,
repo: pin.repo,
pinnedRef: pin.pinnedRef,
...(pinnedDate ? { pinnedDate } : {}),
defaultBranch,
headSha,
behindBy: 0,
commits: [],
stale: false,
};
}
// Full SHA to resolve for count-behind
const fullPinnedSha = pinResolved?.oid ?? pin.pinnedRef;
const { behindBy, commits } = await countBehind(
pin.owner,
pin.repo,
defaultBranch,
fullPinnedSha,
100,
);
const grepMatches = grepRe
? commits.filter((c) => grepRe.test(c.message)).length
: undefined;
return {
source: pin.source,
owner: pin.owner,
repo: pin.repo,
pinnedRef: pin.pinnedRef,
...(pinnedDate ? { pinnedDate } : {}),
defaultBranch,
headSha,
behindBy,
...(grepMatches !== undefined ? { grepMatches } : {}),
commits: commits.map((c) => ({
sha7: c.sha7,
message: c.message,
author: c.author,
date: c.date,
})),
stale: behindBy > 0,
};
} catch (err) {
return {
source: pin.source,
owner: pin.owner,
repo: pin.repo,
pinnedRef: pin.pinnedRef,
defaultBranch: "unknown",
headSha: "",
behindBy: -1,
commits: [],
stale: false,
error: classifyError(err),
};
}
});
const staleCount = pinResults.filter((p) => p.stale).length;
const upToDate = pinResults.filter((p) => !p.stale && p.behindBy >= 0).length;
const result: PinDriftResult = {
localPath,
pins: pinResults,
skipped: allSkipped,
summary: { totalPins: pinResults.length, stale: staleCount, upToDate },
};
if (args.format === "json") return jsonRespond(result);
// -----------------------------------------------------------------------
// Markdown output
// -----------------------------------------------------------------------
const lines: string[] = [];
lines.push(`# Pin Drift: ${localPath}`);
lines.push("");
lines.push(
`**${pinResults.length} pins** — ${staleCount} stale, ${upToDate} up to date` +
(allSkipped.length > 0 ? `, ${allSkipped.length} skipped` : ""),
);
if (staleCount > 0) {
lines.push("");
lines.push("## Stale Pins");
lines.push("| Source | Repo | Behind | Pinned SHA |");
lines.push("|--------|------|--------|------------|");
for (const p of pinResults.filter((x) => x.stale)) {
const sha = p.pinnedRef.substring(0, 12);
lines.push(`| ${p.source} | ${p.owner}/${p.repo} | ${p.behindBy} | \`${sha}\` |`);
}
}
const fresh = pinResults.filter((p) => !p.stale && p.behindBy >= 0);
if (fresh.length > 0) {
lines.push("");
lines.push("## Up to Date");
lines.push(fresh.map((p) => `${p.owner}/${p.repo}`).join(", "));
}
if (allSkipped.length > 0) {
lines.push("");
lines.push("## Skipped");
lines.push("| Source | Key | Reason |");
lines.push("|--------|-----|--------|");
for (const s of allSkipped) {
lines.push(`| ${s.source} | \`${s.key}\` | ${s.reason} |`);
}
}
return lines.join("\n");
},
});
}