Skip to content

Commit

Permalink
fix: call plugins synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
rndquu committed Nov 14, 2024
1 parent a65c27a commit 8f386a0
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions src/github/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,56 +69,50 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
return;
}

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;
}
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),
};

// 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);
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);
}
}
}

0 comments on commit 8f386a0

Please sign in to comment.