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