Play 'Live' only when the public store page proves it: play status checks https://play.google.com/store/apps/details?id=<pkg> (no auth, 4 s timeout, cached 5 min; publicPage in the JSON), a completed production release with a 404 page is 'Sent to Google (in review or not yet published)', 200 is live for the first release or when the page shows the versionName, otherwise 'Released to production (Google may still be reviewing)', offline is unknown; new phases sent / released in the desktop Releases tab, Portfolio, release status | plan; versions matched across the stores without trailing .0 parts (App Store 1.0 and Play 1.0.0 are one row and one train, each side shows its own string); tests/play-public-version-match.test.ts, desktop boot check release-state cases (page 404 and 200) (pitfall 56)

This commit is contained in:
CRTkafa 2026-10-13 20:46:48 +08:00
parent aa72204a06
commit be9ff8b788
18 changed files with 436 additions and 30 deletions

View file

@ -23,6 +23,25 @@ export function cmpVersion(a: string, b: string): number {
return 0;
}
/** Matching key of a version: numeric parts without trailing zero parts, so 1 == 1.0 == 1.0.0 ("1"), 2.10 != 2.1.
* A version with non-numeric parts keeps its own spelling. The stores' strings are shown as they are; this only joins. */
export function versionKey(v: string): string {
const parts = v.trim().split('.');
if (!parts.every(p => /^\d+$/.test(p))) return v.trim();
const n = parts.map(p => String(parseInt(p, 10)));
while (n.length > 1 && n.at(-1) === '0') n.pop();
return n.join('.');
}
/** The same release on both stores (App Store "1.0" and Play "1.0.0"). */
export const sameVersion = (a: string | null | undefined, b: string | null | undefined): boolean => !!a && !!b && versionKey(a) === versionKey(b);
/** The spelling a joined row shows: the longest of the stores' strings (1.0.0 over 1.0), the first one on a tie. */
export function displayVersion(vs: (string | null | undefined)[]): string {
const xs = vs.filter((x): x is string => !!x);
return xs.reduce((a, b) => (b.split('.').length > a.split('.').length ? b : a), xs[0] ?? '');
}
/** App Store version states (appStoreState / appVersionState) -> phase. */
export function applePhase(state: string): ReleasePhase {
if (/^(READY_FOR_SALE|READY_FOR_DISTRIBUTION)$/.test(state)) return 'live';
@ -85,22 +104,56 @@ export function testflightReleases(builds: J[], buildVersions: Record<string, st
/** Leading dotted version of a Play release name ("2.3.0 (45)" -> "2.3.0"). */
export const versionOfName = (name: unknown): string | null => (typeof name === 'string' ? name.match(/\d+(?:\.\d+){0,3}/)?.[0] ?? null : null);
/**
* Play tracks (`play status --json`) -> releases. The Developer API has no review state: a completed production release
* is only "live" when the public store page proves it (`publicPage` in the status JSON): 404 = sent to Google (in
* review or not yet published); 200 = live for the first release or when the page shows this versionName, otherwise
* "released" (an update Google may still be reviewing); page not read = "released" with publicPage unknown.
*/
export function playReleases(data: unknown): StoreRelease[] {
const out: StoreRelease[] = [];
for (const t of ((data as J)?.tracks ?? []) as J[]) {
for (const r of (t?.releases ?? []) as J[]) {
const codes = ((r.versionCodes ?? []) as unknown[]).map(Number).filter(Number.isFinite);
const page = (data as J)?.publicPage as { state?: string; versionName?: string | null } | undefined;
const pageState = page?.state === 'public' || page?.state === 'notPublic' ? page.state : 'unknown';
const raw = (data as J)?.tracks;
const tracks: J[] = Array.isArray(raw) ? raw : [];
const codesOf = (r: J): number[] => (Array.isArray(r?.versionCodes) ? (r.versionCodes as unknown[]) : []).map(Number).filter(Number.isFinite);
const rels = (t: J): J[] => (Array.isArray(t?.releases) ? t.releases : []);
const allCodes = tracks.flatMap(t => rels(t).flatMap(codesOf));
for (const t of tracks) {
for (const r of rels(t)) {
const codes = codesOf(r);
const production = t.track === 'production';
const status = String(r.status ?? 'unknown');
const phase: ReleasePhase = !production ? (status === 'draft' ? 'draft' : 'testing')
: status === 'completed' ? 'live' : status === 'inProgress' ? 'rollout' : status === 'halted' ? 'halted' : status === 'draft' ? 'draft' : 'other';
out.push({ store: 'android', version: versionOfName(r.name), build: codes.length ? String(Math.max(...codes)) : null, state: status, phase, fraction: typeof r.userFraction === 'number' ? r.userFraction : null, track: String(t.track) });
const version = versionOfName(r.name);
const rel: StoreRelease = { store: 'android', version, build: codes.length ? String(Math.max(...codes)) : null, state: status, phase: 'other', fraction: typeof r.userFraction === 'number' ? r.userFraction : null, track: String(t.track) };
rel.phase = !production ? (status === 'draft' ? 'draft' : 'testing')
: status === 'inProgress' ? 'rollout' : status === 'halted' ? 'halted' : status === 'draft' ? 'draft' : 'other';
if (production && status === 'completed') {
const low = codes.length ? Math.min(...codes) : Infinity;
const first = !allCodes.some(c => c < low); // nothing older on any track: the app's first release
rel.phase = pageState === 'notPublic' ? 'sent'
: pageState === 'public' && (first || sameVersion(page?.versionName, version)) ? 'live'
: 'released';
rel.publicPage = pageState;
}
out.push(rel);
}
}
return out;
}
const PHASE_RANK: Record<ReleasePhase, number> = { live: 0, rollout: 1, halted: 2, approved: 3, review: 4, rejected: 5, testing: 6, draft: 7, other: 8 };
/** Plain words for a Play production release's phase (CLI text; the desktop has the same words in i18n). */
export function playReleaseLabel(r: StoreRelease): string {
if (r.phase === 'sent') return 'Sent to Google (in review or not yet published)';
if (r.phase === 'released') return r.publicPage === 'unknown' ? 'Released to production (public store page not readable: live state unknown)' : 'Released to production (Google may still be reviewing)';
if (r.phase === 'live') return 'Live';
return r.state;
}
export const PHASE_RANK: Record<ReleasePhase, number> = { live: 0, rollout: 1, halted: 2, released: 3, approved: 4, review: 5, sent: 6, rejected: 7, testing: 8, draft: 9, other: 10 };
/** Phases where the version reaches (or may reach) users. */
export const REACHING: ReadonlySet<ReleasePhase> = new Set<ReleasePhase>(['live', 'rollout', 'halted', 'released']);
const pick = (rs: StoreRelease[]): StoreRelease | null => [...rs].sort((a, b) => PHASE_RANK[a.phase] - PHASE_RANK[b.phase])[0] ?? null;
/**
@ -110,8 +163,11 @@ const pick = (rs: StoreRelease[]): StoreRelease | null => [...rs].sort((a, b) =>
*/
export function releaseTrains(ios: StoreRelease[], android: StoreRelease[]): { current: ReleaseTrain | null; next: ReleaseTrain | null } {
const all = [...ios, ...android].filter((r): r is StoreRelease & { version: string } => !!r.version);
const train = (v: string): ReleaseTrain => ({ version: v, ios: pick(all.filter(r => r.store === 'ios' && r.version === v)), android: pick(all.filter(r => r.store === 'android' && r.version === v)) });
const live = all.filter(r => r.phase === 'live' || r.phase === 'rollout' || r.phase === 'halted').map(r => r.version).sort(cmpVersion).at(-1) ?? null;
const train = (v: string): ReleaseTrain => {
const i = pick(all.filter(r => r.store === 'ios' && sameVersion(r.version, v))), a = pick(all.filter(r => r.store === 'android' && sameVersion(r.version, v)));
return { version: displayVersion([a?.version, i?.version, v]), ios: i, android: a };
};
const live = all.filter(r => REACHING.has(r.phase)).map(r => r.version).sort(cmpVersion).at(-1) ?? null;
const above = all.filter(r => !live || cmpVersion(r.version, live) > 0).map(r => r.version).sort(cmpVersion).at(-1) ?? null;
return { current: live ? train(live) : null, next: above ? train(above) : null };
}