diff --git a/composer.json b/composer.json index 56314bf..bf19828 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/Tasks/UpdatePackageInfoTask.php b/src/Tasks/UpdatePackageInfoTask.php index a0230d2..b2558f7 100644 --- a/src/Tasks/UpdatePackageInfoTask.php +++ b/src/Tasks/UpdatePackageInfoTask.php @@ -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. @@ -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. @@ -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; } @@ -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 diff --git a/src/Util/ApiLoader.php b/src/Util/ApiLoader.php index 17c391a..8075b94 100644 --- a/src/Util/ApiLoader.php +++ b/src/Util/ApiLoader.php @@ -16,6 +16,7 @@ /** * Handles fetching supported module details + * @deprecated 3.2.0 Will be removed without equivalent functionality */ abstract class ApiLoader { @@ -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'); } diff --git a/src/Util/SupportedAddonsLoader.php b/src/Util/SupportedAddonsLoader.php index 7ade8cd..72ed7ae 100644 --- a/src/Util/SupportedAddonsLoader.php +++ b/src/Util/SupportedAddonsLoader.php @@ -2,11 +2,26 @@ 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 * @@ -14,9 +29,13 @@ class SupportedAddonsLoader extends ApiLoader */ 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'] + )); }); } diff --git a/tests/Util/SupportedAddonsLoaderTest.php b/tests/Util/SupportedAddonsLoaderTest.php index 084d53a..413544f 100644 --- a/tests/Util/SupportedAddonsLoaderTest.php +++ b/tests/Util/SupportedAddonsLoaderTest.php @@ -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 () { @@ -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], + ], + ], ], ];