name: CI

on: [push, pull_request]

jobs:
  hash_generation:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'NO_CI')"
    strategy:
      fail-fast: false
      max-parallel: 3
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
    - name: hash
      run: find ./ -type f -not -path "./vendor/*" -not -path "./Build/*" -not -path "./MainRepository/*" -exec md5sum {} \; > hashs.txt
    - name: Check for modified files
      id: git-check
      run: echo ::set-output name=modified::$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)
    - name: Push changes
      if: steps.git-check.outputs.modified == 'true'
      run: |
        git config --global user.name 'Hash Bot'
        git config --global user.email 'hash.bot@jingga.app'
        git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
        git commit -am "Automated hash generation (NO_CI)"
        git push
  autoformat:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'NO_CI')"
    strategy:
      fail-fast: false
      max-parallel: 3
      matrix:
        php-versions: ['8.1']
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
    - name: Checkout Build Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Build
          path: Build
    - name: Setup PHP, with composer and extensions
      uses: shivammathur/setup-php@master
      with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, gd, zip, dom, mysql, pgsql, sqlite, bcmath, redis, memcached
          ini-values: opcache.jit_buffer_size=256M, opcache.jit=1235, pcre.jit=1
    - name: Get Composer Cache Directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"
    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: ${{ runner.os }}-composer-
    - name: Setup Composer
      run: composer install
    - name: Autoformat
      run: |
        vendor/bin/php-cs-fixer fix ./ --config=Build/Config/.php-cs-fixer.php --allow-risky=yes
        vendor/bin/phpcbf --standard=Build/Config/phpcs.xml ./
        find ./ -type f -not -path "./vendor/*" -not -path "./Build/*" -not -path "./MainRepository/*" -exec md5sum {} \; > hashs.txt
    - name: Check for modified files
      id: git-check
      run: echo ::set-output name=modified::$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)
    - name: Push changes
      if: steps.git-check.outputs.modified == 'true'
      run: |
        git config --global user.name 'Formatter Bot'
        git config --global user.email 'formatter.bot@jingga.app'
        git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
        git commit -am "Automated formatting changes (NO_CI)"
        git push
  code-tests:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'NO_CI')"
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: false
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: oms
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
      postgres:
        image: postgres:10.8
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: root
          POSTGRES_DB: oms
        ports:
        - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
      redis:
        image: redis
        ports:
        - 6379:6379
        options: --entrypoint redis-server
      memcached:
        image: memcached
        ports:
          - 11211:11211
    strategy:
      fail-fast: false
      max-parallel: 3
      matrix:
        php-versions: ['8.1']
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
    - name: Setup mssql
      uses: potatoqualitee/mssqlsuite@v1.7
      with:
        install: sqlengine, sqlpackage
        version: 2017
        sa-password: c0MplicatedP@ssword
        show-log: true
        collation: Latin1_General_BIN
    - name: Run sqlcmd
      run: sqlcmd -S localhost -U sa -P c0MplicatedP@ssword -Q "CREATE DATABASE oms;"
    - name: Checkout Build Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Build
          path: Build
    - name: Checkout Resource Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Resources
          path: Resources
    - name: Setup PHP, with composer and extensions
      uses: shivammathur/setup-php@master
      with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, gd, zip, dom, mysql, pgsql, sqlite, bcmath, redis, memcached, sqlsrv
          ini-values: opcache.jit_buffer_size=256M, opcache.jit=1235, pcre.jit=1
          coverage: pcov
    - name: Get Composer Cache Directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"
    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: ${{ runner.os }}-composer-
    - name: Setup Composer
      run: composer install
    - name: phpunit
      run: vendor/bin/phpunit --coverage-clover tests/coverage.xml --configuration tests/phpunit_no_coverage.xml
  static-tests:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'NO_CI')"
    strategy:
      fail-fast: false
      max-parallel: 3
      matrix:
        php-versions: ['8.1']
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
    - name: Checkout Build Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Build
          path: Build
    - name: Setup PHP, with composer and extensions
      uses: shivammathur/setup-php@master
      with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, gd, zip, dom, mysql, pgsql, sqlite, bcmath, redis, memcached
          ini-values: opcache.jit_buffer_size=256M, opcache.jit=1235, pcre.jit=1
    - name: Get Composer Cache Directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"
    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: ${{ runner.os }}-composer-
    - name: Setup Composer
      run: composer install
    - name: Prepare phpstan
      run: |
        sed -i 's/path: ..\/..\/phpOMS\//path: ..\/..\//g' Build/Config/phpstan-baseline.neon
    - name: phpstan
      run: vendor/bin/phpstan analyse -a Autoloader.php --no-progress -l 9 -c Build/Config/phpstan.neon ./
  codestyle-tests:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'NO_CI')"
    strategy:
      fail-fast: false
      max-parallel: 3
      matrix:
        php-versions: ['8.1']
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
    - name: Checkout Build Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Build
          path: Build
    - name: Setup PHP, with composer and extensions
      uses: shivammathur/setup-php@master
      with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, gd, zip, dom, mysql, pgsql, sqlite, bcmath, redis, memcached
          ini-values: opcache.jit_buffer_size=256M, opcache.jit=1235, pcre.jit=1
    - name: Get Composer Cache Directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"
    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: ${{ runner.os }}-composer-
    - name: Setup Composer
      run: composer install
    - name: phpcs
      run: vendor/bin/phpcs --severity=1 ./ --standard="Build/Config/phpcs.xml" -s --report=full
    - name: rector
      run: vendor/bin/rector process --dry-run --config Build/Config/rector.php ./
#  linting:
#    runs-on: ubuntu-latest
#    if: "!contains(github.event.head_commit.message, 'NO_CI')"
#    strategy:
#      fail-fast: false
#      max-parallel: 3
#    steps:
#    - name: Checkout Repository
#      uses: actions/checkout@main
#      with:
#        fetch-depth: 0
#        submodules: recursive
#        token: ${{ secrets.GH_TOKEN }}
#    - name: Lint Code Base
#      uses: github/super-linter/slim@v4
#      env:
#        VALIDATE_ALL_CODEBASE: false
#        VALIDATE_PHP: true
#        VALIDATE_PHP_BUILTIN: true
#        DEFAULT_BRANCH: develop
#        GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
  custom:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'NO_CI')"
    strategy:
      fail-fast: false
      max-parallel: 3
      matrix:
        php-versions: ['8.1']
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
    - name: Checkout Build Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Build
          path: Build
    - name: Setup PHP, with composer and extensions
      uses: shivammathur/setup-php@master
      with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, gd, zip, dom, mysql, pgsql, sqlite, bcmath, redis, memcached
          ini-values: opcache.jit_buffer_size=256M, opcache.jit=1235, pcre.jit=1
    - name: PHP linting
      run: |
        find ./ -type f \
        -not -path "./tests/Application/Testapp/*" \
        -name '*.php' -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" )
    - name: Php strict
      run: if [[ $(grep -r -L "declare(strict_types=1);" --include=*.php --exclude={*.tpl.php,*Hooks.php,*Routes.php,*SearchCommands.php} ./) -ne "" ]]; then exit 1; fi
