Skip to content

Commit ebb20f6

Browse files
alexeyvclaudebmadcode
authored
chore(discord): suppress link embeds and handle truncated URLs (#1126)
* chore(discord): suppress link embeds and handle truncated URLs - Add wrap_urls() to wrap URLs in <> to suppress Discord embeds - Add strip_trailing_url() to remove partial URLs from truncated text - Update all 6 workflow jobs with body text to use the new helpers - Partial URLs (from truncation) are removed since they are unusable - Complete URLs are wrapped to prevent large embed previews 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]> * fix(discord): preserve URLs when escaping markdown - Replace sed-based esc() with awk version that skips content inside <URL> wrappers, preventing URL corruption from backslash escaping - Reorder pipeline: wrap_urls | esc (wrap first, then escape) - Update comment: "partial" → "incomplete" for clarity 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]> --------- Co-authored-by: Claude Opus 4.5 <[email protected]> Co-authored-by: Brian <[email protected]>
1 parent 82cc108 commit ebb20f6

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

.github/scripts/discord-helpers.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@
22
# Discord notification helper functions
33

44
# Escape markdown special chars and @mentions for safe Discord display
5-
# Bracket expression: ] must be first, then other chars. In POSIX bracket expr, \ is literal.
6-
esc() { sed -e 's/[][\*_()~`>]/\\&/g' -e 's/@/@ /g'; }
5+
# Skips content inside <URL> wrappers to preserve URLs intact
6+
esc() {
7+
awk '{
8+
result = ""; in_url = 0; n = length($0)
9+
for (i = 1; i <= n; i++) {
10+
c = substr($0, i, 1)
11+
if (c == "<" && substr($0, i, 8) ~ /^<https?:/) in_url = 1
12+
if (in_url) { result = result c; if (c == ">") in_url = 0 }
13+
else if (c == "@") result = result "@ "
14+
else if (index("[]\\*_()~`", c) > 0) result = result "\\" c
15+
else result = result c
16+
}
17+
print result
18+
}'
19+
}
720

