Skip to content

Commit

Permalink
chore: added save to db step
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jul 3, 2024
1 parent 2dbe73b commit 22c79f0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/adapters/sqlite/helpers/pull-request-adapter.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
15 changes: 15 additions & 0 deletions src/handlers/pull-request-opened.ts
Original file line number Diff line number Diff line change
@@ -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<Result> {
const {
adapters: {
sqlite: { pullRequest },
},
} = context;
await pullRequest.create(context.payload.pull_request.html_url);
return { status: "ok" };
}
8 changes: 2 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
}
23 changes: 23 additions & 0 deletions src/proxy/index.ts
Original file line number Diff line number Diff line change
@@ -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<Result> } = {
"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);
},
});
2 changes: 1 addition & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K> : never;
Expand Down

0 comments on commit 22c79f0

Please sign in to comment.