diff --git a/github/README.md b/github/README.md index 17b24ffb1d6..e08ae25899c 100644 --- a/github/README.md +++ b/github/README.md @@ -119,7 +119,7 @@ To test locally: MODEL=anthropic/claude-sonnet-4-20250514 \ ANTHROPIC_API_KEY=sk-ant-api03-1234567890 \ GITHUB_RUN_ID=dummy \ - MOCK_TOKEN=github_pat_1234567890 \ + TOKEN=github_pat_1234567890 \ MOCK_EVENT='{"eventName":"issue_comment",...}' \ bun /path/to/opencode/github/index.ts ``` @@ -127,7 +127,7 @@ To test locally: - `MODEL`: The model used by opencode. Same as the `MODEL` defined in the GitHub workflow. - `ANTHROPIC_API_KEY`: Your model provider API key. Same as the keys defined in the GitHub workflow. - `GITHUB_RUN_ID`: Dummy value to emulate GitHub action environment. - - `MOCK_TOKEN`: A GitHub personal access token. This token is used to verify you have `admin` or `write` access to the test repo. Generate a token [here](https://github.com/settings/personal-access-tokens). + - `TOKEN`: A GitHub personal access token with write access to the test repo, used directly for GitHub API calls. Generate a token [here](https://github.com/settings/personal-access-tokens). - `MOCK_EVENT`: Mock GitHub event payload (see templates below). - `/path/to/opencode`: Path to your cloned opencode repo. `bun /path/to/opencode/github/index.ts` runs your local version of `opencode`. diff --git a/github/index.ts b/github/index.ts index e8acd9a9cef..48cd05c1a0d 100644 --- a/github/index.ts +++ b/github/index.ts @@ -334,7 +334,6 @@ function useEnvShare() { function useEnvMock() { return { mockEvent: process.env["MOCK_EVENT"], - mockToken: process.env["MOCK_TOKEN"], } } @@ -343,8 +342,7 @@ function useEnvGithubToken() { } function isMock() { - const { mockEvent, mockToken } = useEnvMock() - return Boolean(mockEvent || mockToken) + return Boolean(useEnvMock().mockEvent) } function isPullRequest() { @@ -367,29 +365,17 @@ function useShareUrl() { } async function getAccessToken() { - const { repo } = useContext() - const envToken = useEnvGithubToken() if (envToken) return envToken + if (isMock()) throw new Error("TOKEN is required when running with MOCK_EVENT") - let response - if (isMock()) { - response = await fetch("https://api.opencode.ai/exchange_github_app_token_with_pat", { - method: "POST", - headers: { - Authorization: `Bearer ${useEnvMock().mockToken}`, - }, - body: JSON.stringify({ owner: repo.owner, repo: repo.repo }), - }) - } else { - const oidcToken = await core.getIDToken("opencode-github-action") - response = await fetch("https://api.opencode.ai/exchange_github_app_token", { - method: "POST", - headers: { - Authorization: `Bearer ${oidcToken}`, - }, - }) - } + const oidcToken = await core.getIDToken("opencode-github-action") + const response = await fetch("https://api.opencode.ai/exchange_github_app_token", { + method: "POST", + headers: { + Authorization: `Bearer ${oidcToken}`, + }, + }) if (!response.ok) { const responseJson = (await response.json()) as { error?: string } diff --git a/packages/function/src/api.ts b/packages/function/src/api.ts index f63563eecf5..a6a7c2d0fcb 100644 --- a/packages/function/src/api.ts +++ b/packages/function/src/api.ts @@ -111,54 +111,4 @@ export default new Hono() ) } }) - /** - * Used by the GitHub action to get GitHub installation access token given user PAT token (used when testing `opencode github run` locally) - */ - .post("/exchange_github_app_token_with_pat", async (c) => { - const body = await c.req.json<{ owner: string; repo: string }>() - const owner = body.owner - const repo = body.repo - - try { - // get Authorization header - const authHeader = c.req.header("Authorization") - const token = authHeader?.replace(/^Bearer /, "") - if (!token) throw new Error("Authorization header is required") - - // Verify permissions - const userClient = new Octokit({ auth: token }) - const { data: repoData } = await userClient.repos.get({ owner, repo }) - if (!repoData.permissions.admin && !repoData.permissions.push && !repoData.permissions.maintain) - throw new Error("User does not have write permissions") - - // Get installation token - const auth = createAppAuth({ - appId: Resource.GITHUB_APP_ID.value, - privateKey: Resource.GITHUB_APP_PRIVATE_KEY.value, - }) - const appAuth = await auth({ type: "app" }) - - // Lookup installation - const appClient = new Octokit({ auth: appAuth.token }) - const { data: installation } = await appClient.apps.getRepoInstallation({ - owner, - repo, - }) - - // Get installation token - const installationAuth = await auth({ - type: "installation", - installationId: installation.id, - }) - - return c.json({ token: installationAuth.token }) - } catch (e: any) { - let error = e - if (e instanceof Error) { - error = e.message - } - - return c.json({ error }, { status: 401 }) - } - }) .all("*", (c) => c.text("Not Found", 404))