821
# Truncate to $1 chars (or 80 if wall-of-text with <3 spaces)
922
trunc() {
@@ -13,3 +26,9 @@ trunc() {
1326
[ "$spaces" -lt 3 ] && [ ${#txt} -gt 80 ] && txt=$(printf '%s' "$txt" | cut -c1-80)
1427
printf '%s' "$txt"
1528
}
29+
30+
# Remove incomplete URL at end of truncated text (incomplete URLs are useless)
31+
strip_trailing_url() { sed -E 's~<?https?://[^[:space:]]*$~~'; }
32+
33+
# Wrap URLs in <> to suppress Discord embeds (keeps links clickable)
34+
wrap_urls() { sed -E 's~https?://[^[:space:]<>]+~<&>~g'; }

.github/workflows/discord.yaml

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ jobs:
5353
5454
TITLE=$(printf '%s' "$PR_TITLE" | trunc $MAX_TITLE | esc)
5555
[ ${#PR_TITLE} -gt $MAX_TITLE ] && TITLE="${TITLE}..."
56-
BODY=$(printf '%s' "$PR_BODY" | trunc $MAX_BODY | esc)
56+
BODY=$(printf '%s' "$PR_BODY" | trunc $MAX_BODY)
57+
if [ -n "$PR_BODY" ] && [ ${#PR_BODY} -gt $MAX_BODY ]; then
58+
BODY=$(printf '%s' "$BODY" | strip_trailing_url)
59+
fi
60+
BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
5761
[ -n "$PR_BODY" ] && [ ${#PR_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
5862
[ -n "$BODY" ] && BODY=" · $BODY"
5963
USER=$(printf '%s' "$PR_USER" | esc)
@@ -91,7 +95,11 @@ jobs:
9195
9296
TITLE=$(printf '%s' "$ISSUE_TITLE" | trunc $MAX_TITLE | esc)
9397
[ ${#ISSUE_TITLE} -gt $MAX_TITLE ] && TITLE="${TITLE}..."
94-
BODY=$(printf '%s' "$ISSUE_BODY" | trunc $MAX_BODY | esc)
98+
BODY=$(printf '%s' "$ISSUE_BODY" | trunc $MAX_BODY)
99+
if [ -n "$ISSUE_BODY" ] && [ ${#ISSUE_BODY} -gt $MAX_BODY ]; then
100+
BODY=$(printf '%s' "$BODY" | strip_trailing_url)
101+
fi
102+
BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
95103
[ -n "$ISSUE_BODY" ] && [ ${#ISSUE_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
96104
[ -n "$BODY" ] && BODY=" · $BODY"
97105
USER=$(printf '%s' "$USER" | esc)
@@ -126,7 +134,11 @@ jobs:
126134
127135
TITLE=$(printf '%s' "$ISSUE_TITLE" | trunc $MAX_TITLE | esc)
128136
[ ${#ISSUE_TITLE} -gt $MAX_TITLE ] && TITLE="${TITLE}..."
129-
BODY=$(printf '%s' "$COMMENT_BODY" | trunc $MAX_BODY | esc)
137+
BODY=$(printf '%s' "$COMMENT_BODY" | trunc $MAX_BODY)
138+
if [ ${#COMMENT_BODY} -gt $MAX_BODY ]; then
139+
BODY=$(printf '%s' "$BODY" | strip_trailing_url)
140+
fi
141+
BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
130142
[ ${#COMMENT_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
131143
USER=$(printf '%s' "$COMMENT_USER" | esc)
132144
@@ -162,7 +174,11 @@ jobs:
162174
163175
TITLE=$(printf '%s' "$PR_TITLE" | trunc $MAX_TITLE | esc)
164176
[ ${#PR_TITLE} -gt $MAX_TITLE ] && TITLE="${TITLE}..."
165-
BODY=$(printf '%s' "$REVIEW_BODY" | trunc $MAX_BODY | esc)
177+
BODY=$(printf '%s' "$REVIEW_BODY" | trunc $MAX_BODY)
178+
if [ -n "$REVIEW_BODY" ] && [ ${#REVIEW_BODY} -gt $MAX_BODY ]; then
179+
BODY=$(printf '%s' "$BODY" | strip_trailing_url)
180+
fi
181+
BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
166182
[ -n "$REVIEW_BODY" ] && [ ${#REVIEW_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
167183
[ -n "$BODY" ] && BODY=": $BODY"
168184
USER=$(printf '%s' "$REVIEW_USER" | esc)
@@ -194,7 +210,11 @@ jobs:
194210
195211
TITLE=$(printf '%s' "$PR_TITLE" | trunc $MAX_TITLE | esc)
196212
[ ${#PR_TITLE} -gt $MAX_TITLE ] && TITLE="${TITLE}..."
197-
BODY=$(printf '%s' "$COMMENT_BODY" | trunc $MAX_BODY | esc)
213+
BODY=$(printf '%s' "$COMMENT_BODY" | trunc $MAX_BODY)
214+
if [ ${#COMMENT_BODY} -gt $MAX_BODY ]; then
215+
BODY=$(printf '%s' "$BODY" | strip_trailing_url)
216+
fi
217+
BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
198218
[ ${#COMMENT_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
199219
USER=$(printf '%s' "$COMMENT_USER" | esc)
200220
@@ -224,7 +244,11 @@ jobs:
224244
225245
REL_NAME=$(printf '%s' "$NAME" | trunc $MAX_TITLE | esc)
226246
[ ${#NAME} -gt $MAX_TITLE ] && REL_NAME="${REL_NAME}..."
227-
BODY=$(printf '%s' "$RELEASE_BODY" | trunc $MAX_BODY | esc)
247+
BODY=$(printf '%s' "$RELEASE_BODY" | trunc $MAX_BODY)
248+
if [ -n "$RELEASE_BODY" ] && [ ${#RELEASE_BODY} -gt $MAX_BODY ]; then
249+
BODY=$(printf '%s' "$BODY" | strip_trailing_url)
250+
fi
251+
BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
228252
[ -n "$RELEASE_BODY" ] && [ ${#RELEASE_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
229253
[ -n "$BODY" ] && BODY=" · $BODY"
230254
TAG_ESC=$(printf '%s' "$TAG" | esc)
@@ -275,7 +299,7 @@ jobs:
275299
run: |
276300
set -o pipefail
277301
[ -z "$WEBHOOK" ] && exit 0
278-
esc() { sed -e 's/[][\*_()~`>]/\\&/g' -e 's/@/@ /g'; }
302+
esc() { sed -e 's/[][\*_()~`]/\\&/g' -e 's/@/@ /g'; }
279303
trunc() { tr '\n\r' ' ' | cut -c1-"$1"; }
280304
281305
REF_TRUNC=$(printf '%s' "$REF" | trunc 100)

0 commit comments

Comments
 (0)