# This workflow is provided via the organization template repository
#
# https://github.com/nextcloud/.github
# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization
#
# SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: MIT

name: Cypress

on: pull_request

concurrency:
  group: cypress-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

env:
  # Adjust APP_NAME if your repository name is different
  APP_NAME: ${{ github.event.repository.name }}

  # This represents the server branch to checkout.
  # Usually it's the base branch of the PR, but for pushes it's the branch itself.
  # e.g. 'main', 'stable27' or 'feature/my-feature'
  # n.b. server will use head_ref, as we want to test the PR branch.
  BRANCH: ${{ github.base_ref || github.ref_name }}

jobs:
  init:
    runs-on: ubuntu-latest-low
    outputs:
      nodeVersion: ${{ steps.versions.outputs.nodeVersion }}
      npmVersion: ${{ steps.versions.outputs.npmVersion }}

    env:
      PUPPETEER_SKIP_DOWNLOAD: true

    steps:
      - name: Disabled on forks
        if: ${{ github.event.pull_request.head.repo.full_name != github.repository }}
        run: |
          echo 'Can not run cypress on forks'
          exit 1

      - name: Checkout app
        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: Check composer.json
        id: check_composer
        uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
        with:
          files: "composer.json"

      - name: Install composer dependencies
        if: steps.check_composer.outputs.files_exists == 'true'
        run: composer install --no-dev

      - name: Read package.json node and npm engines version
        uses: skjnldsv/read-package-engines-version-actions@06d6baf7d8f41934ab630e97d9e6c0bc9c9ac5e4 # v3
        id: versions
        with:
          fallbackNode: "^20"
          fallbackNpm: "^10"

      - name: Set up node ${{ steps.versions.outputs.nodeVersion }}
        uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
        with:
          node-version: ${{ steps.versions.outputs.nodeVersion }}

      - name: Set up npm ${{ steps.versions.outputs.npmVersion }}
        run: npm i -g 'npm@${{ steps.versions.outputs.npmVersion }}'

      - name: Install node dependencies & build app
        run: |
          npm ci
          TESTING=true npm run build --if-present

      - name: Save context
        uses: buildjet/cache/save@e376f15c6ec6dc595375c78633174c7e5f92dc0e # v3
        with:
          key: cypress-context-${{ github.run_id }}
          path: ./

  cypress:
    runs-on: ubuntu-latest
    needs: init

    strategy:
      fail-fast: false
      matrix:
        # Run multiple copies of the current job in parallel
        # Please increase the number or runners as your tests suite grows
        containers: ['component', '1', '2', '3']

    name: runner ${{ matrix.containers }}

    steps:
      - name: Restore context
        uses: buildjet/cache/restore@e376f15c6ec6dc595375c78633174c7e5f92dc0e # v3
        with:
          fail-on-cache-miss: true
          key: cypress-context-${{ github.run_id }}
          path: ./

      - name: Set up node ${{ needs.init.outputs.nodeVersion }}
        uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
        with:
          node-version: ${{ needs.init.outputs.nodeVersion }}

      - name: Set up npm ${{ needs.init.outputs.npmVersion }}
        run: npm i -g 'npm@${{ needs.init.outputs.npmVersion }}'

      - name: Run ${{ matrix.containers == 'component' && 'component' || 'E2E' }} cypress tests
        uses: cypress-io/github-action@496e7dc0edc421a9de8a36a31c793340e00c61bf # v6.7.5
        with:
          record: ${{ secrets.CYPRESS_RECORD_KEY && true }}
          parallel: ${{ secrets.CYPRESS_RECORD_KEY && true }}
          # cypress run type
          component: ${{ matrix.containers == 'component' }}
          group: ${{ secrets.CYPRESS_RECORD_KEY && env.CYPRESS_GROUP }}
          # cypress env
          ci-build-id: ${{ secrets.CYPRESS_RECORD_KEY && env.CYPRESS_BUILD_ID }}
          tag: ${{ secrets.CYPRESS_RECORD_KEY && github.event_name }}
        env:
          # Needs to be prefixed with CYPRESS_
          CYPRESS_BRANCH: ${{ env.BRANCH }}
          # https://github.com/cypress-io/github-action/issues/124
          COMMIT_INFO_MESSAGE: ${{ github.event.pull_request.title }}
          # Needed for some specific code workarounds
          TESTING: true
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          CYPRESS_BUILD_ID: ${{ github.sha }}-${{ github.run_number }}
          CYPRESS_GROUP: Run ${{ matrix.containers == 'component' && 'component' || 'E2E' }}

      - name: Upload snapshots
        uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
        if: always()
        with:
          name: snapshots_${{ matrix.containers }}
          path: cypress/snapshots

      - name: Extract NC logs
        if: failure() && matrix.containers != 'component'
        run: docker logs nextcloud-cypress-tests-${{ env.APP_NAME }} > nextcloud.log

      - name: Upload NC logs
        uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
        if: failure() && matrix.containers != 'component'
        with:
          name: nc_logs_${{ matrix.containers }}
          path: nextcloud.log

  summary:
    runs-on: ubuntu-latest-low
    needs: [init, cypress]

    if: always()

    name: cypress-summary

    steps:
      - name: Summary status
        run: if ${{ needs.init.result != 'success' || ( needs.cypress.result != 'success' && needs.cypress.result != 'skipped' ) }}; then exit 1; fi

