-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy-work-tool.ts
More file actions
203 lines (184 loc) · 6.7 KB
/
my-work-tool.ts
File metadata and controls
203 lines (184 loc) · 6.7 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
import type { FastMCP } from "fastmcp";
import { z } from "zod";
import { gateAuth } from "./github-auth.js";
import { classifyError, graphqlQuery } from "./github-client.js";
import { errorRespond, jsonRespond, truncateText } from "./json.js";
import { FormatSchema } from "./schemas.js";
import { timeAgo } from "./utils.js";
interface GraphQLPullRequest {
__typename: "PullRequest";
number: number;
title: string;
isDraft: boolean;
updatedAt: string;
repository: { nameWithOwner: string };
author: { login: string };
reviewDecision: string | null;
commits: { nodes: { commit: { statusCheckRollup: { state: string } | null } }[] };
}
interface GraphQLIssue {
__typename: "Issue";
number: number;
title: string;
updatedAt: string;
repository: { nameWithOwner: string };
labels: { nodes: { name: string }[] };
}
type SearchNode = GraphQLPullRequest | GraphQLIssue;
interface SearchResponse {
authored: { nodes: SearchNode[] };
reviewRequested: { nodes: SearchNode[] };
assignedIssues: { nodes: SearchNode[] };
}
export function registerMyWorkTool(server: FastMCP): void {
server.addTool({
name: "my_work",
description:
"Personal work queue: your open PRs (CI + review state), PRs awaiting your review, and assigned issues.",
annotations: { readOnlyHint: true },
parameters: z.object({
username: z.string().optional().describe("GitHub username. Defaults to authenticated user."),
maxResults: z.number().int().min(1).max(100).optional().default(30),
blockedOnMe: z
.boolean()
.optional()
.default(false)
.describe(
"When true, filters to items needing your immediate action: authored PRs with CI failure or changes requested, plus all pending review requests.",
),
format: FormatSchema,
}),
execute: async (args) => {
const auth = gateAuth();
if (!auth.ok) return errorRespond(auth.envelope);
let username = args.username;
if (!username) {
try {
const viewer = await graphqlQuery<{ viewer: { login: string } }>(
"query { viewer { login } }",
);
username = viewer.viewer.login;
} catch (err) {
return errorRespond(classifyError(err));
}
}
const max = args.maxResults;
const query = `query {
authored: search(query: "is:pr is:open author:${username}", type: ISSUE, first: ${max}) {
nodes {
... on PullRequest {
__typename number title isDraft updatedAt
repository { nameWithOwner }
author { login }
reviewDecision
commits(last: 1) { nodes { commit { statusCheckRollup { state } } } }
}
}
}
reviewRequested: search(query: "is:pr is:open review-requested:${username}", type: ISSUE, first: ${max}) {
nodes {
... on PullRequest {
__typename number title updatedAt
repository { nameWithOwner }
author { login }
}
}
}
assignedIssues: search(query: "is:issue is:open assignee:${username}", type: ISSUE, first: ${max}) {
nodes {
... on Issue {
__typename number title updatedAt
repository { nameWithOwner }
labels(first: 5) { nodes { name } }
}
}
}
}`;
try {
const data = await graphqlQuery<SearchResponse>(query);
const allAuthoredPrs = data.authored.nodes
.filter((n): n is GraphQLPullRequest => n.__typename === "PullRequest")
.map((n) => ({
repo: n.repository.nameWithOwner,
number: n.number,
title: truncateText(n.title, 80),
draft: n.isDraft,
ci: n.commits.nodes[0]?.commit.statusCheckRollup?.state ?? "NONE",
reviewDecision: n.reviewDecision,
updatedAt: n.updatedAt,
}));
const reviewRequests = data.reviewRequested.nodes
.filter((n): n is GraphQLPullRequest => n.__typename === "PullRequest")
.map((n) => ({
repo: n.repository.nameWithOwner,
number: n.number,
title: truncateText(n.title, 80),
author: n.author.login,
updatedAt: n.updatedAt,
}));
const assignedIssues = data.assignedIssues.nodes
.filter((n): n is GraphQLIssue => n.__typename === "Issue")
.map((n) => ({
repo: n.repository.nameWithOwner,
number: n.number,
title: truncateText(n.title, 80),
labels: n.labels.nodes.map((l) => l.name),
updatedAt: n.updatedAt,
}));
// "Blocked on me" filter: authored PRs where action is needed + all review requests
const authoredPrs = args.blockedOnMe
? allAuthoredPrs.filter(
(pr) =>
pr.ci === "FAILURE" ||
pr.reviewDecision === "CHANGES_REQUESTED" ||
pr.reviewDecision === null,
)
: allAuthoredPrs;
const result = { username, authoredPrs, reviewRequests, assignedIssues };
if (args.format === "json") return jsonRespond(result);
// Markdown
const lines: string[] = [`# My Work (@${username})`, ""];
lines.push(`## Authored PRs (${authoredPrs.length})`);
if (authoredPrs.length === 0) {
lines.push("No open PRs.");
} else {
for (const pr of authoredPrs) {
const ci = pr.ci === "SUCCESS" ? "CI:ok" : pr.ci === "FAILURE" ? "CI:fail" : "CI:?";
const draft = pr.draft ? "[DRAFT] " : "";
const review = pr.reviewDecision?.toLowerCase().replace(/_/g, " ") ?? "pending";
lines.push(
`- ${pr.repo}#${pr.number} ${draft}${pr.title}` +
` — ${ci}, ${review}, ${timeAgo(pr.updatedAt)}`,
);
}
}
lines.push("");
lines.push(`## Review Requests (${reviewRequests.length})`);
if (reviewRequests.length === 0) {
lines.push("No review requests.");
} else {
for (const r of reviewRequests) {
lines.push(
`- ${r.repo}#${r.number} ${r.title} — by ${r.author}, ${timeAgo(r.updatedAt)}`,
);
}
}
lines.push("");
lines.push(`## Assigned Issues (${assignedIssues.length})`);
if (assignedIssues.length === 0) {
lines.push("No assigned issues.");
} else {
for (const iss of assignedIssues) {
const labels = iss.labels.length > 0 ? ` (${iss.labels.join(", ")})` : "";
lines.push(
`- ${iss.repo}#${iss.number} ${iss.title}${labels}, ${timeAgo(iss.updatedAt)}`,
);
}
}
return lines.join("\n");
} catch (err) {
return errorRespond(classifyError(err));
}
},
});
}