Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit ef25bd4

Browse files
iainlanenpalm
andauthored
fix(webhook): Don't log warning when secondary job queue is empty (#3942)
Right now the Terraform module is causing `${SQS_WORKFLOW_JOB_QUEUE}` to be set to an empty string. Since we pass this through currently, and explicitly check for `!== undefined` - not any falsy value - we end up trying to send to an empty queue and logging a warning. Doesn't break anything, but it's noisy in the logs. Fix this by checking for any falsy value instead, and also using `||` instead of `??` when setting the variable in the first place, so an empty string ends up as `undefined`. Also, modify the testsuite to check for the `SQS` being created at all, since that happens earlier on and reproduces the failure. This is a companion to #3943. This one stops the warning, and that one fixes the original cause by not setting the env var in the first place. Both could be merged, ideally. Co-authored-by: Niek Palm <[email protected]>
1 parent 6c48dff commit ef25bd4

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

Diff for: lambdas/functions/webhook/src/ConfigResolver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class Config {
2929
Config.matcherConfig = JSON.parse(matcherConfigVal) as Array<RunnerMatcherConfig>;
3030
logger.debug('Loaded queues config', { matcherConfig: Config.matcherConfig });
3131
}
32-
const workflowJobEventSecondaryQueue = process.env.SQS_WORKFLOW_JOB_QUEUE ?? undefined;
32+
const workflowJobEventSecondaryQueue = process.env.SQS_WORKFLOW_JOB_QUEUE || undefined;
3333
return new Config(repositoryAllowList, workflowJobEventSecondaryQueue);
3434
}
3535

Diff for: lambdas/functions/webhook/src/sqs/index.test.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jest.mock('@aws-sdk/client-sqs', () => ({
1616
}));
1717
jest.mock('@terraform-aws-github-runner/aws-ssm-util');
1818

19+
import { SQS } from '@aws-sdk/client-sqs';
20+
1921
describe('Test sending message to SQS.', () => {
2022
const queueUrl = 'https://sqs.eu-west-1.amazonaws.com/123456789/queued-builds';
2123
const message = {
@@ -98,15 +100,27 @@ describe('Test sending message to SQS.', () => {
98100
expect(result).resolves;
99101
});
100102

101-
it('Does not send webhook events to workflow job event copy queue', async () => {
103+
it('Does not send webhook events to workflow job event copy queue when job queue is not in environment', async () => {
104+
// Arrange
105+
delete process.env.SQS_WORKFLOW_JOB_QUEUE;
106+
const config = await Config.load();
107+
108+
// Act
109+
await sendWebhookEventToWorkflowJobQueue(message, config);
110+
111+
// Assert
112+
expect(SQS).not.toHaveBeenCalled();
113+
});
114+
115+
it('Does not send webhook events to workflow job event copy queue when job queue is set to empty string', async () => {
102116
// Arrange
103117
process.env.SQS_WORKFLOW_JOB_QUEUE = '';
104118
const config = await Config.load();
105119
// Act
106120
await sendWebhookEventToWorkflowJobQueue(message, config);
107121

108122
// Assert
109-
expect(mockSQS.sendMessage).not.toHaveBeenCalledWith(sqsMessage);
123+
expect(SQS).not.toHaveBeenCalled();
110124
});
111125

112126
it('Catch the exception when even copy queue throws exception', async () => {

Diff for: lambdas/functions/webhook/src/sqs/index.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ export const sendActionRequest = async (message: ActionRequestMessage): Promise<
5050
};
5151

5252
export async function sendWebhookEventToWorkflowJobQueue(message: GithubWorkflowEvent, config: Config): Promise<void> {
53-
if (config.workflowJobEventSecondaryQueue != undefined) {
54-
const sqs = new SQS({ region: process.env.AWS_REGION });
55-
const sqsMessage: SendMessageCommandInput = {
56-
QueueUrl: String(config.workflowJobEventSecondaryQueue),
57-
MessageBody: JSON.stringify(message),
58-
};
59-
logger.debug(`Sending Webhook events to the workflow job queue: ${config.workflowJobEventSecondaryQueue}`);
60-
try {
61-
await sqs.sendMessage(sqsMessage);
62-
} catch (e) {
63-
logger.warn(`Error in sending webhook events to workflow job queue: ${(e as Error).message}`);
64-
}
53+
if (!config.workflowJobEventSecondaryQueue) {
54+
return;
55+
}
56+
57+
const sqs = new SQS({ region: process.env.AWS_REGION });
58+
const sqsMessage: SendMessageCommandInput = {
59+
QueueUrl: String(config.workflowJobEventSecondaryQueue),
60+
MessageBody: JSON.stringify(message),
61+
};
62+
63+
logger.debug(`Sending Webhook events to the workflow job queue: ${config.workflowJobEventSecondaryQueue}`);
64+
65+
try {
66+
await sqs.sendMessage(sqsMessage);
67+
} catch (e) {
68+
logger.warn(`Error in sending webhook events to workflow job queue: ${(e as Error).message}`);
6569
}
6670
}

0 commit comments

Comments
 (0)