Skip to content

Commit 1884524

Browse files
committed
feat: add replyIssueComment
1 parent 1d1832a commit 1884524

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

app/github/route.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { handleGithubAuth } from "@lib/handleGithubAuth"
2+
import { replyIssueComment } from "@lib/replyIssueComment"
23
import { summarizePullRequest } from "@lib/summarizePullRequest"
34
import { NextRequest, NextResponse } from "next/server"
45

@@ -9,9 +10,17 @@ export async function POST(req: NextRequest) {
910

1011
try {
1112
if (payload.action == "opened" || payload.action == "synchronize") {
13+
// If a PR is opened or updated, summarize it
1214
const octokit = await handleGithubAuth(payload)
1315

1416
await summarizePullRequest(payload, octokit)
17+
} else if (payload.action == "created") {
18+
if (payload.comment.body.includes("/codex-ask")) {
19+
// If a comment is created, reply to it
20+
const octokit = await handleGithubAuth(payload)
21+
22+
await replyIssueComment(payload, octokit)
23+
}
1524
}
1625

1726
return NextResponse.json("ok")

lib/replyIssueComment.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Octokit } from "@octokit/rest"
2+
import { ChatCompletionRequestMessage } from "openai-streams"
3+
import { generateChatGpt } from "../utils/generateChatGpt"
4+
import { getCodeDiff } from "../utils/getCodeDiff"
5+
6+
export const startDescription = "\n\n<!-- start pr-codex -->"
7+
export const endDescription = "<!-- end pr-codex -->"
8+
const systemPrompt =
9+
"You are a Git diff assistant. Given a code diff, you answer any question related to it. Be concise. Always wrap file names, functions, objects and similar in backticks (`)."
10+
11+
export async function replyIssueComment(payload: any, octokit: Octokit) {
12+
// Get relevant PR information
13+
const { repository, issue, sender, comment } = payload
14+
15+
const question = comment.body.split("/codex-ask")[1].trim()
16+
17+
if (question) {
18+
const { owner, repo, issue_number } = {
19+
owner: repository.owner.login,
20+
repo: repository.name,
21+
issue_number: issue.number
22+
}
23+
24+
// Get the diff content using Octokit and GitHub API
25+
const { codeDiff } = await getCodeDiff(owner, repo, issue_number, octokit)
26+
27+
// If there are changes, trigger workflow
28+
if (codeDiff?.length != 0) {
29+
const messages: ChatCompletionRequestMessage[] = [
30+
{
31+
role: "system",
32+
content: systemPrompt
33+
},
34+
{
35+
role: "user",
36+
content: `${question}\n\nHere is the code diff:\n\n${codeDiff}`
37+
}
38+
]
39+
40+
const codexResponse = await generateChatGpt(messages)
41+
42+
const description = `> ${question}\n\n@${sender.login} ${codexResponse}`
43+
44+
await octokit.issues.createComment({
45+
owner,
46+
repo,
47+
issue_number,
48+
body: description
49+
})
50+
51+
return codexResponse
52+
}
53+
throw new Error("No changes in PR")
54+
}
55+
}

0 commit comments

Comments
 (0)