2023-12-30 22:28:43 +00:00
|
|
|
import semverCoerce from 'semver/functions/coerce';
|
|
|
|
import semverGt from 'semver/functions/gt';
|
2023-11-04 13:20:50 +00:00
|
|
|
import { onMounted, ref } from 'vue';
|
|
|
|
|
|
|
|
import useAuthentication from './useAuthentication';
|
|
|
|
import useConfig from './useConfig';
|
|
|
|
|
2024-06-06 13:16:59 +00:00
|
|
|
interface VersionInfo {
|
2023-11-04 13:20:50 +00:00
|
|
|
latest: string;
|
2023-11-26 07:02:02 +00:00
|
|
|
rc: string;
|
2023-11-04 13:20:50 +00:00
|
|
|
next: string;
|
2024-06-06 13:16:59 +00:00
|
|
|
}
|
2023-11-04 13:20:50 +00:00
|
|
|
|
|
|
|
const version = ref<{
|
|
|
|
latest: string | undefined;
|
|
|
|
current: string;
|
|
|
|
currentShort: string;
|
|
|
|
needsUpdate: boolean;
|
2023-11-26 07:02:02 +00:00
|
|
|
usesNext: boolean;
|
2023-11-04 13:20:50 +00:00
|
|
|
}>();
|
|
|
|
|
|
|
|
async function fetchVersion(): Promise<VersionInfo | undefined> {
|
|
|
|
try {
|
|
|
|
const resp = await fetch('https://woodpecker-ci.org/version.json');
|
2024-06-06 13:16:59 +00:00
|
|
|
const json = (await resp.json()) as VersionInfo;
|
2023-11-04 13:20:50 +00:00
|
|
|
return json;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to fetch version info', error);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-27 20:15:10 +00:00
|
|
|
const isInitialized = ref(false);
|
2023-11-04 13:20:50 +00:00
|
|
|
|
|
|
|
export function useVersion() {
|
2024-01-27 20:15:10 +00:00
|
|
|
if (isInitialized.value) {
|
2023-11-04 13:20:50 +00:00
|
|
|
return version;
|
|
|
|
}
|
2024-01-27 20:15:10 +00:00
|
|
|
isInitialized.value = true;
|
2023-11-04 13:20:50 +00:00
|
|
|
|
|
|
|
const config = useConfig();
|
|
|
|
const current = config.version as string;
|
2023-12-30 22:28:43 +00:00
|
|
|
const currentSemver = semverCoerce(current);
|
2023-11-26 07:02:02 +00:00
|
|
|
const usesNext = current.startsWith('next');
|
2023-11-04 13:20:50 +00:00
|
|
|
|
|
|
|
const { user } = useAuthentication();
|
2024-06-06 13:16:59 +00:00
|
|
|
if (config.skipVersionCheck || user?.admin !== true) {
|
2023-11-04 13:20:50 +00:00
|
|
|
version.value = {
|
|
|
|
latest: undefined,
|
|
|
|
current,
|
|
|
|
currentShort: usesNext ? 'next' : current,
|
|
|
|
needsUpdate: false,
|
2023-11-26 07:02:02 +00:00
|
|
|
usesNext,
|
2023-11-04 13:20:50 +00:00
|
|
|
};
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current === 'dev') {
|
|
|
|
version.value = {
|
|
|
|
latest: undefined,
|
|
|
|
current,
|
|
|
|
currentShort: current,
|
|
|
|
needsUpdate: false,
|
2023-11-26 07:02:02 +00:00
|
|
|
usesNext,
|
2023-11-04 13:20:50 +00:00
|
|
|
};
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(async () => {
|
|
|
|
const versionInfo = await fetchVersion();
|
|
|
|
|
2023-11-26 07:02:02 +00:00
|
|
|
let latest;
|
2023-11-04 13:20:50 +00:00
|
|
|
if (versionInfo) {
|
|
|
|
if (usesNext) {
|
2023-11-26 07:02:02 +00:00
|
|
|
latest = versionInfo.next;
|
|
|
|
} else if (current.includes('rc')) {
|
2023-12-30 22:28:43 +00:00
|
|
|
latest = versionInfo.rc;
|
2023-11-04 13:20:50 +00:00
|
|
|
} else {
|
2023-12-30 22:28:43 +00:00
|
|
|
latest = versionInfo.latest;
|
2023-11-04 13:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 01:10:36 +00:00
|
|
|
let needsUpdate = false;
|
|
|
|
if (usesNext) {
|
|
|
|
needsUpdate = latest !== current;
|
|
|
|
} else if (latest !== undefined && currentSemver !== null) {
|
|
|
|
needsUpdate = semverGt(latest, currentSemver);
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:20:50 +00:00
|
|
|
version.value = {
|
2023-11-26 07:02:02 +00:00
|
|
|
latest,
|
2023-11-04 13:20:50 +00:00
|
|
|
current,
|
|
|
|
currentShort: usesNext ? 'next' : current,
|
2024-02-07 01:10:36 +00:00
|
|
|
needsUpdate,
|
2023-11-26 07:02:02 +00:00
|
|
|
usesNext,
|
2023-11-04 13:20:50 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|