-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomizeWorkflowTimes.php
26 lines (24 loc) · 1.11 KB
/
randomizeWorkflowTimes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
$workflowDirectory = __DIR__ . '/.github/workflows';
$workflowFiles = scandir($workflowDirectory);
$workflowFiles = array_filter($workflowFiles, fn ($file) => str_ends_with($file, '.yml'));
$workflowTimes = [];
foreach ($workflowFiles as $workflowFile) {
$workflowContents = file_get_contents($workflowDirectory . '/' . $workflowFile);
try {
$randomHour = random_int(0, 23);
$randomMinute = random_int(0, 59);
$randomDayOfWeek = random_int(0, 6);
$workflowContents = preg_replace('/- cron: "\d+ \d+ \* \* \d+"/', '- cron: "' . $randomMinute . ' ' . $randomHour . ' * * ' . $randomDayOfWeek . '"', $workflowContents, 1, $count);
if ($count !== 1) {
throw new \RuntimeException('Failed to replace cron in ' . $workflowFile);
}
file_put_contents($workflowDirectory . '/' . $workflowFile, $workflowContents);
} catch (\Random\RandomException $e) {
echo 'Failed to generate random number: ' . $e->getMessage() . PHP_EOL;
exit(1);
} catch (\RuntimeException $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
}
}