Skip to content

Commit

Permalink
chore: restore manifest cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Dec 2, 2024
1 parent f4a193b commit 84e4c7f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/github/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Manifest, manifestSchema } from "@ubiquity-os/plugin-sdk/manifest";
import { Buffer } from "node:buffer";
import { Value } from "@sinclair/typebox/value";

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

export function getPluginsForEvent(plugins: PluginConfiguration["plugins"], event: EmitterWebhookEventName) {
return plugins.filter((plugin) => {
return plugin.uses?.[0].runsOn?.includes(event);
Expand All @@ -16,6 +18,10 @@ export function getManifest(context: GitHubContext, plugin: string | GithubPlugi
}

async function fetchActionManifest(context: GitHubContext<"issue_comment.created">, { owner, repo, ref }: GithubPlugin): Promise<Manifest | null> {
const manifestKey = ref ? `${owner}:${repo}:${ref}` : `${owner}:${repo}`;
if (_manifestCache[manifestKey]) {
return _manifestCache[manifestKey];
}
try {
const { data } = await context.octokit.rest.repos.getContent({
owner,
Expand All @@ -26,7 +32,9 @@ async function fetchActionManifest(context: GitHubContext<"issue_comment.created
if ("content" in data) {
const content = Buffer.from(data.content, "base64").toString();
const contentParsed = JSON.parse(content);
return decodeManifest(contentParsed);
const manifest = decodeManifest(contentParsed);
_manifestCache[manifestKey] = manifest;
return manifest;
}
} catch (e) {
console.warn(`Could not find a manifest for Action ${owner}/${repo}: ${e}`);
Expand All @@ -35,11 +43,16 @@ async function fetchActionManifest(context: GitHubContext<"issue_comment.created
}

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

0 comments on commit 84e4c7f

Please sign in to comment.