Skip to content

Commit

Permalink
NEW Add merge-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 9, 2023
1 parent 98fe0ab commit e451832
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@1a18b2267f80291a81ca1d33e7c851fe09e7dfc4 # v2.22.0
with:
php-version: 7.4
php-version: 8.1

- name: Composer install
run: composer install --prefer-dist --no-progress --no-suggest --ansi --no-interaction --no-scripts --no-plugins --optimize-autoloader

- name: Install PHPUnit
run: wget https://phar.phpunit.de/phpunit-9.5.phar
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"require": {
"php": ">=7.4",
"symfony/console": "^6.3",
"symfony/process": "^6.3"
"symfony/process": "^6.3",
"panlatent/cron-expression-descriptor": "^1"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
Expand Down
18 changes: 15 additions & 3 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,26 @@ function rename_file_if_exists($oldFilename, $newFilename)
function module_is_recipe()
{
global $MODULE_DIR;
if (strpos('/recipe-', $MODULE_DIR) !== false
|| strpos('/silverstripe-installer', $MODULE_DIR) !== false
if (strpos($MODULE_DIR, '/recipe-') !== false
|| strpos($MODULE_DIR, '/silverstripe-installer') !== false
) {
return true;
}
return false;
}

/**
* Determine if the module being processed is gha-*
*
* Example usage:
* module_is_gha()
*/
function module_is_gha()
{
global $MODULE_DIR;
return strpos($MODULE_DIR, '/gha-') !== false;
}

/**
* Determine if the module being processed is one of the modules in a list
*
Expand All @@ -132,7 +144,7 @@ function module_is_one_of($repos)
if (!is_string($repo)) {
error('repo is not a string');
}
if (strpos("/$repo", $MODULE_DIR) !== false) {
if (strpos($MODULE_DIR, "/$repo") !== false) {
return true;
}
}
Expand Down
47 changes: 44 additions & 3 deletions funcs_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,38 @@ function supported_modules($cmsMajor)
'account' => explode('/', $ghrepo)[0],
'repo' => explode('/', $ghrepo)[1],
'cloneUrl' => "git@github.com:$ghrepo.git",
'branch' => max($module['branches'] ?: [-1])
];
}
return $modules;
}

/**
* Hardcoded list of unsupported modules to standardise
*
* This will only be included if the $cmsMajor is the CURRENT_CMS_MAJOR
*/
function extra_modules()
{
$ghrepos = [
'silverstripe/gha-auto-tag',
'silverstripe/gha-ci',
'silverstripe/gha-dispatch-ci',
'silverstripe/gha-generate-matrix',
'silverstripe/gha-keepalive',
'silverstripe/gha-issue',
'silverstripe/gha-pull-request',
'silverstripe/gha-run-tests',
'silverstripe/gha-merge-up',
'silverstripe/gha-tag-release',
'silverstripe/gha-update-js',
];
$modules = [];
foreach ($ghrepos as $ghrepo) {
$modules[] = [
'ghrepo' => $ghrepo,
'account' => explode('/', $ghrepo)[0],
'repo' => explode('/', $ghrepo)[1],
'cloneUrl' => "git@github.com:$ghrepo.git",
];
}
return $modules;
Expand Down Expand Up @@ -265,12 +296,22 @@ function branch_to_checkout($branches, $currentBranch, $currentBranchCmsMajor, $
return (string) $branchToCheckout;
}

/**
* Uses composer.json to workout the current branch cms major version
*
* If composer.json does not exist then it's assumed to be CURRENT_CMS_MAJOR
*/
function current_branch_cms_major(
// this param is only used for unit testing
string $composerJson = ''
) {
// read __composer.json of the current branch
$contents = $composerJson ?: read_file('composer.json');
if ($composerJson) {
$contents = $composerJson;
} elseif (check_file_exists('composer.json')) {
$contents = read_file('composer.json');
} else {
return CURRENT_CMS_MAJOR;
}

$json = json_decode($contents);
if (is_null($json)) {
Expand Down
4 changes: 3 additions & 1 deletion scripts/cms-any/editorconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@
insert_final_newline = false
EOT;

write_file_if_not_exist('.editorconfig', $contents);
if (!module_is_gha()) {
write_file_if_not_exist('.editorconfig', $contents);
}
54 changes: 54 additions & 0 deletions scripts/cms5/merge-ups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

// run 3 days offset from when a weekly CI run is, otherwise run on a random day of the week
$runOnDay = rand(0, 6);
// run at the same hour as the weekly CI run, otherwise run at a random hour of the day
$runOnHour = rand(0, 23);
// run at the same minute as the weekly CI run, otherwise run at a random minute of the hour
// rounded to 15 minutes
$runOnMinute = rand(0, 3) * 15;

if (check_file_exists('.github/workflows/dispatch-ci.yml')) {
$ci = read_file('.github/workflows/dispatch-ci.yml');
preg_match("#- cron: '(.+?) (.+?) (.+?) (.+?) (.+?)'#", $ci, $matches);
[$_, $minute, $hour, $day, $month, $dayOfWeek] = $matches;
if ($dayOfWeek !== '*') {
$days = explode(',', $dayOfWeek);
$runOnDay = ($days[count($days) - 1] + 3) % 7;
}
if ($hour !== '*') {
$hours = explode(',', $hour);
$runOnHour = $hours[0];
}
if ($minute !== '*') {
$runOnMinute = $minute;
}
}

$cron = "$runOnMinute $runOnHour * * $runOnDay";
$humanCron = human_cron($cron);
$account = module_account();

$content = <<<EOT
name: Merge-up
on:
# $humanCron
schedule:
- cron: '$cron'
workflow_dispatch:
jobs:
merge-up:
name: Merge-up
# Only run cron on the $account account
if: (github.event_name == 'schedule' && github.repository_owner == '$account') || (github.event_name != 'schedule')
runs-on: ubuntu-latest
steps:
- name: Merge-up
uses: silverstripe/gha-merge-up@v1
EOT;

if (!module_is_recipe()) {
write_file_if_not_exist('.github/workflows/merge-ups.yml', $content);
}
3 changes: 3 additions & 0 deletions update_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

// modules
$modules = supported_modules($cmsMajor);
if ($cmsMajor === CURRENT_CMS_MAJOR) {
$modules = array_merge($modules, extra_modules());
}
if ($input->getOption('only')) {
$only = explode(',', $input->getOption('only'));
$modules = array_filter($modules, function ($module) use ($only) {
Expand Down

0 comments on commit e451832

Please sign in to comment.