name: CI/CD

on: [push]

jobs:
  autoformat:
    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: 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 ./ --rules=''{"array_syntax": {"syntax": "short"}, "blank_line_after_namespace": true, "global_namespace_import": {"import_classes": false, "import_constants": false, "import_functions": false}, "binary_operator_spaces": {"operators": {"=": "align", ".=": "align", "+=": "align", "-=": "align", "*=": "align", "/=": "align", "|=": "align", "&=": "align", "=>": "align", "??=": "align"}}, "cast_spaces": {"space": "single"}, "class_attributes_separation": { "elements": {"const": "one", "method": "one", "property": "one"} }, "combine_consecutive_issets": true, "compact_nullable_typehint": true, "declare_strict_types": true, "declare_equal_normalize": {"space": "none"}, "elseif": true, "encoding": true, "explicit_indirect_variable": true, "explicit_string_variable": true, "function_to_constant": true, "implode_call": true, "increment_style": {"style": "pre"}, "is_null": true, "yoda_style": {"equal": false, "identical": false, "less_and_greater": false}, "line_ending": true, "logical_operators": true, "lowercase_cast": true, "constant_case": {"case": "lower"}, "lowercase_keywords": true, "modernize_types_casting": true, "native_constant_invocation": true, "native_function_casing": true, "native_function_invocation": {"include": ["@all"]}, "new_with_braces": true, "no_extra_blank_lines": {"tokens": ["break", "case", "continue", "curly_brace_block", "extra", "return", "switch", "throw", "use"]}, "no_spaces_after_function_name": true, "no_alias_functions": true, "no_closing_tag": true, "no_empty_comment": true, "no_empty_phpdoc": true, "no_empty_statement": true, "no_homoglyph_names": true, "no_mixed_echo_print": {"use": "echo"}, "no_php4_constructor": true, "no_singleline_whitespace_before_semicolons": true, "no_spaces_inside_parenthesis": true, "no_trailing_whitespace": true, "no_unneeded_final_method": true, "no_unused_imports": true, "no_useless_return": true, "no_whitespace_before_comma_in_array": true, "no_whitespace_in_blank_line": true, "non_printable_character": true, "normalize_index_brace": true, "ordered_imports": {"sort_algorithm": "alpha"}, "ordered_interfaces": {"order": "alpha"}, "php_unit_construct": true, "php_unit_internal_class": true, "php_unit_set_up_tear_down_visibility": true, "phpdoc_indent": true, "phpdoc_align": {"align": "vertical"}, "phpdoc_annotation_without_dot": true, "phpdoc_scalar": true, "phpdoc_return_self_reference": {"replacements": {"this": "self"}}, "phpdoc_trim": true, "phpdoc_trim_consecutive_blank_line_separation": true, "random_api_migration": true, "self_accessor": true, "return_type_declaration": {"space_before": "one"}, "semicolon_after_instruction": true, "set_type_to_cast": true, "short_scalar_cast": true, "single_blank_line_at_eof": true, "single_line_after_imports": true, "standardize_increment": true, "trailing_comma_in_multiline": true, "trim_array_spaces": true, "visibility_required": true, "void_return": true}'' --allow-risky=yes'
      - 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@karaka.app'
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
          git commit -am "Automated formatting changes"
          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.0']
    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: Checkout Resource Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Resources
          path: Resources
    - name: Checkout phpOMS Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/phpOMS
          path: phpOMS
          token: ${{ secrets.GH_PAT }}
    - name: Checkout Karaka Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Karaka
          path: Karaka
    - 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, imap, bcmath, redis, memcached
          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_default.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.0']
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
        submodules: recursive
        token: ${{ secrets.GH_PAT }}
    - name: Checkout Build Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/Build
          path: Build
    - name: Checkout phpOMS Repository
      uses: actions/checkout@main
      with:
          fetch-depth: 1
          ref: develop
          repository: Karaka-Management/phpOMS
          path: phpOMS
          token: ${{ secrets.GH_PAT }}
    - 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, imap, 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: phpstan
      run: vendor/bin/phpstan analyse -a phpOMS/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.0']
    steps:
    - name: Checkout Repository
      uses: actions/checkout@main
      with:
        fetch-depth: 1
        submodules: recursive
        token: ${{ secrets.GH_PAT }}
    - 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, imap, 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: Install NPM
      uses: actions/setup-node@v3
      with:
        node-version: '14'
        cache: 'npm'
    - run: npm install
    - name: eslint
      run: npx eslint ./ -c Build/Config/.eslintrc.json
  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_PAT }}
    - name: Lint Code Base
      uses: github/super-linter@v4
      env:
        VALIDATE_ALL_CODEBASE: false
        DEFAULT_BRANCH: develop
        GITHUB_TOKEN: ${{ secrets.GH_PAT }}
