Skip to content

Commit

Permalink
Merge pull request #40 from creative-commoners/pulls/main/dispatch-ci
Browse files Browse the repository at this point in the history
ENH Restrict which repos get dispatch-ci
  • Loading branch information
GuySartorelli authored Feb 15, 2024
2 parents 7a6baed + 905f5fb commit 29b904d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
35 changes: 33 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 Down
12 changes: 10 additions & 2 deletions scripts/cms-any/dispatch-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
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';
$shouldHaveDispatchCi = (is_module() || is_composer_plugin()) && !is_docs() && !is_gha_repository();

if ($shouldHaveDispatchCi) {
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 29b904d

Please sign in to comment.