diff --git a/src/adapters/sqlite/helpers/pull-request-adapter.ts b/src/adapters/sqlite/helpers/pull-request-adapter.ts index 367c6780..6383e07a 100644 --- a/src/adapters/sqlite/helpers/pull-request-adapter.ts +++ b/src/adapters/sqlite/helpers/pull-request-adapter.ts @@ -1,9 +1,22 @@ import { DataSource } from "typeorm"; import { Context } from "../../../types"; +import { PullRequest } from "../entities/pull-request"; import { Super } from "./sqlite"; export class PullRequestAdapter extends Super { constructor(sqlite: DataSource, context: Context) { super(sqlite, context); } + + public async create(url: string) { + const pullRequest = new PullRequest(); + + pullRequest.url = url; + try { + return await pullRequest.save(); + } catch (e) { + this.context.logger.error(`Failed to save the url ${url} to the DB.`); + return null; + } + } } diff --git a/src/handlers/pull-request-opened.ts b/src/handlers/pull-request-opened.ts new file mode 100644 index 00000000..cf413e71 --- /dev/null +++ b/src/handlers/pull-request-opened.ts @@ -0,0 +1,15 @@ +import { Result } from "../proxy"; +import { Context } from "../types"; + +/** + * On pull request opening, we want to add the entry to start watching it. + */ +export async function handlePullRequestOpened(context: Context): Promise { + const { + adapters: { + sqlite: { pullRequest }, + }, + } = context; + await pullRequest.create(context.payload.pull_request.html_url); + return { status: "ok" }; +} diff --git a/src/plugin.ts b/src/plugin.ts index ac8cd5a3..68f0e435 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2,6 +2,7 @@ import { Octokit } from "@octokit/rest"; import { Logs } from "@ubiquity-dao/ubiquibot-logger"; import { createAdapters } from "./adapters"; import { initializeDataSource } from "./adapters/sqlite/data-source"; +import { proxyCallbacks } from "./proxy"; import { Context, Env, PluginInputs } from "./types"; import "reflect-metadata"; @@ -23,10 +24,5 @@ export async function plugin(inputs: PluginInputs, env: Env) { }; context.adapters = createAdapters(dataSource, context); - - if (context.eventName === "pull_request.opened") { - // do something - } else { - context.logger.error(`Unsupported event: ${context.eventName}`); - } + return proxyCallbacks[inputs.eventName](context, env); } diff --git a/src/proxy/index.ts b/src/proxy/index.ts new file mode 100644 index 00000000..2e9244c3 --- /dev/null +++ b/src/proxy/index.ts @@ -0,0 +1,23 @@ +import { handlePullRequestOpened } from "../handlers/pull-request-opened"; +import { Context, Env, SupportedEventsU } from "../types"; + +export interface Result { + status: "ok" | "failed" | "skipped"; + content?: string; + reason?: string; +} + +const callbacks: { [k in SupportedEventsU]: (context: Context, env: Env) => Result | Promise } = { + "pull_request.opened": handlePullRequestOpened, + "pull_request.reopened": handlePullRequestOpened, +}; + +export const proxyCallbacks = new Proxy(callbacks, { + get(target, prop: SupportedEventsU) { + if (!(prop in target)) { + console.warn(`${prop} is not supported, skipping.`); + return async () => ({ status: "skipped", reason: "unsupported_event" }); + } + return target[prop].bind(target); + }, +}); diff --git a/src/types/context.ts b/src/types/context.ts index a9163fc8..bfe7f489 100644 --- a/src/types/context.ts +++ b/src/types/context.ts @@ -5,7 +5,7 @@ import { createAdapters } from "../adapters"; import { Env } from "./env"; import { PluginSettings } from "./plugin-inputs"; -export type SupportedEventsU = "pull_request.opened" | "pull_request.reopened" | "pull_request.closed"; +export type SupportedEventsU = "pull_request.opened" | "pull_request.reopened"; export type SupportedEvents = { [K in SupportedEventsU]: K extends WebhookEventName ? WebhookEvent : never;