Skip to content

Commit f947eea

Browse files
committed
Merge branch 'main' into feature/write-update-result-log
2 parents 5dbd212 + ecdd86c commit f947eea

File tree

4 files changed

+130
-17
lines changed

4 files changed

+130
-17
lines changed

app/Jobs/CheckSiteHealth.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function handle(): void
4747
$tufFetcher = App::make(TufFetcher::class);
4848
$latestVersion = $tufFetcher->getLatestVersionForBranch((int) $this->site->cms_version[0]);
4949

50+
// No latest version for branch available, unsupported branch - return
51+
if (!$latestVersion) {
52+
return;
53+
}
54+
5055
// Available version is not newer, exit
5156
if (!version_compare($latestVersion, $this->site->cms_version, ">")) {
5257
return;

app/TUF/ReleaseData.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\TUF;
4+
5+
use App\DTO\BaseDTO;
6+
7+
class ReleaseData extends BaseDTO
8+
{
9+
public function __construct(
10+
public readonly string $version,
11+
public readonly string $stability
12+
) {
13+
}
14+
}

app/TUF/TufFetcher.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public function __construct()
1818
}
1919

2020
/**
21-
* @return Collection
21+
* @return Collection<string, ReleaseData>
2222
*/
2323
public function getReleases(): Collection
2424
{
2525
// Cache response to avoid to make constant calls on the fly
26-
return Cache::remember(
26+
$releases = Cache::remember(
2727
'cms_targets',
2828
(int) config('autoupdates.tuf_repo_cachetime') * 60, // @phpstan-ignore-line
2929
function () {
@@ -41,18 +41,32 @@ function () {
4141
throw new MetadataException("Empty target custom attribute");
4242
}
4343

44-
return [$target['custom']['version'] => $target['custom']];
44+
$release = ReleaseData::from($target['custom']);
45+
46+
return [$release->version => $release];
4547
});
4648
}
4749
);
50+
51+
if (!$releases instanceof Collection) {
52+
throw new MetadataException("Invalid release list");
53+
}
54+
55+
return $releases;
4856
}
4957

50-
public function getLatestVersionForBranch(int $branch): string
58+
public function getLatestVersionForBranch(int $branch): ?string
5159
{
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();
60+
$versionMatch = $this->getReleases()->filter(function (ReleaseData $release) use ($branch): bool {
61+
return $release->stability === "stable" && $release->version[0] === (string) $branch;
62+
})->sort(function (ReleaseData $releaseA, ReleaseData $releaseB): int {
63+
return version_compare($releaseA->version, $releaseB->version);
64+
})->last();
65+
66+
if (!$versionMatch instanceof ReleaseData) {
67+
return null;
68+
}
69+
70+
return $versionMatch->version;
5771
}
5872
}

tests/Unit/TUF/TufFetcherTest.php

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit\TUF;
44

55
use App\TUF\EloquentModelStorage;
6+
use App\TUF\ReleaseData;
67
use App\TUF\TufFetcher;
78
use Illuminate\Support\Facades\App;
89
use Tests\TestCase;
@@ -24,13 +25,15 @@ public function testGetReleasesConvertsLegitResponse()
2425
"Joomla_5.1.2-Stable-Upgrade_Package.zip" => [
2526
"custom" => [
2627
"description" => "Joomla! 5.1.2 Release",
27-
"version" => "5.1.2"
28+
"version" => "5.1.2",
29+
"stability" => "stable",
2830
]
2931
],
3032
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
3133
"custom" => [
3234
"description" => "Joomla! 5.2.1 Release",
33-
"version" => "5.2.1"
35+
"version" => "5.2.1",
36+
"stability" => "stable",
3437
]
3538
]
3639
]));
@@ -39,14 +42,16 @@ public function testGetReleasesConvertsLegitResponse()
3942
$result = $object->getReleases();
4043

4144
$this->assertEquals([
42-
"5.1.2" => [
45+
"5.1.2" => ReleaseData::from([
4346
"description" => "Joomla! 5.1.2 Release",
44-
"version" => "5.1.2"
45-
],
46-
"5.2.1" => [
47+
"version" => "5.1.2",
48+
"stability" => "stable",
49+
]),
50+
"5.2.1" => ReleaseData::from([
4751
"description" => "Joomla! 5.2.1 Release",
48-
"version" => "5.2.1"
49-
],
52+
"version" => "5.2.1",
53+
"stability" => "stable",
54+
]),
5055
], $result->toArray());
5156
}
5257

@@ -74,6 +79,81 @@ public function testGetReleasesThrowsExceptionOnMissingCustom()
7479
$object->getReleases();
7580
}
7681

82+
public function testGetLatestVersionForBranchReturnsNullForMissingBranch()
83+
{
84+
App::bind(StorageInterface::class, fn () => $this->getStorageMock([
85+
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
86+
"custom" => [
87+
"description" => "Joomla! 5.2.1 Release",
88+
"version" => "5.2.1",
89+
"stability" => "stable",
90+
]
91+
]
92+
]));
93+
94+
$object = new TufFetcher();
95+
$result = $object->getLatestVersionForBranch(6);
96+
97+
$this->assertNull($result);
98+
}
99+
100+
public function testGetLatestVersionForBranchChecksBranch()
101+
{
102+
App::bind(StorageInterface::class, fn () => $this->getStorageMock([
103+
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
104+
"custom" => [
105+
"description" => "Joomla! 5.2.1 Release",
106+
"version" => "5.2.1",
107+
"stability" => "stable",
108+
]
109+
],
110+
"Joomla_4.2.1-Stable-Upgrade_Package.zip" => [
111+
"custom" => [
112+
"description" => "Joomla! 4.2.1 Release",
113+
"version" => "4.1.2",
114+
"stability" => "stable",
115+
]
116+
]
117+
]));
118+
119+
$object = new TufFetcher();
120+
$result = $object->getLatestVersionForBranch(4);
121+
122+
$this->assertEquals("4.1.2", $result);
123+
}
124+
125+
public function testGetLatestVersionForBranchChecksOrdering()
126+
{
127+
App::bind(StorageInterface::class, fn () => $this->getStorageMock([
128+
"Joomla_5.2.3-Stable-Upgrade_Package.zip" => [
129+
"custom" => [
130+
"description" => "Joomla! 5.2.3 Release",
131+
"version" => "5.2.3",
132+
"stability" => "stable",
133+
]
134+
],
135+
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
136+
"custom" => [
137+
"description" => "Joomla! 5.2.1 Release",
138+
"version" => "5.2.1",
139+
"stability" => "stable",
140+
]
141+
],
142+
"Joomla_5.2.2-Stable-Upgrade_Package.zip" => [
143+
"custom" => [
144+
"description" => "Joomla! 5.2.2 Release",
145+
"version" => "5.2.2",
146+
"stability" => "stable",
147+
]
148+
]
149+
]));
150+
151+
$object = new TufFetcher();
152+
$result = $object->getLatestVersionForBranch(5);
153+
154+
$this->assertEquals("5.2.3", $result);
155+
}
156+
77157
protected function getStorageMock(array $targets)
78158
{
79159
$targetsMock = $this->getMockBuilder(TargetsMetadata::class)

0 commit comments

Comments
 (0)