Skip to content

Commit afaf051

Browse files
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.
1 parent 6d4f867 commit afaf051

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

lambdas/functions/webhook/src/runners/dispatch.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,39 @@ async function handleWorkflowJob(
3030
githubEvent: string,
3131
matcherConfig: Array<RunnerMatcherConfig>,
3232
): Promise<Response> {
33-
if (body.action === 'queued') {
34-
// sort the queuesConfig by order of matcher config exact match, with all true matches lined up ahead.
35-
matcherConfig.sort((a, b) => {
36-
return a.matcherConfig.exactMatch === b.matcherConfig.exactMatch ? 0 : a.matcherConfig.exactMatch ? -1 : 1;
37-
});
38-
for (const queue of matcherConfig) {
39-
if (canRunJob(body.workflow_job.labels, queue.matcherConfig.labelMatchers, queue.matcherConfig.exactMatch)) {
40-
await sendActionRequest({
41-
id: body.workflow_job.id,
42-
repositoryName: body.repository.name,
43-
repositoryOwner: body.repository.owner.login,
44-
eventType: githubEvent,
45-
installationId: body.installation?.id ?? 0,
46-
queueId: queue.id,
47-
repoOwnerType: body.repository.owner.type,
48-
});
49-
logger.info(`Successfully dispatched job for ${body.repository.full_name} to the queue ${queue.id}`);
50-
return {
51-
statusCode: 201,
52-
body: `Successfully queued job for ${body.repository.full_name} to the queue ${queue.id}`,
53-
};
54-
}
55-
}
56-
logger.warn(`Received event contains runner labels '${body.workflow_job.labels}' that are not accepted.`);
33+
if (body.action !== 'queued') {
5734
return {
58-
statusCode: 202,
59-
body: `Received event contains runner labels '${body.workflow_job.labels}' that are not accepted.`,
35+
statusCode: 201,
36+
body: `Workflow job not queued, not dispatching to queue.`,
6037
};
6138
}
62-
return {
63-
statusCode: 201,
64-
body: `Received not queued and will not be ignored.`,
65-
};
39+
// sort the queuesConfig by order of matcher config exact match, with all true matches lined up ahead.
40+
matcherConfig.sort((a, b) => {
41+
return a.matcherConfig.exactMatch === b.matcherConfig.exactMatch ? 0 : a.matcherConfig.exactMatch ? -1 : 1;
42+
});
43+
for (const queue of matcherConfig) {
44+
if (canRunJob(body.workflow_job.labels, queue.matcherConfig.labelMatchers, queue.matcherConfig.exactMatch)) {
45+
await sendActionRequest({
46+
id: body.workflow_job.id,
47+
repositoryName: body.repository.name,
48+
repositoryOwner: body.repository.owner.login,
49+
eventType: githubEvent,
50+
installationId: body.installation?.id ?? 0,
51+
queueId: queue.id,
52+
repoOwnerType: body.repository.owner.type,
53+
});
54+
logger.info(`Successfully dispatched job for ${body.repository.full_name} to the queue ${queue.id}`);
55+
return {
56+
statusCode: 201,
57+
body: `Successfully queued job for ${body.repository.full_name} to the queue ${queue.id}`,
58+
};
59+
}
60+
}
61+
const notAcceptedErrorMsg = `Received event contains runner labels '${body.workflow_job.labels}' from '${
62+
body.repository.full_name
63+
}' that are not accepted.`;
64+
logger.warn(notAcceptedErrorMsg);
65+
return { statusCode: 202, body: notAcceptedErrorMsg };
6666
}
6767

6868
export function canRunJob(

0 commit comments

Comments
 (0)