Skip to content

Commit

Permalink
Fix webhook payload parsing (pulumi#215)
Browse files Browse the repository at this point in the history
Passes the wehbook payload as a JSON object (which the formatting functions are expecting) rather than as a string.

Signed-off-by: Christian Nunciato <[email protected]>
  • Loading branch information
cnunciato committed Feb 4, 2019
1 parent cde3ef8 commit 55a8d30
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions aws-ts-pulumi-webhooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ webhookHandler.get("/", async (_, res) => {
webhookHandler.post("/", logRequest, authenticateRequest, async (req, res) => {
const webhookKind = req.headers["pulumi-webhook-kind"] as string; // headers[] returns (string | string[]).
const payload = <string>req.body.toString();
const prettyPrintedPayload = JSON.stringify(JSON.parse(payload), null, 2);
const parsedPayload = JSON.parse(payload);
const prettyPrintedPayload = JSON.stringify(parsedPayload, null, 2);

const client = new slack.WebClient(stackConfig.slackToken);

Expand All @@ -69,7 +70,7 @@ webhookHandler.post("/", logRequest, authenticateRequest, async (req, res) => {
}

// Format the Slack message based on the kind of webhook received.
const formattedMessageArgs = formatSlackMessage(webhookKind, payload, messageArgs);
const formattedMessageArgs = formatSlackMessage(webhookKind, parsedPayload, messageArgs);

await client.chat.postMessage(formattedMessageArgs);
res.status(200).end(`posted to Slack channel ${stackConfig.slackChannel}\n`);
Expand Down
2 changes: 1 addition & 1 deletion aws-ts-pulumi-webhooks/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChatPostMessageArguments } from "@slack/client";
// See the Pulumi and Slack webhook documentation for details.
// https://pulumi.io/reference/service/webhooks.html
// https://api.slack.com/docs/message-attachments
export function formatSlackMessage(kind: string, payload: any, messageArgs: ChatPostMessageArguments): ChatPostMessageArguments {
export function formatSlackMessage(kind: string, payload: object, messageArgs: ChatPostMessageArguments): ChatPostMessageArguments {
const cloned: ChatPostMessageArguments = Object.assign({}, messageArgs) as ChatPostMessageArguments;

switch (kind) {
Expand Down

0 comments on commit 55a8d30

Please sign in to comment.