Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken links in triagebot #29

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions functions/triage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ function getPriorityEmoji(
return emoji;
}

function request_message_format_for_summary(
export function request_message_format_for_summary(
message: string,
urgencyEmojis: { [emoji: string]: number },
publicMessage: boolean,
Expand All @@ -573,19 +573,22 @@ function request_message_format_for_summary(
Object.keys(urgencyEmojis).forEach((emoji) => {
message = message.replace(emoji, "");
});
const ZWS = "\u{200B}";

if (publicMessage) {
// add a space between a @ and a name so you dont at people
message = message.replaceAll("<@", "\u200B<@\u200B");
}

// delete url garbage so we dont break urls in the summary due to dangling url bits
const url_regex = "/<.{1,}\|{1}/ig";

message = message.replaceAll(url_regex, ZWS);
message = message.replaceAll("\n", " ");
//truncate text
if (message.length >= 80) message = message.slice(0, 80) + "...";

// Because we truncate the message, there is a possibility that we truncate a link, channel, or username which leaves a dangling
// "<" charater. Leaving this charater in the message will cause the link to the next message to be broken. To prevent this
// after we truncate we replace all danging "<" characters with an empty string. This can cause some channel links or username to not show up
// but its better than breaking the link to the next message.
message = message.replaceAll(/<(?!.*>)/gi, "");

// remove whitespace, newline, etc
message = message.trim();

Expand Down
101 changes: 100 additions & 1 deletion functions/triage_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertEquals } from "testing/asserts.ts";
import { getMentions } from "./triage.ts";

import { getMentions, request_message_format_for_summary } from "./triage.ts";

Deno.test("Epoch to human readable date test", () => {
const mentions = getMentions("No one is here to help");
Expand All @@ -15,3 +16,101 @@ Deno.test("getMentions matches multiple mentions", () => {
const mentions = getMentions("<@ABC123|frodo> <@XYZ123> are here to help");
assertEquals(mentions, ["<@frodo>", "<@XYZ123>"]);
});

Deno.test("request_message_format_for_summary: should @ mention people in ephemeral messages", () => {
const result = request_message_format_for_summary(
"Hello <@ABC123>",
{},
false,
);

assertEquals(result, "Hello <@ABC123>");
});

Deno.test("request_message_format_for_summary: should not @ mention people in public messages", () => {
const result = request_message_format_for_summary(
"Hello <@ABC123>",
{},
true,
);

assertEquals(result, "Hello \u200B<@\u200BABC123>");
});

Deno.test("request_message_format_for_summary: should remove emojis", () => {
const result = request_message_format_for_summary(
":blue-circle: Hello <@ABC123>",
{ ":blue-circle:": 0 },
false,
);

assertEquals(result, "Hello <@ABC123>");
});

Deno.test("request_message_format_for_summary: should remove any dangling <", () => {
const tests = [
// Link
{
actual:
":blue-circle: Issue in <#C09K23K0U|> from <mylink|https://slack-ce.slack.com/archives/C09K23K0U/p1738777064854469> that needs your support: <https://slack-ce.slack.com/archives/C09K23K0U/p1738777064854469>",
expected:
"Issue in <#C09K23K0U|> from mylink|https://slack-ce.slack.com/archives/C09K23K...",
},
// Channel
{
actual:
":blue-circle: Issue in <#C09K23K0U|> from <mylink|https://slack-ce.slack.com> that needs <#C09K23K0U|>",
expected:
"Issue in <#C09K23K0U|> from <mylink|https://slack-ce.slack.com> that needs #C0...",
},
// User
{
actual:
":blue-circle: Issue in <#C09K23K0U|> from <mylink|https://slack-ce.slack.com> that needs <@U09K23K0U>",
expected:
"Issue in <#C09K23K0U|> from <mylink|https://slack-ce.slack.com> that needs @U0...",
},
];

for (const test of tests) {
const result = request_message_format_for_summary(
test.actual,
{ ":blue-circle:": 0 },
false,
);

assertEquals(result, test.expected);
}
});

Deno.test("request_message_format_for_summary: should preseve channels, users and links if it can", () => {
const tests = [
// Link
{
actual:
":blue-circle: <mylink|https://slack-ce.slack.com/archives/C09K23K0U/p1738777064854469>",
expected:
"<mylink|https://slack-ce.slack.com/archives/C09K23K0U/p1738777064854469>",
},
// Channel
{
actual: ":blue-circle: <#C09K23K0U|> <#C09K23K0U>",
expected: "<#C09K23K0U|> <#C09K23K0U>",
},
// User
{
actual: ":blue-circle: <@U09K23K0U>",
expected: "<@U09K23K0U>",
},
];

for (const test of tests) {
const result = request_message_format_for_summary(
test.actual,
{ ":blue-circle:": 0 },
false,
);

assertEquals(result, test.expected);
}
});