Skip to content

Commit

Permalink
FIX Various fixes for processing tooling, workflow and misc repos
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed May 20, 2024
1 parent 7f53703 commit 3c9b13e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 13 deletions.
68 changes: 67 additions & 1 deletion funcs_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ function is_module()
return false;
}

if (is_gha_repository()) {
// gha-generate-matrix has a composer.json, but it's not a module
return false;
}

$contents = read_file('composer.json');
$json = json_decode($contents);
if (is_null($json)) {
Expand All @@ -158,7 +163,7 @@ function is_module()
}

// config isn't technically a Silverstripe CMS module, but we treat it like one.
if ($json->name === 'silverstripe/config') {
if (($json->name ?? '') === 'silverstripe/config') {
return true;
}

Expand Down Expand Up @@ -238,6 +243,7 @@ function is_docs()

/**
* Determine if the module being processed is a gha-* repository e.g. gha-ci
* aka "WORKFLOW"
*
* Example usage:
* is_gha_repository()
Expand All @@ -254,6 +260,56 @@ function is_gha_repository()
);
}

/**
* Determine if the module being processed is "TOOLING"
*
* Example usage:
* is_gha_repository()
*/
function is_tooling()
{
global $GITHUB_REF;
return in_array(
$GITHUB_REF,
array_column(
MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_TOOLING],
'github'
)
);
}

/**
* Determine if the module being processed is "MISC"
*
* Example usage:
* is_gha_repository()
*/
function is_misc()
{
global $GITHUB_REF;
return in_array(
$GITHUB_REF,
array_column(
MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_MISC],
'github'
)
);
}

/**
* Determine if the module being processed has a wildcard major version mapping
* in silverstripe/supported-modules repositories.json
*
* Example usage:
* has_wildcard_major_version_mapping()
*/
function has_wildcard_major_version_mapping()
{
global $GITHUB_REF;
$repoData = MetaData::getMetaDataForRepository($GITHUB_REF);
return array_key_exists('*', $repoData['majorVersionMapping']);
}

/**
* Return the module name without the account e.g. silverstripe/silverstripe-admin with return silverstripe-admin
*
Expand Down Expand Up @@ -358,3 +414,13 @@ function predictable_random_int($scriptName, $max, $offset = 0): int
$remainder = $sum % ($max + 1);
return $remainder + $offset;
}

/**
* Determine if the current brain is either main or master
*/
function current_branch_is_non_semver()
{
global $MODULE_DIR;
$currentBranch = cmd('git rev-parse --abbrev-ref HEAD', $MODULE_DIR);
return in_array($currentBranch, ['main', 'master']);
}
12 changes: 10 additions & 2 deletions funcs_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,23 @@ function current_branch_cms_major(
return MetaData::HIGHEST_STABLE_CMS_MAJOR;
}

$contents = '';
if ($composerJson) {
$contents = $composerJson;
} elseif (check_file_exists('composer.json')) {
$contents = read_file('composer.json');
}
$composerJson = json_decode($contents);
if (is_null($composerJson)) {
$lastError = json_last_error();
error("Could not parse from composer.json - last error was $lastError");
// allow composer.json to be missing for "misc" repos
// except for those repos that do have composer.json file
if (!is_misc() || in_array($GITHUB_REF, [
'silverstripe/silverstripe-frameworktest',
'silverstripe/silverstripe-module'
])) {
$lastError = json_last_error();
error("Could not parse from composer.json - last error was $lastError");
}
}

$repoData = MetaData::getMetaDataForRepository($GITHUB_REF);
Expand Down
7 changes: 7 additions & 0 deletions scripts/cms-any/dispatch-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@

$dispatchCiPath = '.github/workflows/dispatch-ci.yml';
$ciPath = '.github/workflows/ci.yml';
// old logic
$shouldHaveDispatchCi = (is_module() || is_composer_plugin()) && !is_docs() && !is_gha_repository();
// new logic to ensure that behat-extension has dispatch-ci.yml
// We could possibly delete the old logic, though I'm halfway through updating existing PRs
// so do not want to risk breaking this for the next run
if (!has_wildcard_major_version_mapping()) {
$shouldHaveDispatchCi = true;
}

