|
| 1 | +/* |
| 2 | + * This file is part of Nurse Scheduling Project, see <https://github.com/j3soon/nurse-scheduling>. |
| 3 | + * |
| 4 | + * Copyright (C) 2023-2026 Johnson Sun |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +import { GITHUB_TAGS_API_URL, GITHUB_BRANCHES_API_URL } from '@/constants/urls'; |
| 21 | + |
| 22 | +// Current application version from environment variable. |
| 23 | +export const CURRENT_APP_VERSION = process.env.NEXT_PUBLIC_APP_VERSION || 'unknown'; |
| 24 | + |
| 25 | +// Type for release branch entries |
| 26 | +export type BuildEntry = { label: string; url: string }; |
| 27 | + |
| 28 | +/** |
| 29 | + * Parse a version string into its components for comparison. |
| 30 | + * Supports formats like "v1.0.0", "1.0.0", "v1.0", "1.0" |
| 31 | + * Returns null if the version cannot be parsed. |
| 32 | + */ |
| 33 | +export function parseVersion(version: string): { major: number; minor: number; patch: number } | null { |
| 34 | + const match = version.match(/^v?(\d+)\.(\d+)(?:\.(\d+))?/); |
| 35 | + if (!match) return null; |
| 36 | + return { |
| 37 | + major: parseInt(match[1], 10), |
| 38 | + minor: parseInt(match[2], 10), |
| 39 | + patch: match[3] !== undefined ? parseInt(match[3], 10) : 0, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Compare two version strings for sorting (descending order - newest first). |
| 45 | + * Returns negative if a > b, positive if a < b, 0 if equal. |
| 46 | + */ |
| 47 | +export function compareVersionsDescending(a: string, b: string): number { |
| 48 | + const versionA = parseVersion(a); |
| 49 | + const versionB = parseVersion(b); |
| 50 | + |
| 51 | + // Non-parseable versions go to the end |
| 52 | + if (!versionA && !versionB) return 0; |
| 53 | + if (!versionA) return 1; |
| 54 | + if (!versionB) return -1; |
| 55 | + |
| 56 | + // Compare major, minor, patch (descending) |
| 57 | + if (versionA.major !== versionB.major) return versionB.major - versionA.major; |
| 58 | + if (versionA.minor !== versionB.minor) return versionB.minor - versionA.minor; |
| 59 | + return versionB.patch - versionA.patch; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Extract major.minor from a version string (e.g., "v1.0" from "v1.0.0" or "v1.0.0-5-gabcdef") |
| 64 | + */ |
| 65 | +export function getMajorMinor(version: string): string | null { |
| 66 | + const match = version.match(/^(v?\d+\.\d+)/); |
| 67 | + return match ? match[1] : null; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Fetch the latest tag from GitHub, sorted by semver (newest first). |
| 72 | + * Returns the latest tag name or null if fetch fails. |
| 73 | + */ |
| 74 | +export async function fetchLatestTag(): Promise<string | null> { |
| 75 | + try { |
| 76 | + const response = await fetch(GITHUB_TAGS_API_URL); |
| 77 | + if (!response.ok) { |
| 78 | + console.warn('Failed to fetch latest tag:', response.status); |
| 79 | + return null; |
| 80 | + } |
| 81 | + const tags: { name: string }[] = await response.json(); |
| 82 | + if (tags.length === 0) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + // Sort tags by semver (descending) and return the latest |
| 86 | + const sortedTags = tags |
| 87 | + .map((t) => t.name) |
| 88 | + .sort(compareVersionsDescending); |
| 89 | + return sortedTags[0] || null; |
| 90 | + } catch (err) { |
| 91 | + console.warn('Failed to fetch latest tag:', err); |
| 92 | + return null; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Fetch release branches from GitHub and return them as BuildEntry objects, |
| 98 | + * sorted by semver (newest first). |
| 99 | + */ |
| 100 | +export async function fetchReleaseBranches(): Promise<BuildEntry[]> { |
| 101 | + try { |
| 102 | + const response = await fetch(GITHUB_BRANCHES_API_URL); |
| 103 | + if (!response.ok) { |
| 104 | + return []; |
| 105 | + } |
| 106 | + const branches: { name: string }[] = await response.json(); |
| 107 | + const releases = branches |
| 108 | + .map((b) => b.name.match(/^release\/(.+)$/)) |
| 109 | + .filter((m): m is RegExpMatchArray => m !== null) |
| 110 | + .map((m) => ({ |
| 111 | + version: m[1], |
| 112 | + label: `v${m[1]}`, |
| 113 | + url: `https://release-${m[1].replace(/\./g, '-')}.nursescheduling.org`, |
| 114 | + })); |
| 115 | + |
| 116 | + // Sort by version (descending - newest first) |
| 117 | + releases.sort((a, b) => compareVersionsDescending(a.version, b.version)); |
| 118 | + |
| 119 | + return releases.map(({ label, url }) => ({ label, url })); |
| 120 | + } catch { |
| 121 | + // Silently fail - releases just won't show |
| 122 | + return []; |
| 123 | + } |
| 124 | +} |
0 commit comments