Prettify JSON output of generic hooks (#224)

* Pretty print JSON output from webhooks

* Fixup supported body encodings

* changelog

* Fix tests
This commit is contained in:
Will Hunt 2022-03-04 13:36:08 +00:00 committed by GitHub
parent 01d3d96f12
commit f6cbc9481e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 10 deletions

1
changelog.d/224.feature Normal file
View File

@ -0,0 +1 @@
Generic webhook payloads are now pretty printed.

View File

@ -178,13 +178,16 @@ export class GenericHookConnection extends BaseConnection implements IConnection
this.state = validatedConfig;
}
public transformHookData(data: Record<string, unknown>): {plain: string, html?: string} {
public transformHookData(data: Record<string, unknown>|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 = `<p>Received webhook data:</p><p><pre><code class=\\"language-json\\">${JSON.stringify(data, null, 2)}</code></pre></p>`
}
if (typeof data.html === "string") {

View File

@ -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({

View File

@ -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<code>{ &quot;simple&quot;: &quot;data&quot; }</code>",
formatted_body: "<p>Received webhook data:</p><p><pre><code class=\\\"language-json\\\">{\n \"simple\": \"data\"\n}</code></pre></p>",
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: "<strong>Bobs-integration</strong>: Received webhook data:\n\n<code>{ &quot;username&quot;: &quot;Bobs-integration&quot;, &quot;type&quot;: 42 }</code>",
formatted_body: "<strong>Bobs-integration</strong>: <p>Received webhook data:</p><p><pre><code class=\\\"language-json\\\">{\n \"username\": \"Bobs-integration\",\n \"type\": 42\n}</code></pre></p>",
msgtype: "m.notice",
"uk.half-shot.hookshot.webhook_data": webhookData,
},