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

creating new webhook for the options-automator #554

Merged
merged 25 commits into from
Aug 8, 2023
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
28 changes: 25 additions & 3 deletions src/blocks/slackBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { KnownBlock, MrkdwnElement } from '@slack/types';
import { HeaderBlock, KnownBlock, MrkdwnElement, PlainTextElement } from '@slack/types';


export function header(text: PlainTextElement): HeaderBlock {
return {
type: 'header',
text,
};
}

export function divider(): KnownBlock {
return {
Expand All @@ -13,9 +21,23 @@ export function markdown(text: string): MrkdwnElement {
};
}

export function plaintext(text: string): PlainTextElement {
return {
type: 'plain_text',
text,
};
}

export function section(block: MrkdwnElement): KnownBlock {
return {
type: 'section',
text: block,
};
text: block
}
}

export function sectionBlock(fields: MrkdwnElement[]): KnownBlock {
return {
type: 'section',
fields: fields
}
}
2 changes: 2 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const GOCD_ORIGIN =
process.env.GOCD_ORIGIN || 'https://deploy.getsentry.net';
export const FEED_DEPLOY_CHANNEL_ID =
process.env.FEED_DEPLOY_CHANNEL_ID || 'C051ED5GLN4';
export const FEED_OPTIONS_AUTOMATOR_CHANNEL_ID =
process.env.FEED_OPTIONS_AUTOMATOR_CHANNEL_ID || 'C04URUC21C5';
export const FEED_DEV_INFRA_CHANNEL_ID =
process.env.FEED_DEV_INFRA_CHANNEL_ID || 'C05816N2A2K';
export const FEED_ENGINEERING_CHANNEL_ID =
Expand Down
11 changes: 11 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,14 @@ type GoCDApprovalType = 'success' | 'manual';
type GoCDResultType = 'Passed' | 'Failed' | 'Cancelled' | 'Unknown';

type GoCDStateType = 'Passed' | 'Failed' | 'Cancelled' | 'Building';

export interface OptionsAutomatorResponse {
drifted_options: { option_name: string; option_value: string }[];
channel_updated_options: string[];
updated_options: { option_name: string; db_value: string; value: string }[];
set_options: { option_name: string; option_value: string }[];
unset_options: string[];
not_writable_options: { option_name: string; error_msg: string }[];
unregistered_options: string[];
invalid_type_options: {option_name: string, got_type: string, expected_type: string }[];
}
1 change: 1 addition & 0 deletions src/webhooks/options-automator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { handler } from './options-automator';
277 changes: 277 additions & 0 deletions src/webhooks/options-automator/options-automator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
import { buildServer } from '@/buildServer';
import testpayload from '@test/payloads/options-automator/testpayload.json';
import testemptypayload from '@test/payloads/options-automator/testemptypayload.json';
import testparitalpayload from '@test/payloads/options-automator/testpartialpayload.json';
import { GETSENTRY_ORG } from '@/config';
import { bolt } from '@api/slack';
import { messageSlack } from './options-automator';

describe('options-automator webhook', function() {
let fastify;
beforeEach(async function () {
fastify = await buildServer(false);
});

afterEach(function () {
fastify.close();
});

it('correctly inserts options-automator webhook when stage starts', async function () {
const response = await fastify.inject({
method: 'POST',
url: '/metrics/options-automator/webhook',
payload: testemptypayload,
});

expect(response.statusCode).toBe(200);
});
});

describe('test message slack', function() {
let boltPostMessageSpy;
const org = GETSENTRY_ORG;

beforeEach(async function () {
boltPostMessageSpy = jest.spyOn(bolt.client.chat, 'postMessage');
});

afterEach(function () {
jest.clearAllMocks();
});

it('writes to slack', async function() {
const postMessageSpy = jest.spyOn(bolt.client.chat, 'postMessage');
await messageSlack(testpayload);
expect(postMessageSpy).toHaveBeenCalledTimes(2);
const firstMessage = postMessageSpy.mock.calls[0][0];
const secondMessage = postMessageSpy.mock.calls[1][0];
expect(firstMessage).toEqual({
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "✅ Successfully Updated Options: ✅"
}
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*Updated options:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "updated `updated_option_1` with db value `db_value_1` and value `new_value_1`"
}
]
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*Set Options:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "Set `set_option_1` with value `set_value_1`"
},
{
type: "mrkdwn",
text: "Set `set_option_2` with value `set_value_2`"
}
]
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*Unset Options:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "Unset `unset_option_1`"
},
{
type: "mrkdwn",
text: "Unset `unset_option_2`"
}
]
}
],
channel: "C04URUC21C5",
text: "",
unfurl_links: false
});
expect(secondMessage).toEqual({
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "❌ FAILED TO UPDATE: ❌"
}
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*DRIFTED OPTIONS:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "`drifted_option_1` drifted. value on db: `value_1`"
},
{
type: "mrkdwn",
text: "`drifted_option_2` drifted. value on db: `value_2`"
}
]
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*FAILED:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "FAILED TO UPDATE `error_option_1` \nREASON: `Error occurred for option 1`"
},
{
type: "mrkdwn",
text: "FAILED TO UPDATE `error_option_2` \nREASON: `Error occurred for option 2`"
}
]
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*Unregistered Options:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "Option `unregisterd_option_1` is not registered!"
},
{
type: "mrkdwn",
text: "Option `unregisterd_option_2` is not registered!"
}
]
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*Invalid Typed Options:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "Option `invalid_type_option_1` got type `string`, \n but expected type `float`."
},
{
type: "mrkdwn",
text: "Option `invalid_type_option_2` got type `float`, \n but expected type `int`."
}
]
}
],
channel: "C04URUC21C5",
text: "",
unfurl_links: false
});
})
it('writes drift only', async function() {
const postMessageSpy = jest.spyOn(bolt.client.chat, 'postMessage');
await messageSlack(testparitalpayload);
expect(postMessageSpy).toHaveBeenCalledTimes(1);
const message = postMessageSpy.mock.calls[0][0];
expect(message).toEqual({
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "❌ FAILED TO UPDATE: ❌"
}
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*DRIFTED OPTIONS:* "
}
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "`drifted_option_1` drifted. value on db: `value_1`"
},
{
type: "mrkdwn",
text: "`drifted_option_2` drifted. value on db: `value_2`"
}
]
},
],
channel: "C04URUC21C5",
text: "",
unfurl_links: false
});
})
})
Loading
Loading