// Pure helpers for `scripts/promote-release.ts`, which checks a release before the promotion // workflow moves `latest` to it. All registry and file I/O lives in that script. The same shape // as prisma/orm's scripts/promote-release-utils.ts, plus the check that the ORM and Composer // versions a CLI release pins are already `latest`. import { compareVersions } from "./update-product-versions.mjs"; export interface ReleasePackage { readonly name: string; readonly version: string; } export interface RegistryState { readonly versions: readonly string[]; readonly latest: string | undefined; } /** An ORM or Composer package the release pins, and that package's current `latest`. */ export interface ProductPin { readonly name: string; readonly pinned: string; readonly latest: string | undefined; } export interface Move extends ReleasePackage { readonly from: string | undefined; /** The move takes `latest` to an older version than the one it is on. */ readonly back: boolean; } export interface Promotion { /** Packages whose `latest` moves to the release's version. */ readonly moves: readonly Move[]; /** Packages whose `latest` is already at the release's version. */ readonly unchanged: readonly ReleasePackage[]; } /** What the promote job moves. */ export interface PromotionPlan { readonly version: string; readonly packages: readonly ReleasePackage[]; } export class PromotionError extends Error {} const EMPTY: RegistryState = { versions: [], latest: undefined }; const NOT_FOUND = /E404|404 Not Found/; const DEVELOPMENT_BUILD = /-dev\.\d+$/; const EXACT_VERSION = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/; const PRODUCT_PIN = /^@prisma\/(orm|composer)/; /** * A package's state from the output of `npm view versions dist-tags --json`. A package * the registry does not have is empty; any other failure stops the promotion. */ export function registryStateFrom( name: string, result: { status: number | null; stdout: string; stderr: string }, ): RegistryState { if (result.status !== 0) { if (NOT_FOUND.test(`${result.stdout}${result.stderr}`)) return EMPTY; throw new PromotionError( `Could not read ${name} from the registry:\n${result.stderr}`, ); } if (result.stdout.trim() === "") return EMPTY; const view: { versions?: string | string[]; "dist-tags"?: Record; } = JSON.parse(result.stdout); const versions = view.versions ?? []; return { versions: Array.isArray(versions) ? versions : [versions], latest: view["dist-tags"]?.latest, }; } /** Why the release data checked out cannot be the release asked for, or `undefined` when it is. */ export function releaseRootMismatch( rootVersion: string, requested: string, ): string | undefined { if (rootVersion === requested) return undefined; return `The release data is release ${rootVersion}, not ${requested}. Check out the release's tag, v${requested}, as the release data, and run again.`; } /** * What promoting a release does to each package. Refuses, naming them: * - when any package's version is not on the registry, so a promotion changes all packages or none; * - when a move takes `latest` back to an older version, unless `allowDowngrade`; * - when a pinned ORM or Composer version is newer than that package's `latest`, or has no * `latest`: `npm install prisma` would get a CLI ahead of the products users install; * - when a pinned version is older than that package's `latest`, except in a rollback. * A `latest` on a development build is not a release, so moving off it is never a move back. */ export function planPromotion(options: { packages: readonly ReleasePackage[]; registry: ReadonlyMap; pins: readonly ProductPin[]; allowDowngrade: boolean; }): Promotion { const packages = [...options.packages].sort((a, b) => a.name < b.name ? -1 : 1, ); const stateOf = (name: string) => options.registry.get(name) ?? EMPTY; const missing = packages.filter( ({ name, version }) => !stateOf(name).versions.includes(version), ); if (missing.length > 0) { throw new PromotionError( [ "These packages are not on the registry at the release version, so latest moves for none of them:", ...missing.map(({ name, version }) => ` ${name}@${version}`), "If the release was published only minutes ago, the registry may still be catching up: wait a few minutes and run again.", 'Otherwise publish the release first: rerun its "Publish to npm" run, or dispatch that workflow from main with dist-tag=next and dry-run=false.', ].join("\n"), ); } const moves: Move[] = []; const unchanged: ReleasePackage[] = []; for (const pkg of packages) { const from = stateOf(pkg.name).latest; if (from === pkg.version) { unchanged.push(pkg); continue; } const back = from !== undefined && !DEVELOPMENT_BUILD.test(from) && compareVersions(pkg.version, from) < 0; moves.push({ ...pkg, from, back }); } const backwards = moves.filter(({ back }) => back); if (backwards.length > 0 && !options.allowDowngrade) { throw new PromotionError( [ "Promoting this release would move latest back to an older version for these packages:", ...backwards.map( ({ name, version, from }) => ` ${name}: ${from} -> ${version}`, ), "To roll latest back on purpose, run the promotion again with allow-downgrade on.", ].join("\n"), ); } const pinProblems = productPinProblems( options.pins, // A rollback, or a repeat run of one that already finished: nothing moves forward. options.allowDowngrade && moves.every(({ back }) => back), ); if (pinProblems.length > 0) { throw new PromotionError(pinProblems.join("\n")); } return { moves, unchanged }; } function productPinProblems( pins: readonly ProductPin[], rollback: boolean, ): string[] { const problems: string[] = []; for (const { name, pinned, latest } of pins) { if (!EXACT_VERSION.test(pinned)) { problems.push( `The release pins ${name} as "${pinned}", which is not an exact version, so it cannot be compared with latest.`, ); } else if (latest === undefined || compareVersions(pinned, latest) > 0) { problems.push( `The release pins ${name}@${pinned}, but its latest is ${latest ?? "unset"}. Promote the ORM and Composer releases first.`, ); } else if (!rollback && compareVersions(pinned, latest) < 0) { problems.push( `The release pins ${name}@${pinned}, older than its latest, ${latest}. Promote a CLI release that pins ${latest}.`, ); } } return problems; } /** * The ORM and Composer versions the release pins, from the dependencies of its `prisma` and * `@prisma/cli` manifests. Both packages ship the same CLI, so they must pin the same versions. */ export function productPinsOf( manifests: readonly { readonly name: string; readonly dependencies?: Readonly>; }[], ): Map { const pins = new Map(); const differ: string[] = []; for (const manifest of manifests) { for (const [name, version] of Object.entries(manifest.dependencies ?? {})) { if (!PRODUCT_PIN.test(name)) continue; const seen = pins.get(name); if (seen !== undefined && seen !== version) { differ.push(` ${name}: ${seen} and ${version}`); } pins.set(name, version); } } if (differ.length > 0) { throw new PromotionError( ["prisma and @prisma/cli pin different versions of:", ...differ].join( "\n", ), ); } return pins; } export function formatPromotion(promotion: Promotion): string { return [ ...promotion.moves.map( ({ name, version, from, back }) => `${name}: latest moves ${back ? "back " : ""}${from ?? "(none)"} -> ${version}`, ), ...promotion.unchanged.map( ({ name, version }) => `${name}: latest is already ${version}`, ), `${promotion.moves.length} to move, ${promotion.unchanged.length} already there.`, ].join("\n"); } export function promotionPlan( version: string, promotion: Promotion, ): PromotionPlan { return { version, packages: [...promotion.moves, ...promotion.unchanged] .map(({ name, version: packageVersion }) => ({ name, version: packageVersion, })) .sort((a, b) => (a.name < b.name ? -1 : 1)), }; }