|
| 1 | +import type { VercelWebhook } from "@/schemas/vercel"; |
| 2 | + |
| 3 | +const EMOJIS = { |
| 4 | + success: "✅", |
| 5 | + error: "❌", |
| 6 | + pending: "⏳", |
| 7 | + canceled: "🚫", |
| 8 | + promoted: "🔗", |
| 9 | + info: "ℹ️", |
| 10 | + branch: "🌿", |
| 11 | + commit: "📝", |
| 12 | + url: "🌐", |
| 13 | +} as const; |
| 14 | + |
| 15 | +type Formatter = (webhook: VercelWebhook) => string; |
| 16 | + |
| 17 | +const FORMATTERS: Record<string, Formatter> = { |
| 18 | + deployment: formatDeployment, |
| 19 | + domain: formatDomain, |
| 20 | + project: formatProject, |
| 21 | +}; |
| 22 | + |
| 23 | +export function formatWebhook(webhook: VercelWebhook): string { |
| 24 | + const typePrefix = webhook.type.split(".")[0]; |
| 25 | + const formatter = FORMATTERS[typePrefix]; |
| 26 | + return formatter ? formatter(webhook) : formatGeneric(webhook); |
| 27 | +} |
| 28 | + |
| 29 | +function formatDeployment(webhook: VercelWebhook): string { |
| 30 | + const { deployment, links } = webhook.payload; |
| 31 | + if (!deployment) { |
| 32 | + return formatGeneric(webhook); |
| 33 | + } |
| 34 | + |
| 35 | + const state = webhook.type.split(".")[1]; |
| 36 | + const emoji = getEmoji(state); |
| 37 | + const meta = deployment.meta; |
| 38 | + |
| 39 | + const lines: string[] = [ |
| 40 | + `${emoji} <b>Deployment ${state}</b>`, |
| 41 | + "", |
| 42 | + `<b>Project:</b> ${escapeHtml(deployment.name)}`, |
| 43 | + ]; |
| 44 | + |
| 45 | + if (meta?.target) { |
| 46 | + lines.push(`<b>Environment:</b> ${escapeHtml(meta.target)}`); |
| 47 | + } |
| 48 | + |
| 49 | + if (meta?.githubCommitRef) { |
| 50 | + lines.push( |
| 51 | + `${EMOJIS.branch} <b>Branch:</b> <code>${escapeHtml(meta.githubCommitRef)}</code>` |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + if (meta?.githubCommitSha) { |
| 56 | + const shortSha = meta.githubCommitSha.slice(0, 7); |
| 57 | + const commitUrl = `https://github.com/${meta.githubCommitOrg}/${meta.githubCommitRepo}/commit/${meta.githubCommitSha}`; |
| 58 | + lines.push( |
| 59 | + `${EMOJIS.commit} <b>Commit:</b> <a href="${commitUrl}">${shortSha}</a>` |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if (meta?.githubCommitMessage) { |
| 64 | + const message = |
| 65 | + meta.githubCommitMessage.length > 200 |
| 66 | + ? `${meta.githubCommitMessage.slice(0, 200)}...` |
| 67 | + : meta.githubCommitMessage; |
| 68 | + lines.push("", `<pre>${escapeHtml(message)}</pre>`); |
| 69 | + } |
| 70 | + |
| 71 | + if (webhook.type === "deployment.error" && meta?.buildError) { |
| 72 | + const errorText = |
| 73 | + meta.buildError.length > 300 |
| 74 | + ? `${meta.buildError.slice(0, 300)}...` |
| 75 | + : meta.buildError; |
| 76 | + lines.push( |
| 77 | + "", |
| 78 | + "<b>Build Error:</b>", |
| 79 | + `<pre>${escapeHtml(errorText)}</pre>` |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + if (links?.deployment) { |
| 84 | + lines.push( |
| 85 | + "", |
| 86 | + `${EMOJIS.url} <a href="${links.deployment}">View Deployment</a>` |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + return lines.join("\n"); |
| 91 | +} |
| 92 | + |
| 93 | +function formatDomain(webhook: VercelWebhook): string { |
| 94 | + const { domain } = webhook.payload; |
| 95 | + if (!domain) { |
| 96 | + return formatGeneric(webhook); |
| 97 | + } |
| 98 | + |
| 99 | + const state = webhook.type.split(".")[1]; |
| 100 | + return [ |
| 101 | + `${EMOJIS.url} <b>Domain ${state}</b>`, |
| 102 | + "", |
| 103 | + `<b>Domain:</b> ${escapeHtml(domain.name)}`, |
| 104 | + ].join("\n"); |
| 105 | +} |
| 106 | + |
| 107 | +function formatProject(webhook: VercelWebhook): string { |
| 108 | + const { project } = webhook.payload; |
| 109 | + if (!project) { |
| 110 | + return formatGeneric(webhook); |
| 111 | + } |
| 112 | + |
| 113 | + const state = webhook.type.split(".")[1]; |
| 114 | + const emoji = state === "created" ? "🆕" : "🗑️"; |
| 115 | + |
| 116 | + return [ |
| 117 | + `${emoji} <b>Project ${state}</b>`, |
| 118 | + "", |
| 119 | + `<b>Project:</b> ${escapeHtml(project.name || project.id)}`, |
| 120 | + ].join("\n"); |
| 121 | +} |
| 122 | + |
| 123 | +function formatGeneric(webhook: VercelWebhook): string { |
| 124 | + const formattedType = webhook.type |
| 125 | + .split(".") |
| 126 | + .map((p) => p.charAt(0).toUpperCase() + p.slice(1)) |
| 127 | + .join(" • "); |
| 128 | + |
| 129 | + return [ |
| 130 | + `${EMOJIS.info} <b>${escapeHtml(formattedType)}</b>`, |
| 131 | + "", |
| 132 | + `Event received at ${new Date(webhook.createdAt).toISOString()}`, |
| 133 | + ].join("\n"); |
| 134 | +} |
| 135 | + |
| 136 | +function getEmoji(state: string): string { |
| 137 | + const map: Record<string, string> = { |
| 138 | + created: EMOJIS.pending, |
| 139 | + succeeded: EMOJIS.success, |
| 140 | + ready: EMOJIS.success, |
| 141 | + promoted: EMOJIS.promoted, |
| 142 | + error: EMOJIS.error, |
| 143 | + canceled: EMOJIS.canceled, |
| 144 | + }; |
| 145 | + return map[state] || EMOJIS.info; |
| 146 | +} |
| 147 | + |
| 148 | +function escapeHtml(text: string): string { |
| 149 | + return text |
| 150 | + .replace(/&/g, "&") |
| 151 | + .replace(/</g, "<") |
| 152 | + .replace(/>/g, ">"); |
| 153 | +} |
0 commit comments