fix: ignore stale downgrade update suggestions
Prevents older reported versions from showing as updates Falls back to registry data when update API is stale
This commit is contained in:
@@ -117,8 +117,11 @@ async function checkForUpdatesFromApi(currentVersion, options = {}) {
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (typeof data?.latestVersion !== 'string') return null;
|
if (typeof data?.latestVersion !== 'string') return null;
|
||||||
|
|
||||||
|
const versionComparison = compareVersions(data.latestVersion, currentVersion);
|
||||||
|
if (versionComparison < 0) return null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
available: Boolean(data.updateAvailable),
|
available: Boolean(data.updateAvailable) && versionComparison > 0,
|
||||||
version: data.latestVersion,
|
version: data.latestVersion,
|
||||||
currentVersion,
|
currentVersion,
|
||||||
body: typeof data.releaseNotes === 'string' ? data.releaseNotes : undefined,
|
body: typeof data.releaseNotes === 'string' ? data.releaseNotes : undefined,
|
||||||
@@ -650,11 +653,19 @@ export async function getLatestVersion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse semver version to numeric for comparison
|
* Compare semver-like version strings.
|
||||||
*/
|
*/
|
||||||
function parseVersion(version) {
|
function compareVersions(left, right) {
|
||||||
const parts = version.replace(/^v/, '').split('.').map(Number);
|
const a = String(left || '').replace(/^v/, '').split('.').map((part) => Number.parseInt(part || '0', 10));
|
||||||
return (parts[0] || 0) * 10000 + (parts[1] || 0) * 100 + (parts[2] || 0);
|
const b = String(right || '').replace(/^v/, '').split('.').map((part) => Number.parseInt(part || '0', 10));
|
||||||
|
const length = Math.max(a.length, b.length);
|
||||||
|
|
||||||
|
for (let index = 0; index < length; index += 1) {
|
||||||
|
const diff = (a[index] || 0) - (b[index] || 0);
|
||||||
|
if (diff !== 0) return diff;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -671,14 +682,10 @@ export async function fetchChangelogNotes(fromVersion, toVersion) {
|
|||||||
const changelog = await response.text();
|
const changelog = await response.text();
|
||||||
const sections = changelog.split(/^## /m).slice(1);
|
const sections = changelog.split(/^## /m).slice(1);
|
||||||
|
|
||||||
const fromNum = parseVersion(fromVersion);
|
|
||||||
const toNum = parseVersion(toVersion);
|
|
||||||
|
|
||||||
const relevantSections = sections.filter((section) => {
|
const relevantSections = sections.filter((section) => {
|
||||||
const match = section.match(/^\[(\d+\.\d+\.\d+)\]/);
|
const match = section.match(/^\[(\d+\.\d+\.\d+)\]/);
|
||||||
if (!match) return false;
|
if (!match) return false;
|
||||||
const ver = parseVersion(match[1]);
|
return compareVersions(match[1], fromVersion) > 0 && compareVersions(match[1], toVersion) <= 0;
|
||||||
return ver > fromNum && ver <= toNum;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (relevantSections.length === 0) return undefined;
|
if (relevantSections.length === 0) return undefined;
|
||||||
@@ -716,9 +723,7 @@ export async function checkForUpdates(options = {}) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentNum = parseVersion(currentVersion);
|
const available = compareVersions(latestVersion, currentVersion) > 0;
|
||||||
const latestNum = parseVersion(latestVersion);
|
|
||||||
const available = latestNum > currentNum;
|
|
||||||
let changelog;
|
let changelog;
|
||||||
if (available) {
|
if (available) {
|
||||||
changelog = await fetchChangelogNotes(currentVersion, latestVersion);
|
changelog = await fetchChangelogNotes(currentVersion, latestVersion);
|
||||||
|
|||||||
Reference in New Issue
Block a user