Skip to content

Commit

Permalink
ENH Restrict which repos get dispatch-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Feb 15, 2024
1 parent 1eaf525 commit 5fe7e57
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 25 deletions.
46 changes: 44 additions & 2 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -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';
}

/**
Expand All @@ -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
*
Expand All @@ -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
*
Expand Down
11 changes: 9 additions & 2 deletions scripts/cms-any/dispatch-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions scripts/cms-any/license.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion scripts/cms-any/merge-ups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
43 changes: 25 additions & 18 deletions update_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,41 @@
} 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
$cmd = "git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'";
$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");
}
Expand Down

0 comments on commit 5fe7e57

Please sign in to comment.