-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.ts
183 lines (136 loc) · 4.63 KB
/
slack.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as http from "@actions/http-client";
import * as util from "./utils";
let slackhost = "https://hooks.slack.com/services";
interface ISlackMessengerParams {
from : string,
to : string,
portraitEmoji : string,
noticePrefix: string
}
function ghBranchLink(owner : string, repo : string, branch : string) {
return slackLink(
ghRepoUrl(owner, repo) + "/tree/" + branch,
"branch " + branch
);
}
function ghDeployLink(owner : string, repo : string, id : string, count : string) : string {
return slackLink(
ghRepoUrl(owner, repo) + "/runs/" + id,
"Deployment #" + count
);
}
function ghRepoLink(owner : string, repo : string) : string {
return slackLink(ghRepoUrl(owner, repo), owner + "/" + repo);
}
function ghRepoUrl(owner : string, repo : string) : string {
return "https://github.com/" + owner + "/" + repo;
}
function slackLink(url : string, desc : string) : string {
return "<" + url + "|" + desc + ">";
}
function invalidWebHook(hash : string) : boolean {
return hash.match(/^T[A-Z0-9]{7,9}\/B[A-Z0-9]{7,11}\/[A-Za-z0-9]{24}$/) == null;
}
class Messenger {
#webhook = "";
#username = "";
#channel = "";
#portrait = "";
#msgNoticePfx = "";
#client : http.HttpClient;
constructor(params : ISlackMessengerParams, webHook : string) {
if (!this.setWebHook(webHook)) {
throw new Error("Unable to bind webhook provided to Slack.Messenger: " + util.pop_last_error());
}
if (!util.empty(params.from)) {
this.setUser(params.from);
}
if (!util.empty(params.to)) {
this.setChan(params.to);
}
if (!util.empty(params.portraitEmoji)) {
this.setPortrait(params.portraitEmoji);
}
if (!util.empty(params.noticePrefix)) {
this.#msgNoticePfx = params.noticePrefix;
}
this.#client = new http.HttpClient("ftp-and-slack-notify HTTP client / 1.0")
};
async errorNotice(message : string, error : string) : Promise<boolean> {
// Slack has a message length limit of around 12,000 characters.
if (error.length > 11000) {
error = error.substr(0, 11000) + "\n\n*** output too long -- truncated ***\n";
}
if (!util.empty(error)) {
error = "\n*Error details:*\n```\n" + error + "```";
}
return await this.send(this.#msgNoticePfx + ": " + message + "." + error);
}
async notice(message : string) : Promise<boolean> {
return await this.send(this.#msgNoticePfx + ": " + message + ".");
}
async send(message : string, from : string = this.#username, to : string = this.#channel) : Promise<boolean> {
if (invalidWebHook(this.#webhook)) {
util.log_err("Invalid Slack WebHook provided to SlackMSG module.");
return false;
}
try {
let response = await this.#client.postJson(slackhost + "/" + this.#webhook, {
username: util.empty(from) ? undefined : from,
channel: util.empty(to) ? undefined : to,
text: message,
icon_emoji: util.empty(this.#portrait) ? undefined : this.#portrait
});
if (response.statusCode != 200) {
util.log_err("Slack message HTTP submission returned status " + response.statusCode + ".\n" +
"HTTP response:\n" + response.result);
return false;
}
} catch (err) {
util.log_err("Slack message HTTP submission attempt resulted in error: " + err);
return false;
}
return true;
}
setUser(which : string) : boolean {
if (util.empty(which)) {
util.log_err("Attempt to assign an invalid/empty Slack nickname: " + which);
return false;
}
this.#username = which;
return true;
}
setChan(which : string) : boolean {
if (util.empty(which, 2)) {
util.log_err("Attempt to assign an invalid/empty Slack channel: " + which);
return false;
}
this.#channel = which;
return true;
}
setPortrait(which : string) : boolean {
if (util.empty(which, 3)) {
util.log_err("Attempt to assign an invalid/empty Slack icon: " + which);
return false;
}
this.#portrait = util.empty(which, 3) ? "" : which;
return true;
}
setWebHook(hash : string) : boolean {
if (invalidWebHook(hash)) {
// Strips leading webhook host if that's the case.
if (hash.startsWith(slackhost + "/")) {
let trimmed_hash = hash.substr(slackhost.length + 1);
if (!invalidWebHook(trimmed_hash)) {
this.#webhook = trimmed_hash;
return true;
}
}
util.log_err("Invalid webhook provided.");
return false;
}
this.#webhook = hash;
return true;
}
}
export { ghBranchLink, ghDeployLink, ghRepoLink, ISlackMessengerParams, Messenger }