Skip to content

Commit

Permalink
Merge pull request ubiquity-os#167 from gentlementlegen/fix/disable-kv
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 authored Oct 24, 2024
2 parents a882dad + 6037f76 commit ebf9635
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/github/github-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { EmitterWebhookEvent, Webhooks } from "@octokit/webhooks";
import { customOctokit } from "./github-client";
import { GitHubContext, SimplifiedContext } from "./github-context";
import { createAppAuth } from "@octokit/auth-app";
import { CloudflareKv } from "./utils/cloudflare-kv";
import { KvStore } from "./utils/kv-store";
import { PluginChainState } from "./types/plugin";

export type Options = {
environment: "production" | "development";
webhookSecret: string;
appId: string | number;
privateKey: string;
pluginChainState: CloudflareKv<PluginChainState>;
pluginChainState: KvStore<PluginChainState>;
};

export class GitHubEventHandler {
public webhooks: Webhooks<SimplifiedContext>;
public on: Webhooks<SimplifiedContext>["on"];
public onAny: Webhooks<SimplifiedContext>["onAny"];
public onError: Webhooks<SimplifiedContext>["onError"];
public pluginChainState: CloudflareKv<PluginChainState>;
public pluginChainState: KvStore<PluginChainState>;

readonly environment: "production" | "development";
private readonly _webhookSecret: string;
Expand Down
94 changes: 50 additions & 44 deletions src/github/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,50 +69,56 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
return;
}

for (const pluginChain of pluginChains) {
if (await shouldSkipPlugin(context, pluginChain)) {
continue;
}

// invoke the first plugin in the chain
const { plugin, with: settings } = pluginChain.uses[0];
const isGithubPluginObject = isGithubPlugin(plugin);
console.log(`Calling handler ${JSON.stringify(plugin)} for event ${event.name}`);

const stateId = crypto.randomUUID();

const state = {
eventId: context.id,
eventName: context.key,
eventPayload: event.payload,
currentPlugin: 0,
pluginChain: pluginChain.uses,
outputs: new Array(pluginChain.uses.length),
inputs: new Array(pluginChain.uses.length),
};

const ref = isGithubPluginObject ? (plugin.ref ?? (await getDefaultBranch(context, plugin.owner, plugin.repo))) : plugin;
const token = await eventHandler.getToken(event.payload.installation.id);
const inputs = new PluginInput(context.eventHandler, stateId, context.key, event.payload, settings, token, ref);

state.inputs[0] = inputs;
await eventHandler.pluginChainState.put(stateId, state);
await Promise.all(
pluginChains.map(async (pluginChain) => {
if (await shouldSkipPlugin(context, pluginChain)) {
return;
}
if (!("installation" in event.payload) || event.payload.installation?.id === undefined) {
console.log(`No installation found, cannot invoke plugin`, pluginChain);
return;
}

// We wrap the dispatch so a failing plugin doesn't break the whole execution
try {
if (!isGithubPluginObject) {
await dispatchWorker(plugin, await inputs.getWorkerInputs());
} else {
await dispatchWorkflow(context, {
owner: plugin.owner,
repository: plugin.repo,
workflowId: plugin.workflowId,
ref: plugin.ref,
inputs: await inputs.getWorkflowInputs(),
});
// invoke the first plugin in the chain
const { plugin, with: settings } = pluginChain.uses[0];
const isGithubPluginObject = isGithubPlugin(plugin);
console.log(`Calling handler ${JSON.stringify(plugin)} for event ${event.name}`);

const stateId = crypto.randomUUID();

const state = {
eventId: context.id,
eventName: context.key,
eventPayload: event.payload,
currentPlugin: 0,
pluginChain: pluginChain.uses,
outputs: new Array(pluginChain.uses.length),
inputs: new Array(pluginChain.uses.length),
};

const ref = isGithubPluginObject ? (plugin.ref ?? (await getDefaultBranch(context, plugin.owner, plugin.repo))) : plugin;
const token = await eventHandler.getToken(event.payload.installation.id);
const inputs = new PluginInput(context.eventHandler, stateId, context.key, event.payload, settings, token, ref);

state.inputs[0] = inputs;
await eventHandler.pluginChainState.put(stateId, state);

// We wrap the dispatch so a failing plugin doesn't break the whole execution
try {
if (!isGithubPluginObject) {
await dispatchWorker(plugin, await inputs.getWorkerInputs());
} else {
await dispatchWorkflow(context, {
owner: plugin.owner,
repository: plugin.repo,
workflowId: plugin.workflowId,
ref: plugin.ref,
inputs: await inputs.getWorkflowInputs(),
});
}
} catch (e) {
console.error(`An error occurred while processing the plugin chain, will skip plugin ${JSON.stringify(plugin)}`, e);
}
} catch (e) {
console.error(`An error occurred while processing the plugin chain, will skip plugin ${JSON.stringify(plugin)}`, e);
}
}
})
);
}
15 changes: 0 additions & 15 deletions src/github/utils/cloudflare-kv.ts

