From 6479b4a40c76db8e0a7ce825b8dcd0373de9e0b3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Mar 2026 18:06:57 +0000 Subject: [PATCH] fix(github): fix several bugs in github action index.ts - Add missing `isScheduleEvent()` function that was called but never defined, causing a ReferenceError at runtime - Use `MENTIONS` env var in `assertPayloadKeyword()` and `getUserPrompt()` instead of hardcoded `/opencode` and `/oc` trigger phrases - Fix `useEnvGithubToken()` to check `USE_GITHUB_TOKEN` flag and read `GITHUB_TOKEN` env var, matching the action.yml configuration - Add `useEnvOidcBaseUrl()` to support configurable `OIDC_BASE_URL` env var instead of hardcoded `https://api.opencode.ai` https://claude.ai/code/session_018YbkAoFxCwyYaaP2UQ8mGq --- github/index.ts | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/github/index.ts b/github/index.ts index 1a0a9926224..f7e27c66bc6 100644 --- a/github/index.ts +++ b/github/index.ts @@ -241,11 +241,19 @@ function createOpencode() { } } +function useEnvMentions(): string[] { + return (process.env["MENTIONS"] || "/opencode,/oc") + .split(",") + .map((m) => m.trim().toLowerCase()) + .filter(Boolean) +} + function assertPayloadKeyword() { 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`") + const body = payload.comment.body.trim().toLowerCase() + const mentions = useEnvMentions() + if (!mentions.some((m) => body === m || body.includes(m))) { + throw new Error(`Comments must mention ${mentions.map((m) => "`" + m + "`").join(" or ")}`) } } @@ -339,7 +347,17 @@ function useEnvMock() { } function useEnvGithubToken() { - return process.env["TOKEN"] + const value = process.env["USE_GITHUB_TOKEN"] + if (!value || value === "false") return undefined + if (value === "true") { + const token = process.env["GITHUB_TOKEN"] + if (!token) + throw new Error( + "GITHUB_TOKEN environment variable is not set. When using use_github_token, you must provide GITHUB_TOKEN.", + ) + return token + } + throw new Error(`Invalid use_github_token value: ${value}. Must be a boolean.`) } function isMock() { @@ -353,6 +371,10 @@ function isPullRequest() { return Boolean(payload.issue.pull_request) } +function isScheduleEvent() { + return useContext().eventName === "schedule" +} + function useContext() { return isMock() ? (JSON.parse(useEnvMock().mockEvent!) as GitHubContext) : github.context } @@ -366,15 +388,22 @@ function useShareUrl() { return isMock() ? "https://dev.opencode.ai" : "https://opencode.ai" } +function useEnvOidcBaseUrl() { + const value = process.env["OIDC_BASE_URL"] + if (!value) return "https://api.opencode.ai" + return value.replace(/\/+$/, "") +} + async function getAccessToken() { const { repo } = useContext() const envToken = useEnvGithubToken() if (envToken) return envToken + const oidcBaseUrl = useEnvOidcBaseUrl() let response if (isMock()) { - response = await fetch("https://api.opencode.ai/exchange_github_app_token_with_pat", { + response = await fetch(`${oidcBaseUrl}/exchange_github_app_token_with_pat`, { method: "POST", headers: { Authorization: `Bearer ${useEnvMock().mockToken}`, @@ -383,7 +412,7 @@ async function getAccessToken() { }) } else { const oidcToken = await core.getIDToken("opencode-github-action") - response = await fetch("https://api.opencode.ai/exchange_github_app_token", { + response = await fetch(`${oidcBaseUrl}/exchange_github_app_token`, { method: "POST", headers: { Authorization: `Bearer ${oidcToken}`, @@ -416,21 +445,23 @@ async function getUserPrompt() { const payload = context.payload as IssueCommentEvent | PullRequestReviewCommentEvent const reviewContext = getReviewCommentContext() + const mentions = useEnvMentions() let prompt = (() => { const body = payload.comment.body.trim() - if (body === "/opencode" || body === "/oc") { + const bodyLower = body.toLowerCase() + if (mentions.some((m) => bodyLower === m)) { 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 (mentions.some((m) => bodyLower.includes(m))) { 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`") + throw new Error(`Comments must mention ${mentions.map((m) => "`" + m + "`").join(" or ")}`) })() // Handle images