73 lines
2.6 KiB
PHP
73 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Setting;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class VersionCheckController extends Controller
|
|
{
|
|
public function __invoke(): JsonResponse
|
|
{
|
|
$current = Setting::query()->where('key', 'version')->value('value');
|
|
$build = Setting::query()->where('key', 'build')->value('value');
|
|
|
|
$owner = env('GITEA_OWNER');
|
|
$repo = env('GITEA_REPO');
|
|
$apiBase = rtrim((string) env('GITEA_API_BASE', 'https://git.24unix.net/api/v1'), '/');
|
|
$token = env('GITEA_TOKEN');
|
|
|
|
if (!$owner || !$repo) {
|
|
return response()->json([
|
|
'current_version' => $current,
|
|
'current_build' => $build !== null ? (int) $build : null,
|
|
'latest_tag' => null,
|
|
'latest_version' => null,
|
|
'is_latest' => null,
|
|
'error' => 'Missing GITEA_OWNER/GITEA_REPO configuration.',
|
|
]);
|
|
}
|
|
|
|
try {
|
|
$client = Http::acceptJson();
|
|
if ($token) {
|
|
$client = $client->withHeaders(['Authorization' => "token {$token}"]);
|
|
}
|
|
|
|
$response = $client->get("{$apiBase}/repos/{$owner}/{$repo}/releases/latest");
|
|
if (!$response->successful()) {
|
|
return response()->json([
|
|
'current_version' => $current,
|
|
'current_build' => $build !== null ? (int) $build : null,
|
|
'latest_tag' => null,
|
|
'latest_version' => null,
|
|
'is_latest' => null,
|
|
'error' => "Release check failed: {$response->status()}",
|
|
]);
|
|
}
|
|
|
|
$tag = (string) ($response->json('tag_name') ?? '');
|
|
$latestVersion = ltrim($tag, 'v');
|
|
$isLatest = $current && $latestVersion ? $current === $latestVersion : null;
|
|
|
|
return response()->json([
|
|
'current_version' => $current,
|
|
'current_build' => $build !== null ? (int) $build : null,
|
|
'latest_tag' => $tag ?: null,
|
|
'latest_version' => $latestVersion ?: null,
|
|
'is_latest' => $isLatest,
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
return response()->json([
|
|
'current_version' => $current,
|
|
'current_build' => $build !== null ? (int) $build : null,
|
|
'latest_tag' => null,
|
|
'latest_version' => null,
|
|
'is_latest' => null,
|
|
'error' => 'Version check failed.',
|
|
]);
|
|
}
|
|
}
|
|
}
|