diff --git a/src/bot/helpers/grammy-context.ts b/src/bot/helpers/grammy-context.ts index 2598330..e6a8d3c 100644 --- a/src/bot/helpers/grammy-context.ts +++ b/src/bot/helpers/grammy-context.ts @@ -22,24 +22,39 @@ interface Dependencies { } interface ExtendedContextFlavor extends Dependencies { - adapters: ReturnType; + adapters?: ReturnType; } export type GrammyContext = ParseModeFlavor & AutoChatActionFlavor>>; export async function createContextConstructor({ logger, config, octokit }: Dependencies) { - const adapters = createAdapters(await PluginContext.getInstance().getContext()); + let adapters: ReturnType | undefined; + + try { + adapters = createAdapters(await PluginContext.getInstance().getContext()); + } catch (er) { + logger.error("createAdapters in Grammy Context failed", { er }); + } + + if (!adapters) { + throw new Error("Adapters not initialized"); + } + return class extends DefaultContext implements ExtendedContextFlavor { logger: Logger; - adapters = adapters; octokit: RestOctokitFromApp = octokit; config: UbiquityOsContext["env"]; + adapters: ReturnType | undefined = adapters; constructor(update: GrammyTelegramUpdate, api: Api, me: UserFromGetMe) { super(update, api, me); this.logger = logger; this.config = config; + if (!this.adapters) { + throw new Error("Adapters not initialized"); + } + /** * We'll need to add handling to detect forks and in such cases * we'll need to handle the storage differently. diff --git a/src/handlers/workflow-proxy.ts b/src/handlers/workflow-proxy.ts index 95e43b9..2b50556 100644 --- a/src/handlers/workflow-proxy.ts +++ b/src/handlers/workflow-proxy.ts @@ -32,7 +32,6 @@ export function proxyWorkflowCallbacks(context: Context): ProxyCallbacks { for (const r of res) { if (r.status !== 200) { await bubbleUpErrorComment(context, new Error(r.reason)); - await exit(1); } } await exit(0); diff --git a/src/types/plugin-context-single.ts b/src/types/plugin-context-single.ts index e7cd9e7..a35ee49 100644 --- a/src/types/plugin-context-single.ts +++ b/src/types/plugin-context-single.ts @@ -23,12 +23,14 @@ export class PluginContext { public readonly inputs: PluginInputs, public _env: Env ) { + // this will fallback to defaults if it's a telegram bot command this._config = this.inputs.settings; } get env() { return Value.Decode(envValidator.schema, Value.Default(envValidator.schema, this._env)); } + set env(env: Env) { this._env = env; } @@ -82,18 +84,19 @@ 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 { + async getTelegramEventOctokit(): Promise { let octokit: RestOctokitFromApp | null = null; - await this.getApp().eachInstallation(async (installation) => { - if (installation.installation.account?.login === this.config.storageOwner) { - octokit = installation.octokit; - } - }); - - if (!octokit) { - throw new Error("Octokit could not be initialized"); + try { + await this.getApp().eachInstallation((installation) => { + if (installation.installation.account?.login.toLowerCase() === this.config.storageOwner.toLowerCase()) { + octokit = installation.octokit; + } + }); + } catch (er) { + logger.error("Error initializing octokit in getTelegramEventOctokit", { er }); } + return octokit; } diff --git a/src/types/telegram-bot-single.ts b/src/types/telegram-bot-single.ts index cfdd6ad..48519ae 100644 --- a/src/types/telegram-bot-single.ts +++ b/src/types/telegram-bot-single.ts @@ -1,3 +1,4 @@ +import { Octokit } from "octokit"; import { Env } from "."; import { Bot, createBot } from "../bot"; import { createServer } from "../server"; @@ -21,7 +22,17 @@ export class TelegramBotSingleton { }, } = env; - const octokit = await PluginContext.getInstance().getTelegramEventOctokit(); + let octokit: Octokit | null = null; + + try { + octokit = await PluginContext.getInstance().getTelegramEventOctokit(); + } catch (er) { + logger.error("Error initializing octokit in TelegramBotSingleton", { er }); + } + + if (!octokit) { + throw new Error("Octokit not initialized"); + } if (!TelegramBotSingleton._instance) { TelegramBotSingleton._instance = new TelegramBotSingleton();