Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions app/TUF/ReleaseData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\TUF;

use App\DTO\BaseDTO;

class ReleaseData extends BaseDTO
{
public function __construct(
public readonly string $version,
public readonly string $stability
) {
}
}
32 changes: 23 additions & 9 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 @@ -41,18 +41,32 @@ function () {
throw new MetadataException("Empty target custom attribute");
}

return [$target['custom']['version'] => $target['custom']];
$release = ReleaseData::from($target['custom']);

return [$release->version => $release];
});
}
);

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 ($release) {
return $release["stability"] === "Stable";
})->sort(function ($releaseA, $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;
}
}
96 changes: 88 additions & 8 deletions tests/Unit/TUF/TufFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit\TUF;

use App\TUF\EloquentModelStorage;
use App\TUF\ReleaseData;
use App\TUF\TufFetcher;
use Illuminate\Support\Facades\App;
use Tests\TestCase;
Expand All @@ -24,13 +25,15 @@ public function testGetReleasesConvertsLegitResponse()
"Joomla_5.1.2-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.1.2 Release",
"version" => "5.1.2"
"version" => "5.1.2",
"stability" => "stable",
]
],
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.2.1 Release",
"version" => "5.2.1"
"version" => "5.2.1",
"stability" => "stable",
]
]
]));
Expand All @@ -39,14 +42,16 @@ public function testGetReleasesConvertsLegitResponse()
$result = $object->getReleases();

$this->assertEquals([
"5.1.2" => [
"5.1.2" => ReleaseData::from([
"description" => "Joomla! 5.1.2 Release",
"version" => "5.1.2"
],
"5.2.1" => [
"version" => "5.1.2",
"stability" => "stable",
]),
"5.2.1" => ReleaseData::from([
"description" => "Joomla! 5.2.1 Release",
"version" => "5.2.1"
],
"version" => "5.2.1",
"stability" => "stable",
]),
], $result->toArray());
}

Expand Down Expand Up @@ -74,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
Loading