Skip to content

Commit

Permalink
feat: Webhook accept jobs where not all labels are provided in job. (p…
Browse files Browse the repository at this point in the history
…hilips-labs#2209)

feat: Webhook accept jobs where not all labels are provided in job.in job.
  • Loading branch information
ivanprjcts committed Aug 4, 2022
1 parent 4e3b606 commit 6d9116f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
6 changes: 3 additions & 3 deletions modules/webhook/lambdas/webhook/src/webhook/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 () => {
Expand Down
13 changes: 3 additions & 10 deletions modules/webhook/lambdas/webhook/src/webhook/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,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 ${
Expand Down

0 comments on commit 6d9116f

Please sign in to comment.