Skip to content

Commit

Permalink
Update API types
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-lou committed Nov 11, 2024
1 parent 24c53d0 commit cb9999b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 27 deletions.
10 changes: 5 additions & 5 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
1 change: 1 addition & 0 deletions bin/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ GOCD_WEBHOOK_SECRET
KAFKA_CONTROL_PLANE_WEBHOOK_SECRET
SENTRY_OPTIONS_WEBHOOK_SECRET
"
# TODO: Revamp this and make it easier to add secrets & deploy to GCP

secrets=""
for secret_name in $secret_names; do
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"module-alias": "^2.2.2",
"moment-timezone": "^0.5.39",
"pg": "^8.5.1",
"service-registry": "git+ssh://[email protected]:getsentry/service-registry#main",
"service-registry": "git+ssh://[email protected]/getsentry/service-registry/#main",
"source-map-support": "^0.5.21",
"tar": "^6.2.1",
"typescript": "^4.7.0"
Expand Down
1 change: 0 additions & 1 deletion service-registry
Submodule service-registry deleted from 90c2ec
12 changes: 10 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export interface JiraEvent {
export type GenericEvent = {
source: string;
timestamp: number;
service_name?: string; // Official service registry name if applicable
data: (DatadogEvent | JiraEvent | SlackMessage)[];
data: (DatadogEvent | JiraEvent | SlackMessage | ServiceSlackMessage)[];
};

// Currently only used for Slack notifications since
// service registry only contains Slack channels (and not DD or Jira or others)
export interface ServiceSlackMessage {
type: 'service_notification';
service_name: string; // Official service registry service id
text: string;
blocks?: KnownBlock[] | Block[];
}
24 changes: 22 additions & 2 deletions src/webhooks/generic-notifier/generic-notifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import testInvalidPayload from '@test/payloads/generic-notifier/testInvalidPayload.json';
import testPayload from '@test/payloads/generic-notifier/testPayload.json';
import testServicePayload from '@test/payloads/generic-notifier/testServicePayload.json';
import { createNotifierRequest } from '@test/utils/createGenericMessageRequest';

import { buildServer } from '@/buildServer';
import { DATADOG_API_INSTANCE } from '@/config';
import { GenericEvent, SlackMessage } from '@/types';
import { GenericEvent, ServiceSlackMessage, SlackMessage } from '@/types';
import { bolt } from '@api/slack';

import { messageSlack } from './generic-notifier';
import { handleServiceSlackMessage, messageSlack } from './generic-notifier';

describe('generic messages webhook', function () {
let fastify;
Expand Down Expand Up @@ -79,6 +80,25 @@ describe('generic messages webhook', function () {
});
});
});
describe('handleServiceSlackMessage tests', function () {
afterEach(function () {
jest.clearAllMocks();
});

it('writes to slack', async function () {
const postMessageSpy = jest.spyOn(bolt.client.chat, 'postMessage');
await handleServiceSlackMessage(
testServicePayload.data[0] as ServiceSlackMessage
);
expect(postMessageSpy).toHaveBeenCalledTimes(1);
const message = postMessageSpy.mock.calls[0][0];
expect(message).toEqual({
channel: 'feed-datdog',
text: 'Random text here',
unfurl_links: false,
});
});
});

