Skip to content

Commit 344c233

Browse files
authored
Show when app update is available (alexjustesen#2298)
Co-authored-by: Alex Justesen <[email protected]>
1 parent 2d40d98 commit 344c233

File tree

5 files changed

+87
-97
lines changed

5 files changed

+87
-97
lines changed

app/Filament/VersionProviders/SpeedtestTrackerVersionProvider.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

app/Providers/Filament/AdminPanelProvider.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace App\Providers\Filament;
44

5-
use App\Filament\VersionProviders\SpeedtestTrackerVersionProvider;
6-
use Awcodes\FilamentVersions\VersionsPlugin;
5+
use App\Services\GitHub\Repository;
76
use Filament\FontProviders\LocalFontProvider;
87
use Filament\Http\Middleware\Authenticate;
98
use Filament\Http\Middleware\DisableBladeIconComponents;
@@ -44,13 +43,6 @@ public function panel(Panel $panel): Panel
4443
->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
4544
->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
4645
->pages([])
47-
->plugins([
48-
VersionsPlugin::make()
49-
->hasDefaults(false)
50-
->items([
51-
new SpeedtestTrackerVersionProvider,
52-
]),
53-
])
5446
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
5547
->widgets([])
5648
->databaseNotifications()
@@ -85,9 +77,10 @@ public function panel(Panel $panel): Panel
8577
->url('https://github.com/sponsors/alexjustesen', shouldOpenInNewTab: true)
8678
->icon('heroicon-o-banknotes')
8779
->group('Links'),
88-
NavigationItem::make('GitHub')
80+
NavigationItem::make(config('speedtest.build_version'))
8981
->url('https://github.com/alexjustesen/speedtest-tracker', shouldOpenInNewTab: true)
90-
->icon('heroicon-o-code-bracket')
82+
->icon('tabler-brand-github')
83+
->badge(fn (): string => Repository::updateAvailable() ? 'Update Available!' : 'Up to Date')
9184
->group('Links'),
9285
]);
9386
}

app/Services/GitHub/Repository.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace App\Services\GitHub;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
use Illuminate\Support\Facades\Http;
7+
use Illuminate\Support\Facades\Log;
8+
use Throwable;
9+
10+
class Repository
11+
{
12+
/**
13+
* Get the latest tagged version from the GitHub repository.
14+
*
15+
* @return string|false Returns the version tag (e.g., "v1.2.3") or false on failure
16+
*/
17+
public static function getLatestVersion(): string|false
18+
{
19+
return Cache::remember('github.latest_version', now()->addHour(), function () {
20+
try {
21+
$response = Http::retry(3, 100)
22+
->timeout(10)
23+
->withHeaders([
24+
'Accept' => 'application/vnd.github.v3+json',
25+
'User-Agent' => 'speedtest-tracker',
26+
])
27+
->get('https://api.github.com/repos/alexjustesen/speedtest-tracker/releases/latest');
28+
29+
if (! $response->successful()) {
30+
Log::warning('Failed to fetch latest version from GitHub API', [
31+
'status' => $response->status(),
32+
'response' => $response->body(),
33+
]);
34+
35+
return false;
36+
}
37+
38+
$data = $response->json();
39+
40+
if (! isset($data['tag_name'])) {
41+
Log::warning('GitHub API response missing tag_name field', ['response' => $data]);
42+
43+
return false;
44+
}
45+
46+
return $data['tag_name'];
47+
} catch (Throwable $e) {
48+
Log::error('Exception occurred while fetching latest version from GitHub', [
49+
'message' => $e->getMessage(),
50+
'file' => $e->getFile(),
51+
'line' => $e->getLine(),
52+
]);
53+
54+
return false;
55+
}
56+
});
57+
}
58+
59+
/**
60+
* Check if a newer version is available.
61+
*
62+
* @return bool Returns true if a newer version is available, false if up to date or unable to determine
63+
*/
64+
public static function updateAvailable(): bool
65+
{
66+
$currentVersion = config('speedtest.build_version');
67+
$latestVersion = self::getLatestVersion();
68+
69+
if ($latestVersion === false) {
70+
return false; // Unable to determine, assume no update available
71+
}
72+
73+
// Normalize versions by removing 'v' prefix for comparison
74+
$normalizedCurrent = ltrim($currentVersion, 'v');
75+
$normalizedLatest = ltrim($latestVersion, 'v');
76+
77+
// Use version_compare to properly compare semantic versions
78+
$comparison = version_compare($normalizedLatest, $normalizedCurrent);
79+
80+
return $comparison > 0; // 1 = latest is newer, 0 = equal, -1 = latest is older
81+
}
82+
}

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"license": "MIT",
1616
"require": {
1717
"php": "^8.2",
18-
"awcodes/filament-versions": "^2.0.1",
1918
"chrisullyott/php-filesize": "^4.2.1",
2019
"dragonmantank/cron-expression": "^3.4.0",
2120
"filament/filament": "^3.3.31",

composer.lock

Lines changed: 1 addition & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)