Attempt at getting GitHub actions working

This commit is contained in:
UndefinedOffset 2022-02-16 15:19:42 -04:00
parent fff3f08901
commit c965eb0c2e
No known key found for this signature in database
GPG Key ID: 59C4EE2B6468B796
3 changed files with 447 additions and 0 deletions

23
.github/matchers/phpcs.json vendored Normal file
View File

@ -0,0 +1,23 @@
{
"problemMatcher": [
{
"owner": "phpcs",
"severity": "error",
"pattern": [
{
"regexp": "^<file name=\"(?:\\/github\\/workspace\\/)?(.*)\">$",
"file": 1
},
{
"regexp": "<error line=\"(\\d*)\" column=\"(\\d*)\" severity=\"(error|warning)\" message=\"(.*)\" source=\"(.*)(\"\\/>+)$",
"line": 1,
"column": 2,
"severity": 3,
"message": 4,
"code": 5,
"loop": true
}
]
}
]
}

312
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,312 @@
name: CI
on:
push:
branches: [ '**' ]
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:
branches: [ '**' ]
paths-ignore:
- 'docs/**'
- '*.md'
jobs:
silverstripe_410_php74:
name: "Silverstripe 4.10 | PHP 7.4"
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[ci skip]')"
env:
php_version: 7.4
php_extensions: ctype, dom, fileinfo, hash, intl, mbstring, session, simplexml, tokenizer, xml, pdo, mysqli, gd, zip
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: false
MYSQL_ROOT_PASSWORD: testpassword
MYSQL_DATABASE: test_db
ports:
- 3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP Extension Cache
id: cache-env
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
key: php74-ext-cache-${{ hashFiles('.github/workflows/ci.yml') }}
- name: Cache PHP Extensions
uses: actions/cache@v2
with:
path: ${{ steps.cache-env.outputs.dir }}
key: ${{ steps.cache-env.outputs.key }}
restore-keys: ${{ steps.cache-env.outputs.key }}
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
ini-values: log_errors=On, error_log="${{github.workspace}}/artifacts/php_errors.log"
coverage: none
tools: composer:v1
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-ss410-php74-${{ hashFiles('**/composer.json') }}-${{ hashFiles('.github/workflows/ci.yml') }}
restore-keys: ${{ runner.os }}-composer-ss410-php74-
- name: Install Composer dependencies
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer self-update --ansi || true
if [[ $GITHUB_ACCESS_TOKEN ]]; then composer config --ansi -g github-oauth.github.com $GITHUB_ACCESS_TOKEN; fi
composer require silverstripe/admin 1.10.* --no-update
composer require silverstripe/versioned 1.10.*
composer install --verbose --no-interaction --no-progress --no-suggest --optimize-autoloader --ansi
- name: Configure Environment
run: |
cp tests/utils/actions.env.template .env
mkdir artifacts
- name: Perform PHPUnit Tests
env:
SS_DATABASE_PORT: ${{ job.services.mysql.ports['3306'] }}
run: vendor/bin/phpunit --colors=always --printer UndefinedOffset\\SortableGridField\\Tests\\Bootstrap\\GitHubActionsAnnotatorPrinter
silverstripe_410_php80:
name: "Silverstripe 4.10 | PHP 8.0"
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[ci skip]')"
env:
php_version: 8.0
php_extensions: ctype, dom, fileinfo, hash, intl, mbstring, session, simplexml, tokenizer, xml, pdo, mysqli, gd, zip
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: false
MYSQL_ROOT_PASSWORD: testpassword
MYSQL_DATABASE: test_db
ports:
- 3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP Extension Cache
id: cache-env
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
key: php80-ext-cache-${{ hashFiles('.github/workflows/ci.yml') }}
- name: Cache PHP Extensions
uses: actions/cache@v2
with:
path: ${{ steps.cache-env.outputs.dir }}
key: ${{ steps.cache-env.outputs.key }}
restore-keys: ${{ steps.cache-env.outputs.key }}
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
ini-values: log_errors=On, error_log="${{github.workspace}}/artifacts/php_errors.log"
coverage: none
tools: composer:v2
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-ss410-php80-${{ hashFiles('**/composer.json') }}-${{ hashFiles('.github/workflows/ci.yml') }}
restore-keys: ${{ runner.os }}-composer-ss410-php80-
- name: Install Composer dependencies
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer self-update --ansi || true
if [[ $GITHUB_ACCESS_TOKEN ]]; then composer config --ansi -g github-oauth.github.com $GITHUB_ACCESS_TOKEN; fi
composer require silverstripe/admin 1.10.* --no-update
composer require silverstripe/versioned 1.10.*
composer install --verbose --no-interaction --no-progress --no-suggest --optimize-autoloader --ansi
- name: Configure Environment
run: |
cp tests/utils/actions.env.template .env
mkdir artifacts
- name: Perform PHPUnit Tests
env:
SS_DATABASE_PORT: ${{ job.services.mysql.ports['3306'] }}
run: vendor/bin/phpunit --colors=always --printer UndefinedOffset\\SortableGridField\\Tests\\Bootstrap\\GitHubActionsAnnotatorPrinter
silverstripe_49_php74:
name: "Silverstripe 4.9 | PHP 7.4"
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[ci skip]')"
env:
php_version: 7.4
php_extensions: ctype, dom, fileinfo, hash, intl, mbstring, session, simplexml, tokenizer, xml, pdo, mysqli, gd, zip
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: false
MYSQL_ROOT_PASSWORD: testpassword
MYSQL_DATABASE: test_db
ports:
- 3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP Extension Cache
id: cache-env
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
key: php74-ext-cache-${{ hashFiles('.github/workflows/ci.yml') }}
- name: Cache PHP Extensions
uses: actions/cache@v2
with:
path: ${{ steps.cache-env.outputs.dir }}
key: ${{ steps.cache-env.outputs.key }}
restore-keys: ${{ steps.cache-env.outputs.key }}
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
ini-values: log_errors=On, error_log="${{github.workspace}}/artifacts/php_errors.log"
coverage: none
tools: composer:v1
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-ss49-php74-${{ hashFiles('**/composer.json') }}-${{ hashFiles('.github/workflows/ci.yml') }}
restore-keys: ${{ runner.os }}-composer-ss49-php74-
- name: Install Composer dependencies
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer self-update --ansi || true
if [[ $GITHUB_ACCESS_TOKEN ]]; then composer config --ansi -g github-oauth.github.com $GITHUB_ACCESS_TOKEN; fi
composer require silverstripe/admin 1.9.* --no-update
composer require silverstripe/versioned 1.9.*
composer install --verbose --no-interaction --no-progress --no-suggest --optimize-autoloader --ansi
- name: Configure Environment
run: |
cp tests/utils/actions.env.template .env
mkdir artifacts
- name: Perform PHPUnit Tests
env:
SS_DATABASE_PORT: ${{ job.services.mysql.ports['3306'] }}
run: vendor/bin/phpunit --colors=always --printer UndefinedOffset\\SortableGridField\\Tests\\Bootstrap\\GitHubActionsAnnotatorPrinter
phpcs:
name: "PHP_CodeSniffer"
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[ci skip]')"
env:
php_version: 7.4
php_extensions: ctype, dom, fileinfo, hash, intl, mbstring, session, simplexml, tokenizer, xml, pdo, mysqli, gd, zip
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP Extension Cache
id: cache-env
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
key: php74-ext-cache-${{ hashFiles('.github/workflows/ci.yml') }}
- name: Cache PHP Extensions
uses: actions/cache@v2
with:
path: ${{ steps.cache-env.outputs.dir }}
key: ${{ steps.cache-env.outputs.key }}
restore-keys: ${{ steps.cache-env.outputs.key }}
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.php_version }}
extensions: ${{ env.php_extensions }}
ini-values: log_errors=On, error_log="${{github.workspace}}/artifacts/php_errors.log"
coverage: none
tools: composer:v2
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-phpcs-${{ hashFiles('.github/workflows/ci.yml') }}
restore-keys: ${{ runner.os }}-composer-phpcs-
- name: Install Composer dependencies
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
rm composer.json
composer self-update --ansi || true
if [[ $GITHUB_ACCESS_TOKEN ]]; then composer config --ansi -g github-oauth.github.com $GITHUB_ACCESS_TOKEN; fi
composer require squizlabs/php_codesniffer ~3.4
composer install --verbose --no-interaction --no-progress --no-suggest --optimize-autoloader --ansi
mkdir artifacts
- name: Validate Code Style
run: vendor/bin/phpcs --colors --report=full --report-checkstyle=artifacts/phpcs.xml
- name: Annotate Code Style Issues
if: failure()
run: (test -f artifacts/phpcs.xml && echo "::add-matcher::.github/matchers/phpcs.json" && cat artifacts/phpcs.xml && echo "::remove-matcher owner=phpcs::")

