diff --git a/github/index.ts b/github/index.ts index 5add5d256..32b2e42d0 100644 --- a/github/index.ts +++ b/github/index.ts @@ -1,7 +1,7 @@ import { $ } from "bun" import path from "node:path" import * as core from "@actions/core" -import type { IssueCommentEvent, PullRequestReviewCommentEditedEvent } from "@octokit/webhooks-types" +import type { IssueCommentEvent, PullRequestReviewCommentCreatedEvent } from "@octokit/webhooks-types" import { createOpencodeClient } from "@opencode-ai/sdk" import { spawn } from "node:child_process" import type { GitHubIssue, GitHubPullRequest, IssueQueryResponse, PullRequestQueryResponse } from "./src/types" @@ -19,8 +19,6 @@ let exitCode = 0 type PromptFiles = Awaited>["promptFiles"] try { - // TODO - console.log("!@#!@#! Context", Context.state().context) Context.assertEventName("issue_comment", "pull_request_review_comment") assertPayloadKeyword() await assertOpencodeConnected() @@ -131,7 +129,7 @@ function createOpencode() { } function assertPayloadKeyword() { - const payload = Context.payload() + const payload = Context.payload() const body = payload.comment.body.trim() if (!body.match(/(?:^|\s)(?:\/opencode|\/oc)(?=$|\s)/)) { throw new Error("Comments must mention `/opencode` or `/oc`") @@ -179,11 +177,18 @@ function useEnvGithubToken() { return process.env["TOKEN"] } +function isEventPullRequestReviewComment() { + return Context.eventName() === "pull_request_review_comment" +} + function isPullRequest() { + if (isEventPullRequestReviewComment()) return true return Boolean(Context.payload().issue.pull_request) } function useIssueId() { + if (isEventPullRequestReviewComment()) + return Context.payload().pull_request.number return Context.payload().issue.number } @@ -204,7 +209,7 @@ async function createComment() { async function getUserPrompt() { let prompt = (() => { - const body = Context.payload().comment.body.trim() + const body = Context.payload().comment.body.trim() if (body === "/opencode" || body === "/oc") return "Summarize this thread" if (body.includes("/opencode") || body.includes("/oc")) return body throw new Error("Comments must mention `/opencode` or `/oc`") @@ -355,7 +360,9 @@ async function summarize(response: string) { try { return await chat(`Summarize the following in less than 40 characters:\n\n${response}`) } catch (e) { - return `Fix issue: ${Context.payload().issue.title}` + return isEventPullRequestReviewComment() + ? `Fix issue: ${Context.payload().pull_request.title}` + : `Fix issue: ${Context.payload().issue.title}` } } @@ -739,8 +746,8 @@ ${part} const pr = result.repository.pullRequest if (!pr) throw new Error(`PR #${useIssueId()} not found`) - if (Context.eventName() === "pull_request_review_comment_created") { - const comment = Context.payload().comment + if (isEventPullRequestReviewComment()) { + const comment = Context.payload().comment pr.reviewThreads.nodes = pr.reviewThreads.nodes.filter((t) => t.comments.nodes.some((c) => c.id === comment.node_id), )