mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
* Initial plan * Add error handling for Installation.info endpoint in isolated environments * Return -1 for versionsBehind when network request fails Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>
80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
import { version } from "../../package.json";
|
|
import Logger from "@server/logging/Logger";
|
|
import fetch from "./fetch";
|
|
|
|
const dockerhubLink =
|
|
"https://hub.docker.com/v2/repositories/outlinewiki/outline";
|
|
|
|
function isFullReleaseVersion(versionName: string): boolean {
|
|
const releaseRegex = /^(version-)?\d+\.\d+\.\d+$/; // Matches "N.N.N" or "version-N.N.N" for dockerhub releases before v0.56.0"
|
|
return releaseRegex.test(versionName);
|
|
}
|
|
|
|
export async function getVersionInfo(currentVersion: string): Promise<{
|
|
latestVersion: string;
|
|
versionsBehind: number;
|
|
}> {
|
|
try {
|
|
let allVersions: string[] = [];
|
|
let latestVersion: string | null = null;
|
|
let nextUrl: string | null =
|
|
dockerhubLink + "/tags?name=&ordering=last_updated&page_size=100";
|
|
|
|
// Continue fetching pages until the required versions are found or no more pages
|
|
while (nextUrl) {
|
|
const response = await fetch(nextUrl);
|
|
const data = await response.json();
|
|
|
|
// Map and filter the versions to keep only full releases
|
|
const pageVersions = data.results
|
|
.map((result: any) => result.name)
|
|
.filter(isFullReleaseVersion);
|
|
|
|
allVersions = allVersions.concat(pageVersions);
|
|
|
|
// Set the latest version if not already set
|
|
if (!latestVersion && pageVersions.length > 0) {
|
|
latestVersion = pageVersions[0];
|
|
}
|
|
|
|
// Check if the current version is found
|
|
const currentIndex = allVersions.findIndex(
|
|
(version: string) => version === currentVersion
|
|
);
|
|
|
|
if (currentIndex !== -1) {
|
|
const versionsBehind = currentIndex; // The number of versions behind
|
|
return {
|
|
latestVersion: latestVersion || currentVersion, // Fallback to current if no latest found
|
|
versionsBehind,
|
|
};
|
|
}
|
|
|
|
nextUrl = data.next || null;
|
|
}
|
|
|
|
return {
|
|
latestVersion: latestVersion || currentVersion,
|
|
versionsBehind: -1, // Return -1 if current version is not found
|
|
};
|
|
} catch (error) {
|
|
Logger.warn(
|
|
"Failed to fetch version information from Docker Hub. This is expected in isolated environments.",
|
|
{
|
|
currentVersion,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
}
|
|
);
|
|
|
|
// Return fallback values when external request fails
|
|
return {
|
|
latestVersion: currentVersion,
|
|
versionsBehind: -1,
|
|
};
|
|
}
|
|
}
|
|
|
|
export function getVersion(): string {
|
|
return version;
|
|
}
|