|
| 1 | +// notify-slack.js |
| 2 | +require("dotenv").config(); |
| 3 | +const fs = require("fs"); |
| 4 | +const axios = require("axios"); |
| 5 | + |
| 6 | +const WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL; |
| 7 | +const RESULTS_FILE = "test-results/results.json"; |
| 8 | + |
| 9 | +(async () => { |
| 10 | + try { |
| 11 | + if (!WEBHOOK_URL) { |
| 12 | + console.error("❌ Falta SLACK_WEBHOOK_URL en el archivo .env"); |
| 13 | + process.exit(1); |
| 14 | + } |
| 15 | + |
| 16 | + if (!fs.existsSync(RESULTS_FILE)) { |
| 17 | + console.error( |
| 18 | + `❌ No se encontró el archivo de resultados: ${RESULTS_FILE}` |
| 19 | + ); |
| 20 | + process.exit(1); |
| 21 | + } |
| 22 | + |
| 23 | + const results = JSON.parse(fs.readFileSync(RESULTS_FILE, "utf-8")); |
| 24 | + |
| 25 | + // Extrae resumen general |
| 26 | + const total = results.stats?.total || 0; |
| 27 | + const passed = results.stats?.expected || 0; |
| 28 | + const failed = results.stats?.unexpected || 0; |
| 29 | + const skipped = results.stats?.skipped || 0; |
| 30 | + const duration = results.stats?.duration |
| 31 | + ? (results.stats.duration / 1000).toFixed(1) |
| 32 | + : "N/A"; |
| 33 | + |
| 34 | + // Determina estado general |
| 35 | + const statusEmoji = failed > 0 ? "❌" : "✅"; |
| 36 | + const statusText = failed > 0 ? "Con fallos" : "Todo correcto"; |
| 37 | + |
| 38 | + const message = { |
| 39 | + text: `${statusEmoji} *Resultados de pruebas Playwright*`, |
| 40 | + attachments: [ |
| 41 | + { |
| 42 | + color: failed > 0 ? "#FF4D4F" : "#36A64F", |
| 43 | + fields: [ |
| 44 | + { title: "Estado general", value: statusText, short: true }, |
| 45 | + { title: "Total de tests", value: `${total}`, short: true }, |
| 46 | + { title: "✔️ Pasados", value: `${passed}`, short: true }, |
| 47 | + { title: "❌ Fallados", value: `${failed}`, short: true }, |
| 48 | + { title: "⏭️ Omitidos", value: `${skipped}`, short: true }, |
| 49 | + { title: "🕐 Duración", value: `${duration}s`, short: true }, |
| 50 | + ], |
| 51 | + footer: "Playwright QA Bot", |
| 52 | + ts: Math.floor(Date.now() / 1000), |
| 53 | + }, |
| 54 | + ], |
| 55 | + }; |
| 56 | + |
| 57 | + await axios.post(WEBHOOK_URL, message); |
| 58 | + |
| 59 | + console.log("✅ Resultados enviados a Slack correctamente"); |
| 60 | + } catch (error) { |
| 61 | + console.error("⚠️ Error al enviar resultados a Slack:", error.message); |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | +})(); |
0 commit comments