-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Use supported modules repo for branch-based logic
- Loading branch information
1 parent
5e6fcd9
commit f80c990
Showing
7 changed files
with
272 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# For more information about the properties used in | ||
# this file, please see the EditorConfig documentation: | ||
# http://editorconfig.org/ | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
*.json | ||
!composer.json | ||
.phpunit.result.cache | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
<?php | ||
|
||
$autoloadPath = __DIR__ . '/vendor/autoload.php'; | ||
if (!file_exists($autoloadPath)) { | ||
throw new RuntimeException('Run composer install before this script'); | ||
} | ||
|
||
require_once $autoloadPath; | ||
|
||
include 'funcs.php'; | ||
|
||
$defaultBranch = getenv('DEFAULT_BRANCH'); | ||
$minimumCmsMajor = getenv('MINIMUM_CMS_MAJOR'); | ||
$githubRepository = getenv('GITHUB_REPOSITORY'); | ||
|
||
$branches = branches($defaultBranch, $minimumCmsMajor, $githubRepository); | ||
$branches = branches($defaultBranch, $githubRepository); | ||
echo implode(' ', $branches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.6" | ||
}, | ||
"require": { | ||
"silverstripe/supported-modules": "dev-pulls/main/combine-all-versions" | ||
}, | ||
"repositories": { | ||
"silverstripe/supported-modules": { | ||
"type": "vcs", | ||
"url": "git@github.com:creative-commoners/supported-modules.git" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,180 +1,38 @@ | ||
<?php | ||
|
||
// This should always match default branch of silverstripe/framework | ||
const CURRENT_CMS_MAJOR = 5; | ||
use SilverStripe\SupportedModules\BranchLogic; | ||
use SilverStripe\SupportedModules\MetaData; | ||
|
||
// List of major branches to not merge up from | ||
// Add repos in here where the repo was previously unsupported | ||
// Note these are actual major branches, not CMS major versions | ||
const DO_NOT_MERGE_UP_FROM_MAJOR = [ | ||
'silverstripe/silverstripe-linkfield' => '3', | ||
]; | ||
/** | ||
* The path to the composer.json file - note that the action explicitly downloads the composer.json | ||
* file for the default branch of the repository and saves it to this path, rather than using | ||
* the composer.json file in the "current" branch. | ||
*/ | ||
const COMPOSER_JSON_PATH = '__composer.json'; | ||
|
||
function branches( | ||
string $defaultBranch, | ||
string $minimumCmsMajor, | ||
string $githubRepository, | ||
// The following params are purely for unit testing, for the actual github action it will read json files instead | ||
string $composerJson = '', | ||
string $branchesJson = '', | ||
string $tagsJson = '' | ||
) { | ||
if (!is_numeric($defaultBranch)) { | ||
throw new Exception('Default branch must be a number'); | ||
} | ||
if (!ctype_digit($minimumCmsMajor)) { | ||
throw new Exception('Minimum CMS major must be an integer'); | ||
} | ||
|
||
// work out default major | ||
preg_match('#^([0-9]+)+\.?[0-9]*$#', $defaultBranch, $matches); | ||
$defaultMajor = $matches[1]; | ||
|
||
// read __composer.json of the current (default) branch | ||
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(); | ||
throw new Exception("Could not parse __composer.json - last error was $lastError"); | ||
} | ||
$defaultCmsMajor = ''; | ||
$matchedOnBranchThreeLess = false; | ||
$version = ''; | ||
if ($githubRepository === 'silverstripe/developer-docs') { | ||
$version = $defaultBranch; | ||
} | ||
if (!$version) { | ||
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/framework'} ?? ''); | ||
} | ||
if (!$version) { | ||
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/cms'} ?? ''); | ||
} | ||
if (!$version) { | ||
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/mfa'} ?? ''); | ||
} | ||
if (!$version) { | ||
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/assets'} ?? ''); | ||
if ($version) { | ||
$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) { | ||
$defaultCmsMajor += 3; | ||
): array { | ||
if (file_exists(COMPOSER_JSON_PATH)) { | ||
$contents = file_get_contents(COMPOSER_JSON_PATH); | ||
$composerJson = json_decode($contents); | ||
if (is_null($composerJson)) { | ||
$lastError = json_last_error(); | ||
throw new Exception('Could not parse ' . COMPOSER_JSON_PATH . " - last error was $lastError"); | ||
} | ||
} else { | ||
$phpVersion = $json->require->{'php'} ?? ''; | ||
if (substr($phpVersion,0, 4) === '^7.4') { | ||
$defaultCmsMajor = 4; | ||
} elseif (substr($phpVersion,0, 4) === '^8.1') { | ||
$defaultCmsMajor = 5; | ||
} | ||
} | ||
if ($defaultCmsMajor === '') { | ||
throw new Exception('Could not work out what the default CMS major version this module uses'); | ||
} | ||
// work out major diff e.g for silverstripe/admin for CMS 5 => 5 - 2 = 3 | ||
$majorDiff = $defaultCmsMajor - $defaultMajor; | ||
|
||
$minorsWithStableTags = []; | ||
$contents = $tagsJson ?: file_get_contents('__tags.json'); | ||
foreach (json_decode($contents) as $row) { | ||
$tag = $row->name; | ||
if (!preg_match('#^([0-9]+)\.([0-9]+)\.([0-9]+)$#', $tag, $matches)) { | ||
continue; | ||
} | ||
$major = $matches[1]; | ||
$minor = $major. '.' . $matches[2]; | ||
$minorsWithStableTags[$major][$minor] = true; | ||
} | ||
|
||
$branches = []; | ||
$contents = $branchesJson ?: file_get_contents('__branches.json'); | ||
foreach (json_decode($contents) as $row) { | ||
$branch = $row->name; | ||
// filter out non-standard branches | ||
if (!preg_match('#^([0-9]+)+\.?[0-9]*$#', $branch, $matches)) { | ||
continue; | ||
} | ||
// filter out majors that are too old | ||
$major = $matches[1]; | ||
if (($major + $majorDiff) < $minimumCmsMajor) { | ||
continue; | ||
} | ||
// suffix a temporary .999 minor version to major branches so that it's sorted correctly later | ||
if (preg_match('#^[0-9]+$#', $branch)) { | ||
$branch .= '.999'; | ||
} | ||
$branches[] = $branch; | ||
} | ||
|
||
// sort so that newest is first | ||
usort($branches, 'version_compare'); | ||
$branches = array_reverse($branches); | ||
|
||
// remove the temporary .999 | ||
array_walk($branches, function(&$branch) { | ||
$branch = preg_replace('#\.999$#', '', $branch); | ||
}); | ||
|
||
// remove all branches except: | ||
// - the latest major branch in each release line | ||
// - the latest minor branch with a stable tag in each release line | ||
// - any minor branches without stable tags with a higher minor version than the latest minor with a stable tag | ||
$foundMinorInMajor = []; | ||
$foundMinorBranchWithStableTag = []; | ||
foreach ($branches as $i => $branch) { | ||
// only remove minor branches, leave major branches in | ||
if (!preg_match('#^([0-9]+)\.[0-9]+$#', $branch, $matches)) { | ||
continue; | ||
} | ||
$major = $matches[1]; | ||
if (isset($foundMinorBranchWithStableTag[$major]) && isset($foundMinorInMajor[$major])) { | ||
unset($branches[$i]); | ||
continue; | ||
} | ||
// for developer-docs which has no tags, pretend that every branch has a tag | ||
if (isset($minorsWithStableTags[$major][$branch]) || $githubRepository === 'silverstripe/developer-docs') { | ||
$foundMinorBranchWithStableTag[$major] = true; | ||
} | ||
$foundMinorInMajor[$major] = true; | ||
} | ||
|
||
// remove any branches less than or equal to DO_NOT_MERGE_UP_FROM_MAJOR | ||
if (isset(DO_NOT_MERGE_UP_FROM_MAJOR[$githubRepository])) { | ||
$doNotMergeUpFromMajor = DO_NOT_MERGE_UP_FROM_MAJOR[$githubRepository]; | ||
$branches = array_filter($branches, function($branch) use ($doNotMergeUpFromMajor) { | ||
return version_compare($branch, "$doNotMergeUpFromMajor.999999.999999", '>'); | ||
}); | ||
$composerJson = null; | ||
} | ||
|
||
// reverse the array so that oldest is first | ||
$branches = array_reverse($branches); | ||
$repoMetaData = MetaData::getMetaDataForRepository($githubRepository); | ||
$allRepoTags = array_map(fn($x) => $x->name, json_decode(file_get_contents('__tags.json'))); | ||
$allRepoBranches = array_map(fn($x) => $x->name, json_decode(file_get_contents('__branches.json'))); | ||
|
||
$branches = BranchLogic::getBranchesForMergeUp($githubRepository, $repoMetaData, $defaultBranch, $allRepoTags, $allRepoBranches, $composerJson); | ||
// max of 6 branches - also update action.yml if you need to increase this limit | ||
if (count($branches) > 6) { | ||
throw new Exception('More than 6 branches to merge up. Aborting.'); | ||
} | ||
|
||
return $branches; | ||
} |
Oops, something went wrong.