From 959a423a767e8841755a95656373fd35cfa164f9 Mon Sep 17 00:00:00 2001
From: Ivan Martin <ivanprjcts@gmail.com>
Date: Tue, 28 Jun 2022 01:15:59 +0200
Subject: [PATCH] feat: Webhook accept jobs where not all labels are provided
 in job.in job.

---
 .../lambdas/webhook/src/webhook/handler.test.ts     |  6 +++---
 .../webhook/lambdas/webhook/src/webhook/handler.ts  | 13 +++----------
 2 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/modules/webhook/lambdas/webhook/src/webhook/handler.test.ts b/modules/webhook/lambdas/webhook/src/webhook/handler.test.ts
index 500784638a..3a4b98ef68 100644
--- a/modules/webhook/lambdas/webhook/src/webhook/handler.test.ts
+++ b/modules/webhook/lambdas/webhook/src/webhook/handler.test.ts
@@ -159,7 +159,7 @@ describe('handler', () => {
       expect(sendActionRequest).toBeCalled();
     });
 
-    it('Check webhook does not accept jobs where not all labels are provided in job.', async () => {
+    it('Check webhook accept jobs where not all labels are provided in job.', async () => {
       process.env.RUNNER_LABELS = '["self-hosted", "test", "test2"]';
       process.env.ENABLE_WORKFLOW_JOB_LABELS_CHECK = 'true';
       process.env.WORKFLOW_JOB_LABELS_CHECK_ALL = 'true';
@@ -174,8 +174,8 @@ describe('handler', () => {
         { 'X-Hub-Signature': await webhooks.sign(event), 'X-GitHub-Event': 'workflow_job' },
         event,
       );
-      expect(resp.statusCode).toBe(202);
-      expect(sendActionRequest).not.toBeCalled;
+      expect(resp.statusCode).toBe(201);
+      expect(sendActionRequest).toBeCalled();
     });
 
     it('Check webhook does not accept jobs where not all labels are supported by the runner.', async () => {
diff --git a/modules/webhook/lambdas/webhook/src/webhook/handler.ts b/modules/webhook/lambdas/webhook/src/webhook/handler.ts
index 983a11cf52..cbb9e99d2c 100644
--- a/modules/webhook/lambdas/webhook/src/webhook/handler.ts
+++ b/modules/webhook/lambdas/webhook/src/webhook/handler.ts
@@ -178,16 +178,9 @@ function isRepoNotAllowed(repoFullName: string, repositoryWhiteList: string[]):
 
 function canRunJob(job: WorkflowJobEvent, runnerLabels: string[], workflowLabelCheckAll: boolean): boolean {
   const workflowJobLabels = job.workflow_job.labels;
-  let runnerMatch;
-  let jobMatch;
-  if (workflowLabelCheckAll) {
-    runnerMatch = runnerLabels.every((l) => workflowJobLabels.includes(l));
-    jobMatch = workflowJobLabels.every((l) => runnerLabels.includes(l));
-  } else {
-    runnerMatch = runnerLabels.some((l) => workflowJobLabels.includes(l));
-    jobMatch = workflowJobLabels.some((l) => runnerLabels.includes(l));
-  }
-  const match = jobMatch && runnerMatch;
+  const match = workflowLabelCheckAll
+    ? workflowJobLabels.every((l) => runnerLabels.includes(l))
+    : workflowJobLabels.some((l) => runnerLabels.includes(l));
 
   logger.debug(
     `Received workflow job event with labels: '${JSON.stringify(workflowJobLabels)}'. The event does ${