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;