From bca162d6d853cf0dd5076d23220c07e37cf06a46 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:18:27 +0000 Subject: [PATCH 1/4] chore: wrap octokit init --- src/handlers/workflow-proxy.ts | 1 - src/types/plugin-context-single.ts | 15 +++++++++------ src/types/telegram-bot-single.ts | 4 ++++ 3 files changed, 13 insertions(+), 7 deletions(-) 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..71700ec 100644 --- a/src/types/plugin-context-single.ts +++ b/src/types/plugin-context-single.ts @@ -85,12 +85,15 @@ export class PluginContext { 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; - } - }); - + try { + await this.getApp().eachInstallation(async (installation) => { + if (installation.installation.account?.login === this.config.storageOwner) { + octokit = installation.octokit; + } + }); + } catch (er) { + logger.error("Error initializing octokit in getTelegramEventOctokit", { er }); + } if (!octokit) { throw new Error("Octokit could not be initialized"); } diff --git a/src/types/telegram-bot-single.ts b/src/types/telegram-bot-single.ts index cfdd6ad..ef5b437 100644 --- a/src/types/telegram-bot-single.ts +++ b/src/types/telegram-bot-single.ts @@ -23,6 +23,10 @@ export class TelegramBotSingleton { const octokit = await PluginContext.getInstance().getTelegramEventOctokit(); + if (!octokit) { + throw new Error("octokit could not be initialized"); + } + if (!TelegramBotSingleton._instance) { TelegramBotSingleton._instance = new TelegramBotSingleton(); try { From 219f4400ffab8ea22187e92bf7d940a1395c49c4 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:45:57 +0000 Subject: [PATCH 2/4] chore: more error handling (debugging in the dark) --- src/bot/helpers/grammy-context.ts | 21 ++++++++++++++++++--- src/types/plugin-context-single.ts | 8 +++----- src/types/telegram-bot-single.ts | 11 +++++++++-- 3 files changed, 30 insertions(+), 10 deletions(-) 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/types/plugin-context-single.ts b/src/types/plugin-context-single.ts index 71700ec..97a44e7 100644 --- a/src/types/plugin-context-single.ts +++ b/src/types/plugin-context-single.ts @@ -82,11 +82,11 @@ 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; try { - await this.getApp().eachInstallation(async (installation) => { + await this.getApp().eachInstallation((installation) => { if (installation.installation.account?.login === this.config.storageOwner) { octokit = installation.octokit; } @@ -94,9 +94,7 @@ export class PluginContext { } catch (er) { logger.error("Error initializing octokit in getTelegramEventOctokit", { er }); } - if (!octokit) { - throw new Error("Octokit could not be initialized"); - } + return octokit; } diff --git a/src/types/telegram-bot-single.ts b/src/types/telegram-bot-single.ts index ef5b437..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,10 +22,16 @@ 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 could not be initialized"); + throw new Error("Octokit not initialized"); } if (!TelegramBotSingleton._instance) { From 10f5cba497974e050d6c8d0dcd012ba9ec1d3510 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:56:45 +0000 Subject: [PATCH 3/4] chore: lowercase install login --- src/types/plugin-context-single.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/plugin-context-single.ts b/src/types/plugin-context-single.ts index 97a44e7..53d892c 100644 --- a/src/types/plugin-context-single.ts +++ b/src/types/plugin-context-single.ts @@ -87,7 +87,7 @@ export class PluginContext { try { await this.getApp().eachInstallation((installation) => { - if (installation.installation.account?.login === this.config.storageOwner) { + if (installation.installation.account?.login.toLowerCase() === this.config.storageOwner.toLowerCase()) { octokit = installation.octokit; } }); From 8468ef17d528d84c1b7bdf64cc73ce60c76fc877 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:03:42 +0000 Subject: [PATCH 4/4] chore: add defaults comment in PluginContext --- src/types/plugin-context-single.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/types/plugin-context-single.ts b/src/types/plugin-context-single.ts index 53d892c..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; }