Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/beta-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ For each module, the script:
- If a beta tag exists and its base version >= latest, use the beta tag as base
- If the beta tag's base version < latest, start a new prerelease from latest
- If no beta tag exists, create one from latest
3. Computes the next version via `semver.inc(prevTag, 'prerelease', preid)`
3. Computes a candidate via `semver.inc(prevTag, 'prerelease', preid)`
4. Checks the exact package/version endpoint and keeps incrementing until the candidate is not already published

Example: `8.2.1-beta.1009` → `8.2.1-beta.1010`

Expand Down
31 changes: 21 additions & 10 deletions scripts/prepare-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Usage: npx tsx scripts/prepare-release.ts [preid] --scope [scope] --root-dir [dir]
*/
import assert from 'node:assert';
import { readFileSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { inc, lt } from 'semver';
import yargs from 'yargs';
Expand All @@ -23,6 +23,8 @@ import {
getLernaModules,
changeScopeInFile,
setDependencyVersion,
findFirstUnpublishedVersion,
getDistTagsCache,
DistTags,
LernaModule,
} from './prepareRelease';
Expand Down Expand Up @@ -94,12 +96,13 @@ function writeModulePackageJson(module: LernaModule, json: any): void {
* @param {LernaModule[]} allModules - All modules for dependency updates
* @returns {String|undefined} - The new version if set, undefined otherwise
*/
function incrementVersionsForModuleLocation(
async function incrementVersionsForModuleLocation(
preid: string,
module: LernaModule,
tags: DistTags | undefined,
allModules: LernaModule[],
): string | undefined {
probeRegistry: boolean,
): Promise<string | undefined> {
const json = readModulePackageJson(module);

let prevTag: string | undefined = undefined;
Expand All @@ -111,17 +114,22 @@ function incrementVersionsForModuleLocation(
prevTag = lt(version[0], latest[0])
? `${tags.latest}-${preid}`
: tags[preid];
} else {
} else if (tags.latest) {
prevTag = `${tags.latest}-${preid}`;
}
}

if (prevTag) {
const next = inc(prevTag, 'prerelease', undefined, preid);
if (prevTag || (tags && Object.keys(tags).length === 0)) {
const initialCandidate = prevTag
? inc(prevTag, 'prerelease', undefined, preid)
: json.version;
assert(
typeof next === 'string',
typeof initialCandidate === 'string',
`Failed to increment version for ${json.name} prevTag=${prevTag}`,
);
const next = probeRegistry
? await findFirstUnpublishedVersion(json.name, initialCandidate, preid)
: initialCandidate;
console.log(`Setting next version for ${json.name} to ${next}`);
json.version = next;
writeModulePackageJson(module, json);
Expand Down Expand Up @@ -159,22 +167,25 @@ async function incrementVersions(
preid: string,
lernaModules: LernaModule[],
): Promise<void> {
const cachePath = getDistTagsCache();
const probeRegistry = !(cachePath && existsSync(cachePath));
const distTags = await getDistTagsForModules(lernaModules);

for (const m of lernaModules) {
try {
incrementVersionsForModuleLocation(
await incrementVersionsForModuleLocation(
preid,
m,
distTags.get(m),
lernaModules,
probeRegistry,
);
} catch (e) {
// it's not necessarily a blocking error. Let lerna try and publish anyways
console.warn(
console.error(
`Couldn't set next version for ${m.name} at ${m.location}`,
e,
);
throw e;
}
}
}
Expand Down
52 changes: 46 additions & 6 deletions scripts/prepareRelease/distTags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync, writeFileSync, existsSync } from 'fs';
import path from 'path';
import { inc } from 'semver';
import { LernaModule } from './getLernaModules';

export type DistTags = Record<string, string>;
Expand All @@ -11,12 +12,56 @@ export async function getDistTags(packageName: string): Promise<DistTags> {
console.log(`Fetching dist tags for ${packageName}`);
const url = `https://registry.npmjs.org/-/package/${packageName}/dist-tags`;
const response = await fetch(url);
if (response.status === 404) {
return {};
}
if (!response.ok) {
throw new Error(`Failed ${url}: ${response.status} ${response.statusText} ${await response.text()}`);
}
return response.json();
}

export type PackageVersionExists = (
packageName: string,
version: string,
) => Promise<boolean>;

export const packageVersionExists: PackageVersionExists = async (
packageName,
version,
) => {
const url = `https://registry.npmjs.org/${encodeURIComponent(packageName)}/${encodeURIComponent(version)}`;
const response = await fetch(url);
if (response.status === 404 || response.status === 410) {
return false;
}
Comment thread
zahin-mohammad marked this conversation as resolved.
Resolved
if (!response.ok) {
Comment thread
zahin-mohammad marked this conversation as resolved.
Resolved
throw new Error(
`Failed ${url}: ${response.status} ${response.statusText} ${await response.text()}`,
);
}
return true;
};

export async function findFirstUnpublishedVersion(
packageName: string,
initialCandidate: string,
preid: string,
versionExists: PackageVersionExists = packageVersionExists,
): Promise<string> {
let candidate = initialCandidate;
while (await versionExists(packageName, candidate)) {
const next = inc(candidate, 'prerelease', undefined, preid);
if (!next) {
throw new Error(
`Failed to increment occupied version ${packageName}@${candidate}`,
);
}
candidate = next;
}
return candidate;
}

// Add this function to read from cache
export function getDistTagsCache(): string | undefined {
return process.env.BITGO_PREPARE_RELEASE_CACHE_DIST_TAGS;
Expand Down Expand Up @@ -45,12 +90,7 @@ export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<
console.warn(`Skipping ${moduleName} as it's not published to npm`);
return [];
}
try {
return [[moduleName, await getDistTags(moduleName)]];
} catch (e) {
console.warn(`Failed to fetch dist tags for ${moduleName}`, e);
return [];
}
return [[moduleName, await getDistTags(moduleName)]];
})
)
).flat()
Expand Down
77 changes: 77 additions & 0 deletions scripts/tests/prepareRelease/version-allocation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require('tsx/cjs');

const assert = require('node:assert/strict');
const {
findFirstUnpublishedVersion,
getDistTags,
getDistTagsForModuleNames,
} = require('../../prepareRelease/distTags.ts');

describe('prerelease version allocation', function () {
it('keeps an unoccupied candidate', async function () {
const version = await findFirstUnpublishedVersion(
'@bitgo-beta/sdk-core',
'1.2.3-beta.8',
'beta',
async () => false,
);

assert.equal(version, '1.2.3-beta.8');
});

it('skips consecutive occupied candidates', async function () {
const occupied = new Set(['1.2.3-beta.8', '1.2.3-beta.9']);
const version = await findFirstUnpublishedVersion(
'@bitgo-beta/sdk-core',
'1.2.3-beta.8',
'beta',
async (_name, candidate) => occupied.has(candidate),
);

assert.equal(version, '1.2.3-beta.10');
});

it('fails instead of guessing when the registry probe fails', async function () {
await assert.rejects(
findFirstUnpublishedVersion(
'@bitgo-beta/sdk-core',
'1.2.3-beta.8',
'beta',
async () => {
throw new Error('registry unavailable');
},
),
/registry unavailable/,
);
});

it('treats a missing dist-tag document as a new package', async function () {
const originalFetch = global.fetch;
global.fetch = async () =>
new Response('', { status: 404, statusText: 'Not Found' });

try {
assert.deepEqual(await getDistTags('@bitgo-beta/new-package'), {});
} finally {
global.fetch = originalFetch;
}
});

it('fails instead of dropping a module when dist-tag lookup fails', async function () {
const originalFetch = global.fetch;
global.fetch = async () =>
new Response('unavailable', {
status: 503,
statusText: 'Service Unavailable',
});

try {
await assert.rejects(
getDistTagsForModuleNames(['@bitgo-beta/sdk-core']),
/503 Service Unavailable/,
);
} finally {
global.fetch = originalFetch;
}
});
});
1 change: 1 addition & 0 deletions scripts/verify-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ async function verify(preid?: string) {
if (!(await verifyPackage(dir, preid))) {
console.error('Failed to verify outstanding packages.');
if (!RECOVERY_MODE) {
process.exitCode = 1;
Comment thread
zahin-mohammad marked this conversation as resolved.
Resolved
return;
}
// keep bumping/retrying the rest of the packages instead of aborting on the first stuck one
Expand Down
Loading