Open History to see the last commit
1// The Android half of the mobile preview comment. The android job writes the fingerprint and
2// the update; the android-build job adds the client's install link a few minutes later. Each
3// run merges what it knows into the state kept in the comment's first line.
4const MARKER = "mobile-preview-android";
5const PROJECT_ID = "d763fcb8-d37c-41ea-a773-b54a0ab4a454";
6const APP_SCHEME = "t3-code";
7
8function readState(body) {
9 const match = new RegExp(`^<!-- ${MARKER} (\\{.*\\}) -->`).exec(body ?? "");
10 if (!match) return null;
11 try {
12 const state = JSON.parse(match[1]);
13 const text = (value) => (typeof value === "string" ? value : "");
14 return {
15 fingerprint: text(state.fingerprint),
16 client: text(state.client),
17 group: text(state.group),
18 };
19 } catch {
20 return null;
21 }
22}
23
24/**
25 * New values win; a new fingerprint drops the old fingerprint's client. A build that finishes
26 * after the PR moved to another fingerprint changes nothing: its client cannot run the update
27 * the comment now shows.
28 */
29function mergeState(previous, next) {
30 const buildOnly = Boolean(next.client) && !next.group;
31 if (
32 buildOnly &&
33 previous?.fingerprint &&
34 next.fingerprint &&
35 previous.fingerprint !== next.fingerprint
36 ) {
37 return previous;
38 }
39 const fingerprint = next.fingerprint || previous?.fingerprint || "";
40 const sameFingerprint = !previous?.fingerprint || previous.fingerprint === fingerprint;
41 return {
42 fingerprint,
43 client: next.client || (sameFingerprint ? (previous?.client ?? "") : ""),
44 group: next.group || previous?.group || "",
45 };
46}
47
48function render(state, runUrl) {
49 const qr = `https://qr.expo.dev/eas-update?appScheme=${APP_SCHEME}&projectId=${PROJECT_ID}&groupId=${state.group}`;
50 const rows = [
51 `<!-- ${MARKER} ${JSON.stringify(state)} -->`,
52 "🤖 Android preview",
53 "",
54 " | 🤖 Android",
55 "--- | ---",
56 `Fingerprint | ${state.fingerprint || "n/a"}`,
57 `Dev client | ${state.client ? `[Install](${state.client})` : `Building on Blacksmith ([run](${runUrl})); this comment gets the install link when it finishes`}`,
58 ];
59 if (state.group) {
60 rows.push(
61 `Update Details | [Update Permalink](https://expo.dev/projects/${PROJECT_ID}/updates/${state.group})`,
62 `Update QR | <a href="${qr}"><img src="${qr}" width="250px" height="250px" /></a>`,
63 );
64 }
65 return rows.join("\n");
66}
67
68async function github(path, init = {}) {
69 const response = await fetch(`https://api.github.com${path}`, {
70 ...init,
71 headers: {
72 authorization: `Bearer ${process.env.GH_TOKEN}`,
73 accept: "application/vnd.github+json",
74 "content-type": "application/json",
75 ...init.headers,
76 },
77 });
78 if (!response.ok) {
79 throw new Error(
80 `GitHub ${init.method ?? "GET"} ${path}: ${response.status} ${await response.text()}`,
81 );
82 }
83 return response.json();
84}
85
86async function main() {
87 const repository = process.env.GITHUB_REPOSITORY;
88 const pr = process.env.PR;
89 if (!repository || !pr || !process.env.GH_TOKEN) {
90 throw new Error("GITHUB_REPOSITORY, PR and GH_TOKEN are required.");
91 }
92 const runUrl = `${process.env.GITHUB_SERVER_URL}/${repository}/actions/runs/${process.env.GITHUB_RUN_ID}`;
93 let existing = null;
94 for (let page = 1; existing === null; page++) {
95 const comments = await github(
96 `/repos/${repository}/issues/${pr}/comments?per_page=100&page=${page}`,
97 );
98 // Only this workflow's own comment: anyone can post the marker, and nobody else's is editable.
99 existing =
100 comments.find(
101 (comment) =>
102 comment.user?.login === "github-actions[bot]" && readState(comment.body) !== null,
103 ) ?? null;
104 if (comments.length < 100) break;
105 }
106 const state = mergeState(readState(existing?.body), {
107 fingerprint: process.env.FINGERPRINT ?? "",
108 client: process.env.CLIENT ?? "",
109 group: process.env.GROUP ?? "",
110 });
111 const body = JSON.stringify({ body: render(state, runUrl) });
112 if (existing) {
113 await github(`/repos/${repository}/issues/comments/${existing.id}`, { method: "PATCH", body });
114 } else {
115 await github(`/repos/${repository}/issues/${pr}/comments`, { method: "POST", body });
116 }
117}
118
119if (require.main === module) {
120 main().catch((error) => {
121 console.error(error);
122 process.exitCode = 1;
123 });
124}
125
126module.exports = { mergeState, readState, render };