Skip to content

Commit

Permalink
FIX Do not use random number for predictable int
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed May 17, 2024
1 parent 7e78631 commit 7f53703
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 38 deletions.
20 changes: 5 additions & 15 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions scripts/cms-any/dispatch-ci.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

// run on two consecutive days of the week
$dayOfWeek = predictable_random_int(6);
$dayOfWeek = predictable_random_int('dispatch-ci', 6);
$nextDayOfWeek = $dayOfWeek === 6 ? 0 : $dayOfWeek + 1;
$runsOnDaysOfWeek = sprintf('%s,%s', $dayOfWeek, $nextDayOfWeek);
// run at a random hour of the day
$runOnHour = predictable_random_int(23);
$runOnHour = predictable_random_int('dispatch-ci', 23);
// run at a random minute of the hour rounded to 5 minutes
$runOnMinute = predictable_random_int(11) * 5;
$runOnMinute = predictable_random_int('dispatch-ci', 11) * 5;

$cron = "$runOnMinute $runOnHour * * $runsOnDaysOfWeek";
$humanCron = human_cron($cron);
Expand Down
6 changes: 3 additions & 3 deletions scripts/cms-any/keepalive.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

// run on a day of the month up to the 28th
$runOnDay = predictable_random_int(27, 1);
$runOnDay = predictable_random_int('keepalive', 27, 1);
// run at a random hour of the day
$runOnHour = predictable_random_int(23);
$runOnHour = predictable_random_int('keepalive', 23);
// run at a random minute of the hour rounded to 5 minutes
$runOnMinute = predictable_random_int(11) * 5;
$runOnMinute = predictable_random_int('keepalive', 11) * 5;

$cron = "$runOnMinute $runOnHour $runOnDay * *";
$humanCron = human_cron($cron);
Expand Down
6 changes: 3 additions & 3 deletions scripts/cms-any/merge-ups.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

// run on a random day of the week
$runOnDay = predictable_random_int(6);
$runOnDay = predictable_random_int('merge-ups', 6);
// run at a random hour of the day
$runOnHour = predictable_random_int(23);
$runOnHour = predictable_random_int('merge-ups', 23);
// run at a random minute of the hour rounded to 5 minutes
$runOnMinute = predictable_random_int(11) * 5;
$runOnMinute = predictable_random_int('merge-ups', 11) * 5;

// If there's a CI workflow, offset mergeups from the CI run by 3 days
if (check_file_exists('.github/workflows/dispatch-ci.yml')) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/cms-any/update-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
$account = module_account();

// run at a random hour of the day
$runOnHour = predictable_random_int(23);
$runOnHour = predictable_random_int('update-js', 23);
// run at a random minute of the hour rounded to 5 minutes
$runOnMinute = predictable_random_int(11) * 5;
$runOnMinute = predictable_random_int('update-js', 11) * 5;
// run on a 1st of the month
$runOnDay = 1;

Expand Down
25 changes: 13 additions & 12 deletions tests/FuncsScriptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ class FuncsScriptsTest extends TestCase
{
public function testPredictableRandomInt()
{
global $MODULE_DIR;
$MODULE_DIR = 'lorem';
$this->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));
}
}
1 change: 1 addition & 0 deletions update_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f53703

Please sign in to comment.