mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-09 09:55:37 +08:00
Add test-bot
This commit is contained in:
239
.github/workflows/tests.yml
vendored
239
.github/workflows/tests.yml
vendored
@@ -1,8 +1,8 @@
|
|||||||
name: Tests
|
name: v3 Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ "main", "v3" ]
|
branches: [ "v3" ]
|
||||||
types: [ opened, synchronize, reopened ]
|
types: [ opened, synchronize, reopened ]
|
||||||
paths:
|
paths:
|
||||||
- 'src/**'
|
- 'src/**'
|
||||||
@@ -103,114 +103,167 @@ jobs:
|
|||||||
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
|
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
|
||||||
|
|
||||||
- name: "Run PHPUnit Tests"
|
- name: "Run PHPUnit Tests"
|
||||||
run: SPC_LIBC=glibc vendor/bin/phpunit tests/ --no-coverage
|
run: vendor/bin/phpunit tests/ --no-coverage
|
||||||
|
|
||||||
define-matrix:
|
check-gate:
|
||||||
if: false # TODO: enable when refactoring workflows
|
name: "Check: need-test label"
|
||||||
name: "Define Matrix"
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
outputs:
|
outputs:
|
||||||
php: ${{ steps.gendef.outputs.php }}
|
enabled: ${{ steps.gate.outputs.enabled }}
|
||||||
os: ${{ steps.gendef.outputs.os }}
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: Check label
|
||||||
uses: actions/checkout@v4
|
id: gate
|
||||||
|
|
||||||
- name: "Setup PHP"
|
|
||||||
uses: shivammathur/setup-php@v2
|
|
||||||
with:
|
|
||||||
php-version: 8.4
|
|
||||||
extensions: curl, openssl, mbstring
|
|
||||||
|
|
||||||
- name: Define
|
|
||||||
id: gendef
|
|
||||||
run: |
|
run: |
|
||||||
PHP_VERSIONS=$(php src/globals/test-extensions.php php)
|
LABELS='${{ toJSON(github.event.pull_request.labels.*.name) }}'
|
||||||
OS_VERSIONS=$(php src/globals/test-extensions.php os)
|
if echo "$LABELS" | grep -q '"need-test"'; then
|
||||||
echo 'php='"$PHP_VERSIONS" >> "$GITHUB_OUTPUT"
|
echo "enabled=true" >> "$GITHUB_OUTPUT"
|
||||||
echo 'os='"$OS_VERSIONS" >> "$GITHUB_OUTPUT"
|
else
|
||||||
|
echo "enabled=false" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
test-bot:
|
||||||
build:
|
name: "Test Bot: analyze PR"
|
||||||
if: false
|
needs: check-gate
|
||||||
name: "Build PHP Test (PHP ${{ matrix.php }} ${{ matrix.os }})"
|
if: needs.check-gate.outputs.enabled == 'true'
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ubuntu-latest
|
||||||
needs: [define-matrix, php-cs-fixer, phpstan, phpunit]
|
permissions:
|
||||||
timeout-minutes: 120
|
pull-requests: write
|
||||||
strategy:
|
contents: read
|
||||||
matrix:
|
outputs:
|
||||||
php: ${{ fromJSON(needs.define-matrix.outputs.php) }}
|
need_test: ${{ steps.bot.outputs.need_test }}
|
||||||
os: ${{ fromJSON(needs.define-matrix.outputs.os) }}
|
gen_matrix_args: ${{ steps.bot.outputs.gen_matrix_args }}
|
||||||
fail-fast: false
|
gen_matrix_args_tier2: ${{ steps.bot.outputs.gen_matrix_args_tier2 }}
|
||||||
|
php_versions: ${{ steps.bot.outputs.php_versions }}
|
||||||
|
tier2: ${{ steps.bot.outputs.tier2 }}
|
||||||
steps:
|
steps:
|
||||||
- name: "Update runner packages"
|
- uses: actions/checkout@v4
|
||||||
if: ${{ startsWith(matrix.os, 'ubuntu-') }}
|
|
||||||
run: sudo apt-get update && sudo apt-get install -y ca-certificates
|
|
||||||
|
|
||||||
- name: "Checkout"
|
- name: Setup PHP
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: "Setup PHP"
|
|
||||||
uses: shivammathur/setup-php@v2
|
uses: shivammathur/setup-php@v2
|
||||||
with:
|
with:
|
||||||
php-version: 8.4
|
php-version: '8.4'
|
||||||
tools: pecl, composer
|
|
||||||
extensions: curl, openssl, mbstring
|
extensions: curl, openssl, mbstring
|
||||||
ini-values: memory_limit=-1
|
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="<!-- spc-test-bot -->"
|
||||||
|
|
||||||
|
# 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:
|
env:
|
||||||
phpts: nts
|
phpts: nts
|
||||||
|
|
||||||
- name: "Cache composer packages"
|
- name: Install dependencies
|
||||||
id: composer-cache
|
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --no-dev
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: vendor
|
|
||||||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-php-
|
|
||||||
|
|
||||||
# Cache downloaded source
|
- name: Build
|
||||||
- id: cache-download
|
env:
|
||||||
uses: actions/cache@v4
|
SPC_USE_SUDO: "yes"
|
||||||
with:
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
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-') }}
|
|
||||||
run: |
|
run: |
|
||||||
php src/globals/test-extensions.php install_upx_cmd ${{ matrix.os }} ${{ matrix.php }}
|
./bin/spc doctor --auto-fix
|
||||||
echo "UPX_CMD=$(php src/globals/test-extensions.php upx)" >> $env:GITHUB_ENV
|
${{ matrix.combo.build-args }}
|
||||||
|
|
||||||
- name: "Prepare UPX for Linux"
|
- name: Upload logs
|
||||||
if: ${{ startsWith(matrix.os, 'ubuntu-') }}
|
if: always() && hashFiles('log/**') != ''
|
||||||
run: |
|
uses: actions/upload-artifact@v4
|
||||||
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
|
|
||||||
with:
|
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
|
path: log
|
||||||
|
|
||||||
# - name: Setup tmate session
|
|
||||||
# if: ${{ failure() }}
|
|
||||||
# uses: mxschmitt/action-tmate@v3
|
|
||||||
|
|||||||
312
src/StaticPHP/Command/Dev/TestBotCommand.php
Normal file
312
src/StaticPHP/Command/Dev/TestBotCommand.php
Normal file
@@ -0,0 +1,312 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace StaticPHP\Command\Dev;
|
||||||
|
|
||||||
|
use StaticPHP\Artifact\Downloader\Type\GitHubTokenSetupTrait;
|
||||||
|
use StaticPHP\Command\BaseCommand;
|
||||||
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
|
#[AsCommand('dev:test-bot', 'Analyze PR changes and labels, output test-bot metadata JSON', [], true)]
|
||||||
|
class TestBotCommand extends BaseCommand
|
||||||
|
{
|
||||||
|
use GitHubTokenSetupTrait;
|
||||||
|
|
||||||
|
private const string API_BASE = 'https://api.github.com';
|
||||||
|
|
||||||
|
/** Platform labels → os_key used by dev:gen-ext-test-matrix --os= */
|
||||||
|
private const array PLATFORM_LABELS = [
|
||||||
|
'test/linux' => '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('<error>Either --mock-files/--mock-labels (local test) or --pr and --repo (live) are required.</error>');
|
||||||
|
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", [
|
||||||
|
'<!-- spc-test-bot -->',
|
||||||
|
'**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", [
|
||||||
|
'<!-- spc-test-bot -->',
|
||||||
|
'**StaticPHP Test Bot**',
|
||||||
|
'',
|
||||||
|
$detected,
|
||||||
|
'**Active labels**: ' . $labels_str,
|
||||||
|
'**Config**: ' . implode(' + ', $platform_parts) . ' | ' . $php_str,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ use StaticPHP\Command\Dev\LintConfigCommand;
|
|||||||
use StaticPHP\Command\Dev\PackageInfoCommand;
|
use StaticPHP\Command\Dev\PackageInfoCommand;
|
||||||
use StaticPHP\Command\Dev\PackLibCommand;
|
use StaticPHP\Command\Dev\PackLibCommand;
|
||||||
use StaticPHP\Command\Dev\ShellCommand;
|
use StaticPHP\Command\Dev\ShellCommand;
|
||||||
|
use StaticPHP\Command\Dev\TestBotCommand;
|
||||||
use StaticPHP\Command\DoctorCommand;
|
use StaticPHP\Command\DoctorCommand;
|
||||||
use StaticPHP\Command\DownloadCommand;
|
use StaticPHP\Command\DownloadCommand;
|
||||||
use StaticPHP\Command\DumpExtensionsCommand;
|
use StaticPHP\Command\DumpExtensionsCommand;
|
||||||
@@ -87,6 +88,7 @@ class ConsoleApplication extends Application
|
|||||||
new GenExtDocsCommand(),
|
new GenExtDocsCommand(),
|
||||||
new GenDepsDataCommand(),
|
new GenDepsDataCommand(),
|
||||||
new GenExtTestMatrixCommand(),
|
new GenExtTestMatrixCommand(),
|
||||||
|
new TestBotCommand(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// add additional commands from registries
|
// add additional commands from registries
|
||||||
|
|||||||
Reference in New Issue
Block a user