name: "Continuous Integration"
on: pull_request
jobs:
  Test:
    runs-on: ubuntu-20.04
    container: nixos/nix
    steps:
      - name: Install git and tar
        run: apk add git tar

      - name: Cache Whole of NIX
        uses: actions/cache@v2
        with:
          path: /nix
          key: ${{ runner.os }}-alpine-nix

      - name: Checkout Git
        uses: actions/checkout@v2.3.4

      - name: Install Environment
        run: nix-env -i -f ./ci-env.nix

      - name: Cache pnpm modules
        uses: actions/cache@v2
        with:
          path: ~/.pnpm-store
          key: pnpm-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            pnpm-${{ runner.os }}-

      # begin
      - name: Run and Generate Tests
        run: ./scripts/ci/tests.sh

      - name: Upload Test Report
        uses: actions/upload-artifact@v2
        with:
          name: tests-report-${{ github.sha }}
          path: reports

      - name: Upload Test Coverage
        uses: actions/upload-artifact@v2
        with:
          name: tests-coverage-${{ github.sha }}
          path: coverage

  Pre-Commit-Validation:
    runs-on: ubuntu-20.04
    container: nixos/nix
    steps:
      - name: Install git and tar
        run: apk add git tar

      - name: Cache Whole of NIX
        uses: actions/cache@v2
        with:
          path: /nix
          key: ${{ runner.os }}-alpine-nix

      - name: Checkout Git
        uses: actions/checkout@v2.3.4

      - name: Install Environment
        run: nix-env -i -f ./ci-env.nix

      - name: Cache pnpm modules
        uses: actions/cache@v2
        with:
          path: ~/.pnpm-store
          key: pnpm-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            pnpm-${{ runner.os }}-

      - name: Pre-Commit Check
        run: ./scripts/ci/verify-pre-commit.sh

  Report:
    runs-on: ubuntu-20.04
    needs: ["Test"]
    steps:
      - name: Check out
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Download Test Artifacts
        uses: actions/download-artifact@v2
        with:
          name: tests-report-${{ github.sha }}
          path: reports
      - name: Test Report
        uses: dorny/tests-reporter@v1
        with:
          name: Jest Tests
          path: "reports/*.xml"
          reporter: jest-junit
