|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + * |
| 6 | + * Detect Nextcloud version metadata from a list of stable branch numbers. |
| 7 | + * |
| 8 | + * Shared helpers are used by build-index.php. When run as a CLI script, |
| 9 | + * outputs KEY=VALUE pairs (highest_stable, lowest_stable, dev_version) |
| 10 | + * suitable for appending to $GITHUB_OUTPUT or for eval in bash. |
| 11 | + * |
| 12 | + * Usage: php detect-versions.php <branch1> <branch2> ... |
| 13 | + * Example: php detect-versions.php 32 33 34 |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Get the GitHub API headers with optional authentication. |
| 18 | + */ |
| 19 | +function get_github_headers(): string { |
| 20 | + $headers = 'User-Agent: Nextcloud Documentation Builder'; |
| 21 | + if ($token = getenv('GITHUB_TOKEN')) { |
| 22 | + $headers .= "\r\nAuthorization: token $token"; |
| 23 | + } |
| 24 | + return $headers; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Get the repository name for a given version. |
| 29 | + * Nextcloud moved to nextcloud-releases/server starting with version 32. |
| 30 | + */ |
| 31 | +function get_repo_for_version(int $version): string { |
| 32 | + return $version >= 32 ? 'nextcloud-releases/server' : 'nextcloud/server'; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Parse the HTTP status code from the response headers populated by file_get_contents. |
| 37 | + * |
| 38 | + * @param array $headers The $http_response_header array |
| 39 | + */ |
| 40 | +function parse_http_status(array $headers): int { |
| 41 | + preg_match('/HTTP\/[\d.]+ (\d+)/', $headers[0] ?? '', $matches); |
| 42 | + return (int)($matches[1] ?? 0); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Fetch release info for a given version from the GitHub API. |
| 47 | + * |
| 48 | + * Returns an array ['date' => int] if the release exists (HTTP 200). |
| 49 | + * Returns null if the release does not exist (HTTP 404). |
| 50 | + * Exits with code 1 on any other HTTP status (rate limit, server error, etc.) |
| 51 | + * to prevent silently generating empty or incorrect output. |
| 52 | + */ |
| 53 | +function fetch_release_info(int $version): ?array { |
| 54 | + $repo = get_repo_for_version($version); |
| 55 | + $url = sprintf('https://api.github.com/repos/%s/releases/tags/v%d.0.0', $repo, $version); |
| 56 | + |
| 57 | + $context = stream_context_create([ |
| 58 | + 'http' => [ |
| 59 | + 'header' => get_github_headers(), |
| 60 | + 'timeout' => 10, |
| 61 | + 'ignore_errors' => true |
| 62 | + ] |
| 63 | + ]); |
| 64 | + |
| 65 | + $response = @file_get_contents($url, false, $context); |
| 66 | + |
| 67 | + // FIXME: function_exists conditional can be dropped once we don't need to support <8.4.0 |
| 68 | + if (function_exists('http_get_last_response_headers')) { |
| 69 | + /** @var array|null */ |
| 70 | + $http_response_header = \http_get_last_response_headers(); |
| 71 | + } |
| 72 | + |
| 73 | + $status = isset($http_response_header) && is_array($http_response_header) |
| 74 | + ? parse_http_status($http_response_header) |
| 75 | + : 0; |
| 76 | + |
| 77 | + if ($status === 200) { |
| 78 | + $data = json_decode($response, true); |
| 79 | + $publishedAt = $data['published_at'] ?? $data['created_at'] ?? null; |
| 80 | + return ['date' => $publishedAt ? strtotime($publishedAt) : time()]; |
| 81 | + } |
| 82 | + |
| 83 | + if ($status === 404) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + fwrite(STDERR, "GitHub API error (HTTP $status) checking v$version.0.0 — aborting\n"); |
| 88 | + exit(1); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Detect version metadata from a list of stable branch numbers. |
| 93 | + * |
| 94 | + * @param int[] $branches All known stable branch numbers (any order) |
| 95 | + * @return array{ |
| 96 | + * highest_stable: int|null, |
| 97 | + * lowest_stable: int, |
| 98 | + * dev_version: int, |
| 99 | + * released: array<int, int> |
| 100 | + * } |
| 101 | + */ |
| 102 | +function detect_versions(array $branches): array { |
| 103 | + rsort($branches, SORT_NUMERIC); |
| 104 | + $oneYearAgo = time() - (365 * 24 * 60 * 60); |
| 105 | + $released = []; |
| 106 | + $firstOutOfSupportTime = null; |
| 107 | + |
| 108 | + foreach ($branches as $branch) { |
| 109 | + if ($firstOutOfSupportTime !== null) { |
| 110 | + // Older than the first out-of-support version — skip API call, |
| 111 | + // store with the same timestamp (also out of support). |
| 112 | + fwrite(STDERR, "🛑 Version $branch is unsupported\n"); |
| 113 | + $released[$branch] = $firstOutOfSupportTime; |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + $info = fetch_release_info($branch); |
| 118 | + if ($info === null) { |
| 119 | + fwrite(STDERR, "⏳ Version $branch is not released (tag v$branch.0.0 not found)\n"); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + $released[$branch] = $info['date']; |
| 124 | + if ($info['date'] < $oneYearAgo) { |
| 125 | + fwrite(STDERR, "🛑 Version $branch is unsupported (released on " . date('Y-m-d', $info['date']) . ")\n"); |
| 126 | + $firstOutOfSupportTime = $info['date']; |
| 127 | + } else { |
| 128 | + fwrite(STDERR, "✅ Version $branch is supported (released on " . date('Y-m-d', $info['date']) . ")\n"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // highest_stable: highest branch with a confirmed release |
| 133 | + $highestStable = null; |
| 134 | + foreach ($branches as $b) { |
| 135 | + if (isset($released[$b])) { |
| 136 | + $highestStable = $b; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // dev_version: if the highest branch has a release, dev = highest + 1; |
| 142 | + // otherwise the branch exists but isn't released yet (upcoming). |
| 143 | + $devVersion = isset($released[$branches[0]]) ? $branches[0] + 1 : $branches[0]; |
| 144 | + |
| 145 | + // lowest_stable: lowest version still within the support window. |
| 146 | + // Using min($branches) would include ancient branches (e.g. stable10) that still |
| 147 | + // exist on the remote but are long out of support. |
| 148 | + $supportedVersions = array_keys(array_filter($released, fn($date) => $date >= $oneYearAgo)); |
| 149 | + $lowestStable = !empty($supportedVersions) ? min($supportedVersions) : $highestStable; |
| 150 | + |
| 151 | + return [ |
| 152 | + 'highest_stable' => $highestStable, |
| 153 | + 'lowest_stable' => $lowestStable, |
| 154 | + 'dev_version' => $devVersion, |
| 155 | + 'released' => $released, |
| 156 | + ]; |
| 157 | +} |
| 158 | + |
| 159 | +// CLI entry point — only runs when invoked directly, not when require_once'd. |
| 160 | +if (basename(__FILE__) === basename($argv[0])) { |
| 161 | + $branches = array_values(array_filter( |
| 162 | + array_map('intval', array_slice($argv, 1)), |
| 163 | + fn($b) => $b >= 12 |
| 164 | + )); |
| 165 | + |
| 166 | + if (empty($branches)) { |
| 167 | + fwrite(STDERR, "Error: No valid stable branches provided (expected numeric args >= 12).\n"); |
| 168 | + exit(1); |
| 169 | + } |
| 170 | + |
| 171 | + $result = detect_versions($branches); |
| 172 | + |
| 173 | + if ($result['highest_stable'] === null) { |
| 174 | + fwrite(STDERR, "Error: No released stable branch found.\n"); |
| 175 | + exit(1); |
| 176 | + } |
| 177 | + |
| 178 | + fwrite(STDERR, "➡️ Version {$result['dev_version']} (development)\n"); |
| 179 | + |
| 180 | + echo "highest_stable={$result['highest_stable']}\n"; |
| 181 | + echo "lowest_stable={$result['lowest_stable']}\n"; |
| 182 | + echo "dev_version={$result['dev_version']}\n"; |
| 183 | +} |
0 commit comments