From ef0405c37fb8550b55a867cd14ac3b9228c2bfe8 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 21 Dec 2023 11:40:14 +1300 Subject: [PATCH] NEW Update issue/PR templates on default branches --- README.md | 27 ++- funcs_scripts.php | 12 ++ funcs_utils.php | 17 +- run.php | 2 +- .../issue-pr-templates-docs.php | 106 ++++++++++ scripts/default-branch/issue-pr-templates.php | 191 ++++++++++++++++++ tests/FuncsUtilsTest.php | 18 +- update_command.php | 17 +- 8 files changed, 360 insertions(+), 30 deletions(-) create mode 100644 scripts/default-branch/issue-pr-templates-docs.php create mode 100644 scripts/default-branch/issue-pr-templates.php diff --git a/README.md b/README.md index 15be2e5..557be79 100644 --- a/README.md +++ b/README.md @@ -41,32 +41,39 @@ MS_GITHUB_TOKEN=abc123 php run.php update --cms-major=5 --branch=next-minor --dr | Flag | Description | | ---- | ------------| | --cms-major=[version] | The major version of CMS to use (default: 5) | -| --branch=[type] | The branch type to use - next-minor\|next-patch (default: next-patch) | -| --only=[modules] | Only include the specified modules (without account prefix) separated by commas e.g. silverstripe-config,silverstripe-assets | -| --exclude=[modules] | Exclude the specified modules (without account prefix) separated by commas e.g. silverstripe-mfa,silverstripe-totp | +| --branch=[type] | The branch type to use - `next-minor`\|`next-patch`\|`github-default` (default: `next-patch`) | +| --only=[modules] | Only include the specified modules (without account prefix) separated by commas e.g. `silverstripe-config,silverstripe-assets` | +| --exclude=[modules] | Exclude the specified modules (without account prefix) separated by commas e.g. `silverstripe-mfa,silverstripe-totp` | | --dry-run | Do not push to github or create pull-requests | | --account | GitHub account to use for creating pull-requests (default: creative-commoners) | -| --no-delete | Do not delete _data and _modules directories before running | +| --no-delete | Do not delete `_data` and `modules` directories before running | | --update-prs | Update existing open PRs instead of creating new PRs | +**Note** that using `--branch=github-default` will only run scripts in the `scripts/default-branch` directory. + ## GitHub API secondary rate limit -You may hit a secondary GitHub rate limit because this tool may create too many pull-requests in a short space of time. -To help with this the tool will always output the urls of all pull-requests updated and also the repos that were +You may hit a secondary GitHub rate limit because this tool may create too many pull-requests in a short space of time. +To help with this the tool will always output the urls of all pull-requests updated and also the repos that were updated so you can add them to the --exclude flag on subsequent re-runs. ## Adding new scripts -Simply add new scripts to either `scripts/cms-` to run on a specific cms-major or `scripts/cms-any` to run -on any cms-major and they will be automatically picked up and run when the tool is run. Code in the script will be +### Where to add your script + +- `scripts/cms-` to run on a specific cms-major +- `scripts/cms-any` to run on any cms-major +- `scripts/default-branch` to only run on the default branch in GitHub + +Scripts will be automatically picked up and run when the tool is run. Code in the script will be passed through `eval()` on the module that is currently being processed. -Make use of functions in `funcs_scripts.php` such as `write_file_if_not_exist()` and `read_file()` to access the +Make use of functions in `funcs_scripts.php` such as `write_file_if_not_exist()` and `read_file()` to access the correct files in the module that is currently being processed and also to ensure that console output is consistent. Do not use functions in `funcs_utils.php` as they are not intended to be used in scripts. -Scripts will be automatically wrapped in an anoymous function so you do not need to worry about variables crossing +Scripts will be automatically wrapped in an anoymous function so you do not need to worry about variables crossing over into different scripts. ## Updating the tool when a new major version of CMS is updated diff --git a/funcs_scripts.php b/funcs_scripts.php index 819a9af..6082824 100644 --- a/funcs_scripts.php +++ b/funcs_scripts.php @@ -132,6 +132,18 @@ function is_module() && strpos($MODULE_DIR, '/webpack-config') === false; } +/** + * Determine if the module being processed is a source of documentation + * + * Example usage: + * is_docs() + */ +function is_docs() +{ + $moduleName = module_name(); + return $moduleName === 'developer-docs' || $moduleName === 'silverstripe-userhelp-content'; +} + /** * Determine if the module being processed is a gha-* repository e.g. gha-ci * diff --git a/funcs_utils.php b/funcs_utils.php index c1c701f..a25749d 100644 --- a/funcs_utils.php +++ b/funcs_utils.php @@ -127,11 +127,15 @@ function extra_repositories() */ function script_files($cmsMajor) { - if (!ctype_digit($cmsMajor)) { - $cmsMajor = "-$cmsMajor"; + if ($cmsMajor === 'default-branch') { + $dir = 'scripts/default-branch'; + } else { + if (!ctype_digit($cmsMajor)) { + $cmsMajor = "-$cmsMajor"; + } + $scriptFiles = []; + $dir = "scripts/cms$cmsMajor"; } - $scriptFiles = []; - $dir = "scripts/cms$cmsMajor"; if (!file_exists($dir)) { warning("$dir does not exist, no CMS $cmsMajor specific scripts will be run"); return $scriptFiles; @@ -296,7 +300,7 @@ function output_repos_with_prs_created() * * Assumes that for each module there is only a single major version per cms-major version */ -function branch_to_checkout($branches, $currentBranch, $currentBranchCmsMajor, $cmsMajor, $branchOption) +function branch_to_checkout($branches, $defaultBranch, $currentBranch, $currentBranchCmsMajor, $cmsMajor, $branchOption) { $offset = (int) $cmsMajor - (int) $currentBranchCmsMajor; $majorTarget = (int) $currentBranch + $offset; @@ -304,6 +308,9 @@ function branch_to_checkout($branches, $currentBranch, $currentBranchCmsMajor, $ usort($branches, 'version_compare'); $branches = array_reverse($branches); switch ($branchOption) { + case 'github-default': + $branchToCheckout = $defaultBranch; + break; case 'next-patch': $branchToCheckout = array_values(array_filter( $branches, diff --git a/run.php b/run.php index 78c830d..34a028a 100644 --- a/run.php +++ b/run.php @@ -10,7 +10,7 @@ // consts const CURRENT_CMS_MAJOR = '5'; -const BRANCH_OPTIONS = ['next-minor', 'next-patch']; +const BRANCH_OPTIONS = ['next-minor', 'next-patch', 'github-default']; const DEFAULT_BRANCH = 'next-patch'; const DEFAULT_ACCOUNT = 'creative-commoners'; const DATA_DIR = '_data'; diff --git a/scripts/default-branch/issue-pr-templates-docs.php b/scripts/default-branch/issue-pr-templates-docs.php new file mode 100644 index 0000000..a168ee2 --- /dev/null +++ b/scripts/default-branch/issue-pr-templates-docs.php @@ -0,0 +1,106 @@ + +## Description + + +## Issues + +- # + +## PR merge checklist + +- [ ] The target branch is correct + - See [branches and commit messages](https://docs.silverstripe.org/en/contributing/documentation#branches-and-commit-messages) +- [ ] All commits are relevant to the purpose of the PR (e.g. no TODO comments, unrelated rewording/restructuring, or arbitrary changes) + - Small amounts of additional changes are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR. +- [ ] The commit messages follow our [commit message guidelines](https://docs.silverstripe.org/en/contributing/code/#commit-messages) +- [ ] The PR follows our [contribution guidelines](https://docs.silverstripe.org/en/contributing/documentation/) +- [ ] The changes follow our [writing style guide](https://docs.silverstripe.org/en/contributing/documentation/#writing-style) +- [ ] Code examples follow our [coding conventions](https://docs.silverstripe.org/en/contributing/coding_conventions/) +- [ ] CI is green +EOT; + +// The template used when clicking "Open a blank issue" +$defaultIssueTemplate = ""; + +// Defines configuration for form-style issue templates +$config = << $pullRequestTemplate, + '.github/ISSUE_TEMPLATE.md' => $defaultIssueTemplate, + '.github/ISSUE_TEMPLATE/config.yml' => $config, + '.github/ISSUE_TEMPLATE/1_docs_issue.yml' => $documentationIssueTemplate, +]; + +// See issue-pr-templates.php for the non-docs equivalent +if (!is_docs()) { + return; +} + +foreach ($files as $path => $contents) { + write_file_even_if_exists($path, $contents); +} diff --git a/scripts/default-branch/issue-pr-templates.php b/scripts/default-branch/issue-pr-templates.php new file mode 100644 index 0000000..8dbc275 --- /dev/null +++ b/scripts/default-branch/issue-pr-templates.php @@ -0,0 +1,191 @@ + +## Description + + +## Manual testing steps + + +## Issues + +- # + +## PR merge checklist + +- [ ] The target branch is correct + - See [picking the right version](https://docs.silverstripe.org/en/contributing/code/#picking-the-right-version) +- [ ] All commits are relevant to the purpose of the PR (e.g. no debug statements, unrelated refactoring, or arbitrary linting) + - Small amounts of additional linting are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR. +- [ ] The commit messages follow our [commit message guidelines](https://docs.silverstripe.org/en/contributing/code/#commit-messages) +- [ ] The PR follows our [contribution guidelines](https://docs.silverstripe.org/en/contributing/code/) +- [ ] Code changes follow our [coding conventions](https://docs.silverstripe.org/en/contributing/coding_conventions/) +- [ ] This change is covered with tests (or tests aren't necessary for this change) +- [ ] Any relevant User Help/Developer documentation is updated; for impactful changes, information is added to the changelog for the intended release +- [ ] CI is green +EOT; + +// The template used when clicking "Open a blank issue" +$defaultIssueTemplate = ""; + +// Defines configuration for form-style issue templates +$config = << $pullRequestTemplate, + '.github/ISSUE_TEMPLATE.md' => $defaultIssueTemplate, + '.github/ISSUE_TEMPLATE/config.yml' => $config, + '.github/ISSUE_TEMPLATE/1_bug_report.yml' => $bugReportTemplate, + '.github/ISSUE_TEMPLATE/2_feature_request.yml' => $featureRequestTemplate, +]; + +// See issue-pr-templates-docs.php for the docs equivalent +if (is_docs()) { + return; +} + +foreach ($files as $path => $contents) { + write_file_even_if_exists($path, $contents); +} diff --git a/tests/FuncsUtilsTest.php b/tests/FuncsUtilsTest.php index f2e2f8f..017ca0d 100644 --- a/tests/FuncsUtilsTest.php +++ b/tests/FuncsUtilsTest.php @@ -10,12 +10,13 @@ class FuncsUtilsTest extends TestCase public function testBranchToCheckout( $expected, $branches, + $defaultBranch, $currentBranch, $currentBranchCmsMajor, $cmsMajor, $branchOption ) { - $actual = branch_to_checkout($branches, $currentBranch, $currentBranchCmsMajor, $cmsMajor, $branchOption); + $actual = branch_to_checkout($branches, $defaultBranch, $currentBranch, $currentBranchCmsMajor, $cmsMajor, $branchOption); $this->assertSame($expected, $actual); } @@ -23,13 +24,14 @@ public function provideBranchToCheckout() { $branches = ['1.5', '1.6', '1', '2.0', '2.1' , '2.2', '2', '3', 'pulls/2.3/something', 'random']; return [ - ['2', $branches, '2', '5', '5', 'next-minor'], - ['2.2', $branches, '2', '5', '5', 'next-patch'], - ['1', $branches, '2', '5', '4', 'next-minor'], - ['1.6', $branches, '2', '5', '4', 'next-patch'], - ['2', $branches, '1', '4', '5', 'next-minor'], - ['2.2', $branches, '1', '4', '5', 'next-patch'], - ['3', $branches, '1', '4', '6', 'next-minor'], + ['2', $branches, '2', '2', '5', '5', 'next-minor'], + ['2.2', $branches, '2', '2', '5', '5', 'next-patch'], + ['1', $branches, '2', '2', '5', '4', 'next-minor'], + ['1.6', $branches, '2', '2', '5', '4', 'next-patch'], + ['2', $branches, '2', '1', '4', '5', 'next-minor'], + ['2.2', $branches, '2', '1', '4', '5', 'next-patch'], + ['3', $branches, '2', '1', '4', '6', 'next-minor'], + ['2', $branches, '2', '1', '4', '6', 'github-default'], ]; } diff --git a/update_command.php b/update_command.php index 2a17f90..2736c97 100644 --- a/update_command.php +++ b/update_command.php @@ -56,10 +56,14 @@ } // script files - $scriptFiles = array_merge( - script_files('any'), - script_files($cmsMajor), - ); + if ($branchOption === 'github-default') { + $scriptFiles = script_files('default-branch'); + } else { + $scriptFiles = array_merge( + script_files('any'), + script_files($cmsMajor), + ); + } // clone repos & run scripts foreach ($modules as $module) { @@ -138,6 +142,7 @@ $currentBranchCmsMajor = current_branch_cms_major(); $branchToCheckout = branch_to_checkout( $allBranches, + $defaultBranch, $currentBranch, $currentBranchCmsMajor, $cmsMajor, @@ -150,7 +155,7 @@ cmd("git checkout $branchToCheckout", $MODULE_DIR); // ensure that this branch actually supports the cmsMajor we're targetting - if (current_branch_cms_major() !== $cmsMajor) { + if ($branchOption !== 'github-default' && current_branch_cms_major() !== $cmsMajor) { error("Branch $branchToCheckout does not support CMS major version $cmsMajor"); } @@ -196,7 +201,7 @@ // force pushing for cases when doing update-prs // double make check we're on a branch that we are willing to force push $currentBranch = cmd('git rev-parse --abbrev-ref HEAD', $MODULE_DIR); - if (!preg_match('#^pulls/[0-9\.]+/module\-standardiser\-[0-9]{10}$#', $currentBranch)) { + if (!preg_match('#^pulls/([0-9\.]+|master|main)/module\-standardiser\-[0-9]{10}$#', $currentBranch)) { error("Branch $currentBranch is not a pull-request branch"); } cmd("git push -f -u pr-remote $prBranch", $MODULE_DIR);