Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Fetch respositories.json from raw.githubusercontent.com #30

Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
composer.lock
composer.lock
.phpunit.result.cache
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Metadata and some supporting PHP logic for determining which branches of various
> [!IMPORTANT]
> Only the `main` branch of this repository is maintained.

You can fetch the JSON by simply fetching the raw copy of `repositories.json` file, e.g. <https://raw.githubusercontent.com/silverstripe/supported-modules/main/repositories.json>, though you're encouraged to use composer to pull in the data instead where appropriate.
You can fetch the JSON by simply fetching the raw copy of `repositories.json` file, e.g. <https://raw.githubusercontent.com/silverstripe/supported-modules/main/repositories.json>.

If you've included this module as a compser dependency then you can use `SilverStripe\SupportedModules\MetaData::getAllRepositoryMetaData()` which will fetch the latest version of the JSON file from raw.githubusercontent.com.

## Format

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Default">
<directory>tests</directory>
Expand Down
14 changes: 13 additions & 1 deletion src/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

final class MetaData
{
public static $isRunningUnitTests = false;
Copy link
Member

@GuySartorelli GuySartorelli May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should ideally either use a global constant, or better yet have a property for the path set to 'https://raw.githubusercontent.com/silverstripe/supported-modules/main/repositories.json' by default but which unit tests set to the path of the raw file in this repo.
Or for the cleanest and most standard way, mock the request in the unit test.

That said, it's probably not worth doing another round of review for my perfectionism :p This works well enough.


public const CATEGORY_SUPPORTED = 'supportedModules';

public const CATEGORY_WORKFLOW = 'workflow';
Expand Down Expand Up @@ -175,7 +177,17 @@ public static function removeReposNotInCmsMajor(array $metadata, string|int $cms
public static function getAllRepositoryMetaData(bool $categorised = true): array
{
if (empty(self::$repositoryMetaData)) {
$rawJson = file_get_contents(__DIR__ . '/../repositories.json');
if (self::$isRunningUnitTests) {
$rawJson = file_get_contents(__DIR__ . '/../repositories.json');
} else {
// Dynamicallly fetch the latest data from the supported-modules repository
// rather than reading it locally. This is done do that the data is always up-to-date
// and there's no need to run composer update and deploy the code to get the latest data.
$rawJson = file_get_contents('https://raw.githubusercontent.com/silverstripe/supported-modules/main/repositories.json');
if (!$rawJson) {
throw new RuntimeException('Could not fetch repositories.json data');
}
}
$decodedJson = json_decode($rawJson, true);
if ($decodedJson === null) {
throw new RuntimeException('Could not parse repositories.json data: ' . json_last_error_msg());
Expand Down
7 changes: 7 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use SilverStripe\SupportedModules\MetaData;

require_once __DIR__ . '/../vendor/autoload.php';

MetaData::$isRunningUnitTests = true;
Loading