Skip to content

Commit

Permalink
feat: add replyIssueComment
Browse files Browse the repository at this point in the history
  • Loading branch information
jjranalli committed Apr 16, 2023
1 parent 1d1832a commit 1884524
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/github/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { handleGithubAuth } from "@lib/handleGithubAuth"
import { replyIssueComment } from "@lib/replyIssueComment"
import { summarizePullRequest } from "@lib/summarizePullRequest"
import { NextRequest, NextResponse } from "next/server"

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

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

await summarizePullRequest(payload, octokit)
} else if (payload.action == "created") {
if (payload.comment.body.includes("/codex-ask")) {
// If a comment is created, reply to it
const octokit = await handleGithubAuth(payload)

await replyIssueComment(payload, octokit)
}
}

return NextResponse.json("ok")
Expand Down
55 changes: 55 additions & 0 deletions lib/replyIssueComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Octokit } from "@octokit/rest"
import { ChatCompletionRequestMessage } from "openai-streams"
import { generateChatGpt } from "../utils/generateChatGpt"
import { getCodeDiff } from "../utils/getCodeDiff"

export const startDescription = "\n\n<!-- start pr-codex -->"
export const endDescription = "<!-- end pr-codex -->"
const systemPrompt =
"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 (`)."

export async function replyIssueComment(payload: any, octokit: Octokit) {
// Get relevant PR information
const { repository, issue, sender, comment } = payload

const question = comment.body.split("/codex-ask")[1].trim()

if (question) {
const { owner, repo, issue_number } = {
owner: repository.owner.login,
repo: repository.name,
issue_number: issue.number
}

// Get the diff content using Octokit and GitHub API
const { codeDiff } = await getCodeDiff(owner, repo, issue_number, octokit)

// If there are changes, trigger workflow
if (codeDiff?.length != 0) {
const messages: ChatCompletionRequestMessage[] = [
{
role: "system",
content: systemPrompt
},
{
role: "user",
content: `${question}\n\nHere is the code diff:\n\n${codeDiff}`
}
]

const codexResponse = await generateChatGpt(messages)

const description = `> ${question}\n\n@${sender.login} ${codexResponse}`

await octokit.issues.createComment({
owner,
repo,
issue_number,
body: description
})

return codexResponse
}
throw new Error("No changes in PR")
}
}

0 comments on commit 1884524

Please sign in to comment.