diff --git a/changelog.d/224.feature b/changelog.d/224.feature new file mode 100644 index 00000000..b0d3b5d8 --- /dev/null +++ b/changelog.d/224.feature @@ -0,0 +1 @@ +Generic webhook payloads are now pretty printed. \ No newline at end of file diff --git a/src/Connections/GenericHook.ts b/src/Connections/GenericHook.ts index b21975fd..4f9cf6b3 100644 --- a/src/Connections/GenericHook.ts +++ b/src/Connections/GenericHook.ts @@ -178,13 +178,16 @@ export class GenericHookConnection extends BaseConnection implements IConnection this.state = validatedConfig; } - public transformHookData(data: Record): {plain: string, html?: string} { + public transformHookData(data: Record|string): {plain: string, html?: string} { // Supported parameters https://developers.mattermost.com/integrate/incoming-webhooks/#parameters const msg: {plain: string, html?: string} = {plain: ""}; - if (typeof data.text === "string") { - msg.plain += data.text; + if (typeof data === "string") { + return {plain: `Received webhook data: ${data}`}; + } else if (typeof data.text === "string") { + msg.plain = data.text; } else { - msg.plain += `Received webhook data:\n\n\`\`\`${JSON.stringify(data, undefined, 2)}\`\`\``; + msg.plain = "Received webhook data:\n\n" + "```json\n\n" + JSON.stringify(data, null, 2) + "\n\n```"; + msg.html = `

Received webhook data:

${JSON.stringify(data, null, 2)}

` } if (typeof data.html === "string") { diff --git a/src/Webhooks.ts b/src/Webhooks.ts index e0fe6014..43d8931b 100644 --- a/src/Webhooks.ts +++ b/src/Webhooks.ts @@ -4,7 +4,6 @@ import { EventEmitter } from "events"; import { MessageQueue, createMessageQueue } from "./MessageQueue"; import LogWrapper from "./LogWrapper"; import qs from "querystring"; -import { Server } from "http"; import axios from "axios"; import { IGitLabWebhookEvent } from "./Gitlab/WebhookTypes"; import { EmitterWebhookEvent, Webhooks as OctokitWebhooks } from "@octokit/webhooks" @@ -65,7 +64,9 @@ export class Webhooks extends EventEmitter { } this.expressRouter.all( '/:hookId', - express.json({ type: ['application/json', 'application/x-www-form-urlencoded'] }), + express.text({ type: 'text/*'}), + express.urlencoded({ extended: false }), + express.json(), this.onGenericPayload.bind(this), ); this.expressRouter.use(express.json({ diff --git a/tests/connections/GenericHookTest.ts b/tests/connections/GenericHookTest.ts index bd1df81d..dbe51ff0 100644 --- a/tests/connections/GenericHookTest.ts +++ b/tests/connections/GenericHookTest.ts @@ -47,9 +47,9 @@ describe("GenericHookConnection", () => { roomId: ROOM_ID, sender: connection.getUserId(), content: { - body: "Received webhook data:\n\n```{\n \"simple\": \"data\"\n}```", + body: "Received webhook data:\n\n```json\n\n{\n \"simple\": \"data\"\n}\n\n```", format: "org.matrix.custom.html", - formatted_body: "Received webhook data:\n\n{ "simple": "data" }", + formatted_body: "

Received webhook data:

{\n  \"simple\": \"data\"\n}

", msgtype: "m.notice", "uk.half-shot.hookshot.webhook_data": webhookData, }, @@ -101,9 +101,9 @@ describe("GenericHookConnection", () => { roomId: ROOM_ID, sender: connection.getUserId(), content: { - body: "**Bobs-integration**: Received webhook data:\n\n```{\n \"username\": \"Bobs-integration\",\n \"type\": 42\n}```", + body: "**Bobs-integration**: Received webhook data:\n\n```json\n\n{\n \"username\": \"Bobs-integration\",\n \"type\": 42\n}\n\n```", format: "org.matrix.custom.html", - formatted_body: "Bobs-integration: Received webhook data:\n\n{ "username": "Bobs-integration", "type": 42 }", + formatted_body: "Bobs-integration:

Received webhook data:

{\n  \"username\": \"Bobs-integration\",\n  \"type\": 42\n}

", msgtype: "m.notice", "uk.half-shot.hookshot.webhook_data": webhookData, },