if ($shouldHaveDispatchCi) {
if (check_file_exists($ciPath)) {
Expand Down
7 changes: 5 additions & 2 deletions scripts/cms-any/merge-ups.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
rename_file_if_exists('.github/workflows/merge-ups.yml', '.github/workflows/merge-up.yml');
}

if (!module_is_recipe() && !is_meta_repo()) {
write_file_even_if_exists('.github/workflows/merge-up.yml', $content);
if (!module_is_recipe() && !current_branch_is_non_semver()) {
write_file_even_if_exists('.github/workflows/merge-up.yml', $content);
} else {
// remove any merge-up.yml that was previously added though shouldn't be there
delete_file_if_exists('.github/workflows/merge-up.yml');
}
2 changes: 1 addition & 1 deletion scripts/cms-any/update-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
uses: silverstripe/gha-update-js@v1
EOT;

if (check_file_exists('package.json')) {
if (check_file_exists('package.json') && module_name() !== 'silverstripe/supported-module') {
write_file_even_if_exists('.github/workflows/update-js.yml', $content);
}
2 changes: 1 addition & 1 deletion scripts/cms5/phpstan.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Only valid for non-theme modules
if (!is_module() || is_theme() || module_is_recipe()) {
if (!is_module() || is_theme() || module_is_recipe() || is_tooling()) {
return;
}

Expand Down
15 changes: 9 additions & 6 deletions update_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@
}
cmd("git remote add pr-remote $prOrigin", $MODULE_DIR);

$useDefaultBranch = has_wildcard_major_version_mapping();

if ($input->getOption('update-prs')) {
// checkout latest existing pr branch
cmd('git fetch pr-remote', $MODULE_DIR);
$allBranches = explode("\n", cmd('git branch -r', $MODULE_DIR));
// example branch name: pulls/5/module-standardiser-1691550112
$allBranches = array_map('trim', $allBranches);
$allBranches = array_filter($allBranches, function($branch) {
return preg_match('#^pr\-remote/pulls/[0-9\.]+/module\-standardiser\-[0-9]{10}$#', $branch);
return preg_match('#^pr\-remote/pulls/.+?/module\-standardiser\-[0-9]{10}$#', $branch);
});
if (empty($allBranches)) {
warning("Could not find an existing PR branch for $repo - skipping");
Expand Down Expand Up @@ -108,11 +110,12 @@
$defaultBranch = cmd($cmd, $MODULE_DIR);
cmd("git checkout $defaultBranch", $MODULE_DIR);

if (is_meta_repo()) {
$branchToCheckout = $allBranches[0];
$currentBranch = cmd('git rev-parse --abbrev-ref HEAD', $MODULE_DIR);

// checkout the branch to run scripts over
if ($useDefaultBranch) {
$branchToCheckout = $currentBranch;
} else {
// checkout the branch to run scripts over
$currentBranch = cmd('git rev-parse --abbrev-ref HEAD', $MODULE_DIR);
// ensure that we're on a standard next-minor style branch
if (!ctype_digit($currentBranch)) {
$tmp = array_filter($allBranches, fn($branch) => ctype_digit($branch));
Expand All @@ -139,7 +142,7 @@
cmd("git checkout $branchToCheckout", $MODULE_DIR);

// ensure that this branch actually supports the cmsMajor we're targetting
if ($branchOption !== 'github-default' && current_branch_cms_major() !== $cmsMajor) {
if (!$useDefaultBranch && $branchOption !== 'github-default' && current_branch_cms_major() !== $cmsMajor) {
error("Branch $branchToCheckout does not support CMS major version $cmsMajor");
}

Expand Down

0 comments on commit 3c9b13e

Please sign in to comment.