Skip to content

Commit b6e16b7

Browse files
authored
Truncate description/comment on Linear embeds (#327)
### Notes Caps each Linear embed description to 50 words and the recent comment section to 25 words. Which will prevent absurdly large embeds from appearing! Also adds support for `VoiceChannel`
2 parents df9851a + 0bb186a commit b6e16b7

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

Diff for: discord-scripts/fix-linear-embed.ts

+35-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,32 @@ import {
33
EmbedBuilder,
44
TextChannel,
55
ThreadChannel,
6+
VoiceChannel,
67
Message,
78
} from "discord.js"
89
import { Log, Robot } from "hubot"
910
import { LinearClient } from "@linear/sdk"
1011

1112
const { LINEAR_API_TOKEN } = process.env
1213

14+
function truncateToWords(
15+
content: string | undefined,
16+
ifBlank: string,
17+
maxWords = 50,
18+
): string {
19+
if (content === undefined || content.trim() === "") {
20+
return ifBlank
21+
}
22+
23+
const truncatedContent = content.split(" ").slice(0, maxWords).join(" ")
24+
25+
if (truncatedContent !== content) {
26+
return `${truncatedContent}...`
27+
}
28+
29+
return content
30+
}
31+
1332
async function createLinearEmbed(
1433
linearClient: LinearClient,
1534
issueId: string,
@@ -36,7 +55,9 @@ async function createLinearEmbed(
3655
.setURL(
3756
`https://linear.app/${teamName}/issue/${issue.identifier}#comment-${commentId}`,
3857
)
39-
.setDescription(comment.body || "No comment body available.")
58+
.setDescription(
59+
truncateToWords(comment.body, "No comment body available.", 50),
60+
)
4061
.addFields(
4162
{
4263
name: "Issue",
@@ -60,7 +81,9 @@ async function createLinearEmbed(
6081
embed
6182
.setTitle(`Issue: ${issue.title}`)
6283
.setURL(`https://linear.app/${teamName}/issue/${issue.identifier}`)
63-
.setDescription(issue.description || "No description available.")
84+
.setDescription(
85+
truncateToWords(issue.description, "No description available.", 50),
86+
)
6487
.addFields(
6588
{ name: "Status", value: state?.name || "No status", inline: true },
6689
{
@@ -79,7 +102,11 @@ async function createLinearEmbed(
79102
if (comments.nodes.length > 0) {
80103
embed.addFields({
81104
name: "Recent Comment",
82-
value: comments.nodes[0].body,
105+
value: truncateToWords(
106+
comments.nodes[0].body,
107+
"No recent comment.",
108+
25,
109+
),
83110
})
84111
}
85112
}
@@ -97,7 +124,7 @@ async function createLinearEmbed(
97124

98125
async function processLinearEmbeds(
99126
message: string,
100-
channel: TextChannel | ThreadChannel,
127+
channel: TextChannel | ThreadChannel | VoiceChannel,
101128
logger: Log,
102129
linearClient: LinearClient,
103130
) {
@@ -154,7 +181,8 @@ export default function linearEmbeds(discordClient: Client, robot: Robot) {
154181
message.author.bot ||
155182
!(
156183
message.channel instanceof TextChannel ||
157-
message.channel instanceof ThreadChannel
184+
message.channel instanceof ThreadChannel ||
185+
message.channel instanceof VoiceChannel
158186
)
159187
) {
160188
return
@@ -174,7 +202,8 @@ export default function linearEmbeds(discordClient: Client, robot: Robot) {
174202
!newMessage.content ||
175203
!(
176204
newMessage.channel instanceof TextChannel ||
177-
newMessage.channel instanceof ThreadChannel
205+
newMessage.channel instanceof ThreadChannel ||
206+
newMessage.channel instanceof VoiceChannel
178207
) ||
179208
newMessage.author?.bot
180209
) {

0 commit comments

Comments
 (0)