Skip to content

Commit d4861df

Browse files
committed
feat(script): prioritize changelog over thank yous in Discord posts
Discord release notifications now show the changelog (bullet list of changes) first, with contributor thank yous truncated first when hitting the 2000 character limit. Fixes anomalyco#221
1 parent 894f172 commit d4861df

1 file changed

Lines changed: 91 additions & 28 deletions

File tree

‎script/discord-notify.ts‎

Lines changed: 91 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
* Posts release notes to a Discord forum thread as plain markdown.
77
* Uses Discord REST API directly (no library dependencies).
88
*
9-
* Environment variables:
10-
* DISCORD_TOKEN - Discord user token (self-bot)
11-
* DISCORD_THREAD_ID - Forum thread ID to post to
12-
* RELEASE_VERSION - Version being released (e.g., v1.0.166-11)
13-
* RELEASE_CHANGELOG - Changelog content
9+
* Changelog format from publish.ts:
10+
* - Bullet point changes
11+
* - More changes
12+
*
13+
* **Thank you to N community contributors:**
14+
* - @user: commit message
15+
*
16+
* Discord priority: Changelog > Thank Yous (truncated first when exceeding limit)
1417
*/
1518

1619
const DISCORD_API = "https://discord.com/api/v10"
17-
const MAX_CONTENT_LENGTH = 2000 // Discord message content limit
20+
const MAX_CONTENT_LENGTH = 2000
1821

1922
async function postToDiscord(threadId: string, token: string, content: string): Promise<void> {
2023
console.log("Request body:", JSON.stringify({ content }, null, 2).slice(0, 500) + "...")
@@ -36,21 +39,90 @@ async function postToDiscord(threadId: string, token: string, content: string):
3639
console.log("Successfully posted release notes to Discord")
3740
}
3841

42+
/**
43+
* Extract the changelog portion (everything before the thank you section)
44+
*/
45+
function extractChangelogSection(changelog: string): string {
46+
const thankYouIndex = changelog.indexOf("**Thank you to")
47+
if (thankYouIndex > 0) {
48+
return changelog.slice(0, thankYouIndex).trim()
49+
}
50+
return changelog.trim()
51+
}
52+
53+
/**
54+
* Extract the contributor thank you section
55+
*/
3956
function extractContributorSection(changelog: string): string | null {
40-
// Look for the contributor thank you section
4157
const thankYouMatch = changelog.match(/\*\*Thank you to \d+ community contributors?:\*\*[\s\S]*$/)
4258
return thankYouMatch ? thankYouMatch[0].trim() : null
4359
}
4460

45-
function truncateContent(content: string, maxLength: number): string {
46-
if (content.length <= maxLength) return content
61+
/**
62+
* Truncate text at the last newline before maxLength
63+
*/
64+
function truncateAtNewline(text: string, maxLength: number): string {
65+
if (text.length <= maxLength) return text
4766

48-
// Find a good breaking point (end of a line)
49-
const truncated = content.slice(0, maxLength - 50)
67+
const truncated = text.slice(0, maxLength)
5068
const lastNewline = truncated.lastIndexOf("\n")
51-
const cutoff = lastNewline > 0 ? lastNewline : maxLength - 50
5269

53-
return content.slice(0, cutoff) + "\n\n*...see GitHub for full details.*"
70+
// Use the newline if it's in a reasonable position (>50% of max)
71+
if (lastNewline > maxLength * 0.5) {
72+
return truncated.slice(0, lastNewline)
73+
}
74+
75+
return truncated
76+
}
77+
78+
/**
79+
* Build Discord content with smart truncation
80+
* Priority: Header > Changelog > Footer > Thank Yous (truncated first)
81+
*/
82+
function formatDiscordContent(
83+
changelog: string | undefined,
84+
version: string,
85+
releaseUrl: string,
86+
npmUrl: string,
87+
): string {
88+
const header = `**shuvcode ${version}** has been released!\n\n`
89+
const footer = `\n\n[GitHub Release](<${releaseUrl}>) | [npm](<${npmUrl}>)`
90+
const truncationNote = "\n\n*...see GitHub for full details.*"
91+
92+
// If no changelog, just return header + footer
93+
if (!changelog?.trim()) {
94+
return header.trim() + footer
95+
}
96+
97+
// Fixed overhead
98+
const fixedOverhead = header.length + footer.length
99+
100+
// Extract sections
101+
const changelogPart = extractChangelogSection(changelog)
102+
const thankYouPart = extractContributorSection(changelog)
103+
104+
// Calculate available space for content
105+
const availableForContent = MAX_CONTENT_LENGTH - fixedOverhead
106+
107+
// Build full content to check length
108+
const fullContent = thankYouPart ? `${changelogPart}\n\n${thankYouPart}` : changelogPart
109+
110+
// Case 1: Everything fits
111+
if (fullContent.length <= availableForContent) {
112+
return header + fullContent + footer
113+
}
114+
115+
// Case 2: Changelog fits, but not with thank yous
116+
const changelogWithNote = changelogPart + truncationNote
117+
if (changelogWithNote.length <= availableForContent) {
118+
return header + changelogPart + truncationNote + footer
119+
}
120+
121+
// Case 3: Changelog itself needs truncation
122+
const maxChangelogLength = availableForContent - truncationNote.length
123+
const truncatedChangelog = truncateAtNewline(changelogPart, maxChangelogLength)
124+
125+
return header + truncatedChangelog + truncationNote + footer
54126
}
55127

56128
async function main(): Promise<void> {
@@ -78,24 +150,15 @@ async function main(): Promise<void> {
78150
const releaseUrl = `https://github.com/Latitudes-Dev/shuvcode/releases/tag/${cleanVersion}`
79151
const npmUrl = `https://www.npmjs.com/package/shuvcode/v/${cleanVersion.slice(1)}`
80152

81-
// Build plain markdown content
82-
let content = `**shuvcode ${cleanVersion}** has been released!\n\n`
83-
84-
// Only include contributor thank yous section, not the full changelog
85-
if (changelog?.trim()) {
86-
const contributorSection = extractContributorSection(changelog)
87-
if (contributorSection) {
88-
content += contributorSection + "\n\n"
89-
}
90-
}
91-
92-
content += `[GitHub Release](<${releaseUrl}>) | [npm](<${npmUrl}>)`
93-
94-
// Truncate if too long for Discord
95-
content = truncateContent(content, MAX_CONTENT_LENGTH)
153+
// Build content with changelog prioritized over thank yous
154+
const content = formatDiscordContent(changelog, cleanVersion, releaseUrl, npmUrl)
96155

97156
console.log(`Posting release notes for ${cleanVersion} to Discord...`)
98157
console.log(`Content length: ${content.length} characters`)
158+
console.log("Content preview:")
159+
console.log("---")
160+
console.log(content.slice(0, 500) + (content.length > 500 ? "..." : ""))
161+
console.log("---")
99162

100163
await postToDiscord(threadId, token, content)
101164
}

0 commit comments

Comments
 (0)