|
| 1 | +import { Client, EmbedBuilder, TextChannel } from "discord.js" |
| 2 | +import { Log, Robot } from "hubot" |
| 3 | +import { LinearClient } from "@linear/sdk" |
| 4 | + |
| 5 | +const { LINEAR_API_TOKEN } = process.env |
| 6 | + |
| 7 | +async function createLinearEmbed( |
| 8 | + linearClient: LinearClient, |
| 9 | + issueId: string, |
| 10 | + commentId?: string, |
| 11 | + teamName?: string, |
| 12 | +) { |
| 13 | + try { |
| 14 | + const issue = await linearClient.issue(issueId) |
| 15 | + |
| 16 | + const project = issue.project ? await issue.project : null |
| 17 | + const state = issue.state ? await issue.state : null |
| 18 | + const assignee = issue.assignee ? await issue.assignee : null |
| 19 | + const comments = await issue.comments() |
| 20 | + const comment = commentId |
| 21 | + ? comments.nodes.find((c) => c.id.startsWith(commentId)) |
| 22 | + : null |
| 23 | + |
| 24 | + const embed = new EmbedBuilder() |
| 25 | + |
| 26 | + if (comment) { |
| 27 | + // Comment-focused embed |
| 28 | + embed |
| 29 | + .setTitle(`Comment on Issue: ${issue.title}`) |
| 30 | + .setURL( |
| 31 | + `https://linear.app/${teamName}/issue/${issue.identifier}#comment-${commentId}`, |
| 32 | + ) |
| 33 | + .setDescription(comment.body || "No comment body available.") |
| 34 | + .addFields( |
| 35 | + { |
| 36 | + name: "Issue", |
| 37 | + value: `${issue.title} (${state?.name || "No status"})`, |
| 38 | + inline: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "Assignee", |
| 42 | + value: assignee?.name.toString() || "Unassigned", |
| 43 | + inline: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Priority", |
| 47 | + value: issue.priority?.toString() || "None", |
| 48 | + inline: true, |
| 49 | + }, |
| 50 | + ) |
| 51 | + .setFooter({ text: `Project: ${project?.name || "No project"}` }) |
| 52 | + } else { |
| 53 | + // Issue-focused embed |
| 54 | + embed |
| 55 | + .setTitle(`Issue: ${issue.title}`) |
| 56 | + .setURL(`https://linear.app/${teamName}/issue/${issue.identifier}`) |
| 57 | + .setDescription(issue.description || "No description available.") |
| 58 | + .addFields( |
| 59 | + { name: "Status", value: state?.name || "No status", inline: true }, |
| 60 | + { |
| 61 | + name: "Assignee", |
| 62 | + value: assignee?.name.toString() || "Unassigned", |
| 63 | + inline: true, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Priority", |
| 67 | + value: issue.priority?.toString() || "None", |
| 68 | + inline: true, |
| 69 | + }, |
| 70 | + ) |
| 71 | + .setFooter({ text: `Project: ${project?.name || "No project"}` }) |
| 72 | + |
| 73 | + if (comments.nodes.length > 0) { |
| 74 | + embed.addFields({ |
| 75 | + name: "Recent Comment", |
| 76 | + value: comments.nodes[0].body, |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (issue.updatedAt) { |
| 82 | + embed.setTimestamp(new Date(issue.updatedAt)) |
| 83 | + } |
| 84 | + |
| 85 | + return embed |
| 86 | + } catch (error) { |
| 87 | + console.error("Error creating Linear embed:", error) |
| 88 | + return null |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +async function processLinearEmbeds( |
| 93 | + message: string, |
| 94 | + channel: TextChannel, |
| 95 | + logger: Log, |
| 96 | + linearClient: LinearClient, |
| 97 | +) { |
| 98 | + const issueUrlRegex = |
| 99 | + /https:\/\/linear\.app\/([a-zA-Z0-9-]+)\/issue\/([a-zA-Z0-9-]+)(?:.*#comment-([a-zA-Z0-9]+))?/g |
| 100 | + |
| 101 | + const matches = Array.from(message.matchAll(issueUrlRegex)) |
| 102 | + |
| 103 | + if (matches.length === 0) { |
| 104 | + logger.info("No Linear issue links found in message.") |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + const embedPromises = matches.map(async (match) => { |
| 109 | + const teamName = match[1] |
| 110 | + const issueId = match[2] |
| 111 | + const commentId = match[3] || undefined |
| 112 | + |
| 113 | + logger.info( |
| 114 | + `Processing team: ${teamName}, issue: ${issueId}, comment: ${commentId}`, |
| 115 | + ) |
| 116 | + |
| 117 | + const embed = await createLinearEmbed( |
| 118 | + linearClient, |
| 119 | + issueId, |
| 120 | + commentId, |
| 121 | + teamName, |
| 122 | + ) |
| 123 | + |
| 124 | + return { embed, issueId } |
| 125 | + }) |
| 126 | + |
| 127 | + const results = await Promise.all(embedPromises) |
| 128 | + |
| 129 | + results.forEach(({ embed, issueId }) => { |
| 130 | + if (embed) { |
| 131 | + channel |
| 132 | + .send({ embeds: [embed] }) |
| 133 | + .catch((error) => |
| 134 | + logger.error( |
| 135 | + `Failed to send embed for issue ID: ${issueId}: ${error}`, |
| 136 | + ), |
| 137 | + ) |
| 138 | + } else { |
| 139 | + logger.error(`Failed to create embed for issue ID: ${issueId}`) |
| 140 | + } |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +export default function linearEmbeds(discordClient: Client, robot: Robot) { |
| 145 | + const linearClient = new LinearClient({ apiKey: LINEAR_API_TOKEN }) |
| 146 | + |
| 147 | + discordClient.on("messageCreate", async (message) => { |
| 148 | + if (message.author.bot || !(message.channel instanceof TextChannel)) { |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + robot.logger.info(`Processing message: ${message.content}`) |
| 153 | + await processLinearEmbeds( |
| 154 | + message.content, |
| 155 | + message.channel, |
| 156 | + robot.logger, |
| 157 | + linearClient, |
| 158 | + ) |
| 159 | + }) |
| 160 | + |
| 161 | + discordClient.on("messageUpdate", async (oldMessage, newMessage) => { |
| 162 | + if ( |
| 163 | + !newMessage.content || |
| 164 | + !newMessage.channel || |
| 165 | + newMessage.author?.bot || |
| 166 | + !(newMessage.channel instanceof TextChannel) |
| 167 | + ) { |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + robot.logger.info( |
| 172 | + `Processing updated message: ${newMessage.content} (was: ${oldMessage?.content})`, |
| 173 | + ) |
| 174 | + await processLinearEmbeds( |
| 175 | + newMessage.content, |
| 176 | + newMessage.channel, |
| 177 | + robot.logger, |
| 178 | + linearClient, |
| 179 | + ) |
| 180 | + }) |
| 181 | +} |
0 commit comments