Skip to content

Commit b55ef14

Browse files
Techbot121Meta Construct
authored andcommitted
don't show commits that are larger than 10
1 parent c70f36a commit b55ef14

File tree

1 file changed

+90
-67
lines changed

1 file changed

+90
-67
lines changed

app/services/discord/modules/webhook-handler.ts

Lines changed: 90 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const COLOR_BASE = 50;
1010

1111
const DIFF_SIZE = 2048;
1212
const MAX_FIELDS = 10;
13+
const MAX_COMMITS = 5;
1314

1415
const GitHub = new Webhooks({
1516
secret: webhookConfig.github.secret,
@@ -250,58 +251,11 @@ export default async (bot: DiscordBot): Promise<void> => {
250251

251252
if (payload.head_commit && isRemoteMergeCommit(payload.head_commit.message))
252253
commits.splice(0, commits.length, payload.head_commit);
253-
254-
for (const commit of commits) {
255-
const fields: Discord.APIEmbedField[] = [];
256-
const changes = GetGithubChanges(
257-
commit.added,
258-
commit.removed,
259-
commit.modified,
260-
repo.full_name,
261-
payload.ref
262-
);
263-
264-
includesLua =
265-
commit.added.some(str => str.endsWith(".lua")) ||
266-
commit.modified.some(str => str.endsWith(".lua")) ||
267-
commit.removed.some(str => str.endsWith(".lua"));
268-
269-
const oversize = changes.length > MAX_FIELDS;
270-
const changeLen = oversize ? MAX_FIELDS : changes.length;
271-
272-
for (let i = 0; i < changeLen; i++) {
273-
const change = changes[i];
274-
if (i === 24 || oversize ? i === changeLen - 1 : false) {
275-
fields.push({
276-
name: "​",
277-
value: `... and ${changes.length - changeLen} more changes`,
278-
});
279-
break;
280-
} else {
281-
fields.push({
282-
name: i > 0 ? "​" : "---",
283-
value: change.length > 1024 ? "<LINK TOO LONG>" : change,
284-
});
285-
}
286-
}
287-
288-
let diff = isMergeCommit(commit.message) ? undefined : await getGitHubDiff(commit.url);
289-
if (diff) {
290-
diff = diff.replaceAll(/(@@ -\d+,\d+ .+\d+,\d+ @@)[^\n]/g, "$1\n");
291-
diff = diff.replaceAll(/diff.+\nindex.+\n/g, "");
292-
diff = diff.replaceAll("```", "​`​`​`");
293-
}
294-
295-
embeds.push({
296-
title:
297-
commit.message.length > 256
298-
? `${commit.message.substring(0, 250)}. . .`
299-
: commit.message,
300-
description: diff
301-
? `\`\`\`diff\n${
302-
diff.length > DIFF_SIZE - 12 ? diff.substring(0, 4079) + ". . ." : diff
303-
}\`\`\``
304-
: undefined,
254+
255+
if (commits.length > MAX_COMMITS) {
256+
const embed: Discord.APIEmbed = {
257+
title: `${commits.length} commits in this push`,
258+
description: `[View all changes](${payload.compare})`,
305259
author: {
306260
name:
307261
branch !== repo.default_branch
@@ -310,23 +264,92 @@ export default async (bot: DiscordBot): Promise<void> => {
310264
url: repo.html_url,
311265
icon_url: repo.owner.avatar_url,
312266
},
313-
color:
314-
clamp(COLOR_BASE + COLOR_MOD * commit.removed.length, COLOR_BASE, 255) * 65536 +
315-
clamp(COLOR_BASE + COLOR_MOD * commit.added.length, COLOR_BASE, 255) * 256 +
316-
clamp(COLOR_BASE + COLOR_MOD * commit.modified.length, COLOR_BASE, 255),
317-
url: commit.url,
318-
fields: fields,
319-
timestamp: commit.timestamp,
267+
color: 0xFFD700,
268+
timestamp: new Date().toISOString(),
320269
footer: {
321-
text: `${commit.id.substring(0, 6)} by ${
322-
commit.author.username ?? commit.author.name
323-
}${
324-
commit.author.name !== commit.committer.name
325-
? ` via ${commit.committer.username ?? commit.committer.name}`
326-
: ""
327-
}`,
270+
text: `by ${payload.sender.name ?? payload.sender.login}`,
328271
},
329-
});
272+
};
273+
274+
embeds.push(embed);
275+
} else {
276+
for (const commit of commits) {
277+
const fields: Discord.APIEmbedField[] = [];
278+
const changes = GetGithubChanges(
279+
commit.added,
280+
commit.removed,
281+
commit.modified,
282+
repo.full_name,
283+
payload.ref
284+
);
285+
286+
includesLua =
287+
commit.added.some(str => str.endsWith(".lua")) ||
288+
commit.modified.some(str => str.endsWith(".lua")) ||
289+
commit.removed.some(str => str.endsWith(".lua"));
290+
291+
const oversize = changes.length > MAX_FIELDS;
292+
const changeLen = oversize ? MAX_FIELDS : changes.length;
293+
294+
for (let i = 0; i < changeLen; i++) {
295+
const change = changes[i];
296+
if (i === 24 || oversize ? i === changeLen - 1 : false) {
297+
fields.push({
298+
name: "︎",
299+
value: `... and ${changes.length - changeLen} more changes`,
300+
});
301+
break;
302+
} else {
303+
fields.push({
304+
name: i > 0 ? "︎" : "---",
305+
value: change.length > 1024 ? "<LINK TOO LONG>" : change,
306+
});
307+
}
308+
}
309+
310+
let diff = isMergeCommit(commit.message) ? undefined : await getGitHubDiff(commit.url);
311+
if (diff) {
312+
diff = diff.replaceAll(/(@@ -\d+,\d+ .+\d+,\d+ @@)[^\n]/g, "$1\n");
313+
diff = diff.replaceAll(/diff.+\nindex.+\n/g, "");
314+
diff = diff.replaceAll("```", "​`​`​`");
315+
}
316+
317+
embeds.push({
318+
title:
319+
commit.message.length > 256
320+
? `${commit.message.substring(0, 250)}. . .`
321+
: commit.message,
322+
description: diff
323+
? `\`\`\`diff\n${
324+
diff.length > DIFF_SIZE - 12 ? diff.substring(0, 4079) + ". . ." : diff
325+
}\`\`\``
326+
: undefined,
327+
author: {
328+
name:
329+
branch !== repo.default_branch
330+
? (repo.name + "/" + branch).substring(0, 256)
331+
: repo.name.substring(0, 256),
332+
url: repo.html_url,
333+
icon_url: repo.owner.avatar_url,
334+
},
335+
color:
336+
clamp(COLOR_BASE + COLOR_MOD * commit.removed.length, COLOR_BASE, 255) * 65536 +
337+
clamp(COLOR_BASE + COLOR_MOD * commit.added.length, COLOR_BASE, 255) * 256 +
338+
clamp(COLOR_BASE + COLOR_MOD * commit.modified.length, COLOR_BASE, 255),
339+
url: commit.url,
340+
fields: fields,
341+
timestamp: commit.timestamp,
342+
footer: {
343+
text: `${commit.id.substring(0, 6)} by ${
344+
commit.author.username ?? commit.author.name
345+
}${
346+
commit.author.name !== commit.committer.name
347+
? ` via ${commit.committer.username ?? commit.committer.name}`
348+
: ""
349+
}`,
350+
},
351+
});
352+
}
330353
}
331354
const messagePayload = <Discord.WebhookMessageCreateOptions>{
332355
...BaseEmbed,

0 commit comments

Comments
 (0)