GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — t3code/.github/scripts/mobile-preview-comment.cjs at main · ScriptedAlchemy/t3code Expand file tree/ Copy pathmobile-preview-comment.cjsMore file actions 157 lines (150 loc) · 5.48 KB / Copy pathmobile-preview-comment.cjsFile metadata and controls157 lines (150 loc) · 5.48 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 const PROJECT_ID = "d763fcb8-d37c-41ea-a773-b54a0ab4a454"; const APP_SCHEME = "t3-code"; function readState(body) { const match = /^<!-- mobile-preview(?:-(android|ios))? (\{.*\}) -->/.exec(body ?? ""); if (!match) return null; try { const parsed = JSON.parse(match[2]); const state = match[1] ? { [match[1]]: parsed } : parsed; const text = (value) => (typeof value === "string" ? value : ""); return Object.fromEntries( ["android", "ios"] .filter((platform) => state?.[platform]) .map((platform) => [ platform, { fingerprint: text(state[platform].fingerprint), client: text(state[platform].client), group: text(state[platform].group), }, ]), ); } catch { return null; } } /** * New values win; a new fingerprint drops the old fingerprint's client. A build that finishes * after the PR moved to another fingerprint changes nothing: its client cannot run the update * the comment now shows. */ function mergeState(previous, next) { if (!next.client && !next.group) return previous; const buildOnly = Boolean(next.client) && !next.group; if ( buildOnly && previous?.fingerprint && next.fingerprint && previous.fingerprint !== next.fingerprint ) { return previous; } const fingerprint = next.fingerprint || previous?.fingerprint || ""; const sameFingerprint = !previous?.fingerprint || previous.fingerprint === fingerprint; return { fingerprint, client: next.client || (sameFingerprint ? (previous?.client ?? "") : ""), group: next.group || previous?.group || "", }; } function render(state, runUrl) { const platforms = ["android", "ios"]; const cells = (value) => platforms.map((platform) => value(state[platform])); const rows = [ `<!-- mobile-preview ${JSON.stringify(state)} -->`, "🚀 Mobile preview", "", " | 🤖 Android | 🍎 iOS", "--- | --- | ---", `Fingerprint | ${cells((entry) => entry?.fingerprint || "n/a").join(" | ")}`, `Dev client | ${cells((entry) => (entry?.client ? `[Install](${entry.client})` : entry?.fingerprint ? `Client unavailable ([build run](${runUrl}))` : "n/a")).join(" | ")}`, `Update Details | ${cells((entry) => (entry?.group ? `[Update Permalink](https://expo.dev/projects/${PROJECT_ID}/updates/${entry.group})` : "n/a")).join(" | ")}`, `Update QR | ${cells((entry) => { if (!entry?.group) return "n/a"; const qr = `https://qr.expo.dev/eas-update?appScheme=${APP_SCHEME}&projectId=${PROJECT_ID}&groupId=${entry.group}`; return `<a href="${qr}"><img src="${qr}" width="250px" height="250px" /></a>`; }).join(" | ")}`, ]; return rows.join("\n"); } async function github(path, init = {}) { const response = await fetch(`https://api.github.com${path}`, { ...init, headers: { authorization: `Bearer ${process.env.GH_TOKEN}`, accept: "application/vnd.github+json", "content-type": "application/json", ...init.headers, }, }); if (!response.ok) { throw new Error( `GitHub ${init.method ?? "GET"} ${path}: ${response.status} ${await response.text()}`, ); } return response.status === 204 ? null : response.json(); } async function main({ env = process.env, request = github } = {}) { const repository = env.GITHUB_REPOSITORY; const pr = env.PR; if (!repository || !pr || !env.GH_TOKEN) { throw new Error("GITHUB_REPOSITORY, PR and GH_TOKEN are required."); } if (!env.ANDROID_FINGERPRINT && !env.IOS_FINGERPRINT) return; if (env.PR_HEAD_SHA) { const pull = await request(`/repos/${repository}/pulls/${pr}`); if (pull.head.sha !== env.PR_HEAD_SHA) return; } const runUrl = `${env.GITHUB_SERVER_URL}/${repository}/actions/runs/${env.GITHUB_RUN_ID}`; const previews = []; for (let page = 1; ; page++) { const comments = await request( `/repos/${repository}/issues/${pr}/comments?per_page=100&page=${page}`, ); previews.push( ...comments.filter( (comment) => comment.user?.login === "github-actions[bot]" && (readState(comment.body) !== null || comment.body?.startsWith( `<!-- continuous-deploy-fingerprint-projectId:${PROJECT_ID} -->`, )), ), ); if (comments.length < 100) break; } const existing = previews.find((comment) => comment.body.startsWith("<!-- mobile-preview ")) ?? previews[0]; const state = Object.assign( {}, ...previews.map((comment) => readState(comment.body)), readState(existing?.body), ); for (const platform of ["android", "ios"]) { const prefix = platform.toUpperCase(); const next = { fingerprint: env[`${prefix}_FINGERPRINT`] ?? "", client: env[`${prefix}_CLIENT`] ?? "", group: env[`${prefix}_GROUP`] ?? "", }; if (next.fingerprint) state[platform] = mergeState(state[platform], next); } const body = JSON.stringify({ body: render(state, runUrl) }); if (existing) { await request(`/repos/${repository}/issues/comments/${existing.id}`, { method: "PATCH", body }); } else { await request(`/repos/${repository}/issues/${pr}/comments`, { method: "POST", body }); } for (const comment of previews) { if (comment.id !== existing.id) { await request(`/repos/${repository}/issues/comments/${comment.id}`, { method: "DELETE" }); } } } if (require.main === module) { main().catch((error) => { console.error(error); process.exitCode = 1; }); } module.exports = { main, mergeState, readState, render }; |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |