Skip to content

Commit

Permalink
better handle pre-releases
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Dec 22, 2024
1 parent c3fcb68 commit a83bf17
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
2 changes: 2 additions & 0 deletions buildutils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
"@jupyterlab/buildutils": "~4.4.0-alpha.1",
"commander": "^6.2.0",
"fs-extra": "^9.1.0",
"semver": "^7.5.4",
"typescript": "~5.0.2"
},
"devDependencies": {
"@types/fs-extra": "^9.0.10",
"@types/node": "^14.6.1",
"@types/semver": "^7.5.6",
"rimraf": "^3.0.2"
}
}
56 changes: 44 additions & 12 deletions buildutils/src/get-latest-lab-version.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
import * as fs from 'fs-extra';
import * as semver from 'semver';

function convertPythonVersion(version: string): string {
return version
.replace('a', '-alpha')
.replace('b', '-beta')
.replace('rc', '-rc');
}

function extractVersionFromReleases(
releases: any,
versionTag: string
versionTag: string,
currentVersion: string
): string | null {
for (const release of releases) {
const tagName: string = release['tag_name'];
if (versionTag === 'latest') {
if (!release['prerelease'] && !release['draft']) {
return tagName;
}
} else if (versionTag === tagName) {
return tagName;
}
const npmCurrentVersion = convertPythonVersion(currentVersion);
const isCurrentPreRelease = semver.prerelease(npmCurrentVersion) !== null;

if (versionTag === 'latest') {
// Find first version that is newer than current and matches pre-release criteria
const release = releases.find((r: any) => {
const version = r['tag_name'].substring(1); // Remove 'v' prefix for semver
const npmVersion = convertPythonVersion(version);
return (
(isCurrentPreRelease || !r['prerelease']) &&
semver.gte(npmVersion, npmCurrentVersion)
);
});
return release ? release['tag_name'] : null;
} else {
// Find exact version match
const release = releases.find((r: any) => r['tag_name'] === versionTag);
return release ? release['tag_name'] : null;
}
return null;
}

function extractCurrentJupyterLabVersion(): string {
const toml = fs.readFileSync('pyproject.toml', 'utf8');
const match = toml.match(/jupyterlab>=([^,]+)/);
if (!match) {
throw new Error('Could not find JupyterLab version in pyproject.toml');
}
return match[1];
}

async function findVersion(versionTag: string): Promise<string> {
Expand All @@ -22,10 +50,14 @@ async function findVersion(versionTag: string): Promise<string> {
const error_message = `Failed to fetch package.json from ${url}. HTTP status code: ${response.status}`;
throw new Error(error_message);
}

const currentVersion = extractCurrentJupyterLabVersion();

const releases: any = await response.json();
const version: string | null = extractVersionFromReleases(
releases,
versionTag
versionTag,
currentVersion
);
if (version === null) {
const error_message = 'Invalid release tag';
Expand Down

0 comments on commit a83bf17

Please sign in to comment.