|
1 | 1 | import { IncomingWebhook } from '@slack/webhook'; |
2 | 2 |
|
3 | | -export function truncateError(message, maxLength = 150) { |
| 3 | +export function normalizeErrorForSlack(message, maxLength = 120) { |
4 | 4 | if (!message) { |
5 | 5 | return 'No error details'; |
6 | 6 | } |
7 | | - return message.length > maxLength ? `${message.substring(0, maxLength)}...` : message; |
| 7 | + |
| 8 | + const withoutEmojiShortcodes = String(message).replace(/:[a-z0-9_+\-]+:/gi, ' '); |
| 9 | + const firstMeaningfulLine = withoutEmojiShortcodes |
| 10 | + .split(/\r?\n/) |
| 11 | + .map(line => line.trim()) |
| 12 | + .find(Boolean); |
| 13 | + const normalized = (firstMeaningfulLine || withoutEmojiShortcodes) |
| 14 | + .replace(/\s+/g, ' ') |
| 15 | + .trim(); |
| 16 | + |
| 17 | + if (!normalized) { |
| 18 | + return 'No error details'; |
| 19 | + } |
| 20 | + |
| 21 | + return normalized.length > maxLength ? `${normalized.substring(0, maxLength - 3)}...` : normalized; |
| 22 | +} |
| 23 | + |
| 24 | +export function truncateError(message, maxLength = 120) { |
| 25 | + return normalizeErrorForSlack(message, maxLength); |
| 26 | +} |
| 27 | + |
| 28 | +export function createSlackBlocks(summary, dateDisplay, options) { |
| 29 | + const { |
| 30 | + owner, |
| 31 | + repository, |
| 32 | + branch, |
| 33 | + reportTitle, |
| 34 | + topN, |
| 35 | + workflowsScanned, |
| 36 | + failedRunCount, |
| 37 | + workflowCount, |
| 38 | + } = options; |
| 39 | + |
| 40 | + const topItems = summary.slice(0, topN); |
| 41 | + const broken = topItems.filter(item => item.brokenCount > 0); |
| 42 | + const flaky = topItems.filter(item => item.brokenCount === 0 && item.flakyCount > 0); |
| 43 | + |
| 44 | + const blocks = [ |
| 45 | + { |
| 46 | + type: 'header', |
| 47 | + text: { |
| 48 | + type: 'plain_text', |
| 49 | + text: `${reportTitle} - Top ${topN}`, |
| 50 | + emoji: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + type: 'context', |
| 55 | + elements: [ |
| 56 | + { |
| 57 | + type: 'mrkdwn', |
| 58 | + text: |
| 59 | + `Period (UTC): ${dateDisplay} | Repo: ${repository} | Workflows: ${workflowsScanned.join(', ')} | ` + |
| 60 | + `Failed CI Runs: ${failedRunCount}/${workflowCount} from ${branch}` + |
| 61 | + `\nFound: ${broken.length} broken, ${flaky.length} flaky`, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + { type: 'divider' }, |
| 66 | + ]; |
| 67 | + |
| 68 | + if (topItems.length === 0) { |
| 69 | + blocks.push({ |
| 70 | + type: 'rich_text', |
| 71 | + elements: [ |
| 72 | + { |
| 73 | + type: 'rich_text_section', |
| 74 | + elements: [{ type: 'text', text: 'No flaky or broken tests found ✅' }], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }); |
| 78 | + return blocks; |
| 79 | + } |
| 80 | + |
| 81 | + if (broken.length > 0) { |
| 82 | + blocks.push({ |
| 83 | + type: 'rich_text', |
| 84 | + elements: [ |
| 85 | + { |
| 86 | + type: 'rich_text_section', |
| 87 | + elements: [ |
| 88 | + { type: 'emoji', name: 'x' }, |
| 89 | + { type: 'text', text: ' ' }, |
| 90 | + { type: 'text', text: 'Broken', style: { bold: true } }, |
| 91 | + ], |
| 92 | + }, |
| 93 | + ], |
| 94 | + }); |
| 95 | + |
| 96 | + broken.forEach((test, index) => { |
| 97 | + const globalIndex = index + 1; |
| 98 | + const fileUrl = `https://github.com/${owner}/${repository}/blob/${branch}/${test.path}`; |
| 99 | + const runUrl = |
| 100 | + test.lastBrokenRunUrl || |
| 101 | + (test.lastBrokenRunId |
| 102 | + ? `https://github.com/${owner}/${repository}/actions/runs/${test.lastBrokenRunId}` |
| 103 | + : null); |
| 104 | + const elements = [ |
| 105 | + { type: 'text', text: ` ${globalIndex}. ` }, |
| 106 | + { type: 'link', url: fileUrl, text: test.name }, |
| 107 | + { type: 'text', text: ` (${test.projectName})` }, |
| 108 | + { type: 'text', text: ` failed ${test.brokenCount}x`, style: { bold: true } }, |
| 109 | + ]; |
| 110 | + |
| 111 | + if (runUrl) { |
| 112 | + elements.push({ type: 'text', text: ' - ' }, { type: 'link', url: runUrl, text: 'run log' }); |
| 113 | + } |
| 114 | + |
| 115 | + blocks.push({ |
| 116 | + type: 'rich_text', |
| 117 | + elements: [{ type: 'rich_text_section', elements }], |
| 118 | + }); |
| 119 | + |
| 120 | + blocks.push({ |
| 121 | + type: 'rich_text', |
| 122 | + elements: [ |
| 123 | + { |
| 124 | + type: 'rich_text_section', |
| 125 | + elements: [{ type: 'text', text: ` ${truncateError(test.lastBrokenError)}`, style: { italic: true } }], |
| 126 | + }, |
| 127 | + ], |
| 128 | + }); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + if (broken.length > 0 && flaky.length > 0) { |
| 133 | + blocks.push({ type: 'divider' }); |
| 134 | + } |
| 135 | + |
| 136 | + if (flaky.length > 0) { |
| 137 | + blocks.push({ |
| 138 | + type: 'rich_text', |
| 139 | + elements: [ |
| 140 | + { |
| 141 | + type: 'rich_text_section', |
| 142 | + elements: [ |
| 143 | + { type: 'emoji', name: 'large_yellow_circle' }, |
| 144 | + { type: 'text', text: ' ' }, |
| 145 | + { type: 'text', text: 'Flaky', style: { bold: true } }, |
| 146 | + ], |
| 147 | + }, |
| 148 | + ], |
| 149 | + }); |
| 150 | + |
| 151 | + flaky.forEach((test, index) => { |
| 152 | + const globalIndex = broken.length + index + 1; |
| 153 | + const fileUrl = `https://github.com/${owner}/${repository}/blob/${branch}/${test.path}`; |
| 154 | + const runUrl = |
| 155 | + test.lastFlakyRunUrl || |
| 156 | + (test.lastFlakyRunId |
| 157 | + ? `https://github.com/${owner}/${repository}/actions/runs/${test.lastFlakyRunId}` |
| 158 | + : null); |
| 159 | + const elements = [ |
| 160 | + { type: 'text', text: ` ${globalIndex}. ` }, |
| 161 | + { type: 'link', url: fileUrl, text: test.name }, |
| 162 | + { type: 'text', text: ` (${test.projectName})` }, |
| 163 | + { type: 'text', text: ` flaky ${test.flakyCount}x`, style: { bold: true } }, |
| 164 | + ]; |
| 165 | + |
| 166 | + if (runUrl) { |
| 167 | + elements.push({ type: 'text', text: ' - ' }, { type: 'link', url: runUrl, text: 'run log' }); |
| 168 | + } |
| 169 | + |
| 170 | + blocks.push({ |
| 171 | + type: 'rich_text', |
| 172 | + elements: [{ type: 'rich_text_section', elements }], |
| 173 | + }); |
| 174 | + |
| 175 | + blocks.push({ |
| 176 | + type: 'rich_text', |
| 177 | + elements: [ |
| 178 | + { |
| 179 | + type: 'rich_text_section', |
| 180 | + elements: [{ type: 'text', text: ` ${truncateError(test.lastFlakyError)}`, style: { italic: true } }], |
| 181 | + }, |
| 182 | + ], |
| 183 | + }); |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + return blocks; |
8 | 188 | } |
9 | 189 |
|
10 | 190 | export async function sendSlackBatched(webhookUrl, blocks) { |
|
0 commit comments