name: E2E matrix (Windows)

on:
  schedule:
    - cron: "0 0 * * *"
  workflow_dispatch:
    inputs:
      debug_enabled:
        description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
        required: false
        default: false

permissions: {}
jobs:
  e2e:
    permissions:
      contents: read  #  to fetch code (actions/checkout)

    runs-on: windows-latest
    strategy:
      matrix:
        node_version:
          - '16'
        package_manager:
          - npm
        packages:
          - e2e-angular-core
          - e2e-angular-extensions
          - e2e-nx-run,e2e-nx-misc,e2e-nx-plugin
          - e2e-cra-to-nx
          - e2e-make-angular-cli-faster
          - e2e-jest
          - e2e-linter
          - e2e-cypress
          - e2e-react
          - e2e-next
          - e2e-node
          - e2e-web
          - e2e-storybook,e2e-storybook-angular
          - e2e-workspace-create
          - e2e-add-nx-to-monorepo
          - e2e-graph-client
      fail-fast: false

    name: ${{ matrix.packages }}
    steps:
    - name: Checkout
      uses: actions/checkout@v3
      with:
        fetch-depth: 0

    - name: Use Node.js ${{ matrix.node_version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node_version }}
        registry-url: http://localhost:4872

    - name: Yarn cache directory path
      id: yarn-cache-dir-path
      shell: bash
      run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

    - name: Cache yarn
      uses: actions/cache@v3
      with:
        path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
        key: windows-node-${{ matrix.node_version }}-yarn-${{ hashFiles('yarn.lock') }}
        restore-keys: |
          windows-node-${{ matrix.node_version }}-yarn-
          windows-node-${{ matrix.node_version }}-
          windows-

    - name: Install packages
      run: yarn install --prefer-offline --frozen-lockfile --non-interactive

    - name: Run e2e tests
      run: yarn nx run-many --target=e2e --projects="${{ join(matrix.packages) }}" --parallel=1
      env:
        GIT_AUTHOR_EMAIL: test@test.com
        GIT_AUTHOR_NAME: Test
        GIT_COMMITTER_EMAIL: test@test.com
        GIT_COMMITTER_NAME: Test
        NX_E2E_CI_CACHE_KEY: e2e-gha-windows-${{ matrix.node_version }}-${{ matrix.package_manager }}
        NX_E2E_RUN_CYPRESS: ${{ 'true' }}
        NODE_OPTIONS: --max_old_space_size=8192
        SELECTED_PM: ${{ matrix.package_manager }}
        npm_config_registry: http://localhost:4872
        NX_VERBOSE_LOGGING: ${{ 'true' }}
        NX_E2E_SKIP_BUILD_CLEANUP: ${{ 'true' }}
        NX_CACHE_DIRECTORY: ${{ 'tmp' }}

    - name: Setup tmate session
      if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled && failure() }}
      uses: mxschmitt/action-tmate@v3.8
      with:
        sudo: false # disable sudo for windows debugging

  report-success:
    if: ${{ success() && github.repository_owner == 'nrwl' && github.event_name == 'workflow_dispatch' }} # only once it's fixed on manual dispatch
    needs: e2e
    runs-on: ubuntu-latest
    name: Report success
    steps:
      - name: Send notification
        uses: ravsamhq/notify-slack-action@v1
        with:
          status: ${{ needs.e2e.result }}
          message_format: '{emoji} *{workflow}* {status_message} (last commit <{commit_url}|{commit_sha}>)'
          notification_title: '{workflow} has {status_message}'
          footer: '<{run_url}|View Run>'
          mention_users: 'U01UELKLYF2,U9NPA6C90'
          mention_users_when: 'failure,warnings'
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.ACTION_MONITORING_SLACK }}

  report:
    if: ${{ always() && github.repository_owner == 'nrwl' && github.event_name != 'workflow_dispatch' }}
    needs: e2e
    runs-on: ubuntu-latest
    name: Report status
    steps:
      - name: Send notification
        uses: ravsamhq/notify-slack-action@v1
        with:
          status: ${{ needs.e2e.result }}
          message_format: '{emoji} *{workflow}* {status_message} (last commit <{commit_url}|{commit_sha}>)'
          notification_title: '{workflow} has {status_message}'
          footer: '<{run_url}|View Run>'
          mention_users: 'U01UELKLYF2,U9NPA6C90'
          mention_users_when: 'failure,warnings'
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.ACTION_MONITORING_SLACK }}
