|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | +Creates or updates the sticky PR comment (same marker as post-pr-report.js) |
| 4 | +to indicate that CI testing has started. Designed to run early in workflows |
| 5 | +so contributors immediately see progress. |
| 6 | +
|
| 7 | +Auth options (same as post-pr-report.js): |
| 8 | + - Preferred: GitHub App via env CI_PR_BOT_APP_ID, CI_PR_BOT_INSTALLATION_ID, CI_PR_BOT_PRIVATE_KEY |
| 9 | + - Fallback: GH_PR_COMMENT_TOKEN (or GITHUB_TOKEN/GH_TOKEN) |
| 10 | +
|
| 11 | +Env expected from CircleCI: |
| 12 | + - CIRCLE_PROJECT_SLUG (or CIRCLE_PROJECT_USERNAME + CIRCLE_PROJECT_REPONAME) |
| 13 | + - CIRCLE_BUILD_NUM (for context, optional) |
| 14 | + - CIRCLE_PULL_REQUESTS or CIRCLE_PULL_REQUEST (PR URL) OR CIRCLE_SHA1 (to resolve PR) |
| 15 | +*/ |
| 16 | + |
| 17 | +const { createAppAuth } = require('@octokit/auth-app'); |
| 18 | + |
| 19 | +// Prefer descriptive env var names; fall back to legacy |
| 20 | +const GH_TOKEN = process.env.GH_PR_COMMENT_TOKEN || process.env.GITHUB_TOKEN || process.env.GH_TOKEN || ''; |
| 21 | +const APP_ID_ENV = process.env.CI_PR_BOT_APP_ID || process.env.APP_ID; |
| 22 | +const INSTALLATION_ID_ENV = process.env.CI_PR_BOT_INSTALLATION_ID || process.env.INSTALLATION_ID; |
| 23 | +const APP_PRIVATE_KEY_ENV = process.env.CI_PR_BOT_PRIVATE_KEY || process.env.APP_PRIVATE_KEY; |
| 24 | +const HAS_APP_CREDS = !!(APP_ID_ENV && INSTALLATION_ID_ENV && APP_PRIVATE_KEY_ENV); |
| 25 | + |
| 26 | +const SLUG = process.env.CIRCLE_PROJECT_USERNAME && process.env.CIRCLE_PROJECT_REPONAME |
| 27 | + ? `gh/${process.env.CIRCLE_PROJECT_USERNAME}/${process.env.CIRCLE_PROJECT_REPONAME}` |
| 28 | + : (process.env.CIRCLE_PROJECT_SLUG || ''); |
| 29 | +const PR_URLS = (process.env.CIRCLE_PULL_REQUESTS || process.env.CIRCLE_PULL_REQUEST || '').split(',').map(s=>s.trim()).filter(Boolean); |
| 30 | +const SHA = process.env.CIRCLE_SHA1 || ''; |
| 31 | +const MARKER = '<!-- remix-e2e-report -->'; |
| 32 | +const STATUS_CONTEXT = 'remix/e2e-report'; |
| 33 | +const REPORT_SET_STATUS = process.env.REPORT_SET_STATUS === '1'; |
| 34 | + |
| 35 | +function exit(msg) { console.error(`[post-pr-started] ${msg}`); process.exit(2); } |
| 36 | +function log(...a){ console.log('[post-pr-started]', ...a); } |
| 37 | + |
| 38 | +if (!HAS_APP_CREDS && !GH_TOKEN) exit('Missing GitHub auth: set GH_PR_COMMENT_TOKEN or CI_PR_BOT_* app credentials'); |
| 39 | +if (!SLUG) exit('Missing CircleCI slug env'); |
| 40 | + |
| 41 | +function formatRunTime() { |
| 42 | + const now = new Date(); |
| 43 | + return now.toLocaleString('en-US', { |
| 44 | + weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', |
| 45 | + hour: '2-digit', minute: '2-digit', timeZoneName: 'short' |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +(async () => { |
| 50 | + const { owner, repo } = parseSlug(SLUG); |
| 51 | + const prNumber = await resolvePrNumber(owner, repo, PR_URLS, SHA); |
| 52 | + if (!prNumber) { |
| 53 | + log('Cannot resolve PR number from env; skipping comment update.'); |
| 54 | + process.exit(0); |
| 55 | + } |
| 56 | + |
| 57 | + // Fetch existing comments to find sticky |
| 58 | + const existing = await gh(`GET /repos/${owner}/${repo}/issues/${prNumber}/comments?per_page=100`); |
| 59 | + const mine = (existing || []).find(c => typeof c.body === 'string' && c.body.includes(MARKER)); |
| 60 | + |
| 61 | + const runTime = formatRunTime(); |
| 62 | + const startedBody = [ |
| 63 | + MARKER, |
| 64 | + '🟡 CI: tests have started. Waiting for results…', |
| 65 | + '', |
| 66 | + `_Last update: ${runTime}_`, |
| 67 | + '', |
| 68 | + '_This comment will be updated automatically once results are available._' |
| 69 | + ].join('\n'); |
| 70 | + |
| 71 | + if (mine && mine.id) { |
| 72 | + await gh(`PATCH /repos/${owner}/${repo}/issues/comments/${mine.id}`, { body: startedBody }); |
| 73 | + log(`Updated sticky PR comment #${mine.id} to "started" state`); |
| 74 | + } else { |
| 75 | + const created = await gh(`POST /repos/${owner}/${repo}/issues/${prNumber}/comments`, { body: startedBody }); |
| 76 | + log(`Created sticky PR comment id=${created.id}`); |
| 77 | + } |
| 78 | + |
| 79 | + if (REPORT_SET_STATUS && SHA) { |
| 80 | + await gh(`POST /repos/${owner}/${repo}/statuses/${SHA}`, { |
| 81 | + state: 'pending', |
| 82 | + description: 'E2E tests running', |
| 83 | + context: STATUS_CONTEXT |
| 84 | + }); |
| 85 | + log(`Set commit status ${STATUS_CONTEXT}: pending`); |
| 86 | + } |
| 87 | +})().catch(e => { console.error(e); process.exit(1); }); |
| 88 | + |
| 89 | +function parseSlug(slug) { |
| 90 | + const m = String(slug).match(/^(?:gh|github)\/([^/]+)\/([^/]+)$/); |
| 91 | + if (!m) exit(`Bad slug: ${slug}`); |
| 92 | + return { owner: m[1], repo: m[2] }; |
| 93 | +} |
| 94 | + |
| 95 | +async function resolvePrNumber(owner, repo, prUrls, sha) { |
| 96 | + for (const u of prUrls) { |
| 97 | + const m = String(u).trim().match(/\/pull\/(\d+)/); |
| 98 | + if (m) return Number(m[1]); |
| 99 | + } |
| 100 | + if (!sha) return null; |
| 101 | + const res = await gh(`GET /repos/${owner}/${repo}/commits/${sha}/pulls`, null, |
| 102 | + { accept: 'application/vnd.github.groot-preview+json' }); |
| 103 | + if (Array.isArray(res) && res[0]?.number) return res[0].number; |
| 104 | + return null; |
| 105 | +} |
| 106 | + |
| 107 | +async function gh(pathname, body, extraHeaders) { |
| 108 | + const [method, endpoint] = pathname.includes(' ') ? pathname.split(' ', 2) : ['GET', pathname]; |
| 109 | + const authHeader = await getAuthHeader(); |
| 110 | + const res = await fetch(`https://api.github.com${endpoint}`, { |
| 111 | + method, |
| 112 | + headers: { |
| 113 | + Authorization: authHeader, |
| 114 | + 'Content-Type': 'application/json', |
| 115 | + ...(extraHeaders || {}) |
| 116 | + }, |
| 117 | + body: body ? JSON.stringify(body) : undefined |
| 118 | + }); |
| 119 | + if (!res.ok) { |
| 120 | + const t = await res.text(); |
| 121 | + throw new Error(`GitHub ${res.status} ${endpoint}: ${t}`); |
| 122 | + } |
| 123 | + return res.json(); |
| 124 | +} |
| 125 | + |
| 126 | +async function getAuthHeader() { |
| 127 | + const appId = APP_ID_ENV; |
| 128 | + const instId = INSTALLATION_ID_ENV; |
| 129 | + let pk = APP_PRIVATE_KEY_ENV; |
| 130 | + |
| 131 | + if (appId && instId && pk) { |
| 132 | + // Normalize private key newlines |
| 133 | + if (pk.includes('\\n') && !pk.includes('\n')) pk = pk.replace(/\\n/g, '\n'); |
| 134 | + if (!pk.includes('-----BEGIN')) { |
| 135 | + throw new Error('Invalid private key format: missing PEM headers.'); |
| 136 | + } |
| 137 | + const auth = createAppAuth({ appId: String(appId), privateKey: String(pk), installationId: String(instId) }); |
| 138 | + const { token } = await auth({ type: 'installation' }); |
| 139 | + return `token ${token}`; |
| 140 | + } |
| 141 | + if (!GH_TOKEN) throw new Error('GH_PR_COMMENT_TOKEN missing (or configure CI_PR_BOT_* app credentials)'); |
| 142 | + return `token ${GH_TOKEN}`; |
| 143 | +} |
0 commit comments