From 629b5b6b2d1ff8b7314caf040b691701823745ff Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 9 May 2026 15:54:01 +0800 Subject: [PATCH] Add test-bot --- .github/workflows/tests.yml | 239 ++++++++------ src/StaticPHP/Command/Dev/TestBotCommand.php | 312 +++++++++++++++++++ src/StaticPHP/ConsoleApplication.php | 2 + 3 files changed, 460 insertions(+), 93 deletions(-) create mode 100644 src/StaticPHP/Command/Dev/TestBotCommand.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0b2979f1..57817e74 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,8 +1,8 @@ -name: Tests +name: v3 Tests on: pull_request: - branches: [ "main", "v3" ] + branches: [ "v3" ] types: [ opened, synchronize, reopened ] paths: - 'src/**' @@ -103,114 +103,167 @@ jobs: run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist - name: "Run PHPUnit Tests" - run: SPC_LIBC=glibc vendor/bin/phpunit tests/ --no-coverage + run: vendor/bin/phpunit tests/ --no-coverage - define-matrix: - if: false # TODO: enable when refactoring workflows - name: "Define Matrix" + check-gate: + name: "Check: need-test label" runs-on: ubuntu-latest outputs: - php: ${{ steps.gendef.outputs.php }} - os: ${{ steps.gendef.outputs.os }} + enabled: ${{ steps.gate.outputs.enabled }} steps: - - name: "Checkout" - uses: actions/checkout@v4 - - - name: "Setup PHP" - uses: shivammathur/setup-php@v2 - with: - php-version: 8.4 - extensions: curl, openssl, mbstring - - - name: Define - id: gendef + - name: Check label + id: gate run: | - PHP_VERSIONS=$(php src/globals/test-extensions.php php) - OS_VERSIONS=$(php src/globals/test-extensions.php os) - echo 'php='"$PHP_VERSIONS" >> "$GITHUB_OUTPUT" - echo 'os='"$OS_VERSIONS" >> "$GITHUB_OUTPUT" + LABELS='${{ toJSON(github.event.pull_request.labels.*.name) }}' + if echo "$LABELS" | grep -q '"need-test"'; then + echo "enabled=true" >> "$GITHUB_OUTPUT" + else + echo "enabled=false" >> "$GITHUB_OUTPUT" + fi - - build: - if: false - name: "Build PHP Test (PHP ${{ matrix.php }} ${{ matrix.os }})" - runs-on: ${{ matrix.os }} - needs: [define-matrix, php-cs-fixer, phpstan, phpunit] - timeout-minutes: 120 - strategy: - matrix: - php: ${{ fromJSON(needs.define-matrix.outputs.php) }} - os: ${{ fromJSON(needs.define-matrix.outputs.os) }} - fail-fast: false + test-bot: + name: "Test Bot: analyze PR" + needs: check-gate + if: needs.check-gate.outputs.enabled == 'true' + runs-on: ubuntu-latest + permissions: + pull-requests: write + contents: read + outputs: + need_test: ${{ steps.bot.outputs.need_test }} + gen_matrix_args: ${{ steps.bot.outputs.gen_matrix_args }} + gen_matrix_args_tier2: ${{ steps.bot.outputs.gen_matrix_args_tier2 }} + php_versions: ${{ steps.bot.outputs.php_versions }} + tier2: ${{ steps.bot.outputs.tier2 }} steps: - - name: "Update runner packages" - if: ${{ startsWith(matrix.os, 'ubuntu-') }} - run: sudo apt-get update && sudo apt-get install -y ca-certificates + - uses: actions/checkout@v4 - - name: "Checkout" - uses: actions/checkout@v4 - - - name: "Setup PHP" + - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.4 - tools: pecl, composer + php-version: '8.4' extensions: curl, openssl, mbstring ini-values: memory_limit=-1 + tools: composer + + - name: Install dependencies + run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --no-dev + + - name: Run dev:test-bot + id: bot + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + BOT_JSON=$(php -d opcache.enable_cli=0 bin/spc dev:test-bot \ + --pr=${{ github.event.pull_request.number }} \ + --repo=${{ github.repository }} 2>/dev/null) + + echo "need_test=$(echo "$BOT_JSON" | jq -r '.need_test')" >> "$GITHUB_OUTPUT" + echo "gen_matrix_args=$(echo "$BOT_JSON" | jq -r '.gen_matrix_args')" >> "$GITHUB_OUTPUT" + echo "gen_matrix_args_tier2=$(echo "$BOT_JSON" | jq -r '.gen_matrix_args_tier2')" >> "$GITHUB_OUTPUT" + echo "php_versions=$(echo "$BOT_JSON" | jq -c '.php_versions')" >> "$GITHUB_OUTPUT" + echo "tier2=$(echo "$BOT_JSON" | jq -r '.tier2')" >> "$GITHUB_OUTPUT" + + COMMENT_BODY=$(echo "$BOT_JSON" | jq -r '.comment_body') + MARKER="" + + # Find existing bot comment id + EXISTING_ID=$(gh api \ + repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \ + --jq "[.[] | select(.body | startswith(\"$MARKER\")) | .id] | first // empty") + + if [ -n "$EXISTING_ID" ]; then + gh api --method PATCH \ + repos/${{ github.repository }}/issues/comments/"$EXISTING_ID" \ + -f body="$COMMENT_BODY" + else + gh pr comment ${{ github.event.pull_request.number }} \ + --repo ${{ github.repository }} \ + --body "$COMMENT_BODY" + fi + + gen-matrix: + name: "Generate test matrix" + needs: test-bot + if: needs.test-bot.outputs.need_test == 'true' + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.build.outputs.matrix }} + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.4' + extensions: curl, openssl, mbstring + ini-values: memory_limit=-1 + tools: composer + + - name: Install dependencies + run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --no-dev + + - name: Build matrix + id: build + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GEN_MATRIX_ARGS: ${{ needs.test-bot.outputs.gen_matrix_args }} + GEN_MATRIX_ARGS_TIER2: ${{ needs.test-bot.outputs.gen_matrix_args_tier2 }} + PHP_VERSIONS: ${{ needs.test-bot.outputs.php_versions }} + TIER2: ${{ needs.test-bot.outputs.tier2 }} + run: | + # Tier1 matrix + MATRIX1=$(bin/spc dev:gen-ext-test-matrix $GEN_MATRIX_ARGS 2>/dev/null) + + # Merge Tier2 if requested + if [ "$TIER2" = "true" ] && [ -n "$GEN_MATRIX_ARGS_TIER2" ]; then + MATRIX2=$(bin/spc dev:gen-ext-test-matrix $GEN_MATRIX_ARGS_TIER2 2>/dev/null) + COMBINED=$(jq -n --argjson m1 "$MATRIX1" --argjson m2 "$MATRIX2" '$m1 + $m2') + else + COMBINED=$MATRIX1 + fi + + # Expand PHP versions: cartesian product of entries × php_versions + FINAL=$(echo "$COMBINED" | jq --argjson versions "$PHP_VERSIONS" \ + '[.[] | . as $entry | $versions[] | $entry + {"php-version": .}]') + + echo "matrix=$(echo "$FINAL" | jq -c '{"combo": .}')" >> "$GITHUB_OUTPUT" + + ext-test: + name: "Ext test: ${{ matrix.combo.extension }} (PHP ${{ matrix.combo.php-version }} · ${{ matrix.combo.os }}-${{ matrix.combo.arch }})" + needs: gen-matrix + runs-on: ${{ matrix.combo.runner }} + timeout-minutes: 120 + strategy: + fail-fast: false + matrix: ${{ fromJSON(needs.gen-matrix.outputs.matrix) }} + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.4 + extensions: curl, openssl, mbstring + ini-values: memory_limit=-1 + tools: composer env: phpts: nts - - name: "Cache composer packages" - id: composer-cache - uses: actions/cache@v4 - with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- + - name: Install dependencies + run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --no-dev - # Cache downloaded source - - id: cache-download - uses: actions/cache@v4 - with: - path: downloads - key: php-dependencies-${{ matrix.os }} - - - name: "Install Dependencies" - run: composer update -vvv --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --no-plugins - - - name: "Run Build Tests (doctor)" - run: php src/globals/test-extensions.php doctor_cmd ${{ matrix.os }} ${{ matrix.php }} - - - name: "Prepare UPX for Windows" - if: ${{ startsWith(matrix.os, 'windows-') }} + - name: Build + env: + SPC_USE_SUDO: "yes" + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - php src/globals/test-extensions.php install_upx_cmd ${{ matrix.os }} ${{ matrix.php }} - echo "UPX_CMD=$(php src/globals/test-extensions.php upx)" >> $env:GITHUB_ENV + ./bin/spc doctor --auto-fix + ${{ matrix.combo.build-args }} - - name: "Prepare UPX for Linux" - if: ${{ startsWith(matrix.os, 'ubuntu-') }} - run: | - php src/globals/test-extensions.php install_upx_cmd ${{ matrix.os }} ${{ matrix.php }} - echo "UPX_CMD=$(php src/globals/test-extensions.php upx)" >> $GITHUB_ENV - - - name: "Run Build Tests (download)" - run: php src/globals/test-extensions.php download_cmd ${{ matrix.os }} ${{ matrix.php }} - - - name: "Run Build Tests (build)" - run: php src/globals/test-extensions.php build_cmd ${{ matrix.os }} ${{ matrix.php }} - - - name: "Run Build Tests (build - embed for non-windows)" - if: ${{ !startsWith(matrix.os, 'windows-') }} - run: php src/globals/test-extensions.php build_embed_cmd ${{ matrix.os }} ${{ matrix.php }} - - - name: "Upload logs" - if: ${{ always() && hashFiles('log/**') != '' }} - uses: actions/upload-artifact@v7 + - name: Upload logs + if: always() && hashFiles('log/**') != '' + uses: actions/upload-artifact@v4 with: - name: build-logs-${{ matrix.os }}-${{ matrix.php }} + name: logs-${{ matrix.combo.os }}-${{ matrix.combo.arch }}-${{ matrix.combo.extension }}-php${{ matrix.combo.php-version }} path: log - -# - name: Setup tmate session -# if: ${{ failure() }} -# uses: mxschmitt/action-tmate@v3 diff --git a/src/StaticPHP/Command/Dev/TestBotCommand.php b/src/StaticPHP/Command/Dev/TestBotCommand.php new file mode 100644 index 00000000..0d237818 --- /dev/null +++ b/src/StaticPHP/Command/Dev/TestBotCommand.php @@ -0,0 +1,312 @@ + 'Linux', + 'test/windows' => 'Windows', + 'test/macos' => 'Darwin', + ]; + + private const string TIER2_LABEL = 'test/tier2'; + + /** PHP version labels → version string (8.5 is always included as default) */ + private const array PHP_VERSION_LABELS = [ + 'test/php-83' => '8.3', + 'test/php-84' => '8.4', + ]; + + private const string DEFAULT_PHP_VERSION = '8.5'; + + protected bool $no_motd = true; + + public function configure(): void + { + $this->addOption('pr', null, InputOption::VALUE_REQUIRED, 'Pull request number') + ->addOption('repo', null, InputOption::VALUE_REQUIRED, 'Repository in owner/repo format (e.g. owner/repo)') + ->addOption('mock-files', null, InputOption::VALUE_REQUIRED, 'Comma-separated file paths to simulate PR changed files (skips GitHub API, for local testing)', '') + ->addOption('mock-labels', null, InputOption::VALUE_REQUIRED, 'Comma-separated labels to simulate PR labels (skips GitHub API, for local testing)', ''); + } + + public function handle(): int + { + $mock_files_raw = (string) $this->input->getOption('mock-files'); + $mock_labels_raw = (string) $this->input->getOption('mock-labels'); + $is_mock = $mock_files_raw !== '' || $mock_labels_raw !== ''; + + if ($is_mock) { + // Local testing mode: skip all GitHub API calls + $changed_files = array_map( + fn ($f) => ['filename' => trim($f)], + array_filter(explode(',', $mock_files_raw)) + ); + $label_names = array_map('trim', array_filter(explode(',', $mock_labels_raw))); + } else { + $pr = (int) $this->input->getOption('pr'); + $repo = (string) $this->input->getOption('repo'); + + if ($pr <= 0 || $repo === '') { + $this->output->writeln('Either --mock-files/--mock-labels (local test) or --pr and --repo (live) are required.'); + return static::USER_ERROR; + } + + $headers = array_merge( + $this->getGitHubTokenHeaders(), + ['Accept: application/vnd.github+json', 'X-GitHub-Api-Version: 2022-11-28'], + ); + + // Fetch changed files (paginated, up to 300) + $changed_files = $this->fetchPaginatedFiles($repo, $pr, $headers); + + // Fetch current labels on the PR/issue + $labels_raw = $this->apiGet( + sprintf('%s/repos/%s/issues/%d/labels', self::API_BASE, $repo, $pr), + $headers + ); + $label_names = array_column($labels_raw ?? [], 'name'); + } + + // Analyze changed files → extensions, libs, targets + [$extensions, $libs, $targets] = $this->analyzeChangedFiles($changed_files); + + // Resolve active platform OS keys (used as filters, not as trigger) + $os_keys = []; + foreach (self::PLATFORM_LABELS as $label => $os_key) { + if (in_array($label, $label_names, true)) { + $os_keys[] = $os_key; + } + } + $tier2 = in_array(self::TIER2_LABEL, $label_names, true); + $need_test = in_array('need-test', $label_names, true); + + // Resolve PHP versions (default always included) + $php_versions = [self::DEFAULT_PHP_VERSION]; + foreach (self::PHP_VERSION_LABELS as $label => $version) { + if (in_array($label, $label_names, true)) { + $php_versions[] = $version; + } + } + $php_versions = array_unique($php_versions); + sort($php_versions); + + // Build gen_matrix_args whenever need-test is set. + // Platform labels narrow the OS scope; absent = no --os filter (all platforms). + $gen_matrix_args = ''; + $gen_matrix_args_tier2 = ''; + if ($need_test) { + $flag_parts = []; + if (!empty($extensions)) { + $flag_parts[] = '--for-extensions=' . implode(',', $extensions); + } + if (!empty($libs)) { + $flag_parts[] = '--for-libs=' . implode(',', $libs); + } + if (!empty($os_keys)) { + $flag_parts[] = '--os=' . implode(',', $os_keys); + } + $gen_matrix_args = implode(' ', $flag_parts); + + if ($tier2) { + // Tier2 covers Linux + macOS only (never Windows) + $tier2_os = array_values(array_filter( + !empty($os_keys) ? $os_keys : ['Linux', 'Darwin'], + fn ($k) => $k !== 'Windows' + )); + if (!empty($tier2_os)) { + $tier2_parts = array_values(array_filter($flag_parts, fn ($f) => !str_starts_with($f, '--os='))); + $tier2_parts[] = '--os=' . implode(',', $tier2_os); + $tier2_parts[] = '--tier2'; + $gen_matrix_args_tier2 = implode(' ', $tier2_parts); + } + } + } + + $comment_body = $this->buildCommentBody( + $extensions, + $libs, + $targets, + $label_names, + $os_keys, + $tier2, + $php_versions, + $need_test, + ); + + $result = [ + 'need_test' => $need_test, + 'extensions' => array_values($extensions), + 'libs' => array_values($libs), + 'targets' => array_values($targets), + 'gen_matrix_args' => $gen_matrix_args, + 'gen_matrix_args_tier2' => $gen_matrix_args_tier2, + 'php_versions' => array_values($php_versions), + 'tier2' => $tier2, + 'comment_body' => $comment_body, + ]; + + $this->output->write(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + return static::SUCCESS; + } + + /** + * Fetch all changed files for a PR across up to 3 pages (max 300 files). + */ + private function fetchPaginatedFiles(string $repo, int $pr, array $headers): array + { + $files = []; + for ($page = 1; $page <= 3; ++$page) { + $url = sprintf('%s/repos/%s/pulls/%d/files?per_page=100&page=%d', self::API_BASE, $repo, $pr, $page); + $batch = $this->apiGet($url, $headers); + if (empty($batch)) { + break; + } + $files = array_merge($files, $batch); + if (count($batch) < 100) { + break; + } + } + return $files; + } + + /** + * Perform a GET request and return decoded JSON array, or null on failure. + */ + private function apiGet(string $url, array $headers): ?array + { + $data = default_shell()->executeCurl($url, headers: $headers); + $decoded = json_decode($data ?: '', true); + return is_array($decoded) ? $decoded : null; + } + + /** + * Analyze changed file paths and classify them into extensions, libs, and targets. + * + * @return array{string[], string[], string[]} + */ + private function analyzeChangedFiles(array $files): array + { + $extensions = []; + $libs = []; + $targets = []; + + foreach ($files as $file) { + $path = $file['filename'] ?? ''; + + if (preg_match('#^src/Package/Extension/([^/]+)\.php$#', $path, $m)) { + $name = strtolower($m[1]); + $extensions[$name] = $name; + } elseif (preg_match('#^config/pkg/ext/ext-([^/]+)\.yml$#', $path, $m)) { + $extensions[$m[1]] = $m[1]; + } elseif (preg_match('#^src/Package/Library/([^/]+)\.php$#', $path, $m)) { + $name = strtolower($m[1]); + $libs[$name] = $name; + } elseif (preg_match('#^config/pkg/lib/([^/]+)\.yml$#', $path, $m)) { + $libs[$m[1]] = $m[1]; + } elseif (preg_match('#^src/Package/Target/([^/]+)\.php$#', $path, $m)) { + $name = strtolower($m[1]); + $targets[$name] = $name; + } elseif (preg_match('#^config/pkg/target/([^/]+)\.yml$#', $path, $m)) { + $targets[$m[1]] = $m[1]; + } + } + + sort($extensions); + sort($libs); + sort($targets); + + return [$extensions, $libs, $targets]; + } + + private function buildCommentBody( + array $extensions, + array $libs, + array $targets, + array $label_names, + array $os_keys, + bool $tier2, + array $php_versions, + bool $need_test, + ): string { + $fmt = static fn (array $items): string => !empty($items) + ? '`' . implode('`, `', $items) . '`' + : '_none_'; + + $detected = sprintf( + '**Detected**: Extensions: %s | Libraries: %s | Targets: %s', + $fmt($extensions), + $fmt($libs), + $fmt($targets), + ); + + // Case 1: need-test absent → invite the author to add it + if (!$need_test) { + return implode("\n", [ + '', + '**StaticPHP Test Bot**', + '', + $detected, + '', + 'To trigger extension build tests on this PR, add the `need-test` label:', + '', + '**Gate**: `need-test`', + '**Platform filter** (optional, default all): `test/linux` `test/windows` `test/macos` · `test/tier2`', + '**PHP version** (optional, default 8.5): `test/php-83` `test/php-84`', + ]); + } + + // Case 2: need-test present → show what will run + // os_keys empty = no filter = all platforms + $effective_os = !empty($os_keys) + ? $os_keys + : array_values(self::PLATFORM_LABELS); // all OS keys + + $platform_parts = []; + foreach (self::PLATFORM_LABELS as $_label => $os_key) { + if (!in_array($os_key, $effective_os, true)) { + continue; + } + $platform_parts[] = match ($os_key) { + 'Linux' => 'Linux x86_64', + 'Darwin' => 'macOS arm64', + 'Windows' => 'Windows x86_64', + default => $os_key, + }; + } + if ($tier2) { + if (in_array('Linux', $effective_os, true)) { + $platform_parts[] = 'Linux aarch64 (Tier2)'; + } + if (in_array('Darwin', $effective_os, true)) { + $platform_parts[] = 'macOS x86_64 (Tier2)'; + } + } + + $php_str = implode(', ', array_map(fn ($v) => "PHP {$v}", $php_versions)) . ' NTS'; + $active_test_labels = array_values(array_filter($label_names, fn ($l) => str_starts_with($l, 'test/'))); + $labels_str = !empty($active_test_labels) ? '`' . implode('`, `', $active_test_labels) . '`' : '_none_'; + + return implode("\n", [ + '', + '**StaticPHP Test Bot**', + '', + $detected, + '**Active labels**: ' . $labels_str, + '**Config**: ' . implode(' + ', $platform_parts) . ' | ' . $php_str, + ]); + } +} diff --git a/src/StaticPHP/ConsoleApplication.php b/src/StaticPHP/ConsoleApplication.php index cc10554e..cf305e05 100644 --- a/src/StaticPHP/ConsoleApplication.php +++ b/src/StaticPHP/ConsoleApplication.php @@ -19,6 +19,7 @@ use StaticPHP\Command\Dev\LintConfigCommand; use StaticPHP\Command\Dev\PackageInfoCommand; use StaticPHP\Command\Dev\PackLibCommand; use StaticPHP\Command\Dev\ShellCommand; +use StaticPHP\Command\Dev\TestBotCommand; use StaticPHP\Command\DoctorCommand; use StaticPHP\Command\DownloadCommand; use StaticPHP\Command\DumpExtensionsCommand; @@ -87,6 +88,7 @@ class ConsoleApplication extends Application new GenExtDocsCommand(), new GenDepsDataCommand(), new GenExtTestMatrixCommand(), + new TestBotCommand(), ]); // add additional commands from registries