From afaf051dc141f792defb557cdc7c3ff4136ade2c Mon Sep 17 00:00:00 2001 From: Victor Alvarez Date: Fri, 12 Sep 2025 16:58:49 +0900 Subject: [PATCH] fix(webhook): correct workflow job dispatch logic Refactors the workflow job dispatch logic to handle non-'queued' actions first, returning early with a 201 status. Updates the sorting and dispatching logic for matcherConfig, and improves warning messages for unaccepted runner labels by including the repository name. This ensures jobs are only dispatched when appropriate and provides clearer logging. --- .../functions/webhook/src/runners/dispatch.ts | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lambdas/functions/webhook/src/runners/dispatch.ts b/lambdas/functions/webhook/src/runners/dispatch.ts index a7a3a0307c..1c33f31dd8 100644 --- a/lambdas/functions/webhook/src/runners/dispatch.ts +++ b/lambdas/functions/webhook/src/runners/dispatch.ts @@ -30,39 +30,39 @@ async function handleWorkflowJob( githubEvent: string, matcherConfig: Array, ): Promise { - if (body.action === 'queued') { - // sort the queuesConfig by order of matcher config exact match, with all true matches lined up ahead. - matcherConfig.sort((a, b) => { - return a.matcherConfig.exactMatch === b.matcherConfig.exactMatch ? 0 : a.matcherConfig.exactMatch ? -1 : 1; - }); - for (const queue of matcherConfig) { - if (canRunJob(body.workflow_job.labels, queue.matcherConfig.labelMatchers, queue.matcherConfig.exactMatch)) { - await sendActionRequest({ - id: body.workflow_job.id, - repositoryName: body.repository.name, - repositoryOwner: body.repository.owner.login, - eventType: githubEvent, - installationId: body.installation?.id ?? 0, - queueId: queue.id, - repoOwnerType: body.repository.owner.type, - }); - logger.info(`Successfully dispatched job for ${body.repository.full_name} to the queue ${queue.id}`); - return { - statusCode: 201, - body: `Successfully queued job for ${body.repository.full_name} to the queue ${queue.id}`, - }; - } - } - logger.warn(`Received event contains runner labels '${body.workflow_job.labels}' that are not accepted.`); + if (body.action !== 'queued') { return { - statusCode: 202, - body: `Received event contains runner labels '${body.workflow_job.labels}' that are not accepted.`, + statusCode: 201, + body: `Workflow job not queued, not dispatching to queue.`, }; } - return { - statusCode: 201, - body: `Received not queued and will not be ignored.`, - }; + // sort the queuesConfig by order of matcher config exact match, with all true matches lined up ahead. + matcherConfig.sort((a, b) => { + return a.matcherConfig.exactMatch === b.matcherConfig.exactMatch ? 0 : a.matcherConfig.exactMatch ? -1 : 1; + }); + for (const queue of matcherConfig) { + if (canRunJob(body.workflow_job.labels, queue.matcherConfig.labelMatchers, queue.matcherConfig.exactMatch)) { + await sendActionRequest({ + id: body.workflow_job.id, + repositoryName: body.repository.name, + repositoryOwner: body.repository.owner.login, + eventType: githubEvent, + installationId: body.installation?.id ?? 0, + queueId: queue.id, + repoOwnerType: body.repository.owner.type, + }); + logger.info(`Successfully dispatched job for ${body.repository.full_name} to the queue ${queue.id}`); + return { + statusCode: 201, + body: `Successfully queued job for ${body.repository.full_name} to the queue ${queue.id}`, + }; + } + } + const notAcceptedErrorMsg = `Received event contains runner labels '${body.workflow_job.labels}' from '${ + body.repository.full_name + }' that are not accepted.`; + logger.warn(notAcceptedErrorMsg); + return { statusCode: 202, body: notAcceptedErrorMsg }; } export function canRunJob(