This file was deleted.

55 changes: 55 additions & 0 deletions src/github/utils/kv-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* KvStore is an interface representing a simple key-value store.
*
* @template T - The type of the value to be stored and retrieved.
*/
export interface KvStore<T> {
get(id: string): Promise<T | null>;
put(id: string, state: T): Promise<void>;
}

/**
* CloudflareKv is a class that provides an interface to interact with
* Cloudflare KV (Key-Value) storage.
*
* It implements the KvStore interface to handle generic types.
*
* @template T - The type of the values being stored.
*/
// export class CloudflareKv<T> implements KvStore<T> {
// private _kv: KVNamespace;
//
// constructor(kv: KVNamespace) {
// this._kv = kv;
// }
//
// get(id: string): Promise<T | null> {
// return this._kv.get(id, "json");
// }
//
// put(id: string, state: T): Promise<void> {
// return this._kv.put(id, JSON.stringify(state));
// }
// }

/**
* A class that implements the KvStore interface, representing an empty key-value store.
* All get operations return null and put operations do nothing, but log the action.
*
* @template T - The type of values to be stored.
*/
export class EmptyStore<T> implements KvStore<T> {
constructor(kv: KVNamespace) {
console.log(`Creating empty kv`, kv);
}

get(id: string): Promise<T | null> {
console.log(`get KV ${id}`);
return Promise.resolve(null);
}

put(id: string, state: T): Promise<void> {
console.log(`put KV ${id} ${state}`);
return Promise.resolve();
}
}
4 changes: 2 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Value } from "@sinclair/typebox/value";
import { GitHubEventHandler } from "./github/github-event-handler";
import { bindHandlers } from "./github/handlers";
import { Env, envSchema } from "./github/types/env";
import { CloudflareKv } from "./github/utils/cloudflare-kv";
import { EmptyStore } from "./github/utils/kv-store";
import { WebhookEventName } from "@octokit/webhooks-types";

export default {
Expand All @@ -18,7 +18,7 @@ export default {
webhookSecret: env.APP_WEBHOOK_SECRET,
appId: env.APP_ID,
privateKey: env.APP_PRIVATE_KEY,
pluginChainState: new CloudflareKv(env.PLUGIN_CHAIN_STATE),
pluginChainState: new EmptyStore(env.PLUGIN_CHAIN_STATE),
});
bindHandlers(eventHandler);
await eventHandler.webhooks.verifyAndReceive({ id, name: eventName, payload: await request.text(), signature: signatureSha256 });
Expand Down
6 changes: 5 additions & 1 deletion tests/dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ jest.mock("@octokit/auth-app", () => ({
createAppAuth: jest.fn(() => () => jest.fn(() => "1234")),
}));

jest.mock("../src/github/utils/cloudflare-kv", () => ({
jest.mock("../src/github/utils/kv-store", () => ({
CloudflareKv: jest.fn().mockImplementation(() => ({
get: jest.fn(),
put: jest.fn(),
})),
EmptyStore: jest.fn().mockImplementation(() => ({
get: jest.fn(),
put: jest.fn(),
})),
}));

jest.mock("../src/github/types/plugin", () => {
Expand Down

0 comments on commit ebf9635

Please sign in to comment.