diff --git a/src/github/handlers/index.ts b/src/github/handlers/index.ts index e84843d..165b274 100644 --- a/src/github/handlers/index.ts +++ b/src/github/handlers/index.ts @@ -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"; @@ -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 } @@ -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}`); diff --git a/src/github/handlers/issue-comment/created.ts b/src/github/handlers/issue-comment/created.ts deleted file mode 100644 index 5109e72..0000000 --- a/src/github/handlers/issue-comment/created.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { GitHubContext } from "../../github-context"; - -export async function issueCommentCreated(event: GitHubContext<"issue_comment.created">) { - if (event.payload.comment.user.type === "Bot") { - console.log("Skipping bot comment"); - return null; - } - - await event.octokit.issues.createComment({ - owner: event.payload.repository.owner.login, - repo: event.payload.repository.name, - issue_number: event.payload.issue.number, - body: "Hello from the worker!", - }); -} diff --git a/src/github/handlers/repository-dispatch.ts b/src/github/handlers/repository-dispatch.ts index 6ca1f3b..31c472a 100644 --- a/src/github/handlers/repository-dispatch.ts +++ b/src/github/handlers/repository-dispatch.ts @@ -2,7 +2,6 @@ import { GitHubContext } from "../github-context"; import { dispatchWorkflow, getDefaultBranch } from "../utils/workflow-dispatch"; import { Value } from "@sinclair/typebox/value"; import { DelegatedComputeInputs, PluginChainState, expressionRegex, pluginOutputSchema } from "../types/plugin"; -import { PluginChain } from "../types/config"; export async function repositoryDispatch(context: GitHubContext<"repository_dispatch">) { console.log("Repository dispatch event received", context.payload.client_payload); @@ -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++; @@ -68,16 +67,14 @@ export async function repositoryDispatch(context: GitHubContext<"repository_disp }); } -function findAndReplaceExpressions(plugin: PluginChain[0], state: PluginChainState): Record { - const settings: Record = {}; - - for (const key in plugin.with) { - const value = plugin.with[key]; +function findAndReplaceExpressions(settings: object, state: PluginChainState): Record { + const newSettings: Record = {}; + 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("."); @@ -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 {