Skip to content

Commit 8547491

Browse files
authored
Add a webhook log success field to allow filtering in the UI (#2061)
* Add a success field to allow filtering in the UI * add test assertions
1 parent e6d8d48 commit 8547491

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

packages/api/src/controllers/webhook-log.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const requestsFieldsMap: FieldsMap = {
2626
userId: `webhook_log.data->>'userId'`,
2727
event: `webhook_log.data->>'event'`,
2828
statusCode: `webhook_log.data->'response'->>'status'`,
29+
success: `webhook_log.data->>'success'`,
2930
resourceId: {
3031
val: `webhook_log.data->'request'->>'body'`,
3132
type: "full-text",

packages/api/src/controllers/webhook.test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,15 @@ describe("controllers/webhook", () => {
350350
expect(resent.webhookId).toBe(webhookRequest.webhookId);
351351
expect(resent.id).not.toBe(webhookRequest.id);
352352

353-
const logRes = await client.get(`/webhook/${generatedWebhook.id}/log`);
353+
let logRes = await client.get(`/webhook/${generatedWebhook.id}/log`);
354+
expect(logRes.status).toBe(200);
355+
logJson = await logRes.json();
356+
expect(logJson.length).toBe(2);
357+
358+
// try query filters
359+
logRes = await client.get(
360+
`/webhook/${generatedWebhook.id}/log?filters=[{"id":"success","value":"false"}]`
361+
);
354362
expect(logRes.status).toBe(200);
355363
logJson = await logRes.json();
356364
expect(logJson.length).toBe(2);

packages/api/src/schema/api-schema.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ components:
244244
duration:
245245
type: number
246246
description: The time taken (in seconds) to make the webhook request
247+
success:
248+
type: boolean
249+
description: Whether the webhook request was successful
247250
request:
248251
type: object
249252
properties:

packages/api/src/schema/db-schema.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ components:
614614
index: true
615615
createdAt:
616616
index: true
617+
success:
618+
index: true
617619
response:
618620
type: object
619621
properties:

packages/api/src/webhooks/cannon.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export default class WebhookCannon {
413413
responseBody = await resp.text();
414414
statusCode = resp.status;
415415

416-
if (resp.status >= 200 && resp.status < 300) {
416+
if (isSuccess(resp)) {
417417
// 2xx requests are cool. all is good
418418
logger.info(`webhook ${webhook.id} fired successfully`);
419419
return true;
@@ -449,7 +449,7 @@ export default class WebhookCannon {
449449
);
450450
} catch (e) {
451451
console.log(
452-
`Unable to store response of webhook ${webhook.id} url: ${webhook.url}`
452+
`Unable to store response of webhook ${webhook.id} url: ${webhook.url} error: ${e}`
453453
);
454454
}
455455
await this.storeTriggerStatus(
@@ -585,6 +585,10 @@ export default class WebhookCannon {
585585
}
586586
}
587587

588+
function isSuccess(resp: Response) {
589+
return resp?.status >= 200 && resp?.status < 300;
590+
}
591+
588592
async function storeResponse(
589593
webhook: DBWebhook,
590594
eventId: string,
@@ -596,9 +600,12 @@ async function storeResponse(
596600
params
597601
): Promise<WebhookLog> {
598602
const hrDuration = process.hrtime(startTime);
599-
const encodedResponseBody = Buffer.from(
600-
responseBody.substring(0, 1024)
601-
).toString("base64");
603+
let encodedResponseBody = "";
604+
if (responseBody) {
605+
encodedResponseBody = Buffer.from(responseBody.substring(0, 1024)).toString(
606+
"base64"
607+
);
608+
}
602609

603610
const webhookLog = {
604611
id: uuid(),
@@ -608,11 +615,12 @@ async function storeResponse(
608615
userId: webhook.userId,
609616
createdAt: Date.now(),
610617
duration: hrDuration[0] + hrDuration[1] / 1e9,
618+
success: isSuccess(resp),
611619
response: {
612620
body: encodedResponseBody,
613-
redirected: resp.redirected,
614-
status: resp.status,
615-
statusText: resp.statusText,
621+
redirected: resp?.redirected,
622+
status: resp?.status,
623+
statusText: resp?.statusText,
616624
},
617625
request: {
618626
url: webhook.url,
@@ -694,7 +702,9 @@ export async function storeTriggerStatus(
694702
): Promise<void> {
695703
try {
696704
let status: DBWebhook["status"] = { lastTriggeredAt: triggerTime };
697-
let encodedResponseBody = Buffer.from(responseBody).toString("base64");
705+
let encodedResponseBody = responseBody
706+
? Buffer.from(responseBody).toString("base64")
707+
: "";
698708
if (statusCode >= 300 || !statusCode) {
699709
status = {
700710
...status,

0 commit comments

Comments
 (0)