xref: /PHP-8.1/.github/nightly_matrix.php (revision e72854e8)
1<?php
2
3const BRANCHES = [
4    ['ref' => 'master', 'version' => [8, 5]],
5    ['ref' => 'PHP-8.4', 'version' => [8, 4]],
6    ['ref' => 'PHP-8.3', 'version' => [8, 3]],
7    ['ref' => 'PHP-8.2', 'version' => [8, 2]],
8    ['ref' => 'PHP-8.1', 'version' => [8, 1]],
9];
10
11function get_branch_commit_cache_file_path(): string {
12    return dirname(__DIR__) . '/branch-commit-cache.json';
13}
14
15function get_branches() {
16    $branch_commit_cache_file = get_branch_commit_cache_file_path();
17    $branch_commit_map = [];
18    if (file_exists($branch_commit_cache_file)) {
19        $branch_commit_map = json_decode(file_get_contents($branch_commit_cache_file), JSON_THROW_ON_ERROR);
20    }
21
22    $changed_branches = [];
23    foreach (BRANCHES as $branch) {
24        $previous_commit_hash = $branch_commit_map[$branch['ref']] ?? null;
25        $current_commit_hash = trim(shell_exec('git rev-parse origin/' . $branch['ref']));
26
27        if ($previous_commit_hash !== $current_commit_hash) {
28            $changed_branches[] = $branch;
29        }
30
31        $branch_commit_map[$branch['ref']] = $current_commit_hash;
32    }
33
34    file_put_contents($branch_commit_cache_file, json_encode($branch_commit_map));
35
36    return $changed_branches;
37}
38
39function get_current_version(): array {
40    $file = dirname(__DIR__) . '/main/php_version.h';
41    $content = file_get_contents($file);
42    preg_match('(^#define PHP_MAJOR_VERSION (?<num>\d+)$)m', $content, $matches);
43    $major = (int) $matches['num'];
44    preg_match('(^#define PHP_MINOR_VERSION (?<num>\d+)$)m', $content, $matches);
45    $minor = (int) $matches['num'];
46    return [$major, $minor];
47}
48
49$trigger = $argv[1] ?? 'schedule';
50$attempt = (int) ($argv[2] ?? 1);
51$monday = date('w', time()) === '1';
52$discard_cache = $monday
53    || ($trigger === 'schedule' && $attempt !== 1)
54    || $trigger === 'workflow_dispatch';
55if ($discard_cache) {
56    @unlink(get_branch_commit_cache_file_path());
57}
58$branch = $argv[3] ?? 'master';
59$branches = $branch === 'master'
60    ? get_branches()
61    : [['ref' => $branch, 'version' => get_current_version()]];
62
63$f = fopen(getenv('GITHUB_OUTPUT'), 'a');
64fwrite($f, 'branches=' . json_encode($branches, JSON_UNESCAPED_SLASHES) . "\n");
65fclose($f);
66