Skip to content

Commit

Permalink
perf(webhook): Use @aws-sdk/client-sqs in the webhook Lambda (#2924)
Browse files Browse the repository at this point in the history
* Use a regular boolean

* Add "@aws-sdk/client-sqs" and remove "aws-sdk"

* Format

---------

Co-authored-by: Niek Palm <npalm@users.noreply.github.com>
  • Loading branch information
fortum-vaanavil and npalm authored Feb 10, 2023
1 parent e8cae3a commit b8898ef
Show file tree
Hide file tree
Showing 4 changed files with 670 additions and 21 deletions.
2 changes: 1 addition & 1 deletion modules/webhook/lambdas/webhook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
"typescript": "^4.9.4"
},
"dependencies": {
"@aws-sdk/client-sqs": "^3.264.0",
"@aws-sdk/client-ssm": "^3.245.0",
"@octokit/rest": "^19.0.5",
"@octokit/webhooks": "^10.7.0",
"aws-lambda": "^1.0.7",
"aws-sdk": "^2.1292.0",
"tslog": "^3.3.4"
}
}
16 changes: 6 additions & 10 deletions modules/webhook/lambdas/webhook/src/sqs/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { SQS } from 'aws-sdk';
import { SendMessageCommandInput } from '@aws-sdk/client-sqs';

import { ActionRequestMessage, GithubWorkflowEvent, sendActionRequest, sendWebhookEventToWorkflowJobQueue } from '.';
import workflowjob_event from '../../test/resources/github_workflowjob_event.json';

const mockSQS = {
sendMessage: jest.fn(() => {
{
return { promise: jest.fn() };
return {};
}
}),
};
jest.mock('aws-sdk', () => ({
jest.mock('@aws-sdk/client-sqs', () => ({
SQS: jest.fn().mockImplementation(() => mockSQS),
}));

Expand All @@ -26,10 +26,6 @@ describe('Test sending message to SQS.', () => {
queueFifo: false,
};

const sqsMessage: SQS.Types.SendMessageRequest = {
QueueUrl: queueUrl,
MessageBody: JSON.stringify(message),
};
afterEach(() => {
jest.clearAllMocks();
});
Expand All @@ -40,7 +36,7 @@ describe('Test sending message to SQS.', () => {
...message,
queueFifo: false,
};
const sqsMessage: SQS.Types.SendMessageRequest = {
const sqsMessage: SendMessageCommandInput = {
QueueUrl: queueUrl,
MessageBody: JSON.stringify(no_fifo_message),
};
Expand All @@ -58,7 +54,7 @@ describe('Test sending message to SQS.', () => {
...message,
queueFifo: true,
};
const sqsMessage: SQS.Types.SendMessageRequest = {
const sqsMessage: SendMessageCommandInput = {
QueueUrl: queueUrl,
MessageBody: JSON.stringify(fifo_message),
};
Expand All @@ -74,7 +70,7 @@ describe('Test sending message to SQS.', () => {
const message: GithubWorkflowEvent = {
workflowJobEvent: JSON.parse(JSON.stringify(workflowjob_event)),
};
const sqsMessage: SQS.Types.SendMessageRequest = {
const sqsMessage: SendMessageCommandInput = {
QueueUrl: 'https://sqs.eu-west-1.amazonaws.com/123456789/webhook_events_workflow_job_queue',
MessageBody: JSON.stringify(message),
};
Expand Down
17 changes: 8 additions & 9 deletions modules/webhook/lambdas/webhook/src/sqs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SQS, SendMessageCommandInput } from '@aws-sdk/client-sqs';
import { WorkflowJobEvent } from '@octokit/webhooks-types';
import { SQS } from 'aws-sdk';
import { bool } from 'aws-sdk/clients/signer';

import { LogFields, logger } from '../webhook/logger';

Expand All @@ -11,19 +10,19 @@ export interface ActionRequestMessage {
repositoryOwner: string;
installationId: number;
queueId: string;
queueFifo: bool;
queueFifo: boolean;
}

export interface MatcherConfig {
labelMatchers: string[][];
exactMatch: bool;
exactMatch: boolean;
}

export interface QueueConfig {
matcherConfig: MatcherConfig;
id: string;
arn: string;
fifo: bool;
fifo: boolean;
}
export interface GithubWorkflowEvent {
workflowJobEvent: WorkflowJobEvent;
Expand All @@ -32,7 +31,7 @@ export interface GithubWorkflowEvent {
export const sendActionRequest = async (message: ActionRequestMessage): Promise<void> => {
const sqs = new SQS({ region: process.env.AWS_REGION });

const sqsMessage: SQS.Types.SendMessageRequest = {
const sqsMessage: SendMessageCommandInput = {
QueueUrl: message.queueId,
MessageBody: JSON.stringify(message),
};
Expand All @@ -42,15 +41,15 @@ export const sendActionRequest = async (message: ActionRequestMessage): Promise<
sqsMessage.MessageGroupId = String(message.id);
}

await sqs.sendMessage(sqsMessage).promise();
await sqs.sendMessage(sqsMessage);
};

export const sendWebhookEventToWorkflowJobQueue = async (message: GithubWorkflowEvent): Promise<void> => {
const webhook_events_workflow_job_queue = process.env.SQS_WORKFLOW_JOB_QUEUE || undefined;

if (webhook_events_workflow_job_queue != undefined) {
const sqs = new SQS({ region: process.env.AWS_REGION });
const sqsMessage: SQS.Types.SendMessageRequest = {
const sqsMessage: SendMessageCommandInput = {
QueueUrl: String(process.env.SQS_WORKFLOW_JOB_QUEUE),
MessageBody: JSON.stringify(message),
};
Expand All @@ -59,7 +58,7 @@ export const sendWebhookEventToWorkflowJobQueue = async (message: GithubWorkflow
LogFields.print(),
);
try {
await sqs.sendMessage(sqsMessage).promise();
await sqs.sendMessage(sqsMessage);
} catch (e) {
logger.warn(`Error in sending webhook events to workflow job queue: ${(e as Error).message}`, LogFields.print());
}
Expand Down
Loading

0 comments on commit b8898ef

Please sign in to comment.