From e376ca3ea917a52ae104d7a6f942ab445aca982a Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:29:35 +0000 Subject: [PATCH] chore: add token fallback, add debugg log --- package.json | 2 +- src/types/env.ts | 1 + src/types/plugin-context-single.ts | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 09bec77..2de380f 100644 --- a/package.json +++ b/package.json @@ -100,4 +100,4 @@ "@commitlint/config-conventional" ] } -} \ No newline at end of file +} diff --git a/src/types/env.ts b/src/types/env.ts index 18208f4..b970673 100644 --- a/src/types/env.ts +++ b/src/types/env.ts @@ -92,6 +92,7 @@ export const env = T.Object({ .Encode((obj) => JSON.stringify(obj)), APP_ID: T.String(), APP_PRIVATE_KEY: T.String(), + TEMP_SAFE_PAT: T.Optional(T.String()), }); export type Env = StaticDecode; diff --git a/src/types/plugin-context-single.ts b/src/types/plugin-context-single.ts index 984abc0..5e8a577 100644 --- a/src/types/plugin-context-single.ts +++ b/src/types/plugin-context-single.ts @@ -83,17 +83,27 @@ export class PluginContext { * This can be used with events from both Telegram and GitHub, this token comes from * the worker's environment variables i.e the Storage App. */ - async getTelegramEventOctokit(): Promise { - let octokit: RestOctokitFromApp | null = null; + async getTelegramEventOctokit(): Promise { + let octokit: RestOctokitFromApp | Octokit | null = null; try { await this.getApp().eachInstallation((installation) => { + logger.info("Checking installation", { installation: installation.installation.account?.login, storageOwner: this.config.storageOwner }); if (installation.installation.account?.login.toLowerCase() === this.config.storageOwner.toLowerCase()) { octokit = installation.octokit; } }); } catch (er) { - logger.error("Error initializing octokit in getTelegramEventOctokit", { er }); + logger.error("Error initializing octokit in getTelegramEventOctokit", { er: String(er) }); + } + + if (!octokit) { + logger.info("Falling back to TEMP_SAFE_PAT for octokit"); + octokit = new Octokit({ auth: this.env.TEMP_SAFE_PAT }); + } + + if (!octokit) { + throw new Error("Octokit could not be initialized"); } return octokit;