it('checks that slack msg is sent', async function () {
const postMessageSpy = jest.spyOn(bolt.client.chat, 'postMessage');
Expand Down
51 changes: 39 additions & 12 deletions src/webhooks/generic-notifier/generic-notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { v1 } from '@datadog/datadog-api-client';
import * as Sentry from '@sentry/node';
import { FastifyReply, FastifyRequest } from 'fastify';

import { DatadogEvent, GenericEvent, SlackMessage } from '@types';
import {
DatadogEvent,
GenericEvent,
ServiceSlackMessage,
SlackMessage,
} from '@types';

import { bolt } from '@/api/slack';
import { DATADOG_API_INSTANCE } from '@/config';
Expand Down Expand Up @@ -37,7 +42,9 @@ export async function genericEventNotifier(
}
for (const message of body.data) {
if (message.type === 'slack') {
await messageSlack(message, body.service_name);
await messageSlack(message);
} else if (message.type === 'service_notification') {
await handleServiceSlackMessage(message);

Check warning on line 47 in src/webhooks/generic-notifier/generic-notifier.ts

View check run for this annotation

Codecov / codecov/patch

src/webhooks/generic-notifier/generic-notifier.ts#L47

Added line #L47 was not covered by tests
} else if (message.type === 'datadog') {
await sendEventToDatadog(message, body.timestamp);
}
Expand Down Expand Up @@ -71,26 +78,46 @@ export async function sendEventToDatadog(
}
}

export async function messageSlack(
message: SlackMessage,
service_name?: string
) {
let channels = message.channels ?? [];
if (service_name !== undefined) {
const service = getService(service_name);
channels = service.alert_slack_channels;
export async function messageSlack(message: SlackMessage) {
const channels = message.channels ?? [];
for (const channel of channels) {
try {
const args = {
channel: channel,
blocks: message.blocks,
text: message.text,
unfurl_links: false,
};
if (message.blocks) {
args.blocks = message.blocks;

Check warning on line 92 in src/webhooks/generic-notifier/generic-notifier.ts

View check run for this annotation

Codecov / codecov/patch

src/webhooks/generic-notifier/generic-notifier.ts#L92

Added line #L92 was not covered by tests
}
await bolt.client.chat.postMessage(args);
} catch (err) {
Sentry.setContext('slack msg:', { text: message.text });
Sentry.captureException(err);

Check warning on line 97 in src/webhooks/generic-notifier/generic-notifier.ts

View check run for this annotation

Codecov / codecov/patch

src/webhooks/generic-notifier/generic-notifier.ts#L96-L97

Added lines #L96 - L97 were not covered by tests
}
}
}

export async function handleServiceSlackMessage(message: ServiceSlackMessage) {
const service = getService(message.service_name);
const channels = service.alert_slack_channels ?? [];
for (const channel of channels) {
try {
await bolt.client.chat.postMessage({
const args = {
channel: channel,
blocks: message.blocks,
text: message.text,
unfurl_links: false,
});
};
if (message.blocks) {
args.blocks = message.blocks;

Check warning on line 114 in src/webhooks/generic-notifier/generic-notifier.ts

View check run for this annotation

Codecov / codecov/patch

src/webhooks/generic-notifier/generic-notifier.ts#L114

Added line #L114 was not covered by tests
}
await bolt.client.chat.postMessage(args);
} catch (err) {
Sentry.setContext('slack msg:', { text: message.text });
Sentry.captureException(err);

Check warning on line 119 in src/webhooks/generic-notifier/generic-notifier.ts

View check run for this annotation

Codecov / codecov/patch

src/webhooks/generic-notifier/generic-notifier.ts#L118-L119

Added lines #L118 - L119 were not covered by tests
}
}
// TODO: Add other types of notifications (Jira, DD, etc.)
}
12 changes: 12 additions & 0 deletions test/payloads/generic-notifier/testServicePayload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"source": "example-service",
"timestamp": 0,
"data": [
{
"type": "service_notification",
"service_name": "eng_pipes_gh_notifications",
"text": "Random text here",
"channels": ["#aaaaaa"]
}
]
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8237,7 +8237,7 @@ fsevents@~2.1.2:
pg: ^8.5.1
pino-pretty: ^4.5.0
prettier: ^2.4.1
service-registry: "git+ssh://[email protected]:getsentry/service-registry#main"
service-registry: "git+ssh://[email protected]/getsentry/service-registry/#main"
source-map-support: ^0.5.21
tar: ^6.2.1
ts-jest: ^29.1.5
Expand All @@ -8258,10 +8258,10 @@ fsevents@~2.1.2:
languageName: node
linkType: hard

"service-registry@git+ssh://[email protected]:getsentry/service-registry#main":
"service-registry@git+ssh://[email protected]/getsentry/service-registry/#main":
version: 1.0.3
resolution: "service-registry@git+ssh://[email protected]:getsentry/service-registry#commit=90c2ec6c0448c131a65d035d183baa5338769ad2"
checksum: 3a8ac0cbef1f680526c68d1d1a3a8fb82801097c2644ea4d7ca0f73cf3dce8c118a6ca69dad1ef39e346ce1611e9df1db3f57a2e9e9f0da37eec9811f6237110
resolution: "service-registry@git+ssh://[email protected]/getsentry/service-registry/#commit=547cedf90415cf59e1fa074591e3d962762dbbda"
checksum: fd335a638d5715857575ec64acc663eed2ca6589ab1c5f104de65b8af933c697799aab4a45102565e33c97b2899b2703f0d0e589a4747e7cf5bb87ce5d2695d7
languageName: node
linkType: hard

Expand Down

0 comments on commit cb9999b

Please sign in to comment.