-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels-sync-tool.ts
More file actions
215 lines (193 loc) · 7.37 KB
/
labels-sync-tool.ts
File metadata and controls
215 lines (193 loc) · 7.37 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
import type { FastMCP } from "fastmcp";
import { z } from "zod";
import { gateAuth } from "./github-auth.js";
import { classifyError, getOctokit } from "./github-client.js";
import { errorRespond, jsonRespond } from "./json.js";
export interface LabelInput {
name: string;
color: string;
description?: string;
}
export interface LabelsSyncFailure {
name: string;
action: "create" | "update" | "delete";
error: string;
}
export interface LabelsSyncResult {
created: string[];
updated: string[];
deleted: string[];
skipped: string[];
failures: LabelsSyncFailure[];
dryRun?: boolean;
}
export function registerLabelsSyncTool(server: FastMCP): void {
server.addTool({
name: "labels_sync",
description:
"Synchronize labels in a GitHub repository. Creates new labels, updates existing ones, and optionally deletes extra labels not in the provided list.",
annotations: { readOnlyHint: false },
parameters: z.object({
owner: z.string().describe("GitHub owner or organization."),
repo: z.string().describe("GitHub repository name."),
labels: z
.array(
z.object({
name: z.string().describe("Label name."),
color: z.string().describe("Label color (hex code without #, e.g., 'ffffff')."),
description: z.string().optional().describe("Label description."),
}),
)
.describe("Array of labels to sync to the repository."),
deleteExtra: z
.boolean()
.optional()
.default(false)
.describe("If true, delete labels not in the provided list."),
dryRun: z
.boolean()
.optional()
.default(false)
.describe(
"If true, compute and return the planned changes (created/updated/deleted/skipped) WITHOUT executing any mutation.",
),
}),
execute: async (args) => {
// gateAuth before try so auth errors are not swallowed by the catch
const auth = gateAuth();
if (!auth.ok) return errorRespond(auth.envelope);
try {
const octokit = getOctokit();
const { owner, repo, labels, deleteExtra, dryRun } = args;
// Fetch ALL existing labels using pagination (fixes cap at 100)
const existingRaw = await octokit.paginate(octokit.issues.listLabelsForRepo, {
owner,
repo,
per_page: 100,
});
const existingLabels = new Map(
existingRaw.map((label) => [label.name.toLowerCase(), label]),
);
const result: LabelsSyncResult = {
created: [],
updated: [],
deleted: [],
skipped: [],
failures: [],
};
const providedNames = new Set(labels.map((l) => l.name.toLowerCase()));
if (dryRun) {
// Compute planned changes without executing any mutations
for (const label of labels) {
const existing = existingLabels.get(label.name.toLowerCase());
const normalizedColor = label.color.replace(/^#/, "");
if (existing) {
const needsUpdate =
existing.color.toLowerCase() !== normalizedColor.toLowerCase() ||
(label.description && existing.description !== label.description) ||
(!label.description && existing.description);
if (needsUpdate) {
result.updated.push(label.name);
} else {
result.skipped.push(label.name);
}
} else {
result.created.push(label.name);
}
}
if (deleteExtra) {
for (const [lowerName, label] of existingLabels) {
if (!providedNames.has(lowerName)) {
result.deleted.push(label.name);
}
}
}
return jsonRespond({ ...result, dryRun: true });
}
// Execute create/update operations with Promise.allSettled (partial failure safe)
const createUpdateOps = labels.map(async (label) => {
const existing = existingLabels.get(label.name.toLowerCase());
const normalizedColor = label.color.replace(/^#/, "");
if (existing) {
const needsUpdate =
existing.color.toLowerCase() !== normalizedColor.toLowerCase() ||
(label.description && existing.description !== label.description) ||
(!label.description && existing.description);
if (needsUpdate) {
await octokit.issues.updateLabel({
owner,
repo,
name: label.name,
color: normalizedColor,
...(label.description ? { description: label.description } : {}),
});
return { action: "update" as const, name: label.name };
}
return { action: "skip" as const, name: label.name };
}
await octokit.issues.createLabel({
owner,
repo,
name: label.name,
color: normalizedColor,
...(label.description ? { description: label.description } : {}),
});
return { action: "create" as const, name: label.name };
});
const createUpdateResults = await Promise.allSettled(createUpdateOps);
createUpdateResults.forEach((r, i) => {
if (r.status === "fulfilled") {
if (r.value.action === "update") result.updated.push(r.value.name);
else if (r.value.action === "create") result.created.push(r.value.name);
else result.skipped.push(r.value.name);
} else {
// Determine what action was attempted
const label = labels[i];
const labelName = label?.name ?? `label[${i}]`;
const existing = label ? existingLabels.get(label.name.toLowerCase()) : undefined;
const action = existing ? ("update" as const) : ("create" as const);
result.failures.push({
name: labelName,
action,
error: r.reason instanceof Error ? r.reason.message : String(r.reason),
});
}
});
// Delete extra labels if requested
if (deleteExtra) {
const labelsToDelete = Array.from(existingLabels.entries()).filter(
([lowerName]) => !providedNames.has(lowerName),
);
const deleteOps = labelsToDelete.map(async ([, label]) => {
await octokit.issues.deleteLabel({
owner,
repo,
name: label.name,
});
return label.name;
});
const deleteResults = await Promise.allSettled(deleteOps);
deleteResults.forEach((r, i) => {
if (r.status === "fulfilled") {
result.deleted.push(r.value);
} else {
const labelName = labelsToDelete[i]?.[1]?.name ?? "unknown";
result.failures.push({
name: labelName,
action: "delete" as const,
error: r.reason instanceof Error ? r.reason.message : String(r.reason),
});
}
});
}
return jsonRespond(result);
} catch (err) {
console.error(
`[labels_sync] Failed to sync labels for ${args.owner}/${args.repo}:`,
err instanceof Error ? err.message : String(err),
);
return errorRespond(classifyError(err));
}
},
});
}