Skip to content

Commit

Permalink
ENH Use new main branch of supported-modules data
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed May 8, 2024
1 parent baf2287 commit ec09587
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 17 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"php": "^8.1",
"silverstripe/framework": "^5",
"silverstripe/reports": "^5",
"silverstripe/supported-modules": "dev-main",
"symbiote/silverstripe-queuedjobs": "^5",
"guzzlehttp/guzzle": "^7.5"
},
Expand Down
24 changes: 15 additions & 9 deletions src/Tasks/UpdatePackageInfoTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
use SilverStripe\ORM\Queries\SQLDelete;
use SilverStripe\ORM\DataObjectSchema;
use BringYourOwnIdeas\Maintenance\Model\Package;
use SilverStripe\Core\Manifest\VersionProvider;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Deprecation;
use SilverStripe\SupportedModules\BranchLogic;
use SilverStripe\SupportedModules\MetaData;

/**
* Parses a composer lock file in order to cache information about the installation.
Expand Down Expand Up @@ -169,10 +172,6 @@ public function run($request)
$composerLock = $this->getComposerLoader()->getLock();
$rawPackages = array_merge($composerLock->packages, (array) $composerLock->{'packages-dev'});
$packages = $this->getPackageInfo($rawPackages);

// Get "name" from $packages and put into an array
$moduleNames = array_column($packages ?? [], 'Name');

$supportedPackages = $this->getSupportedPackages();

// Extensions to the process that add data may rely on external services.
Expand Down Expand Up @@ -217,15 +216,22 @@ public function getPackageInfo($packageList)
}

/**
* Return an array of supported modules as fetched from silverstripe/supported-modules. Outputs a message and returns null
* if an error occurs
* Return an array of supported modules as fetched from silverstripe/supported-modules.
* Outputs a message and returns null if an error occurs
*
* @return null|array
*/
public function getSupportedPackages()
{
try {
return $this->getSupportedAddonsLoader()->getAddonNames() ?: [];
$repos = MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_SUPPORTED];
$version = VersionProvider::singleton()->getModuleVersion('silverstripe/framework');
preg_match('/^([0-9]+)/', $version, $matches);
$cmsMajor = BranchLogic::getCmsMajor(MetaData::getMetaDataForRepository('silverstripe/silverstripe-framework'), $matches[1] ?? '');
return array_filter(array_map(
fn(array $item) => isset($item['majorVersionMapping'][$cmsMajor]) ? $item['packagist'] : null,
$repos
));
} catch (RuntimeException $exception) {
echo $exception->getMessage() . PHP_EOL;
}
Expand All @@ -234,8 +240,8 @@ public function getSupportedPackages()
}

/**
* Return an array of module health information as fetched from addons.silverstripe.org. Outputs a message and
* returns null if an error occurs
* Return an array of module health information as fetched from addons.silverstripe.org.
* Outputs a message and returns null if an error occurs
*
* @param string[] $moduleNames
* @return null|array
Expand Down
7 changes: 6 additions & 1 deletion src/Util/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* Handles fetching supported module details
* @deprecated 3.2.0 Will be removed without equivalent functionality
*/
abstract class ApiLoader
{
Expand Down Expand Up @@ -75,7 +76,11 @@ public function doRequest($endpoint, callable $callback)
$responseJson = $this->parseResponseContents($response->getBody()->getContents(), $failureMessage);

if (str_contains($endpoint, 'addons.silverstripe.org')) {
Deprecation::notice('3.2.0', 'addons.silverstripe.org is no longer operational. Use packagist instead.', Deprecation::SCOPE_GLOBAL);
Deprecation::notice(
'3.2.0',
'addons.silverstripe.org is no longer operational. Use packagist instead.',
Deprecation::SCOPE_GLOBAL
);
if (!isset($responseJson['success']) || !$responseJson['success']) {
throw new RuntimeException($failureMessage . 'Response returned unsuccessfully');
}
Expand Down
23 changes: 21 additions & 2 deletions src/Util/SupportedAddonsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,40 @@

namespace BringYourOwnIdeas\Maintenance\Util;

use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask;
use SilverStripe\Dev\Deprecation;

/**
* Handles fetching supported addon details from silverstripe/supported-modules
* @deprecated 3.2.0 Use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask::getSupportedPackages() instead.
*/
class SupportedAddonsLoader extends ApiLoader
{
public function __construct()
{
Deprecation::withNoReplacement(
fn() => Deprecation::notice(
'3.2.0',
'Use ' . UpdatePackageInfoTask::class . '::getSupportedPackages() instead.',
Deprecation::SCOPE_CLASS
)
);
}

/**
* Return the list of supported modules
*
* @return array
*/
public function getAddonNames()
{
$endpoint = 'https://raw.githubusercontent.com/silverstripe/supported-modules/5/modules.json';
// Check for a cached value and return if one is available
$endpoint = 'https://raw.githubusercontent.com/silverstripe/supported-modules/main/repositories.json';
return $this->doRequest($endpoint, function ($responseJson) {
return array_map(fn(array $item) => $item['composer'], $responseJson);
return array_filter(array_map(
fn(array $item) => isset($item['majorVersionMapping'][5]) ? $item['packagist'] : null,
$responseJson['supportedModules']
));
});
}

Expand Down
56 changes: 51 additions & 5 deletions tests/Util/SupportedAddonsLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp(): void

public function testCallsSupportedAddonsEndpoint()
{
$endpoint = 'https://raw.githubusercontent.com/silverstripe/supported-modules/5/modules.json';
$endpoint = 'https://raw.githubusercontent.com/silverstripe/supported-modules/main/repositories.json';
$this->loader->expects($this->once())
->method('doRequest')
->with($endpoint, function () {
Expand All @@ -42,11 +42,57 @@ public function testCallbackReturnsAddonsFromBody()

$result = $this->loader->getAddonNames();
$mockResponse = [
[
'composer' => 'foo/bar'
'supportedModules' => [
[
'github' => 'some/repo1',
'packagist' => 'foo/bar',
'majorVersionMapping' => [
4 => [4],
5 => [5],
],
],
[
'github' => 'some/repo2',
'packagist' => 'bin/baz',
'majorVersionMapping' => [
5 => [5, 6],
],
],
[
'github' => 'some/repo3',
'packagist' => 'bin/baz2',
'majorVersionMapping' => [
4 => [4],
],
],
],
[
'composer' => 'bin/baz'
'workflow' => [
[
'github' => 'some/repo4',
'packagist' => 'bin/baz1',
'majorVersionMapping' => [
4 => [4],
5 => [5],
],
],
],
'tooling' => [
[
'github' => 'some/repo5',
'packagist' => 'bin/baz2',
'majorVersionMapping' => [
5 => [5, 6],
],
],
],
'misc' => [
[
'github' => 'some/repo6',
'packagist' => 'bin/baz3',
'majorVersionMapping' => [
4 => [4],
],
],
],
];

Expand Down

0 comments on commit ec09587

Please sign in to comment.