Repository navigation
Expand file tree
/
Copy pathhandleGitHubWebhook.ts
More file actions
112 lines (93 loc) · 3.64 KB
/
Copy pathhandleGitHubWebhook.ts
File metadata and controls
112 lines (93 loc) · 3.64 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
import { NextResponse } from "next/server";
import { getCorsHeaders } from "@/lib/networking/getCorsHeaders";
import { verifyGitHubWebhook } from "./verifyGitHubWebhook";
import { encodeGitHubThreadId } from "./encodeGitHubThreadId";
import { extractPRComment } from "./extractPRComment";
import { getCodingAgentPRState, setCodingAgentPRState } from "./prState";
import { triggerUpdatePR } from "@/lib/trigger/triggerUpdatePR";
import { postGitHubComment } from "./postGitHubComment";
const BOT_MENTION = "@recoup-coding-agent";
/**
* Handles incoming GitHub webhook requests for PR comment feedback.
* Supports both issue_comment and pull_request_review_comment events.
* Verifies signature, extracts PR context, and triggers update-pr when the bot is mentioned.
*
* @param request - The incoming webhook request
*/
export async function handleGitHubWebhook(request: Request): Promise<NextResponse> {
const body = await request.text();
const signature = request.headers.get("x-hub-signature-256") ?? "";
const event = request.headers.get("x-github-event") ?? "";
const secret = process.env.GITHUB_WEBHOOK_SECRET;
if (!secret || !(await verifyGitHubWebhook(body, signature, secret))) {
return NextResponse.json(
{ status: "error", error: "Unauthorized" },
{ status: 401, headers: getCorsHeaders() },
);
}
const payload = JSON.parse(body);
const extracted = extractPRComment(event, payload);
if (!extracted) {
return NextResponse.json({ status: "ignored" }, { headers: getCorsHeaders() });
}
let { thread, branch, commentBody } = extracted;
const fullRepo = `${thread.owner}/${thread.repo}`;
const token = process.env.GITHUB_TOKEN;
if (!branch) {
const prResponse = await fetch(
`https://api.github.com/repos/${fullRepo}/pulls/${thread.prNumber}`,
{
headers: {
Authorization: `token ${token}`,
Accept: "application/vnd.github+json",
},
},
);
if (!prResponse.ok) {
return NextResponse.json(
{ status: "error", error: "Failed to fetch PR details" },
{ status: 500, headers: getCorsHeaders() },
);
}
const prData = await prResponse.json();
branch = prData.head.ref;
}
const prState = await getCodingAgentPRState(fullRepo, branch);
if (!prState) {
return NextResponse.json({ status: "no_state" }, { headers: getCorsHeaders() });
}
if (prState.status === "running" || prState.status === "updating") {
return NextResponse.json({ status: "busy" }, { headers: getCorsHeaders() });
}
if (prState.status !== "pr_created" || !prState.snapshotId || !prState.prs?.length) {
return NextResponse.json({ status: "no_state" }, { headers: getCorsHeaders() });
}
const feedback = commentBody.replace(BOT_MENTION, "").trim();
await setCodingAgentPRState(fullRepo, branch, {
...prState,
status: "updating",
});
const threadId = encodeGitHubThreadId(thread);
try {
await triggerUpdatePR({
feedback,
snapshotId: prState.snapshotId,
branch: prState.branch,
repo: prState.repo,
callbackThreadId: threadId,
});
} catch (error) {
await setCodingAgentPRState(fullRepo, branch, prState);
console.error("Failed to trigger update-pr:", error);
return NextResponse.json(
{ status: "error", error: "Failed to trigger update" },
{ status: 500, headers: getCorsHeaders() },
);
}
try {
await postGitHubComment(fullRepo, thread.prNumber, "Got your feedback. Updating the PRs...");
} catch (error) {
console.error("Failed to acknowledge update-pr:", error);
}
return NextResponse.json({ status: "update_triggered" }, { headers: getCorsHeaders() });
}