diff --git a/funcs_scripts.php b/funcs_scripts.php index 979d8fe..e972f87 100644 --- a/funcs_scripts.php +++ b/funcs_scripts.php @@ -350,21 +350,11 @@ function human_cron(string $cron): string * between 1 and 28 * Note that this will return the exact same value every time it is called for a given filename in a given module */ -function predictable_random_int($max, $offset = 0): int +function predictable_random_int($scriptName, $max, $offset = 0): int { - global $MODULE_DIR; - $callingFile = debug_backtrace()[0]['file']; - // remove absolute path e.g. /home/myuser/... - $moduleStandardiserDir = basename(__DIR__); - $dirQuoted = preg_quote($moduleStandardiserDir); - // double $dirQuoted is for github actions CI where there which will have a directory strcture - // with /module-standardiser/module-standardiser/... - if (!preg_match("#/$dirQuoted/$dirQuoted/(.+)$#", $callingFile, $matches)) { - preg_match("#/$dirQuoted/(.+)$#", $callingFile, $matches); - } - $relativePath = $matches[1]; - $chars = str_split("$MODULE_DIR-$relativePath"); + $chars = str_split(module_name() . $scriptName); $codes = array_map(fn($c) => ord($c), $chars); - mt_srand(array_sum($codes)); - return mt_rand(0, $max) + $offset; + $sum = array_sum($codes); + $remainder = $sum % ($max + 1); + return $remainder + $offset; } diff --git a/scripts/cms-any/dispatch-ci.php b/scripts/cms-any/dispatch-ci.php index 41e2450..eb0ec48 100644 --- a/scripts/cms-any/dispatch-ci.php +++ b/scripts/cms-any/dispatch-ci.php @@ -1,13 +1,13 @@ assertSame(0, predictable_random_int(15)); - $this->assertSame(25, predictable_random_int(30)); - $this->assertSame(45, predictable_random_int(30, 20)); - $MODULE_DIR = 'donuts'; - $this->assertSame(13, predictable_random_int(15)); - // use eval to simulate calling from a different file - // it will suffix "(19) : eval()'d code" to the calling file in debug_backtrace() - $ret = null; - eval('$ret = predictable_random_int(15);'); - $this->assertSame(2, $ret); + global $GITHUB_REF; + // set $GITHUB_REF because by module_name() which is used by predictable_random_int() + $GITHUB_REF = 'myaccount/lorem'; + $this->assertSame(1, predictable_random_int('test-script', 15)); + // Setting a higher max does more than just add to the result, it's somewhat random + $this->assertSame(23, predictable_random_int('test-script', 30)); + // Setting an offset simply adds to the result of the same max as above + $this->assertSame(43, predictable_random_int('test-script', 30, 20)); + // Changing $GITHUB_REF will change the result + $GITHUB_REF = 'myaccount/donuts'; + $this->assertSame(15, predictable_random_int('test-script', 15)); + // Changing the script name will change the result + $this->assertSame(6, predictable_random_int('different-script', 15)); } } diff --git a/update_command.php b/update_command.php index da64500..e4ad904 100644 --- a/update_command.php +++ b/update_command.php @@ -47,6 +47,7 @@ $cloneUrl = $module['cloneUrl']; $MODULE_DIR = MODULES_DIR . "/$repo"; $GITHUB_REF = "$account/$repo"; + // clone repo // always clone the actual remote even when doing update-prs even though this is slower // reason is because we read origin in .git/config to workout the actual $account in