name: PHP Code Testing

on:
  push:
    branches:
      - master
      - 'releases/**'
      - 'feat_**'
      - staging
      - production
  pull_request:
  workflow_dispatch:

jobs:
  tests:
    if: "! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
    defaults:
      run:
        working-directory: ./server
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php-versions: ['7.4']
    name: Testing ${{ matrix.php-versions }}
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Dump GitHub context
      env:
        GITHUB_CONTEXT: ${{ toJson(github) }}
      run: echo "$GITHUB_CONTEXT"

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: mbstring, intl
        ini-values: post_max_size=256M, log_errors = on, error_reporting = 32767, error_log = /tmp/php.log
        coverage: xdebug, pcov

    - name: Start webserver
      run: |
        nohup php -S 0.0.0.0:8080 &
        sleep 5 && curl 127.0.0.1:8080/.htaccess # request a simple static file

    - name: Setup NodeJS
      uses: actions/setup-node@v2
      with:
        node-version: '12'

    - name: WebPack Setup
      working-directory: server/resources
      run: |
        npm install
        ./node_modules/.bin/webpack --version
        ./node_modules/.bin/webpack --config webpack.prod.js
        ls dist

    - name: Shutdown Ubuntu MySQL (SUDO)
      run: sudo service mysql stop

    - uses: mirromutth/mysql-action@v1.1
      with:
        mysql root password: 'password'
        mysql user: 'user'
        mysql password: 'password'

    - name: Install dependencies
      run: |
        composer install --prefer-dist --no-progress
        npm init --yes
        npm install --save-dev cypress

    - name: Mysql test
      run: mysql -u root -h "127.0.0.1" -ppassword -e "SHOW DATABASES;"

    - name: Cypress run
      uses: cypress-io/github-action@v2
      with:
        browser: chrome
        record: true
        working-directory: server
        tag: php-${{ matrix.php-versions }}
      env:
        CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - uses: actions/upload-artifact@v2
      if: ${{ always() }}
      with:
        name: php_log_${{ github.run_id }}
        path: /tmp/php.log

    - uses: actions/upload-artifact@v2
      if: failure()
      with:
        name: cypress_screenshots_php-${{ matrix.php-versions }}_${{ github.run_id }}
        path: server/cypress/screenshots

  code_quality:
    if: "! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
    defaults:
      run:
        working-directory: ./server
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php-versions: ['7.4']
    name: Code Quality ${{ matrix.php-versions }}
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: mbstring, intl
        ini-values: post_max_size=256M, log_errors = on, error_reporting = 32767, error_log = /tmp/php.log
        coverage: xdebug, pcov

    - name: Setup NodeJS
      uses: actions/setup-node@v2
      with:
        node-version: '12'

    - name: WebPack Setup
      run: |
        cd resources
        npm install
        ./node_modules/.bin/webpack --version
        ./node_modules/.bin/webpack --config webpack.prod.js
        ls dist

    - name: Validate composer.json and composer.lock
      run: composer validate

    - name: Install dependencies
      run: composer install --prefer-dist --no-progress

    - name: PhpCS report
      run: ./vendor/bin/phpcs . --extensions="php,js" --ignore="resources/dist,resources/node_modules,vendor,sw.js" --config-set ignore_errors_on_exit 1 --config-set ignore_warnings_on_exit 1 --report=json > /tmp/phpcs_report.json

    - uses: actions/upload-artifact@v2
      if: ${{ always() }}
      with:
        name: phpcs_report_${{ github.run_id }}
        path: /tmp/phpcs_report.json

    - uses: actions/upload-artifact@v2
      if: ${{ always() }}
      with:
        name: webpack_report_${{ github.run_id }}
        path: server/resources/dist/report.html

    - uses: actions/upload-artifact@v2
      with:
        name: webpack_dist_${{ github.run_id }}
        path: server/resources/dist

  staging_deployment:
    if: github.event.ref == 'refs/heads/staging'
    needs: tests
    runs-on: ubuntu-latest
    environment: staging
    name: Deploy to staging
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: 7.4
        extensions: mbstring, intl
        ini-values: post_max_size=256M, log_errors = on, error_reporting = 32767, error_log = /tmp/php.log
        coverage: xdebug, pcov

    - name: Setup NodeJS
      uses: actions/setup-node@v2
      with:
        node-version: '12'

    - name: Deploy
      run: |
        ls
        sudo wget -O deployment.phar https://github.com/dg/ftp-deployment/releases/download/v3.4.0/deployment.phar
        echo '<?php $remotes = [ "staging" => [ "remote" => "${{ secrets.DEPLOY_URL }}", "sentry_env" => "staging", "skip_composer_upload" => true ] ]; ?>' > deployment_remotes.php
        cat deployment_remotes.php
        ls
        php deployment.phar deployment.php --section staging
        php -r 'require("deployment_remotes.php");'
        cat deployment.log | grep "After-jobs:" ; exit $?

    - uses: actions/upload-artifact@v2
      if: ${{ always() }}
      with:
        name: deploy_php_log_${{ github.run_id }}
        path: /tmp/php.log

    - uses: actions/upload-artifact@v2
      if: ${{ always() }}
      with:
        name: deploy_log_${{ github.run_id }}
        path: deployment.log

  production_deployment:
    if: github.event.ref == 'refs/heads/production'
    needs: tests
    runs-on: ubuntu-latest
    environment: production
    name: Deploy to production
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: 7.4
        extensions: mbstring, intl
        ini-values: post_max_size=256M, log_errors = on, error_reporting = 32767, error_log = /tmp/php.log
        coverage: xdebug, pcov

    - name: Setup NodeJS
      uses: actions/setup-node@v2
      with:
        node-version: '12'

    - name: Deploy
      run: |
        ls
        sudo wget -O deployment.phar https://github.com/dg/ftp-deployment/releases/download/v3.4.0/deployment.phar
        echo '<?php $remotes = [ "production" => [ "remote" => "${{ secrets.DEPLOY_URL }}", "sentry_env" => "prod", "skip_composer_upload" => true ] ]; ?>' > deployment_remotes.php
        cat deployment_remotes.php
        ls
        php deployment.phar deployment.php --section production
        php -r 'require("deployment_remotes.php");'
        cat deployment.log | grep "After-jobs:" ; exit $?

    - uses: actions/upload-artifact@v2
      if: ${{ always() }}
      with:
        name: deploy_php_log_${{ github.run_id }}
        path: /tmp/php.log

    - uses: actions/upload-artifact@v2
      if: ${{ always() }}
      with:
        name: deploy_log_${{ github.run_id }}
        path: deployment.log

