Skip to content

Commit

Permalink
feat: added manifest to hello-world-plugin.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jul 21, 2024
1 parent 7b21ae4 commit d93b5c3
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 27 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ How to run a "hello-world" plugin the Cloudflare way:
plugins:
- skipBotEvents: true
uses:
# hello-world-plugin
# hello-world-plugin
- plugin: http://127.0.0.1:9090
runsOn: [ "issue_comment.created" ]
type: github
with:
response: world
```
Expand Down
19 changes: 3 additions & 16 deletions src/github/handlers/help-command.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import { getConfig } from "../utils/config";
import { GithubPlugin } from "../types/plugin-configuration";
import { GitHubContext } from "../github-context";
import { manifestSchema, manifestValidator } from "../../types/manifest";
import { Value } from "@sinclair/typebox/value";
import { getManifest } from "../utils/plugins";

async function parseCommandsFromManifest(context: GitHubContext<"issue_comment.created">, plugin: string | GithubPlugin) {
const commands: string[] = [];
const manifest = await getManifest(context, plugin);
if (manifest) {
Value.Default(manifestSchema, manifest);
const errors = manifestValidator.testReturningErrors(manifest);
if (errors !== null) {
console.error(`Failed to load the manifest for ${JSON.stringify(plugin)}`);
for (const error of errors) {
console.error(error);
}
} else {
if (manifest?.commands) {
for (const [key, value] of Object.entries(manifest.commands)) {
commands.push(`| \`/${getContent(key)}\` | ${getContent(value.description)} | \`${getContent(value["ubiquity:example"])}\` |`);
}
}
if (manifest?.commands) {
for (const [key, value] of Object.entries(manifest.commands)) {
commands.push(`| \`/${getContent(key)}\` | ${getContent(value.description)} | \`${getContent(value["ubiquity:example"])}\` |`);
}
}
return commands;
Expand Down
3 changes: 1 addition & 2 deletions src/github/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ async function shouldSkipPlugin(event: EmitterWebhookEvent, context: GitHubConte
const manifest = await getManifest(context, pluginChain.uses[0].plugin);
if (
context.key === "issue_comment.created" &&
pluginChain.command &&
manifest &&
!Object.keys(manifest.commands).some(
(command) => "comment" in context.payload && typeof context.payload.comment !== "string" && context.payload.comment?.body.startsWith(command)
(command) => "comment" in context.payload && typeof context.payload.comment !== "string" && context.payload.comment?.body.startsWith(`/${command}`)
)
) {
console.log(`Skipping plugin chain ${pluginChain.command} because command does not match`);
Expand Down
3 changes: 0 additions & 3 deletions src/github/types/plugin-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ export type PluginChain = StaticDecode<typeof pluginChainSchema>;
const handlerSchema = T.Array(
T.Object({
name: T.Optional(T.String()),
description: T.Optional(T.String()),
command: T.Optional(T.String()),
example: T.Optional(T.String()),
uses: pluginChainSchema,
skipBotEvents: T.Boolean({ default: true }),
}),
Expand Down
19 changes: 16 additions & 3 deletions src/github/utils/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { GithubPlugin, isGithubPlugin, PluginConfiguration } from "../types/plugin-configuration";
import { EmitterWebhookEventName } from "@octokit/webhooks";
import { GitHubContext } from "../github-context";
import { Manifest } from "../../types/manifest";
import { Manifest, manifestSchema, manifestValidator } from "../../types/manifest";
import { Buffer } from "node:buffer";
import { Value } from "@sinclair/typebox/value";

const _manifestCache: Record<string, Manifest> = {};

Expand All @@ -29,7 +30,7 @@ async function fetchActionManifest(context: GitHubContext<"issue_comment.created
});
if ("content" in data) {
const content = Buffer.from(data.content, "base64").toString();
const manifest = JSON.parse(content) as Manifest;
const manifest = decodeManifest(JSON.parse(content));
_manifestCache[manifestKey] = manifest;
return manifest;
}
Expand All @@ -46,11 +47,23 @@ async function fetchWorkerManifest(url: string): Promise<Manifest | null> {
const manifestUrl = `${url}/manifest.json`;
try {
const result = await fetch(manifestUrl);
const manifest = (await result.json()) as Manifest;
const manifest = decodeManifest(await result.json());
_manifestCache[url] = manifest;
return manifest;
} catch (e) {
console.warn(`Could not find a manifest for ${manifestUrl}: ${e}`);
}
return null;
}

function decodeManifest(manifest: unknown) {
const defaultManifest = Value.Default(manifestSchema, manifest);
const errors = manifestValidator.testReturningErrors(manifest as Readonly<unknown>);
if (errors !== null) {
for (const error of errors) {
console.error(error);
}
throw new Error("Manifest is invalid.");
}
return defaultManifest as Manifest;
}
2 changes: 1 addition & 1 deletion src/types/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const commandSchema = T.Object({
export const manifestSchema = T.Object({
name: T.String({ minLength: 1 }),
description: T.String({ default: "" }),
commands: T.Record(T.String(), commandSchema),
commands: T.Record(T.String(), commandSchema, { default: {} }),
"ubiquity:listeners": T.Optional(T.Array(runEvent, { default: [] })),
});

Expand Down
4 changes: 4 additions & 0 deletions tests/__mocks__/hello-world-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createServer } from "@mswjs/http-middleware";
import { Octokit } from "@octokit/core";
import { http, HttpResponse } from "msw";
import manifest from "./manifest.json";

type KernelInput = {
authToken: string;
Expand Down Expand Up @@ -43,6 +44,9 @@ const handlers = [
output: `{ "result": "success", "message": "${body.settings.response}" }`,
});
}),
http.get("/manifest.json", () => {
return HttpResponse.json(manifest);
}),
];

const httpServer = createServer(...handlers);
Expand Down
11 changes: 11 additions & 0 deletions tests/__mocks__/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Hello world",
"description": "Hello world plugin for testing.",
"ubiquity:listeners": [ "issue_comment.created" ],
"commands": {
"hello": {
"ubiquity:example": "/hello",
"description": "Greet Ubiquibot!"
}
}
}

0 comments on commit d93b5c3

Please sign in to comment.