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

feat: use handlebar to build up webhook body #351

Merged
merged 6 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 2 additions & 3 deletions packages/api/src/models/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface IWebhook {
// to strip the additional properties that are related to the Mongoose internal representation -> webhook.headers.toJSON()
queryParams?: MongooseMap;
headers?: MongooseMap;
body?: MongooseMap;
body?: string;
}

const WebhookSchema = new Schema<IWebhook>(
Expand Down Expand Up @@ -63,8 +63,7 @@ const WebhookSchema = new Schema<IWebhook>(
required: false,
},
body: {
type: Map,
of: String,
type: String,
required: false,
},
},
Expand Down
40 changes: 7 additions & 33 deletions packages/api/src/tasks/checkAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,22 @@ export const handleSendGenericWebhook = async (

// BODY

let parsedBody: Record<string, string | number | symbol> = {};
let body = '';
if (webhook.body) {
const injectedBody = injectIntoPlaceholders(JSON.stringify(webhook.body), {
$HDX_ALERT_URL: message.hdxLink,
$HDX_ALERT_TITLE: message.title,
$HDX_ALERT_BODY: message.body,
const handlebars = Handlebars.create();
body = handlebars.compile(webhook.body)({
HDX_ALERT_URL: message.hdxLink,
HDX_ALERT_TITLE: message.title,
HDX_ALERT_BODY: message.body,
});
parsedBody = JSON.parse(injectedBody);
}

try {
// TODO: retries/backoff etc -> switch to request-error-tolerant api client
const response = await fetch(url, {
method: 'POST',
headers: headers as Record<string, string>,
body: JSON.stringify(parsedBody),
body,
});

if (!response.ok) {
Expand All @@ -299,32 +299,6 @@ export const handleSendGenericWebhook = async (
}
};

type HDXGenericWebhookTemplateValues = {
$HDX_ALERT_URL?: string;
$HDX_ALERT_TITLE?: string;
$HDX_ALERT_BODY?: string;
};

export function injectIntoPlaceholders(
placeholderString: string,
valuesToInject: HDXGenericWebhookTemplateValues,
) {
return placeholderString.replace(/(\$\w+)/g, function (match) {
const replacement =
valuesToInject[match as keyof HDXGenericWebhookTemplateValues] || match;
return escapeJsonValues(replacement);
});
}

export function escapeJsonValues(value: string): string {
return value
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
}

export const buildAlertMessageTemplateHdxLink = ({
alert,
dashboard,
Expand Down
24 changes: 13 additions & 11 deletions packages/app/src/TeamPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,14 @@ export default function TeamPage() {
return;
}

if (body && !isValidJson(body)) {
toast.error('Please enter valid JSON for body');
return;
}

saveWebhook.mutate(
{
name,
service: service,
url,
description,
headers: headers ? JSON.parse(headers) : undefined,
body: body ? JSON.parse(body) : undefined,
body,
},
{
onSuccess: () => {
Expand Down Expand Up @@ -597,9 +592,7 @@ export default function TeamPage() {
height="100px"
extensions={[
json(),
placeholder(
'{\n\t"text": "$HDX_ALERT_URL | $HDX_ALERT_TITLE | $HDX_ALERT_BODY",\n}',
),
placeholder('{\n\t"text": "{{HDX_ALERT_BODY}}"\n}'),
]}
theme={hdxJSONTheme}
onChange={onBodyChange}
Expand All @@ -619,8 +612,17 @@ export default function TeamPage() {
</span>
<br />
<span>
<code>$HDX_ALERT_URL</code>, <code>$HDX_ALERT_TITLE</code>
, <code>$HDX_ALERT_BODY</code>
<code>
{'{{'}HDX_ALERT_URL{'}}'}
</code>
,{' '}
<code>
{'{{'}HDX_ALERT_TITLE{'}}'}
</code>
,{' '}
<code>
{'{{'}HDX_ALERT_BODY{'}}'}
</code>
</span>
</Alert>
<Button
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ const api = {
description?: string;
queryParams?: Map<string, string>;
headers?: Map<string, string>;
body?: Map<string, string>;
body?: string;
}
>(async ({ service, url, name, description, queryParams, headers, body }) =>
server(`webhooks`, {
Expand Down
Loading