Skip to content

Commit 951de85

Browse files
committed
Trigger update if newer version is available after health check
1 parent 0154bfe commit 951de85

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

app/Jobs/CheckSiteHealth.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use App\Models\Site;
88
use App\RemoteSite\Connection;
9+
use App\TUF\TufFetcher;
910
use Carbon\Carbon;
1011
use Illuminate\Contracts\Queue\ShouldQueue;
1112
use Illuminate\Foundation\Queue\Queueable;
13+
use Illuminate\Support\Str;
1214

1315
class CheckSiteHealth implements ShouldQueue
1416
{
@@ -39,5 +41,19 @@ public function handle(): void
3941
// @phpstan-ignore-next-line
4042
$this->site->last_seen = Carbon::now();
4143
$this->site->save();
44+
45+
// Check if a newer Joomla version for that site is available
46+
$latestVersion = (new TufFetcher())->getLatestVersionForBranch((int) $this->site->cms_version[0]);
47+
48+
// Available version is not newer, exit
49+
if (!version_compare($latestVersion, $this->site->cms_version, ">")) {
50+
return;
51+
}
52+
53+
// We have a newer version, queue Update
54+
UpdateSite::dispatch(
55+
$this->site,
56+
$latestVersion
57+
);
4258
}
4359
}

app/TUF/TufFetcher.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public function __construct()
1717
$this->updateStorage = App::make(StorageInterface::class);
1818
}
1919

20-
public function getReleases(): mixed
20+
/**
21+
* @return Collection
22+
*/
23+
public function getReleases(): Collection
2124
{
2225
// Cache response to avoid to make constant calls on the fly
2326
return Cache::remember(
@@ -43,4 +46,13 @@ function () {
4346
}
4447
);
4548
}
49+
50+
public function getLatestVersionForBranch(int $branch): string
51+
{
52+
return $this->getReleases()->filter(function ($release) {
53+
return $release["stability"] === "Stable";
54+
})->sort(function ($releaseA, $releaseB) {
55+
return version_compare($releaseA["version"], $releaseB["version"], '<');
56+
})->pluck('version')->first();
57+
}
4658
}

0 commit comments

Comments
 (0)