View File

@ -0,0 +1,112 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Bootstrap;
use PHPUnit\Framework\TestFailure;
use PHPUnit\TextUI\DefaultResultPrinter;
class GitHubActionsAnnotatorPrinter extends DefaultResultPrinter
{
protected $currentType = null;
/**
* Handles printing of the defects
* @param array $defects Array of Test Failures
* @param string $type Type of the failure
*/
protected function printDefects(array $defects, string $type): void
{
$this->currentType = $type;
parent::printDefects($defects, $type);
}
/**
* Handles printing of a single defect
* @param TestFailure $defect Test Failure Object
* @param int $count Current position
*/
protected function printDefect(TestFailure $defect, int $count): void
{
parent::printDefect($defect, $count);
$this->printGitHubAnnotation($defect);
}
/**
* Prints a GitHub Annotation Command
* @param TestFailure $defect Defect to print
*/
protected function printGitHubAnnotation(TestFailure $defect)
{
$e = $defect->thrownException();
$errorLines = array_filter(
explode("\n", (string) $e),
function ($l) {
return $l;
}
);
$error = end($errorLines);
$lineIndex = strrpos($error, ":");
$path = substr($error, 0, $lineIndex);
$line = substr($error, $lineIndex + 1);
if (!$path) {
list($path, $line) = $this->getReflectionFromTest(
$defect->getTestName()
);
}
$message = explode("\n", trim($defect->getTestName() . "\n\n" . (string) $e));
$message = implode('%0A', $message);
$type = $this->getCurrentType();
$file = "file={$this->relativePath($path)}";
$line = "line={$line}";
$this->write("::{$type} $file,$line::{$message}\n");
}
/**
* Gets the current type of the defect
* @return string
*/
protected function getCurrentType()
{
if (in_array($this->currentType, ['error', 'failure'])) {
return 'error';
}
return 'warning';
}
/**
* Gets the relative path to the file
* @param string $path Path to make relative
* @return string
*/
protected function relativePath($path)
{
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
//Normalize
$relative = str_replace('\\', '/', $relative);
return $relative;
}
/**
* Gets the file name and start line for the test
* @param string $name Name of the test
* @return array
*/
protected function getReflectionFromTest($name)
{
list($klass, $method) = explode('::', $name);
$c = new \ReflectionClass($klass);
$m = $c->getMethod($method);
return [$m->getFileName(), $m->getStartLine()];
}
}