-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo-status-tool.ts
More file actions
241 lines (221 loc) · 7.5 KB
/
repo-status-tool.ts
File metadata and controls
241 lines (221 loc) · 7.5 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
import { execFileSync } from "node:child_process";
import type { FastMCP } from "fastmcp";
import { z } from "zod";
import { gateAuth } from "./github-auth.js";
import {
classifyError,
graphqlQuery,
parallelApi,
resolveLocalRepoRemote,
} from "./github-client.js";
import {
errorRespond,
jsonRespond,
type McpErrorEnvelope,
mkLocalRepoNoRemote,
truncateText,
} from "./json.js";
import { FormatSchema, LocalOrRemoteRepoSchema, MAX_REPOS_PER_REQUEST } from "./schemas.js";
import { type CheckNode, normalizeFailedChecks, sha7, timeAgo } from "./utils.js";
interface RepoQueryResult {
repository: {
defaultBranchRef: {
name: string;
target: {
oid: string;
messageHeadline: string;
author: { user: { login: string } | null; date: string };
statusCheckRollup: {
state: string;
contexts: { nodes: CheckNode[] };
} | null;
};
} | null;
openPRs: { totalCount: number };
openIssues: { totalCount: number };
};
}
export interface RepoResult {
owner: string;
repo: string;
defaultBranch?: string;
latestCommit?: { sha7: string; message: string; author: string; date: string };
ci?: { status: string; failedChecks?: { name: string; conclusion: string }[] };
openPRs?: number;
draftPRs?: number;
openIssues?: number;
local?: { branch: string; dirty: number; ahead: number; behind: number };
error?: McpErrorEnvelope;
}
function getLocalGitState(localPath: string): RepoResult["local"] | undefined {
try {
const branch = execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
cwd: localPath,
encoding: "utf8",
}).trim();
const dirtyOut = execFileSync("git", ["status", "--short"], {
cwd: localPath,
encoding: "utf8",
});
const dirty = dirtyOut.split("\n").filter((l) => l.trim()).length;
let ahead = 0;
let behind = 0;
try {
const aheadStr = execFileSync("git", ["rev-list", "--count", "@{upstream}..HEAD"], {
cwd: localPath,
encoding: "utf8",
}).trim();
const behindStr = execFileSync("git", ["rev-list", "--count", "HEAD..@{upstream}"], {
cwd: localPath,
encoding: "utf8",
}).trim();
ahead = Number.parseInt(aheadStr, 10) || 0;
behind = Number.parseInt(behindStr, 10) || 0;
} catch {
// no upstream configured
}
return { branch, dirty, ahead, behind };
} catch {
return undefined;
}
}
export function formatRepoStatusMarkdown(results: RepoResult[]): string {
return results
.map((r) => {
if (r.error) return `## ${r.owner}/${r.repo}\nError (${r.error.code}): ${r.error.message}`;
const lines: string[] = [];
lines.push(`## ${r.owner}/${r.repo} (${r.defaultBranch ?? "?"})`);
if (r.latestCommit) {
lines.push(
`Latest: \`${r.latestCommit.sha7}\` ${r.latestCommit.message}` +
` — ${r.latestCommit.author}, ${r.latestCommit.date}`,
);
}
if (r.ci) {
const state = r.ci.status === "success" ? "passing" : "failing";
const extra = r.ci.failedChecks?.map((c) => c.name).join(", ");
lines.push(`CI: ${state}${extra ? `: ${extra}` : ""}`);
} else {
lines.push("CI: not configured");
}
const draft = r.draftPRs ? ` (${r.draftPRs} draft)` : "";
lines.push(`PRs: ${r.openPRs ?? 0} open${draft} · Issues: ${r.openIssues ?? 0} open`);
if (r.local) {
lines.push(
`[Local: ${r.local.branch}, ${r.local.dirty} dirty, ` +
`${r.local.ahead} ahead / ${r.local.behind} behind]`,
);
}
return lines.join("\n");
})
.join("\n\n");
}
const REPO_STATUS_QUERY = `
query RepoStatus($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
defaultBranchRef {
name
target {
... on Commit {
oid
messageHeadline
author { user { login } date }
statusCheckRollup {
state
contexts(first: 20) {
nodes {
... on CheckRun { name conclusion }
... on StatusContext { context state }
}
}
}
}
}
}
openPRs: pullRequests(states: OPEN) { totalCount }
openIssues: issues(states: OPEN) { totalCount }
}
}`;
// The GraphQL API doesn't expose a draft:true filter on pullRequests.totalCount directly.
// Use a separate search-based query for draft count to avoid fetching 100 nodes.
const DRAFT_COUNT_QUERY = `
query DraftCount($q: String!) {
search(query: $q, type: ISSUE, first: 0) { issueCount }
}`;
export function registerRepoStatusTool(server: FastMCP): void {
server.addTool({
name: "repo_status",
description: `Multi-repo dashboard: HEAD commit, CI status, open PR/issue counts. Accepts up to ${MAX_REPOS_PER_REQUEST} repos; include \`localPath\` for local git state.`,
annotations: { readOnlyHint: true },
parameters: z.object({
repos: z
.array(LocalOrRemoteRepoSchema)
.min(1)
.max(MAX_REPOS_PER_REQUEST)
.describe("Repos to query."),
format: FormatSchema,
}),
execute: async (args) => {
const auth = gateAuth();
if (!auth.ok) return errorRespond(auth.envelope);
const results = await parallelApi(args.repos, async (repoRef) => {
let owner: string;
let repo: string;
let localState: RepoResult["local"] | undefined;
if ("localPath" in repoRef) {
const resolved = resolveLocalRepoRemote(repoRef.localPath);
if (!resolved) {
return {
owner: "unknown",
repo: repoRef.localPath,
error: mkLocalRepoNoRemote(repoRef.localPath),
};
}
owner = resolved.owner;
repo = resolved.repo;
localState = getLocalGitState(repoRef.localPath);
} else {
owner = repoRef.owner;
repo = repoRef.repo;
}
try {
const [data, draftData] = await Promise.all([
graphqlQuery<RepoQueryResult>(REPO_STATUS_QUERY, { owner, name: repo }),
graphqlQuery<{ search: { issueCount: number } }>(DRAFT_COUNT_QUERY, {
q: `repo:${owner}/${repo} is:pr is:open draft:true`,
}),
]);
const r = data.repository;
const result: RepoResult = { owner, repo };
if (r.defaultBranchRef) {
result.defaultBranch = r.defaultBranchRef.name;
const c = r.defaultBranchRef.target;
result.latestCommit = {
sha7: sha7(c.oid),
message: truncateText(c.messageHeadline, 60),
author: c.author.user?.login ?? "unknown",
date: timeAgo(c.author.date),
};
const rollup = c.statusCheckRollup;
if (rollup) {
const failed = normalizeFailedChecks(rollup.contexts.nodes);
result.ci = {
status: rollup.state.toLowerCase(),
...(failed.length > 0 ? { failedChecks: failed } : {}),
};
}
}
result.openPRs = r.openPRs.totalCount;
result.draftPRs = draftData.search.issueCount;
result.openIssues = r.openIssues.totalCount;
if (localState) result.local = localState;
return result;
} catch (err) {
return { owner, repo, error: classifyError(err) };
}
});
if (args.format === "json") return jsonRespond({ repos: results });
return formatRepoStatusMarkdown(results);
},
});
}