summaryrefslogtreecommitdiffhomepage
path: root/packages/function/src/api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/function/src/api.ts')
-rw-r--r--packages/function/src/api.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/packages/function/src/api.ts b/packages/function/src/api.ts
index c5ac8b4a4..4ba7cbf65 100644
--- a/packages/function/src/api.ts
+++ b/packages/function/src/api.ts
@@ -232,6 +232,9 @@ export default {
)
}
+ /**
+ * Used by the GitHub action to get GitHub installation access token given the OIDC token
+ */
if (request.method === "POST" && method === "exchange_github_app_token") {
const EXPECTED_AUDIENCE = "opencode-github-action"
const GITHUB_ISSUER = "https://token.actions.githubusercontent.com"
@@ -285,6 +288,38 @@ export default {
})
}
+ /**
+ * Used by the opencode CLI to check if the GitHub app is installed
+ */
+ if (request.method === "GET" && method === "get_github_app_installation") {
+ const owner = url.searchParams.get("owner")
+ const repo = url.searchParams.get("repo")
+
+ 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 octokit = new Octokit({ auth: appAuth.token })
+ let installation
+ try {
+ const ret = await octokit.apps.getRepoInstallation({ owner, repo })
+ installation = ret.data
+ } catch (err) {
+ if (err instanceof Error && err.message.includes("Not Found")) {
+ // not installed
+ } else {
+ throw err
+ }
+ }
+
+ return new Response(JSON.stringify({ installation }), {
+ headers: { "Content-Type": "application/json" },
+ })
+ }
+
return new Response("Not Found", { status: 404 })
},
}