Skip to content

Commit

Permalink
chore: merge develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed May 7, 2024
2 parents 45cdc4a + c263e2f commit 5ca157b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
13 changes: 11 additions & 2 deletions src/github/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EmitterWebhookEvent } from "@octokit/webhooks";
import { GitHubEventHandler } from "../github-event-handler";
import { getConfig } from "../utils/config";
import { issueCommentCreated } from "./issue-comment/created";
import { repositoryDispatch } from "./repository-dispatch";
import { dispatchWorkflow, getDefaultBranch } from "../utils/workflow-dispatch";
import { DelegatedComputeInputs } from "../types/plugin";
Expand All @@ -17,7 +16,6 @@ function tryCatchWrapper(fn: (event: EmitterWebhookEvent) => unknown) {
}

export function bindHandlers(eventHandler: GitHubEventHandler) {
eventHandler.on("issue_comment.created", issueCommentCreated);
eventHandler.on("repository_dispatch", repositoryDispatch);
eventHandler.onAny(tryCatchWrapper((event) => handleEvent(event, eventHandler))); // onAny should also receive GithubContext but the types in octokit/webhooks are weird
}
Expand Down Expand Up @@ -46,8 +44,19 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp

for (const pluginChain of pluginChains) {
if (pluginChain.skipBotEvents && "sender" in event.payload && event.payload.sender?.type === "Bot") {
console.log("Skipping plugin chain because sender is a bot");
continue;
}
if (
context.key === "issue_comment.created" &&
pluginChain.command &&
"comment" in context.payload &&
!context.payload.comment.body.startsWith(pluginChain.command)
) {
console.log("Skipping plugin chain because command does not match");
continue;
}

// invoke the first plugin in the chain
const { plugin, with: settings } = pluginChain.uses[0];
console.log(`Calling handler for event ${event.name}`);
Expand Down
8 changes: 0 additions & 8 deletions src/github/handlers/issue-comment/created.ts

This file was deleted.

21 changes: 10 additions & 11 deletions src/github/handlers/repository-dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GitHubContext } from "../github-context";
import { PluginChain } from "../types/plugin-configuration";
import { dispatchWorkflow, getDefaultBranch } from "../utils/workflow-dispatch";
import { Value } from "@sinclair/typebox/value";
import { DelegatedComputeInputs, PluginChainState, expressionRegex, pluginOutputSchema } from "../types/plugin";
Expand Down Expand Up @@ -52,7 +51,7 @@ export async function repositoryDispatch(context: GitHubContext<"repository_disp
const defaultBranch = await getDefaultBranch(context, nextPlugin.plugin.owner, nextPlugin.plugin.repo);
const token = await context.eventHandler.getToken(state.eventPayload.installation.id);
const ref = nextPlugin.plugin.ref ?? defaultBranch;
const settings = findAndReplaceExpressions(nextPlugin, state);
const settings = findAndReplaceExpressions(nextPlugin.with, state);
const inputs = new DelegatedComputeInputs(pluginOutput.state_id, state.eventName, state.eventPayload, settings, token, ref);

state.currentPlugin++;
Expand All @@ -68,16 +67,14 @@ export async function repositoryDispatch(context: GitHubContext<"repository_disp
});
}

function findAndReplaceExpressions(plugin: PluginChain[0], state: PluginChainState): Record<string, unknown> {
const settings: Record<string, unknown> = {};

for (const key in plugin.with) {
const value = plugin.with[key];
function findAndReplaceExpressions(settings: object, state: PluginChainState): Record<string, unknown> {
const newSettings: Record<string, unknown> = {};

for (const [key, value] of Object.entries(settings)) {
if (typeof value === "string") {
const matches = value.match(expressionRegex);
if (!matches) {
settings[key] = value;
newSettings[key] = value;
continue;
}
const parts = matches[1].split(".");
Expand All @@ -88,16 +85,18 @@ function findAndReplaceExpressions(plugin: PluginChain[0], state: PluginChainSta

if (parts[1] === "output") {
const outputProperty = parts[2];
settings[key] = getPluginOutputValue(state, pluginId, outputProperty);
newSettings[key] = getPluginOutputValue(state, pluginId, outputProperty);
} else {
throw new Error(`Invalid expression: ${value}`);
}
} else if (typeof value === "object" && value !== null) {
newSettings[key] = findAndReplaceExpressions(value, state);
} else {
settings[key] = value;
newSettings[key] = value;
}
}

return settings;
return newSettings;
}

function getPluginOutputValue(state: PluginChainState, pluginId: string, outputKey: string): unknown {
Expand Down

0 comments on commit 5ca157b

Please sign in to comment.