-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.ts
More file actions
430 lines (374 loc) · 12.3 KB
/
webhook.ts
File metadata and controls
430 lines (374 loc) · 12.3 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
import { createHmac, timingSafeEqual } from "node:crypto";
import { existsSync, readFileSync, statSync } from "node:fs";
import { createServer } from "node:http";
import { extname, join, resolve } from "node:path";
import type { ClickUpWebhookPayload } from "./clickup/types.js";
import type {
GitHubPullRequestEvent,
GitHubPullRequestReviewEvent,
} from "./github/types.js";
import { handleAdminApi } from "./admin/api.js";
import { handleUpgrade, setupWebSocket } from "./admin/websocket.js";
import { addComment, getRepoUrl, getTask } from "./clickup/client.js";
import { getConfig } from "./config.js";
import {
getReviewRunByPR,
getSetting,
hasActiveRun,
hasCompletedRun,
resetReviewRun,
updateReviewRun,
} from "./db.js";
import { taskEvents } from "./events.js";
import { executeTask } from "./executor.js";
import { createChildLogger } from "./logger.js";
import { enqueueReview } from "./review/queue.js";
const log = createChildLogger("webhook");
let running = false;
let runningTaskId: null | string = null;
const queue: string[] = [];
const MIME_TYPES: Record<string, string> = {
".css": "text/css",
".html": "text/html",
".js": "application/javascript",
".json": "application/json",
".png": "image/png",
".svg": "image/svg+xml",
".woff": "font/woff",
".woff2": "font/woff2",
};
export function enqueueTask(taskId: string): void {
if (running) {
log.info({ taskId }, "Task queued — another task is in progress");
queue.push(taskId);
taskEvents.emit("queue:changed", { queue: [...queue], runningTaskId });
return;
}
running = true;
queue.push(taskId);
taskEvents.emit("queue:changed", { queue: [...queue], runningTaskId });
void drainQueue();
}
export function getQueueStatus(): {
queue: string[];
runningTaskId: null | string;
} {
return { queue: [...queue], runningTaskId };
}
export function startWebhookServer(): void {
const config = getConfig();
setupWebSocket();
const webDistPath = resolve(import.meta.dirname, "..", "web", "dist");
const server = createServer((req, res) => {
const url = req.url ?? "/";
// Health check
if (req.method === "GET" && url === "/health") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ status: "ok" }));
return;
}
// Admin API
if (url.startsWith("/api/")) {
handleAdminApi(req, res);
return;
}
// Webhook endpoint
if (req.method === "POST" && url === "/webhook") {
void (async () => {
try {
const rawBody = await readBody(req);
const signature = req.headers["x-signature"] as string | undefined;
if (!signature || !verifySignature(rawBody, signature)) {
log.warn("Webhook request with invalid signature");
res.writeHead(401);
res.end("Unauthorized");
return;
}
const payload = JSON.parse(
rawBody.toString(),
) as ClickUpWebhookPayload;
// Respond 200 immediately — ClickUp requires <7s response
res.writeHead(200);
res.end("OK");
if (payload.event !== "taskAssigneeUpdated") {
log.debug({ event: payload.event }, "Ignoring non-assignee event");
return;
}
const assigneeAdded = payload.history_items.find(
(item) =>
item.field === "assignee_add" &&
String(item.after.id) === config.CLICKUP_CLAUDE_USER_ID,
);
if (!assigneeAdded) {
log.debug(
{ taskId: payload.task_id },
"Assignee change does not involve Claude user",
);
return;
}
log.info(
{ taskId: payload.task_id },
"Claude user assigned — enqueuing task",
);
enqueueTask(payload.task_id);
} catch (err) {
log.error(err, "Error handling webhook request");
if (!res.headersSent) {
res.writeHead(500);
res.end("Internal Server Error");
}
}
})();
return;
}
// GitHub webhook endpoint
if (req.method === "POST" && url === "/github-webhook") {
void (async () => {
try {
const rawBody = await readBody(req);
if (
!verifyGitHubSignature(
rawBody,
req.headers["x-hub-signature-256"] as string | undefined,
)
) {
log.warn("GitHub webhook request with invalid signature");
res.writeHead(401);
res.end("Unauthorized");
return;
}
res.writeHead(200);
res.end("OK");
const event = req.headers["x-github-event"] as string | undefined;
if (event === "pull_request_review") {
const payload = JSON.parse(
rawBody.toString(),
) as GitHubPullRequestReviewEvent;
handleGitHubReviewEvent(payload);
return;
}
if (event !== "pull_request") {
log.debug({ event }, "Ignoring non-PR GitHub event");
return;
}
const payload = JSON.parse(
rawBody.toString(),
) as GitHubPullRequestEvent;
handleGitHubPREvent(payload);
} catch (err) {
log.error(err, "Error handling GitHub webhook");
if (!res.headersSent) {
res.writeHead(500);
res.end("Internal Server Error");
}
}
})();
return;
}
// Static file serving for the admin SPA
if (req.method === "GET" && existsSync(webDistPath)) {
const urlPath = url.split("?")[0] ?? "/";
let filePath = join(webDistPath, urlPath);
// Try the exact file path
if (existsSync(filePath) && statSync(filePath).isFile()) {
const ext = extname(filePath);
const mime = MIME_TYPES[ext] ?? "application/octet-stream";
res.writeHead(200, { "Content-Type": mime });
res.end(readFileSync(filePath));
return;
}
// SPA fallback — serve index.html for non-file routes
filePath = join(webDistPath, "index.html");
if (existsSync(filePath)) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(readFileSync(filePath));
return;
}
}
res.writeHead(404);
res.end("Not Found");
});
// WebSocket upgrade handler
server.on("upgrade", (req, socket, head) => {
const url = new URL(
req.url ?? "/",
`http://${req.headers.host ?? "localhost"}`,
);
if (url.pathname === "/ws") {
handleUpgrade(req, socket, head);
} else {
socket.destroy();
}
});
server.listen(config.WEBHOOK_PORT, () => {
log.info({ port: config.WEBHOOK_PORT }, "Webhook server listening");
});
}
async function drainQueue(): Promise<void> {
while (queue.length > 0) {
const taskId = queue.shift();
if (!taskId) break;
runningTaskId = taskId;
taskEvents.emit("queue:changed", { queue: [...queue], runningTaskId });
try {
await processTask(taskId);
} catch (err) {
log.error({ error: err, taskId }, "Error processing queued task");
}
}
running = false;
runningTaskId = null;
taskEvents.emit("queue:changed", { queue: [], runningTaskId: null });
}
function handleGitHubPREvent(payload: GitHubPullRequestEvent): void {
if (getSetting("reviews_enabled", "true") !== "true") {
log.debug("Reviews disabled via settings, ignoring GitHub PR event");
return;
}
const config = getConfig();
if (!config.GITHUB_USERNAME) {
log.debug("GITHUB_USERNAME not configured, ignoring GitHub PR event");
return;
}
const action = payload.action;
const pr = payload.pull_request;
const repo = payload.repository.full_name;
// Handle synchronize (new commits pushed) — trigger re-review if changes_requested
if (action === "synchronize") {
const existing = getReviewRunByPR(repo, pr.number);
if (existing?.status === "changes_requested") {
log.info(
{ prNumber: pr.number, repo, reviewId: existing.id },
"New commits pushed — triggering re-review",
);
resetReviewRun(existing.id);
void (async () => {
const { enqueueReviewById } = await import("./review/queue.js");
enqueueReviewById(existing.id);
})();
}
return;
}
// Match review_requested where the requested reviewer is us
if (action === "review_requested") {
if (payload.requested_reviewer?.login !== config.GITHUB_USERNAME) {
log.debug({ action, repo }, "Review request not for us");
return;
}
} else if (action === "assigned") {
if (payload.assignee?.login !== config.GITHUB_USERNAME) {
log.debug({ action, repo }, "Assignment not for us");
return;
}
} else {
log.debug({ action }, "Ignoring non-review PR action");
return;
}
// Dedup: skip if already tracked and not failed
const existing = getReviewRunByPR(repo, pr.number);
if (existing && existing.status !== "failed") {
log.debug({ prNumber: pr.number, repo }, "Review already tracked");
return;
}
log.info({ prNumber: pr.number, repo }, "Enqueuing PR review");
enqueueReview({
pr_branch: pr.head.ref,
pr_number: pr.number,
pr_title: pr.title,
pr_url: pr.html_url,
repo_full_name: repo,
});
}
function handleGitHubReviewEvent(payload: GitHubPullRequestReviewEvent): void {
if (payload.action !== "submitted") {
log.debug(
{ action: payload.action },
"Ignoring non-submitted review event",
);
return;
}
const repo = payload.repository.full_name;
const prNumber = payload.pull_request.number;
const reviewState = payload.review.state;
const existing = getReviewRunByPR(repo, prNumber);
if (existing?.status !== "ready") {
log.debug({ prNumber, repo }, "No ready review to update");
return;
}
if (reviewState === "changes_requested") {
log.info(
{ prNumber, repo, reviewId: existing.id },
"Marking review as changes_requested",
);
updateReviewRun(existing.id, { status: "changes_requested" });
taskEvents.emit("review:status", {
reviewId: existing.id,
status: "changes_requested",
});
} else {
log.info(
{ prNumber, repo, reviewId: existing.id },
"Marking review as approved",
);
updateReviewRun(existing.id, { status: "approved" });
taskEvents.emit("review:status", {
reviewId: existing.id,
status: "approved",
});
}
}
async function processTask(taskId: string): Promise<void> {
if (hasActiveRun(taskId) || hasCompletedRun(taskId)) {
log.debug({ taskId }, "Skipping task — already processed");
return;
}
const task = await getTask(taskId);
const repoUrl = getRepoUrl(task);
if (!repoUrl) {
log.warn({ taskId, taskName: task.name }, "Task missing GitHub Repo field");
await addComment(
taskId,
"⚠️ Claude Task Runner: This task is missing a GitHub Repo URL in the custom field. Please add it so I can work on this task.",
);
return;
}
log.info({ repoUrl, taskId, taskName: task.name }, "Processing task");
await executeTask(task, repoUrl);
}
function readBody(req: import("node:http").IncomingMessage): Promise<Buffer> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
req.on("data", (chunk: Buffer) => {
chunks.push(chunk);
});
req.on("end", () => {
resolve(Buffer.concat(chunks));
});
req.on("error", reject);
});
}
function verifyGitHubSignature(
rawBody: Buffer,
signature: string | undefined,
): boolean {
const config = getConfig();
if (!config.GITHUB_WEBHOOK_SECRET) {
// If no secret configured, skip verification (allow manual testing)
return true;
}
if (!signature) return false;
const hmac = createHmac("sha256", config.GITHUB_WEBHOOK_SECRET);
hmac.update(rawBody);
const expected = "sha256=" + hmac.digest("hex");
const expectedBuf = Buffer.from(expected);
const signatureBuf = Buffer.from(signature);
if (expectedBuf.length !== signatureBuf.length) return false;
return timingSafeEqual(expectedBuf, signatureBuf);
}
function verifySignature(rawBody: Buffer, signature: string): boolean {
const config = getConfig();
const hmac = createHmac("sha256", config.WEBHOOK_SECRET);
hmac.update(rawBody);
const expected = hmac.digest("hex");
return signature === expected;
}