Skip to content

Commit

Permalink
Merge pull request #17 from creative-commoners/pulls/1.0/detection
Browse files Browse the repository at this point in the history
Handle different repositories
  • Loading branch information
GuySartorelli authored Aug 22, 2023
2 parents 8b9062a + f6d57d8 commit f54f9bf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
24 changes: 23 additions & 1 deletion funcs.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

// This should always match default branch of silverstripe/framework
const CURRENT_CMS_MAJOR = 5;

function branches(
string $defaultBranch,
string $minimumCmsMajor,
Expand All @@ -21,7 +24,20 @@ function branches(
$defaultMajor = $matches[1];

// read __composer.json of the current (default) branch
$contents = $composerJson ?: file_get_contents('__composer.json');
if ($composerJson) {
$contents = $composerJson;
} elseif (file_exists('__composer.json')) {
$contents = file_get_contents('__composer.json');
} else {
// respository such as silverstripe/eslint-config or silverstripe/gha-auto-tag
// make some fake json so that this branch is treated as though it's latest supported major version
$contents = json_encode([
'require' => [
'silverstripe/framework' => '^' . CURRENT_CMS_MAJOR,
],
], JSON_UNESCAPED_SLASHES);
}

$json = json_decode($contents);
if (is_null($json)) {
$lastError = json_last_error();
Expand All @@ -46,6 +62,12 @@ function branches(
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/assets'} ?? '');
$matchedOnBranchThreeLess = true;
}
if (!$version) {
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'cwp/starter-theme'} ?? '');
if ($version) {
$version += 1;
}
}
if (preg_match('#^([0-9]+)+\.?[0-9]*$#', $version, $matches)) {
$defaultCmsMajor = $matches[1];
if ($matchedOnBranchThreeLess) {
Expand Down
58 changes: 58 additions & 0 deletions tests/BranchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,64 @@ public function provideBranches()
]
EOT,
],
'cwp-watea-theme' => [
'expected' => ['3.2', '3', '4.0', '4'],
'defaultBranch' => '4',
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
"cwp/starter-theme": "^4"
}
}
EOT,
'branchesJson' => <<<EOT
[
{"name": "3"},
{"name": "3.0"},
{"name": "3.1"},
{"name": "3.2"},
{"name": "4"},
{"name": "4.0"}
]
EOT,
'tagsJson' => <<<EOT
[
{"name": "4.0.0"},
{"name": "5.0.9"},
{"name": "3.2.0"},
{"name": "3.1.0"},
{"name": "3.0.0"}
]
EOT,
],
'gha-ci' => [
'expected' => ['1.4', '1'],
'defaultBranch' => '1',
'minimumCmsMajor' => '4',
'githubRepository' => 'silverstripe/gha-ci',
'composerJson' => '',
'branchesJson' => <<<EOT
[
{"name": "1"},
{"name": "1.0"},
{"name": "1.1"},
{"name": "1.2"},
{"name": "1.3"},
{"name": "1.4"}
]
EOT,
'tagsJson' => <<<EOT
[
{"name": "1.4.0"},
{"name": "1.3.0"},
{"name": "1.2.0"},
{"name": "1.1.0"},
{"name": "1.0.0"}
]
EOT,
],
];
}
}

0 comments on commit f54f9bf

Please sign in to comment.