From 5fe7e5748d8cdbb4020e62f98dd87a52d5adbacc Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 15 Feb 2024 17:06:31 +1300 Subject: [PATCH] ENH Restrict which repos get dispatch-ci --- funcs_scripts.php | 46 +++++++++++++++++++++++++++++++-- scripts/cms-any/dispatch-ci.php | 11 ++++++-- scripts/cms-any/license.php | 4 +-- scripts/cms-any/merge-ups.php | 2 +- update_command.php | 43 +++++++++++++++++------------- 5 files changed, 81 insertions(+), 25 deletions(-) diff --git a/funcs_scripts.php b/funcs_scripts.php index eb25285..edf381e 100644 --- a/funcs_scripts.php +++ b/funcs_scripts.php @@ -134,7 +134,7 @@ function module_is_recipe() error("Could not parse from composer.json - last error was $lastError"); } - return $json->type === 'silverstripe-recipe'; + return $json->type ?? '' === 'silverstripe-recipe'; } /** @@ -167,7 +167,29 @@ function is_module() 'silverstripe-recipe', 'silverstripe-theme', ]; - return in_array($json->type, $moduleTypes); + return in_array($json->type ?? '', $moduleTypes); +} + +/** + * Determine if the module being processed is a composer plugin + * + * Example usage: + * is_composer_plugin() + */ +function is_composer_plugin() +{ + if (!check_file_exists('composer.json')) { + return false; + } + + $contents = read_file('composer.json'); + $json = json_decode($contents); + if (is_null($json)) { + $lastError = json_last_error(); + error("Could not parse from composer.json - last error was $lastError"); + } + + return $json->type ?? '' === 'composer-plugin'; } /** @@ -192,6 +214,15 @@ function is_theme() return $json->type === 'silverstripe-theme'; } +/** + * Determine if the module being processed is a meta repository + */ +function is_meta_repo() +{ + $moduleName = module_name(); + return $moduleName === '.github'; +} + /** * Determine if the module being processed is a source of documentation * @@ -216,6 +247,17 @@ function is_gha_repository() return !is_module() && strpos($MODULE_DIR, '/gha-') !== false; } +/** + * Determine if the module being processed should include a dispatch-ci.yml file + * + * Example usage: + * should_use_dispatch_ci() + */ +function should_use_dispatch_ci() +{ + return (is_module() || is_composer_plugin()) && !is_docs() && !is_gha_repository(); +} + /** * Return the module name without the account e.g. silverstripe/silverstripe-admin with return silverstripe-admin * diff --git a/scripts/cms-any/dispatch-ci.php b/scripts/cms-any/dispatch-ci.php index ac1501c..72286c0 100644 --- a/scripts/cms-any/dispatch-ci.php +++ b/scripts/cms-any/dispatch-ci.php @@ -32,6 +32,13 @@ uses: silverstripe/gha-dispatch-ci@v1 EOT; -if (check_file_exists('.github/workflows/ci.yml')) { - write_file_even_if_exists('.github/workflows/dispatch-ci.yml', $content); +$dispatchCiPath = '.github/workflows/dispatch-ci.yml'; +$ciPath = '.github/workflows/ci.yml'; + +if (should_use_dispatch_ci()) { + if (check_file_exists($ciPath)) { + write_file_even_if_exists($dispatchCiPath, $content); + } +} else { + delete_file_if_exists($dispatchCiPath); } diff --git a/scripts/cms-any/license.php b/scripts/cms-any/license.php index c25e36d..6ea6599 100644 --- a/scripts/cms-any/license.php +++ b/scripts/cms-any/license.php @@ -41,8 +41,8 @@ foreach (['LICENSE.md', 'license.md', 'license'] as $filename) { rename_file_if_exists($filename, $licenseFilename); } -// only update licence contents if module is on silverstripe account -if (module_account() === 'silverstripe') { +// only update licence contents if module is on silverstripe account and is not a meta repo +if (module_account() === 'silverstripe' && !is_meta_repo()) { if (check_file_exists($licenseFilename)) { $oldContents = read_file($licenseFilename); $newContents = str_replace('SilverStripe', 'Silverstripe', $oldContents); diff --git a/scripts/cms-any/merge-ups.php b/scripts/cms-any/merge-ups.php index 45dc22a..9c1e31d 100644 --- a/scripts/cms-any/merge-ups.php +++ b/scripts/cms-any/merge-ups.php @@ -54,6 +54,6 @@ rename_file_if_exists('.github/workflows/merge-ups.yml', '.github/workflows/merge-up.yml'); } -if (!module_is_recipe()) { +if (!module_is_recipe() && !is_meta_repo()) { write_file_even_if_exists('.github/workflows/merge-up.yml', $content); } diff --git a/update_command.php b/update_command.php index 81ed009..aabb8ac 100644 --- a/update_command.php +++ b/update_command.php @@ -94,7 +94,10 @@ } else { // get all branches $allBranches = explode("\n", cmd('git branch -r', $MODULE_DIR)); + $allBranches = array_filter($allBranches, fn($branch) => !str_contains($branch, 'HEAD ->')); $allBranches = array_map(fn($branch) => trim(str_replace('origin/', '', $branch)), $allBranches); + // reset index + $allBranches = array_values($allBranches); // reset to the default branch so that we can then calculate the correct branch to checkout // this is needed for scenarios where we may be on something unparsable like pulls/5/lorem-ipsum @@ -102,26 +105,30 @@ $defaultBranch = cmd($cmd, $MODULE_DIR); cmd("git checkout $defaultBranch", $MODULE_DIR); - // 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)); - if (empty($tmp)) { - error('Could not find a next-minor style branch'); + if (is_meta_repo()) { + $branchToCheckout = $allBranches[0]; + } 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)); + if (empty($tmp)) { + error('Could not find a next-minor style branch'); + } + $currentBranch = max($tmp); + cmd("git checkout $currentBranch", $MODULE_DIR); } - $currentBranch = max($tmp); - cmd("git checkout $currentBranch", $MODULE_DIR); + $currentBranchCmsMajor = current_branch_cms_major(); + $branchToCheckout = branch_to_checkout( + $allBranches, + $defaultBranch, + $currentBranch, + $currentBranchCmsMajor, + $cmsMajor, + $branchOption + ); } - $currentBranchCmsMajor = current_branch_cms_major(); - $branchToCheckout = branch_to_checkout( - $allBranches, - $defaultBranch, - $currentBranch, - $currentBranchCmsMajor, - $cmsMajor, - $branchOption - ); if (!in_array($branchToCheckout, $allBranches)) { error("Could not find branch to checkout for $repo using --branch=$branchOption"); }