-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseBotMention.ts
More file actions
41 lines (37 loc) 路 1.45 KB
/
Copy pathparseBotMention.ts
File metadata and controls
41 lines (37 loc) 路 1.45 KB
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
/**
* Detect and strip @-mentions of the GitHub App bot login in comment bodies.
* Logins look like `pr-agent[bot]`; also accept the slug without the `[bot]` suffix.
*/
/** Login forms that count as tagging this bot (full login + optional slug without `[bot]`). */
export function botMentionLogins(botLogin: string): readonly string[] {
const trimmed = botLogin.trim();
if (!trimmed) return [];
const logins = [trimmed];
const lower = trimmed.toLowerCase();
if (lower.endsWith("[bot]")) {
const slug = trimmed.slice(0, -"[bot]".length);
if (slug.length > 0) logins.push(slug);
}
return logins;
}
function mentionPattern(botLogin: string): RegExp {
const alternation = botMentionLogins(botLogin)
.map((login) => login.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
.join("|");
// Word-ish boundary: avoid matching `@pr-agent-extra` when login is `pr-agent`.
return new RegExp(`(^|[^A-Za-z0-9_-])@(${alternation})(?![A-Za-z0-9_-])`, "gi");
}
export function commentMentionsBot(body: string, botLogin: string): boolean {
if (!botLogin.trim()) return false;
return mentionPattern(botLogin).test(body);
}
/** Remove bot @-mentions; collapse leftover whitespace. */
export function stripBotMentions(body: string, botLogin: string): string {
if (!botLogin.trim()) return body;
return body
.replace(mentionPattern(botLogin), "$1")
.replace(/[ \t]+\n/g, "\n")
.replace(/\n{3,}/g, "\n\n")
.replace(/[ \t]{2,}/g, " ")
.trim();
}