diff --git a/src/handlers/proxy.ts b/src/handlers/proxy.ts deleted file mode 100644 index 1515ede3..00000000 --- a/src/handlers/proxy.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Context, Env, SupportedEventsU } from "../types"; -import { userSelfAssign, userStartStop } from "./user-start-stop"; - -export enum HttpStatusCode { - OK = 200, - NOT_MODIFIED = 304, -} - -export interface Result { - status: HttpStatusCode; - content?: string; - reason?: string; -} - -const callbacks: { [k in SupportedEventsU]: (context: Context, env: Env) => Result | Promise } = { - "issues.assigned": userSelfAssign, - "issue_comment.created": userStartStop, -}; - -export function proxyCallbacks({ logger }: Context) { - return new Proxy(callbacks, { - get(target, prop: SupportedEventsU) { - if (!(prop in target)) { - logger.error(`${prop} is not supported, skipping.`); - return async () => ({ status: "skipped", reason: "unsupported_event" }); - } - return target[prop].bind(target); - }, - }); -} diff --git a/src/handlers/result-types.ts b/src/handlers/result-types.ts new file mode 100644 index 00000000..adbfe769 --- /dev/null +++ b/src/handlers/result-types.ts @@ -0,0 +1,10 @@ +export enum HttpStatusCode { + OK = 200, + NOT_MODIFIED = 304, +} + +export interface Result { + status: HttpStatusCode; + content?: string; + reason?: string; +} diff --git a/src/handlers/shared/start.ts b/src/handlers/shared/start.ts index f7de8f71..19a66494 100644 --- a/src/handlers/shared/start.ts +++ b/src/handlers/shared/start.ts @@ -1,6 +1,6 @@ import { Context, ISSUE_TYPE, Label } from "../../types"; import { addAssignees, addCommentToIssue, getAssignedIssues, getAvailableOpenedPullRequests, getTimeValue, isParentIssue } from "../../utils/issue"; -import { HttpStatusCode, Result } from "../proxy"; +import { HttpStatusCode, Result } from "../result-types"; import { hasUserBeenUnassigned } from "./check-assignments"; import { checkTaskStale } from "./check-task-stale"; import { generateAssignmentComment, getDeadline } from "./generate-assignment-comment"; diff --git a/src/handlers/shared/stop.ts b/src/handlers/shared/stop.ts index d56cf0f9..21161510 100644 --- a/src/handlers/shared/stop.ts +++ b/src/handlers/shared/stop.ts @@ -1,6 +1,6 @@ import { Assignee, Context, Sender } from "../../types"; import { addCommentToIssue, closePullRequestForAnIssue } from "../../utils/issue"; -import { HttpStatusCode, Result } from "../proxy"; +import { HttpStatusCode, Result } from "../result-types"; export async function stop(context: Context, issue: Context["payload"]["issue"], sender: Sender, repo: Context["payload"]["repository"]): Promise { const { logger } = context; diff --git a/src/handlers/user-start-stop.ts b/src/handlers/user-start-stop.ts index 5f050e65..143287d6 100644 --- a/src/handlers/user-start-stop.ts +++ b/src/handlers/user-start-stop.ts @@ -1,6 +1,6 @@ import { Context, isContextCommentCreated } from "../types"; import { addCommentToIssue } from "../utils/issue"; -import { HttpStatusCode, Result } from "./proxy"; +import { HttpStatusCode, Result } from "./result-types"; import { getDeadline } from "./shared/generate-assignment-comment"; import { start } from "./shared/start"; import { stop } from "./shared/stop"; diff --git a/src/plugin.ts b/src/plugin.ts index db6f5c34..84763253 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2,7 +2,7 @@ import { Octokit } from "@octokit/rest"; import { createClient } from "@supabase/supabase-js"; import { LogReturn, Logs } from "@ubiquity-dao/ubiquibot-logger"; import { createAdapters } from "./adapters"; -import { proxyCallbacks } from "./handlers/proxy"; +import { userSelfAssign, userStartStop } from "./handlers/user-start-stop"; import { Context, Env, PluginInputs } from "./types"; import { addCommentToIssue } from "./utils/issue"; @@ -23,7 +23,14 @@ export async function startStopTask(inputs: PluginInputs, env: Env) { context.adapters = createAdapters(supabase, context); try { - return proxyCallbacks(context)[inputs.eventName](context, env); + switch (context.eventName) { + case "issue_comment.created": + return await userStartStop(context); + case "issues.assigned": + return await userSelfAssign(context); + default: + context.logger.error(`Unsupported event: ${context.eventName}`); + } } catch (err) { let errorMessage; if (err instanceof LogReturn) {