Skip to content

Commit

Permalink
cs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SniperSister committed Jan 18, 2025
1 parent 9529651 commit 5083abc
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/Jobs/CheckSiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function handle(): void
$tufFetcher = App::make(TufFetcher::class);
$latestVersion = $tufFetcher->getLatestVersionForBranch((int) $this->site->cms_version[0]);

// No latest version for branch available, unsupported branch - return
if (!$latestVersion) {
return;
}

// Available version is not newer, exit
if (!version_compare($latestVersion, $this->site->cms_version, ">")) {
return;
Expand Down
28 changes: 20 additions & 8 deletions app/TUF/TufFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public function __construct()
}

/**
* @return Collection
* @return Collection<string, ReleaseData>
*/
public function getReleases(): Collection
{
// Cache response to avoid to make constant calls on the fly
return Cache::remember(
$releases = Cache::remember(
'cms_targets',
(int) config('autoupdates.tuf_repo_cachetime') * 60, // @phpstan-ignore-line
function () {
Expand All @@ -47,14 +47,26 @@ function () {
});
}
);

if (!$releases instanceof Collection) {
throw new MetadataException("Invalid release list");
}

return $releases;
}

public function getLatestVersionForBranch(int $branch): string
public function getLatestVersionForBranch(int $branch): ?string
{
return $this->getReleases()->filter(function (ReleaseData $release) {
return $release->stability === "Stable";
})->sort(function (ReleaseData $releaseA, ReleaseData $releaseB) {
return version_compare($releaseA->version, $releaseB->version, '<');
})->pluck('version')->first();
$versionMatch = $this->getReleases()->filter(function (ReleaseData $release) use ($branch): bool {
return $release->stability === "stable" && $release->version[0] === (string) $branch;
})->sort(function (ReleaseData $releaseA, ReleaseData $releaseB): int {
return version_compare($releaseA->version, $releaseB->version);
})->last();

if (!$versionMatch instanceof ReleaseData) {
return null;
}

return $versionMatch->version;
}
}
75 changes: 75 additions & 0 deletions tests/Unit/TUF/TufFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,81 @@ public function testGetReleasesThrowsExceptionOnMissingCustom()
$object->getReleases();
}

public function testGetLatestVersionForBranchReturnsNullForMissingBranch()
{
App::bind(StorageInterface::class, fn() => $this->getStorageMock([
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.2.1 Release",
"version" => "5.2.1",
"stability" => "stable",
]
]
]));

$object = new TufFetcher();
$result = $object->getLatestVersionForBranch(6);

$this->assertNull($result);
}

public function testGetLatestVersionForBranchChecksBranch()
{
App::bind(StorageInterface::class, fn() => $this->getStorageMock([
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.2.1 Release",
"version" => "5.2.1",
"stability" => "stable",
]
],
"Joomla_4.2.1-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 4.2.1 Release",
"version" => "4.1.2",
"stability" => "stable",
]
]
]));

$object = new TufFetcher();
$result = $object->getLatestVersionForBranch(4);

$this->assertEquals("4.1.2", $result);
}

public function testGetLatestVersionForBranchChecksOrdering()
{
App::bind(StorageInterface::class, fn() => $this->getStorageMock([
"Joomla_5.2.3-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.2.3 Release",
"version" => "5.2.3",
"stability" => "stable",
]
],
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.2.1 Release",
"version" => "5.2.1",
"stability" => "stable",
]
],
"Joomla_5.2.2-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.2.2 Release",
"version" => "5.2.2",
"stability" => "stable",
]
]
]));

$object = new TufFetcher();
$result = $object->getLatestVersionForBranch(5);

$this->assertEquals("5.2.3", $result);
}

protected function getStorageMock(array $targets)
{
$targetsMock = $this->getMockBuilder(TargetsMetadata::class)
Expand Down

0 comments on commit 5083abc

Please sign in to comment.