Skip to content

Commit

Permalink
feat: manifest is now read and cached from the target repo
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jul 19, 2024
1 parent bc05c58 commit 9c66d70
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions src/github/handlers/help-command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { getConfig } from "../utils/config";
import { GithubPlugin, isGithubPlugin } from "../types/plugin-configuration";
import { GithubPlugin } from "../types/plugin-configuration";
import { GitHubContext } from "../github-context";
import { Manifest, manifestSchema, manifestValidator } from "../../types/manifest";
import { manifestSchema, manifestValidator } from "../../types/manifest";
import { Value } from "@sinclair/typebox/value";
import { getManifest } from "../utils/plugins";
import { Buffer } from "node:buffer";

async function parseCommandsFromManifest(context: GitHubContext<"issue_comment.created">, plugin: string | GithubPlugin) {
const commands: string[] = [];
const manifest = await (isGithubPlugin(plugin) ? fetchActionManifest(context, plugin) : fetchWorkerManifest(plugin));
const manifest = await getManifest(context, plugin);
if (manifest) {
Value.Default(manifestSchema, manifest);
const errors = manifestValidator.testReturningErrors(manifest);
Expand All @@ -19,7 +20,7 @@ async function parseCommandsFromManifest(context: GitHubContext<"issue_comment.c
} else {
if (manifest?.commands) {
for (const [key, value] of Object.entries(manifest.commands)) {
commands.push(`| \`/${getContent(key)}\` | ${getContent(value.description)} | \`${getContent(value["ubiquity:example"])}\` |`);
commands.push(`| \`/${getContent(key)}\` | ${getContent(value.description)} | \`${getContent(value["ubiquibot:example"])}\` |`);
}
}
}
Expand Down Expand Up @@ -54,31 +55,3 @@ export async function postHelpCommand(context: GitHubContext<"issue_comment.crea
function getContent(content: string | undefined) {
return content ? content.replace("|", "\\|") : "-";
}

async function fetchActionManifest(context: GitHubContext<"issue_comment.created">, { owner, repo }: GithubPlugin): Promise<Manifest | null> {
try {
const { data } = await context.octokit.repos.getContent({
owner,
repo,
path: "manifest.json",
});
if ("content" in data) {
const content = Buffer.from(data.content, "base64").toString();
return JSON.parse(content);
}
} catch (e) {
console.warn(`Could not find a manifest for ${owner}/${repo}: ${e}`);
}
return null;
}

async function fetchWorkerManifest(url: string): Promise<Manifest | null> {
const manifestUrl = `${url}/manifest.json`;
try {
const result = await fetch(manifestUrl);
return (await result.json()) as Manifest;
} catch (e) {
console.warn(`Could not find a manifest for ${manifestUrl}: ${e}`);
}
return null;
}

0 comments on commit 9c66d70

Please sign in to comment.