-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
208 lines (208 loc) · 7.79 KB
/
mod.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
exFetch,
type ExFetchEventRetryPayload
} from "EXFETCH/mod.ts";
import {
addSecretMask,
writeDebug,
writeError
} from "GHACTIONS/log.ts";
import {
getInput,
getInputBoolean,
getInputNumber,
setOutput
} from "GHACTIONS/parameter.ts";
import type {
JSONArray,
JSONObject,
} from "ISJSON/mod.ts";
import { parse as yamlParse } from "STD/yaml/parse";
import { StringTruncator } from "STRINGOVERFLOW/mod.ts";
import {
resolveContent,
resolveEmbeds,
resolveFiles,
resolveKey,
resolveMentions,
resolvePoll,
resolveThreadID,
resolveThreadName,
resolveThreadTags,
resolveUsername
} from "./_payload.ts";
console.log("Initialize.");
const splitterNewLine = /\r?\n/g;
const splitterCommonDelimiter = /,|\r?\n/g;
const userAgent = `SendDiscordWebhook.GitHubAction/7.0.5`;
writeDebug(`Environment Variables:\n\t${Object.entries(Deno.env.toObject()).map(([key, value]: [string, string]): string => {
return `${key} = ${value}`;
}).join("\n\t")}`);
console.log("Parse input.");
try {
const truncateEnable: boolean = getInputBoolean("truncate_enable", { fallback: false }) ?? true;
const stringTruncator: StringTruncator | undefined = truncateEnable ? new StringTruncator(128, {
ellipsisMark: getInput("truncate_ellipsis", { fallback: false }),
//@ts-ignore Validate by the module.
ellipsisPosition: getInput("truncate_position", { fallback: false })
}) : undefined;
const key: string = resolveKey(getInput("key", { require: true }));
addSecretMask(key);
const username: string | undefined = resolveUsername(getInput("username"), stringTruncator);
const avatarURL: string = getInput("avatar_url");
const content: string | undefined = resolveContent(getInput("content"), getInput("content_links_no_embed").split(splitterNewLine).filter((value: string): boolean => {
return (value.length > 0);
}), stringTruncator);
const embeds: JSONArray | undefined = resolveEmbeds(yamlParse(getInput("embeds")), stringTruncator);
const poll: JSONObject | undefined = resolvePoll({
allowMultiSelect: getInputBoolean("poll_allow_multiselect"),
answers: yamlParse(getInput("poll_answers")),
duration: getInputNumber("poll_duration", { fallback: false }) ?? -1,
question: getInput("poll_question")
});
const files: FormData | undefined = await resolveFiles(getInput("files").split(splitterNewLine).map((file: string): string => {
return file.trim();
}).filter((file: string): boolean => {
return (file.length > 0);
}), getInputBoolean("files_glob", { fallback: false }) ?? true);
const allowedMentions: JSONObject = resolveMentions({
parseEveryone: getInputBoolean("allowed_mentions_parse_everyone", { fallback: false }) ?? true,
parseRoles: getInputBoolean("allowed_mentions_parse_roles", { fallback: false }) ?? true,
parseUsers: getInputBoolean("allowed_mentions_parse_users", { fallback: false }) ?? true,
roles: getInput("allowed_mentions_roles").split(splitterCommonDelimiter).map((value: string): string => {
return value.trim();
}).filter((value: string): boolean => {
return (value.length > 0);
}),
users: getInput("allowed_mentions_users").split(splitterCommonDelimiter).map((value: string): string => {
return value.trim();
}).filter((value: string): boolean => {
return (value.length > 0);
})
});
const tts: boolean = getInputBoolean("tts");
const threadID: string | undefined = resolveThreadID(getInput("thread_id"));
const threadName: string | undefined = resolveThreadName(getInput("thread_name"), stringTruncator);
const threadTags: string[] | undefined = resolveThreadTags(getInput("thread_tags").split(splitterCommonDelimiter).map((value: string): string => {
return value.trim();
}).filter((value: string): boolean => {
return (value.length > 0);
}));
const notification: boolean = getInputBoolean("notification", { fallback: false }) ?? true;
const wait: boolean = getInputBoolean("wait", { fallback: false }) ?? true;
if (
(typeof content === "undefined" && typeof embeds === "undefined" && typeof files === "undefined" && typeof poll === "undefined") ||
((
typeof content !== "undefined" ||
typeof embeds !== "undefined" ||
typeof files !== "undefined"
) && typeof poll !== "undefined")
) {
throw new Error(`Only one of the group of inputs must be defined: \`content\`, \`embeds\`, and/or \`files\`; \`poll\`!`);
}
if (typeof threadID !== "undefined" && typeof threadName !== "undefined") {
throw new Error(`Only one of the group of inputs can be defined: \`thread_id\`; \`thread_name\` and \`thread_tags\` (Optional)!`);
}
const methodForm: boolean = getInputBoolean("method_form");
const discordWebhookUrlParameters: URLSearchParams = new URLSearchParams();
if (typeof threadID !== "undefined") {
discordWebhookUrlParameters.set("thread_id", threadID);
}
if (wait) {
discordWebhookUrlParameters.set("wait", "true");
}
const requestPayload: JSONObject = {
tts,
allowed_mentions: allowedMentions
};
if (typeof content !== "undefined") {
requestPayload.content = content;
}
if (typeof username !== "undefined") {
requestPayload.username = username;
}
if (avatarURL.length > 0) {
requestPayload.avatar_url = avatarURL;
}
if (typeof embeds !== "undefined") {
requestPayload.embeds = embeds;
}
if (!notification) {
requestPayload.flags = 1 << 12;
}
if (typeof threadName !== "undefined") {
requestPayload.thread_name = threadName;
}
if (typeof threadTags !== "undefined") {
requestPayload.applied_tags = threadTags;
}
if (typeof poll !== "undefined") {
requestPayload.poll = poll;
}
const requestPayloadStringify: string = JSON.stringify(requestPayload);
const requestHeaders: Headers = new Headers();
const requestBody: string | FormData = ((): string | FormData => {
if (
methodForm ||
typeof files !== "undefined"
) {
// IMPORTANT: Do not set the request header `Content-Type`, `fetch` automatically set this when use `FormData`.
const result: FormData = (typeof files === "undefined") ? new FormData() : files;
result.append("payload_json", requestPayloadStringify);
writeDebug(`Body:\n\t${Array.from(result.entries(), ([key, value]: [string, FormDataEntryValue]): string => {
return `${key} = ${value}`;
}).join("\n\t")}`);
return result;
}
requestHeaders.set("Content-Type", "application/json");
writeDebug(`Body: ${requestPayloadStringify}`);
return requestPayloadStringify;
})();
console.log(`Post network request to Discord.`);
const response: Response = await exFetch(`https://discord.com/api/webhooks/${key}${(discordWebhookUrlParameters.size > 0) ? `?${discordWebhookUrlParameters.toString()}` : ""}`, {
body: requestBody,
headers: requestHeaders,
method: "POST",
redirect: "follow"
}, {
retry: {
onRetry({
countCurrent,
countMaximum,
statusCode,
statusText,
timeWait
}: ExFetchEventRetryPayload): void {
console.log(`Last network request failed with status \`${statusCode} ${statusText}. Retry #${countCurrent}/${countMaximum} after ${timeWait / 1000} seconds\`.`);
},
timeWait: {
maximum: 120000,
minimum: 10000
}
},
userAgent
}).catch((reason: Error): never => {
throw new Error(`Unexpected web request issue: ${reason?.message ?? reason}`);
});
const responseText: string = await response.text();
setOutput({
response: responseText,
status_code: response.status,
status_ok: response.ok,
status_text: response.statusText
});
if (!response.ok) {
throw new Error(`Unexpected response status \`${response.status} ${response.statusText}\`: ${responseText}`);
}
console.log(`Response Status: ${response.status} ${response.statusText}`);
console.log(`Response Content: ${responseText}`);
} catch (error) {
if (error instanceof AggregateError) {
writeError(`${error.name}: ${error.message}\n\t${error.errors.join("\n\t")}`);
} else if (error instanceof Error) {
writeError(`${error.name}: ${error.message}`);
} else {
writeError(String(error));
}
Deno.exit(1);
}