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 10 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
24 changes: 23 additions & 1 deletion 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,
};
}

export function SectionBlock(fields: MrkdwnElement[]): KnownBlock {
return {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add this to the above section() function.

export function section(block: MrkdwnElement, fields: MrkdownElement[])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since section is already being used by other functions rewriting it is probably out of scope, spoke with Hubert and trying to make either parameter optional just makes the code too messy and unreadable, so I'm going to keep it as two functions

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 || 'C05JJ5JNZAB';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you pick this channel id?
#proj-options-automator seems to be C04URUC21C5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was my test channel id while I was testing locally, will correct it before merging.

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: 10 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type CheckRun = EmitterWebhookEvent<'check_run'>['payload']['check_run'];
//
// Need to do this as we can't convert a string literal union to an array of literals
export const CHECK_RUN_PROPERTIES = ['id', 'head_sha', 'html_url'] as const;
export type CheckRunProperty = typeof CHECK_RUN_PROPERTIES[number];
export type CheckRunProperty = (typeof CHECK_RUN_PROPERTIES)[number];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like an autoformatting change I didn't make this change

export type CheckRunForRequiredChecksText = Pick<CheckRun, CheckRunProperty>;

export type GoCDResponse = GoCDStageResponse | GoCDAgentResponse;
Expand Down Expand Up @@ -170,3 +170,12 @@ 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[];
error_options: { option_name: string; error_msg: 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';
Empty file.
137 changes: 137 additions & 0 deletions src/webhooks/options-automator/options-automator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import * as Sentry from '@sentry/node';
import { KnownBlock } from '@slack/types';
import { FastifyRequest } from 'fastify';

import { OptionsAutomatorResponse } from '@types';

import { bolt } from '@/api/slack';
import * as slackblocks from '@/blocks/slackBlocks';
import { FEED_OPTIONS_AUTOMATOR_CHANNEL_ID } from '@/config';

export async function handler(
request: FastifyRequest<{ Body: OptionsAutomatorResponse }>
) {
const { body }: { body: OptionsAutomatorResponse } = request;
await messageSlack(body);
return {};
}

export async function messageSlack(message: OptionsAutomatorResponse) {
const successBlock: KnownBlock[] = [
slackblocks.header(slackblocks.plaintext('✅ Successfully Updated Options: ✅')),
...(message.channel_updated_options.length > 0
? [
slackblocks.divider(),
slackblocks.section(slackblocks.markdown('*Channel updated options:* ')),
slackblocks.SectionBlock(
message.channel_updated_options.map((option) =>
slackblocks.markdown(`channel updated \`${option}\``)
)
kneeyo1 marked this conversation as resolved.
Show resolved Hide resolved
),
]
: []),
...(message.updated_options.length > 0
? [
slackblocks.divider(),
slackblocks.section(slackblocks.markdown('*Updated options:* ')),
slackblocks.SectionBlock(
message.updated_options.map((option) =>
slackblocks.markdown(
`updated \`${option.option_name}\` with db value \`${option.db_value}\` and value \`${option.value}\``
)
)
),
]
: []),
...(message.set_options.length > 0
? [
slackblocks.divider(),
slackblocks.section(slackblocks.markdown('*Set Options:* ')),
slackblocks.SectionBlock(
message.set_options.map((option) =>
slackblocks.markdown(
`Set \`${option.option_name}\` with value \`${option.option_value}\``
)
)
),
]
: []),
...(message.unset_options.length > 0
? [
slackblocks.divider(),
slackblocks.section(slackblocks.markdown('*Unset Options:* ')),
slackblocks.SectionBlock(
message.unset_options.map((option) =>
slackblocks.markdown(`Unset \`${option}\``)
)
),
]
: []),
];

const failedBlock: KnownBlock[] = [
slackblocks.header(slackblocks.plaintext('❌ FAILED TO UPDATE: ❌')),
...(message.drifted_options.length > 0
? [
slackblocks.divider(),
slackblocks.section(slackblocks.markdown('*DRIFTED OPTIONS:* ')),
slackblocks.SectionBlock(
message.drifted_options.map((option) =>
slackblocks.markdown(`\`${option}\` drifted.`)
)
),
]
: []),
...(message.error_options.length > 0
? [
slackblocks.divider(),
slackblocks.section(slackblocks.markdown('*FAILED:* ')),
slackblocks.SectionBlock(
message.error_options.map((option) =>
slackblocks.markdown(
`FAILED TO UPDATE \`${option.option_name}\` \nREASON: \`${option.error_msg}\``
)
)
),
]
: []),
...(message.set_options.length > 0
? [
slackblocks.divider(),
slackblocks.section(slackblocks.markdown('*Set Options:* ')),
slackblocks.SectionBlock(
message.set_options.map((option) =>
slackblocks.markdown(
`Set \`${option.option_name}\` with value \`${option.option_value}\``
)
)
),
]
: []),
];

try {
// @ts-ignore
if (successBlock.length > 1) {
await bolt.client.chat.postMessage({
channel: FEED_OPTIONS_AUTOMATOR_CHANNEL_ID,
blocks: successBlock,
text: "",
unfurl_links: false,
});
}

if (failedBlock.length > 1) {
await bolt.client.chat.postMessage({
channel: FEED_OPTIONS_AUTOMATOR_CHANNEL_ID,
blocks: failedBlock,
text: "",
unfurl_links: false,
});
}
return;
} catch (err) {
Sentry.captureException(err);
return;
}
}
Loading