|
| 1 | +import { createHmac, randomUUID } from 'node:crypto'; |
| 2 | +import { readFileSync, readdirSync } from 'node:fs'; |
| 3 | +import { resolve, dirname, basename } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +// load .env if present (no external dependencies, optional for CI) |
| 7 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 8 | +const envPath = resolve(__dirname, '..', '.env'); |
| 9 | +try { |
| 10 | + for (const line of readFileSync(envPath, 'utf-8').split('\n')) { |
| 11 | + const match = line.match(/^([^#=]+)=(.*)$/); |
| 12 | + if (match) { |
| 13 | + const key = match[1].trim(); |
| 14 | + const val = match[2].trim().replace(/^["']|["']$/g, ''); |
| 15 | + if (!process.env[key]) process.env[key] = val; |
| 16 | + } |
| 17 | + } |
| 18 | +} catch { |
| 19 | + // no .env file -- rely on environment variables (e.g. in CI) |
| 20 | +} |
| 21 | + |
| 22 | +const FUNCTION_URL = process.env.FUNCTION_URL; |
| 23 | +const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET; |
| 24 | + |
| 25 | +if (!FUNCTION_URL) { |
| 26 | + console.error('FUNCTION_URL is not set in .env'); |
| 27 | + process.exit(1); |
| 28 | +} |
| 29 | +if (!WEBHOOK_SECRET) { |
| 30 | + console.error('WEBHOOK_SECRET is not set in .env'); |
| 31 | + process.exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +// discover available events from payloads directory |
| 35 | +// filename format: <event-name>.json where dots in event name map to dots in filename |
| 36 | +// e.g. pull_request.opened.json -> event "pull_request", action "opened" |
| 37 | +// ping.json -> event "ping", no action |
| 38 | +const payloadsDir = resolve(__dirname, '..', 'tests', 'fixtures'); |
| 39 | +const EVENTS = {}; |
| 40 | +for (const file of readdirSync(payloadsDir).filter(f => f.endsWith('.json'))) { |
| 41 | + const label = basename(file, '.json'); |
| 42 | + const parts = label.split('.'); |
| 43 | + // first part before the dot is the github event name (e.g. "pull_request", "pull_request_review") |
| 44 | + // but event names can contain underscores, so we use the payload's action field to determine the split |
| 45 | + const payload = JSON.parse(readFileSync(resolve(payloadsDir, file), 'utf-8')); |
| 46 | + const eventName = payload.action ? label.slice(0, label.lastIndexOf('.')) : label; |
| 47 | + EVENTS[label] = { name: eventName, payload }; |
| 48 | +} |
| 49 | + |
| 50 | +// sign a payload the same way GitHub does |
| 51 | +function sign(payload) { |
| 52 | + return 'sha256=' + createHmac('sha256', WEBHOOK_SECRET).update(payload).digest('hex'); |
| 53 | +} |
| 54 | + |
| 55 | +// send a webhook event to the function |
| 56 | +async function sendEvent(eventName, payload) { |
| 57 | + const body = JSON.stringify(payload); |
| 58 | + const deliveryId = randomUUID(); |
| 59 | + const signature = sign(body); |
| 60 | + |
| 61 | + console.log(`Sending '${eventName}' event (delivery: ${deliveryId})...`); |
| 62 | + |
| 63 | + const response = await fetch(FUNCTION_URL, { |
| 64 | + method: 'POST', |
| 65 | + headers: { |
| 66 | + 'content-type': 'application/json', |
| 67 | + 'x-github-delivery': deliveryId, |
| 68 | + 'x-github-event': eventName, |
| 69 | + 'x-hub-signature-256': signature, |
| 70 | + }, |
| 71 | + body, |
| 72 | + }); |
| 73 | + |
| 74 | + const responseBody = await response.text(); |
| 75 | + return { status: response.status, body: responseBody }; |
| 76 | +} |
| 77 | + |
| 78 | +const MAX_RETRIES = Number(process.env.SMOKE_RETRIES) || 3; |
| 79 | +const RETRY_DELAY_MS = Number(process.env.SMOKE_RETRY_DELAY_MS) || 5000; |
| 80 | + |
| 81 | +function sleep(ms) { |
| 82 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 83 | +} |
| 84 | + |
| 85 | +async function runOne(label, eventName, payload) { |
| 86 | + for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) { |
| 87 | + try { |
| 88 | + const { status, body } = await sendEvent(eventName, payload); |
| 89 | + if (status >= 200 && status < 300) { |
| 90 | + console.log(` ${label}: OK (${status})`); |
| 91 | + return true; |
| 92 | + } |
| 93 | + console.error(` ${label}: FAILED (${status})${attempt < MAX_RETRIES ? ' - retrying...' : ''}`); |
| 94 | + if (body) console.error(` Response: ${body}`); |
| 95 | + } catch (error) { |
| 96 | + console.error(` ${label}: ERROR - ${error.message}${attempt < MAX_RETRIES ? ' - retrying...' : ''}`); |
| 97 | + } |
| 98 | + if (attempt < MAX_RETRIES) await sleep(RETRY_DELAY_MS); |
| 99 | + } |
| 100 | + return false; |
| 101 | +} |
| 102 | + |
| 103 | +async function main() { |
| 104 | + const arg = process.argv[2] || 'ping'; |
| 105 | + const labels = Object.keys(EVENTS).sort(); |
| 106 | + |
| 107 | + if (arg !== 'all' && !(arg in EVENTS)) { |
| 108 | + console.error(`Unknown event type: ${arg}`); |
| 109 | + console.error(`Supported events: ${labels.join(', ')}, all`); |
| 110 | + process.exit(1); |
| 111 | + } |
| 112 | + |
| 113 | + let failed = false; |
| 114 | + |
| 115 | + if (arg === 'all') { |
| 116 | + console.log('Running all smoke tests...\n'); |
| 117 | + for (const label of labels) { |
| 118 | + const event = EVENTS[label]; |
| 119 | + const ok = await runOne(label, event.name, event.payload); |
| 120 | + if (!ok) failed = true; |
| 121 | + } |
| 122 | + console.log(failed ? '\nSome tests failed.' : '\nAll tests passed.'); |
| 123 | + } else { |
| 124 | + const event = EVENTS[arg]; |
| 125 | + const ok = await runOne(arg, event.name, event.payload); |
| 126 | + if (!ok) failed = true; |
| 127 | + } |
| 128 | + |
| 129 | + process.exit(failed ? 1 : 0); |
| 130 | +} |
| 131 | + |
| 132 | +main(); |
0 commit comments