- Release.product and requires; unique (product, version); latest version per product (latest_version stays Cross Events' latest) - Release sources per product (Releases page): repository, tag prefix, notes file. Defaults: every product from crosscode-platform plus Cross Events' v1.2.3 tags from hope-events. A product's releases are only removed when all its sources were read and found tags - "— requires accounts 1.2.0" in a notes heading: Request update answers 409 REQUIREMENT_NOT_MET until the church's install of that product is new enough; install page and Releases page show it; v2 replies carry requires - @crosscode/contracts 1.1.0 vendored (optional requires on releases) - Alerts, dashboard and install lists compare against each product's latest Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
4.9 KiB
JavaScript
111 lines
4.9 KiB
JavaScript
/**
|
|
* Releases per product (Hub 2.0) and their notes (Releases page). The highest version of a product
|
|
* is its latest. `latest_version` keeps the Cross Events latest for older code paths and the
|
|
* Settings page.
|
|
*
|
|
* Requirements: a release can need a minimum version of a sibling product (`requires`, from
|
|
* "— requires accounts 1.2.0" in WHATS-NEW.md). An install can't ask for an update until the
|
|
* church's install of each required product runs that version or later.
|
|
*/
|
|
|
|
const prisma = require('../config/db');
|
|
const { compareVersions } = require('./alerts');
|
|
const { setSettings } = require('./settings');
|
|
const { PRODUCT_NAMES } = require('./products');
|
|
|
|
const VERSION_RE = /^\d+\.\d+\.\d+$/;
|
|
const MAX_SENT = 20;
|
|
|
|
/** Newest first. */
|
|
function sortReleases(releases) {
|
|
return [...releases].sort((a, b) => compareVersions(b.version, a.version));
|
|
}
|
|
|
|
const requirementsOf = r => (Array.isArray(r.requires) ? r.requires.filter(q => q && q.product && VERSION_RE.test(q.version)) : []);
|
|
|
|
/**
|
|
* What an install on `version` is told (§3): every release newer than it, newest first — at most
|
|
* 20, so a long-neglected install still gets a reply of sensible size. `withRequires` (protocol v2)
|
|
* adds each release's requirements (contracts 1.1.0); v1 replies stay as they were.
|
|
*/
|
|
function releasesFor(releases, version, { withRequires = false } = {}) {
|
|
return sortReleases(releases)
|
|
.filter(r => !version || compareVersions(r.version, version) > 0)
|
|
.slice(0, MAX_SENT)
|
|
.map(r => {
|
|
const out = { version: r.version, releasedAt: new Date(r.releasedAt).toISOString(), urgent: !!r.urgent, notes: r.notes || '' };
|
|
const requires = requirementsOf(r);
|
|
return withRequires && requires.length ? { ...out, requires } : out;
|
|
});
|
|
}
|
|
|
|
/** { events: '2.1.1', website: '1.0.0', … } — the highest release of each product that has any. */
|
|
function latestByProduct(releases) {
|
|
const out = {};
|
|
for (const r of releases) {
|
|
const p = r.product || 'events';
|
|
if (!out[p] || compareVersions(r.version, out[p]) > 0) out[p] = r.version;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function latestVersions(db = prisma) {
|
|
return latestByProduct(await db.release.findMany({ select: { product: true, version: true } }));
|
|
}
|
|
|
|
/** Keep latest_version on the Cross Events latest (blank when there are none). Returns the map. */
|
|
async function syncLatestVersion(db = prisma) {
|
|
const latest = await latestVersions(db);
|
|
await setSettings({ latest_version: latest.events || '' });
|
|
return latest;
|
|
}
|
|
|
|
async function releasesForInstall(product, version, options = {}, db = prisma) {
|
|
return releasesFor(await db.release.findMany({ where: { product } }), version, options);
|
|
}
|
|
|
|
/**
|
|
* What updating `install` from its version to `target` needs from its siblings: the highest
|
|
* required version per product over every release it would get (so a requirement in a skipped
|
|
* release still counts). `siblings` are the church's other installs in use: [{ product, version }].
|
|
* Returns [{ product, version, current }] for the ones not met (current: null = not installed).
|
|
*/
|
|
function unmetRequirements({ releases, from, target, siblings }) {
|
|
const needed = {};
|
|
for (const r of releases) {
|
|
if (from && compareVersions(r.version, from) <= 0) continue;
|
|
if (compareVersions(r.version, target) > 0) continue;
|
|
for (const q of requirementsOf(r)) {
|
|
if (!needed[q.product] || compareVersions(q.version, needed[q.product]) > 0) needed[q.product] = q.version;
|
|
}
|
|
}
|
|
const out = [];
|
|
for (const [product, version] of Object.entries(needed)) {
|
|
const running = siblings.filter(s => s.product === product && s.version).map(s => s.version).sort((a, b) => compareVersions(b, a))[0] || null;
|
|
if (!running || compareVersions(running, version) < 0) out.push({ product, version, current: running });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** "Accounts must be updated to 1.2.0 first (it runs 1.1.0)." for each unmet requirement. */
|
|
function requirementMessage(unmet) {
|
|
return unmet.map(u => (u.current
|
|
? `${PRODUCT_NAMES[u.product] || u.product} must be updated to ${u.version} first (it runs ${u.current}).`
|
|
: `${PRODUCT_NAMES[u.product] || u.product} ${u.version} or later must be installed first.`)).join(' ');
|
|
}
|
|
|
|
/** The requirements `install` hasn't met for updating to `target` (database version). */
|
|
async function unmetRequirementsFor(install, target, db = prisma) {
|
|
if (!target) return [];
|
|
const [releases, siblings] = await Promise.all([
|
|
db.release.findMany({ where: { product: install.product } }),
|
|
db.install.findMany({ where: { customerId: install.customerId, id: { not: install.id }, releasedAt: null }, select: { product: true, version: true } }),
|
|
]);
|
|
return unmetRequirements({ releases, from: install.version, target, siblings });
|
|
}
|
|
|
|
module.exports = {
|
|
VERSION_RE, sortReleases, releasesFor, releasesForInstall, latestByProduct, latestVersions, syncLatestVersion,
|
|
unmetRequirements, unmetRequirementsFor, requirementMessage,
|
|
};
|