diff --git a/lambdas/functions/webhook/src/ConfigResolver.ts b/lambdas/functions/webhook/src/ConfigResolver.ts index 15a4ed6589..9296bf0d90 100644 --- a/lambdas/functions/webhook/src/ConfigResolver.ts +++ b/lambdas/functions/webhook/src/ConfigResolver.ts @@ -29,7 +29,7 @@ export class Config { Config.matcherConfig = JSON.parse(matcherConfigVal) as Array; logger.debug('Loaded queues config', { matcherConfig: Config.matcherConfig }); } - const workflowJobEventSecondaryQueue = process.env.SQS_WORKFLOW_JOB_QUEUE ?? undefined; + const workflowJobEventSecondaryQueue = process.env.SQS_WORKFLOW_JOB_QUEUE || undefined; return new Config(repositoryAllowList, workflowJobEventSecondaryQueue); } diff --git a/lambdas/functions/webhook/src/sqs/index.test.ts b/lambdas/functions/webhook/src/sqs/index.test.ts index c8b66adcb5..cf27625c83 100644 --- a/lambdas/functions/webhook/src/sqs/index.test.ts +++ b/lambdas/functions/webhook/src/sqs/index.test.ts @@ -16,6 +16,8 @@ jest.mock('@aws-sdk/client-sqs', () => ({ })); jest.mock('@terraform-aws-github-runner/aws-ssm-util'); +import { SQS } from '@aws-sdk/client-sqs'; + describe('Test sending message to SQS.', () => { const queueUrl = 'https://sqs.eu-west-1.amazonaws.com/123456789/queued-builds'; const message = { @@ -98,7 +100,19 @@ describe('Test sending message to SQS.', () => { expect(result).resolves; }); - it('Does not send webhook events to workflow job event copy queue', async () => { + it('Does not send webhook events to workflow job event copy queue when job queue is not in environment', async () => { + // Arrange + delete process.env.SQS_WORKFLOW_JOB_QUEUE; + const config = await Config.load(); + + // Act + await sendWebhookEventToWorkflowJobQueue(message, config); + + // Assert + expect(SQS).not.toHaveBeenCalled(); + }); + + it('Does not send webhook events to workflow job event copy queue when job queue is set to empty string', async () => { // Arrange process.env.SQS_WORKFLOW_JOB_QUEUE = ''; const config = await Config.load(); @@ -106,7 +120,7 @@ describe('Test sending message to SQS.', () => { await sendWebhookEventToWorkflowJobQueue(message, config); // Assert - expect(mockSQS.sendMessage).not.toHaveBeenCalledWith(sqsMessage); + expect(SQS).not.toHaveBeenCalled(); }); it('Catch the exception when even copy queue throws exception', async () => { diff --git a/lambdas/functions/webhook/src/sqs/index.ts b/lambdas/functions/webhook/src/sqs/index.ts index 8a1e8e20af..03539ddbf6 100644 --- a/lambdas/functions/webhook/src/sqs/index.ts +++ b/lambdas/functions/webhook/src/sqs/index.ts @@ -50,17 +50,21 @@ export const sendActionRequest = async (message: ActionRequestMessage): Promise< }; export async function sendWebhookEventToWorkflowJobQueue(message: GithubWorkflowEvent, config: Config): Promise { - if (config.workflowJobEventSecondaryQueue != undefined) { - const sqs = new SQS({ region: process.env.AWS_REGION }); - const sqsMessage: SendMessageCommandInput = { - QueueUrl: String(config.workflowJobEventSecondaryQueue), - MessageBody: JSON.stringify(message), - }; - logger.debug(`Sending Webhook events to the workflow job queue: ${config.workflowJobEventSecondaryQueue}`); - try { - await sqs.sendMessage(sqsMessage); - } catch (e) { - logger.warn(`Error in sending webhook events to workflow job queue: ${(e as Error).message}`); - } + if (!config.workflowJobEventSecondaryQueue) { + return; + } + + const sqs = new SQS({ region: process.env.AWS_REGION }); + const sqsMessage: SendMessageCommandInput = { + QueueUrl: String(config.workflowJobEventSecondaryQueue), + MessageBody: JSON.stringify(message), + }; + + logger.debug(`Sending Webhook events to the workflow job queue: ${config.workflowJobEventSecondaryQueue}`); + + try { + await sqs.sendMessage(sqsMessage); + } catch (e) { + logger.warn(`Error in sending webhook events to workflow job queue: ${(e as Error).message}`); } }