Skip to content

Commit

Permalink
Trigger update if newer version is available after health check
Browse files Browse the repository at this point in the history
  • Loading branch information
SniperSister committed Nov 17, 2024
1 parent 0154bfe commit 951de85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/Jobs/CheckSiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use App\Models\Site;
use App\RemoteSite\Connection;
use App\TUF\TufFetcher;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Str;

class CheckSiteHealth implements ShouldQueue
{
Expand Down Expand Up @@ -39,5 +41,19 @@ public function handle(): void
// @phpstan-ignore-next-line
$this->site->last_seen = Carbon::now();
$this->site->save();

// Check if a newer Joomla version for that site is available
$latestVersion = (new TufFetcher())->getLatestVersionForBranch((int) $this->site->cms_version[0]);

// Available version is not newer, exit
if (!version_compare($latestVersion, $this->site->cms_version, ">")) {
return;
}

// We have a newer version, queue Update
UpdateSite::dispatch(
$this->site,
$latestVersion
);
}
}
14 changes: 13 additions & 1 deletion app/TUF/TufFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public function __construct()
$this->updateStorage = App::make(StorageInterface::class);
}

public function getReleases(): mixed
/**
* @return Collection
*/
public function getReleases(): Collection

Check failure on line 23 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\TUF\TufFetcher::getReleases() return type with generic class Illuminate\Support\Collection does not specify its types: TKey, TValue
{
// Cache response to avoid to make constant calls on the fly
return Cache::remember(

Check failure on line 26 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\TUF\TufFetcher::getReleases() should return Illuminate\Support\Collection but returns mixed.
Expand All @@ -43,4 +46,13 @@ function () {
}
);
}

public function getLatestVersionForBranch(int $branch): string
{
return $this->getReleases()->filter(function ($release) {

Check failure on line 52 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\TUF\TufFetcher::getLatestVersionForBranch() should return string but returns mixed.
return $release["stability"] === "Stable";

Check failure on line 53 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot access offset 'stability' on mixed.
})->sort(function ($releaseA, $releaseB) {

Check failure on line 54 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $callback of method Illuminate\Support\Collection<(int|string),mixed>::sort() expects (callable(mixed, mixed): int)|int|null, Closure(mixed, mixed): bool given.
return version_compare($releaseA["version"], $releaseB["version"], '<');

Check failure on line 55 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot access offset 'version' on mixed.

Check failure on line 55 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot access offset 'version' on mixed.

Check failure on line 55 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $version1 of function version_compare expects string, mixed given.

Check failure on line 55 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $version2 of function version_compare expects string, mixed given.
})->pluck('version')->first();
}
}

0 comments on commit 951de85

Please sign in to comment.