mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 13:17:08 +00:00
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:
parent
01d3d96f12
commit
f6cbc9481e
1
changelog.d/224.feature
Normal file
1
changelog.d/224.feature
Normal file
@ -0,0 +1 @@
|
||||
Generic webhook payloads are now pretty printed.
|
@ -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") {
|
||||
|
@ -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({
|
||||
|
@ -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>{ "simple": "data" }</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>{ "username": "Bobs-integration", "type": 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,
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user