Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webhook): Don't log warning when secondary job queue is empty #3942

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lambdas/functions/webhook/src/ConfigResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Config {
Config.matcherConfig = JSON.parse(matcherConfigVal) as Array<RunnerMatcherConfig>;
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);
}

Expand Down
18 changes: 16 additions & 2 deletions lambdas/functions/webhook/src/sqs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -98,15 +100,27 @@ 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();
// Act
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 () => {
Expand Down
28 changes: 16 additions & 12 deletions lambdas/functions/webhook/src/sqs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ export const sendActionRequest = async (message: ActionRequestMessage): Promise<
};

export async function sendWebhookEventToWorkflowJobQueue(message: GithubWorkflowEvent, config: Config): Promise<void> {
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}`);
}
}