diff options
| author | Aiden Cline <[email protected]> | 2025-11-25 23:51:46 -0600 |
|---|---|---|
| committer | Aiden Cline <[email protected]> | 2025-11-25 23:51:46 -0600 |
| commit | 2fbd462e6e35263274e975a2084324b8721f886f (patch) | |
| tree | c1d19ec2b61e58e88807e36c6f7fbe9d655756c3 /github/index.ts | |
| parent | e1cc98d448f8db971fc2fbed6055839b2f233d2f (diff) | |
| download | opencode-2fbd462e6e35263274e975a2084324b8721f886f.tar.gz opencode-2fbd462e6e35263274e975a2084324b8721f886f.zip | |
Reapply "feat(github): add ability to react to PR Review Comments in Workflow (#4705)"
This reverts commit e1cc98d448f8db971fc2fbed6055839b2f233d2f.
Diffstat (limited to 'github/index.ts')
| -rw-r--r-- | github/index.ts | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/github/index.ts b/github/index.ts index b681ff92f..6d826326e 100644 --- a/github/index.ts +++ b/github/index.ts @@ -5,7 +5,7 @@ import { graphql } from "@octokit/graphql" import * as core from "@actions/core" import * as github from "@actions/github" import type { Context as GitHubContext } from "@actions/github/lib/context" -import type { IssueCommentEvent } from "@octokit/webhooks-types" +import type { IssueCommentEvent, PullRequestReviewCommentEvent } from "@octokit/webhooks-types" import { createOpencodeClient } from "@opencode-ai/sdk" import { spawn } from "node:child_process" @@ -124,7 +124,7 @@ let exitCode = 0 type PromptFiles = Awaited<ReturnType<typeof getUserPrompt>>["promptFiles"] try { - assertContextEvent("issue_comment") + assertContextEvent("issue_comment", "pull_request_review_comment") assertPayloadKeyword() await assertOpencodeConnected() @@ -241,19 +241,43 @@ function createOpencode() { } function assertPayloadKeyword() { - const payload = useContext().payload as IssueCommentEvent + const payload = useContext().payload as IssueCommentEvent | PullRequestReviewCommentEvent const body = payload.comment.body.trim() if (!body.match(/(?:^|\s)(?:\/opencode|\/oc)(?=$|\s)/)) { throw new Error("Comments must mention `/opencode` or `/oc`") } } +function getReviewCommentContext() { + const context = useContext() + if (context.eventName !== "pull_request_review_comment") { + return null + } + + const payload = context.payload as PullRequestReviewCommentEvent + return { + file: payload.comment.path, + diffHunk: payload.comment.diff_hunk, + line: payload.comment.line, + originalLine: payload.comment.original_line, + position: payload.comment.position, + commitId: payload.comment.commit_id, + originalCommitId: payload.comment.original_commit_id, + } +} + async function assertOpencodeConnected() { let retry = 0 let connected = false do { try { - await client.app.get<true>() + await client.app.log<true>({ + body: { + service: "github-workflow", + level: "info", + message: "Prepare to react to Github Workflow event", + }, + }) connected = true break } catch (e) {} @@ -383,11 +407,24 @@ async function createComment() { } async function getUserPrompt() { + const context = useContext() + const payload = context.payload as IssueCommentEvent | PullRequestReviewCommentEvent + const reviewContext = getReviewCommentContext() + let prompt = (() => { - const payload = useContext().payload as IssueCommentEvent const body = payload.comment.body.trim() - if (body === "/opencode" || body === "/oc") return "Summarize this thread" - if (body.includes("/opencode") || body.includes("/oc")) return body + if (body === "/opencode" || body === "/oc") { + if (reviewContext) { + return `Review this code change and suggest improvements for the commented lines:\n\nFile: ${reviewContext.file}\nLines: ${reviewContext.line}\n\n${reviewContext.diffHunk}` + } + return "Summarize this thread" + } + if (body.includes("/opencode") || body.includes("/oc")) { + if (reviewContext) { + return `${body}\n\nContext: You are reviewing a comment on file "${reviewContext.file}" at line ${reviewContext.line}.\n\nDiff context:\n${reviewContext.diffHunk}` + } + return body + } throw new Error("Comments must mention `/opencode` or `/oc`") })() |
