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

Implement embeds by issueId #329

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
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
88 changes: 77 additions & 11 deletions discord-scripts/fix-linear-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

// track processed message to avoid duplicates if original message is edited
const processedMessages = new Map<string, Set<string>>()
// let us also track sent embeds to delete them if the original message is deleted or edited WIP
const sentEmbeds = new Map<string, Message>()

const ISSUE_PREFIXES = ["THESIS", "MEZO", "ENG"]
const issueTagRegex = new RegExp(
`\\b(${ISSUE_PREFIXES.join("|")})-\\d+\\b`,
"g",
)
const issueUrlRegex =
/https:\/\/linear\.app\/([a-zA-Z0-9-]+)\/issue\/([a-zA-Z0-9-]+)(?:.*#comment-([a-zA-Z0-9]+))?/g

function truncateToWords(
content: string | undefined,
Expand Down Expand Up @@ -120,7 +130,7 @@

return embed
} catch (error) {
console.error("Error creating Linear embed:", error)

Check warning on line 133 in discord-scripts/fix-linear-embed.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
return null
}
}
Expand All @@ -132,32 +142,55 @@
logger: Log,
linearClient: LinearClient,
) {
const issueUrlRegex =
/https:\/\/linear\.app\/([a-zA-Z0-9-]+)\/issue\/([a-zA-Z0-9-]+)(?:.*#comment-([a-zA-Z0-9]+))?/g

const matches = Array.from(message.matchAll(issueUrlRegex))
const urlMatches = Array.from(message.matchAll(issueUrlRegex))
const issueMatches = Array.from(message.matchAll(issueTagRegex))

if (matches.length === 0) {
if (urlMatches.length === 0 && issueMatches.length === 0) {
return
}

const processedIssues = processedMessages.get(messageId) || new Set<string>()
processedMessages.set(messageId, processedIssues)

const embedPromises = matches.map(async (match) => {
const uniqueMatches = new Set<string>()

urlMatches.forEach((match) => {
const teamName = match[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for all of these you can use named capturing groups.

const issueId = match[2]
const commentId = match[3] || undefined
const uniqueKey = `${issueId}-${commentId}`
const uniqueKey = `${issueId}-${commentId || ""}`

if (!processedIssues.has(uniqueKey)) {
processedIssues.add(uniqueKey)
uniqueMatches.add(JSON.stringify({ issueId, commentId, teamName }))
}
})

issueMatches.forEach((match) => {
const issueId = match[0]

if (processedIssues.has(uniqueKey)) {
return null
if (
Array.from(uniqueMatches).some(
(uniqueMatch) => JSON.parse(uniqueMatch).issueId === issueId,
)
) {
return
}

processedIssues.add(uniqueKey)
const uniqueKey = `${issueId}`
if (!processedIssues.has(uniqueKey)) {
processedIssues.add(uniqueKey)
uniqueMatches.add(
JSON.stringify({ issueId, commentId: undefined, teamName: undefined }),
)
}
})

const embedPromises = Array.from(uniqueMatches).map(async (matchString) => {
const { issueId, commentId, teamName } = JSON.parse(matchString)

logger.info(
`Processing team: ${teamName}, issue: ${issueId}, comment: ${commentId}`,
`Processing issue: ${issueId}, comment: ${commentId}, team: ${teamName}`,
)

const embed = await createLinearEmbed(
Expand All @@ -169,6 +202,7 @@

return { embed, issueId }
})

const results = await Promise.all(embedPromises)

results
Expand All @@ -180,6 +214,9 @@
if (embed) {
channel
.send({ embeds: [embed] })
.then((sentMessage) => {
sentEmbeds.set(messageId, sentMessage)
})
.catch((error) =>
logger.error(
`Failed to send embed for issue ID: ${issueId}: ${error}`,
Expand Down Expand Up @@ -229,6 +266,23 @@
return
}

const matches =
Array.from(newMessage.content.matchAll(issueTagRegex)).length > 0 ||
Array.from(newMessage.content.matchAll(issueUrlRegex)).length > 0

if (!matches) {
const embedMessage = sentEmbeds.get(newMessage.id)
if (embedMessage) {
await embedMessage.delete().catch((error) => {
robot.logger.error(
`Failed to delete embed for message ID: ${newMessage.id}: ${error}`,
)
})
sentEmbeds.delete(newMessage.id)
}
return
}

robot.logger.info(
`Processing updated message: ${newMessage.content} (was: ${oldMessage?.content})`,
)
Expand All @@ -240,4 +294,16 @@
linearClient,
)
})

discordClient.on("messageDelete", async (message) => {
const embedMessage = sentEmbeds.get(message.id)
if (embedMessage) {
await embedMessage.delete().catch((error) => {
robot.logger.error(
`Failed to delete embed for message ID: ${message.id}: ${error}`,
)
})
sentEmbeds.delete(message.id)
}
})
}
Loading