forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
163 lines (153 loc) · 5.38 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import { ChatPostMessageArguments } from "@slack/web-api";
// Return a formatted copy of the Slack message object, based on the kind of Pulumi webhook received.
// See the Pulumi and Slack webhook documentation for details.
// https://www.pulumi.com/docs/intro/console/extensions/webhooks/
// https://api.slack.com/docs/message-attachments
export function formatSlackMessage(kind: string, payload: object, messageArgs: ChatPostMessageArguments): ChatPostMessageArguments {
const cloned: ChatPostMessageArguments = Object.assign({}, messageArgs) as ChatPostMessageArguments;
switch (kind) {
case "stack":
return formatStack(payload, cloned);
case "team":
return formatTeam(payload, cloned);
case "stack_preview":
case "stack_update":
return formatUpdate(kind, payload, cloned);
case "ping":
return formatPing(payload, cloned);
default:
return cloned;
}
}
function formatStack(payload: any, args: ChatPostMessageArguments): ChatPostMessageArguments {
const summary = `${payload.organization.githubLogin}/${payload.projectName}/${payload.stackName} ${payload.action}.`;
args.text = summary;
args.attachments = [
{
fallback: summary,
fields: [
{
title: "User",
value: `${payload.user.name} (${payload.user.githubLogin})`,
short: true,
},
{
title: "Action",
value: payload.action,
short: true,
},
],
},
];
return args;
}
function formatTeam(payload: any, args: ChatPostMessageArguments): ChatPostMessageArguments {
const summary = `${payload.organization.githubLogin} team ${payload.action}.`;
args.text = summary;
args.attachments = [
{
fallback: summary,
fields: [
{
title: "User",
value: `${payload.user.name} (${payload.user.githubLogin})`,
short: true,
},
{
title: "Team",
value: payload.team.name,
short: true,
},
{
title: "Stack",
value: `${payload.organization.githubLogin}/${payload.stackName}`,
short: true,
},
{
title: "Action",
value: payload.action,
short: true,
},
{
title: "Members",
value: payload.team.members.map((m: any) => `• ${m.name} (${m.githubLogin})`).join("\n"),
short: false,
},
{
title: "Stacks",
value: payload.team.stacks.map((s: any) => `• ${s.stackName} (${s.permission})`).join("\n"),
short: false,
},
],
},
];
return args;
}
function formatUpdate(kind: "stack_preview" | "stack_update", payload: any, args: ChatPostMessageArguments): ChatPostMessageArguments {
const summary = `${payload.organization.githubLogin}/${payload.projectName}/${payload.stackName} ${payload.kind} ${payload.result}.`;
args.text = `${resultEmoji(payload.result)} ${summary}`;
args.attachments = [
{
fallback: `${summary}: ${payload.updateUrl}`,
color: resultColor(payload.result),
fields: [
{
title: "User",
value: `${payload.user.name} (${payload.user.githubLogin})`,
short: true,
},
{
title: "Stack",
value: `${payload.organization.githubLogin}/${payload.stackName}`,
short: true,
},
{
title: "Resource Changes",
value: Object.keys(payload.resourceChanges)
.map(key => `• ${titleCase(key)}: ${payload.resourceChanges[key]}`)
.join("\n"),
short: true,
},
{
title: "Kind",
value: titleCase(kind.replace("stack_", "")),
short: true,
},
{
title: "Permalink",
value: payload.updateUrl,
short: false,
},
],
},
];
return args;
}
function formatPing(payload: any, args: ChatPostMessageArguments) {
args.text = payload.message;
return args;
}
function resultColor(result: string): string {
switch (result) {
case "succeeded":
return "#36a64f";
case "failed":
return "#e01563";
default:
return "#e9a820";
}
}
function resultEmoji(result: string): string {
switch (result) {
case "succeeded":
return ":tropical_drink:";
case "failed":
return ":rotating_light:";
default:
return "";
}
}
function titleCase(s: string): string {
return [s.charAt(0).toUpperCase(), s.substr(1)].join("");
}