name: Quality checks

on:
  pull_request: { }
  # Trigger workflow for release branches.
  push:
    branches:
      - main
      - "[0-9]+.[0-9]+.x"

jobs:
  setup:
    name: Project Setup
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        php:
          - 8.1
    steps:
      # We want to reuse our project setup to run different jobs at once.
      - uses: actions/cache@v3
        id: opensocial-project-build
        with:
          path: ./*
          key: ${{ github.sha }}-${{ matrix.php }}

      # Set PHP {{ matrix.php }} as default.
      # https://github.com/shivammathur/setup-php
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none

      # We check out the code in a separate folder since we want to use the
      # result of the merge rather than the pre-merged code.
      # A separate path is used to keep our working directory clean.
      - name: Setup a temporary repository
        uses: actions/checkout@v3
        with:
          path: ${{ github.workspace }}/tmp/repository
          # Since composer must clone from this we require all the history.
          fetch-depth: 0

      # Create a branch for the specific commit so that composer can check it
      # out.
      - name: Checkout out to current commit in temporary repository
        run: cd $GITHUB_WORKSPACE/tmp/repository && git checkout -b $GITHUB_SHA

      # Prepare our composer.json to do a full project set-up.
      - name: Copy tests/composer.json to root of workspace
        run: cp $GITHUB_WORKSPACE/tmp/repository/tests/composer.json composer.json

      # Set up composer with our desired PHP version.
      - name: Setup Composer Config similar to temporary repository
        run: composer config repositories.social vcs $GITHUB_WORKSPACE/tmp/repository

      # Install the version of Open Social under test.
      - name: Composer require Open Social of current branch's latest commit.
        run: composer require goalgorilla/open_social:dev-${{ github.sha }}#${{ github.sha }}

      - name: Set file permissions
        run: mkdir -p $GITHUB_WORKSPACE/sites/default/files && sudo chmod -R 664 $GITHUB_WORKSPACE/sites/default/files

  phpstan:
    needs: setup
    name: PHPStan check
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        php:
          - 8.1
    steps:
      - uses: actions/cache/restore@v3
        id: opensocial-project-build-load
        with:
          path: ./*
          key: ${{ github.sha }}-${{ matrix.php }}
          fail-on-cache-miss: true

      # Set PHP {{ matrix.php }} as default.
      # https://github.com/shivammathur/setup-php
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
          tools: cs2pr

      - name: PHPStan Version
        run: $GITHUB_WORKSPACE/vendor/bin/phpstan --version

      # Run PHPStan
      - name: Run PHPStan analysis
        run: $GITHUB_WORKSPACE/vendor/bin/phpstan analyse -c $GITHUB_WORKSPACE/html/profiles/contrib/social/phpstan.neon --error-format=checkstyle | cs2pr

  phpcs:
    needs: setup
    name: PHPCS check
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/cache/restore@v3
        id: opensocial-project-build-load
        with:
          path: ./*
          key: ${{ github.sha }}-8.1
          fail-on-cache-miss: true

      # Set PHP {{ matrix.php }} as default.
      # https://github.com/shivammathur/setup-php
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.1
          coverage: none
          tools: cs2pr

      # Copy phpcs.xml folder to root.
      - name: Copy PHPCS.xml file from temporary repository to root of workspace.
        run: cp $GITHUB_WORKSPACE/tmp/repository/phpcs.xml phpcs.xml

      - name: Run PHPCS checks
        id: phpcs
        run: vendor/bin/phpcs  --report-full --report-checkstyle=./phpcs-report.xml

      # https://github.com/staabm/annotate-pull-request-from-checkstyle#using-php_codesniffer
      - name: Show PHPCS results in PR
        if: ${{ always() && steps.phpcs.outcome == 'failure' }}
        run: cs2pr ./phpcs-report.xml

  phpunit:
    needs: setup
    name: PHPUnit Tests
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        php:
          - 8.1
    steps:
      - uses: actions/cache/restore@v3
        id: opensocial-project-build-load
        with:
          path: ./*
          key: ${{ github.sha }}-${{ matrix.php }}
          fail-on-cache-miss: true

      # Set PHP {{ matrix.php }} as default.
      # https://github.com/shivammathur/setup-php
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          ini-values: zend.assertions=1
          coverage: none
          tools: cs2pr

      # https://github.com/shivammathur/setup-php#phpunit
      - name: Setup problem matchers for PHPUnit
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      # Run PHPUnit testing.
      - name: Run PHPUnit test suite
        run: $GITHUB_WORKSPACE/vendor/bin/phpunit -c $GITHUB_WORKSPACE/html/profiles/contrib/social/phpunit.xml.dist --log-junit $GITHUB_WORKSPACE/test-reports/phpunit.xml
        env:
          SIMPLETEST_DB: sqlite://tmp/db.sqlite

  spellcheck:
    name: Spell Check
    runs-on: ubuntu-22.04
    continue-on-error: true
    steps:
      - uses: actions/checkout@v3
      - uses: streetsidesoftware/cspell-action@v2
