Skip to content

Commit

Permalink
chore: update plugin schema logic (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jul 12, 2024
1 parent b746886 commit af78d17
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/github/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
return;
}

const pluginChains = config.plugins[context.key].concat(config.plugins["*"]);
const pluginChains = config.plugins.filter((plugin) => {
console.log("Plugin runs on", plugin.name, plugin.runsOn);
if (plugin.runsOn) {
return plugin.runsOn.includes(event.name);
}
return false;
});

if (pluginChains.length === 0) {
console.log(`No handler found for event ${event.name}`);
Expand Down
5 changes: 3 additions & 2 deletions src/github/types/plugin-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type as T } from "@sinclair/typebox";
import { StaticDecode } from "@sinclair/typebox";
import { StandardValidator } from "typebox-validators";
import { githubWebhookEvents } from "./webhook-events";
import { runEvent } from "../../types/manifest";

const pluginNameRegex = new RegExp("^([0-9a-zA-Z-._]+)\\/([0-9a-zA-Z-._]+)(?::([0-9a-zA-Z-._]+))?(?:@([0-9a-zA-Z-._]+(?:\\/[0-9a-zA-Z-._]+)?))?$");

Expand Down Expand Up @@ -65,12 +65,13 @@ const handlerSchema = T.Array(
example: T.Optional(T.String()),
uses: pluginChainSchema,
skipBotEvents: T.Boolean({ default: true }),
runsOn: T.Optional(T.Array(runEvent, { default: [] })),
}),
{ default: [] }
);

export const configSchema = T.Object({
plugins: T.Record(T.Enum(githubWebhookEvents), handlerSchema, { default: {} }),
plugins: handlerSchema,
});

export const configSchemaValidator = new StandardValidator(configSchema);
Expand Down
4 changes: 4 additions & 0 deletions src/types/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { type Static, Type as T } from "@sinclair/typebox";
import { StandardValidator } from "typebox-validators";
import { emitterEventNames } from "@octokit/webhooks/dist-types/generated/webhook-names";

export const runEvent = T.Union(emitterEventNames.map((o) => T.Literal(o)));

export const commandSchema = T.Object({
description: T.String({ minLength: 1 }),
Expand All @@ -10,6 +13,7 @@ export const manifestSchema = T.Object({
name: T.String({ minLength: 1 }),
description: T.String({ minLength: 1 }),
commands: T.Record(T.String(), commandSchema),
"ubiquity:runsOn": T.Optional(T.Array(runEvent, { default: [] })),
});

export const manifestValidator = new StandardValidator(manifestSchema);
Expand Down

0 comments on commit af78d17

Please sign in to comment.