<commit_before>name: "🎳 End2end"
on:
  pull_request:
    types: [ labeled, opened, synchronize, reopened ]
    branches:
      - master
      - release_3_*
  workflow_dispatch:
  repository_dispatch:
    types: [ run-e2e-branches ]

jobs:
  end2end:
    # The first condition is triggered when we set the new label
    # The second one when we update the PR with new commits without changing labels
    # The third one when external workflow
    # The fourth one is for the manual button
    if: |
      github.event.label.name == 'run end2end' ||
      contains(github.event.pull_request.labels.*.name, 'run end2end') ||
      github.event_name == 'repository_dispatch' ||
      github.event_name == 'workflow_dispatch'
    name: "End-to-end"
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: tests
    env:
      CYPRESS_CI: TRUE
    steps:

      - name: Define branch name
        working-directory: .
        run: |
          if ${{ github.event_name == 'repository_dispatch' }}
          then
            echo "Set branch from manual input: ${{ github.event.client_payload.branch }}"
            BRANCH="${{ github.event.client_payload.branch }}"
          else
            echo "Set branch from default value : ${{ github.head_ref || github.ref_name }} "
            BRANCH="${{ github.head_ref || github.ref_name }}"
          fi
          echo "BRANCH=${BRANCH}" >> $GITHUB_ENV

      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ env.BRANCH }}
          token: ${{ secrets.BOT_HUB_TOKEN || github.token  }}  # Important to launch CI on a commit from a bot

      - name: Branch name
        run: echo running on branch ${GITHUB_REF##*/} with ${CYPRESS_CI}

      - name: Install Lizmap as a QGIS Server plugin
        run: make build-plugins

      - name: Show Lizmap environment and versions
        run: make show-qgis-server-versions && make env && cat .env

      - name: Pull docker images
        run: ./run-docker pull

      # In this step, this action saves a list of existing images,
      # the cache is created without them in the post run.
      # It also restores the cache if it exists.
      - uses: satackey/action-docker-layer-caching@v0.0.11
        # Ignore the failure of a step and avoid terminating the job.
        continue-on-error: true

      - name: Build and start docker images
        run: ./run-docker up --build -d

      - name: Wait docker images ready and install Lizmap
        run: sleep 30 && ./lizmap-ctl install

      - name: Load SQL data
        run: cd qgis-projects/tests && ./load_sql.sh

      # Playwright
      - uses: actions/setup-node@v3
      - name: Install dependencies
        run: cd end2end && npm ci

      - name: Install Playwright
        run: cd end2end && npx playwright install --with-deps chromium

      - name: Run Playwright tests
        run: cd end2end && npx playwright test --project=chromium

      - name: Upload test results
        if: failure()
        uses: actions/upload-artifact@v3.1.1
        with:
          name: playwright-report
          path: playwright-report

      # Install NPM dependencies, cache them correctly
      # and run all Cypress tests
      - name: Cypress run
        uses: cypress-io/github-action@v4.2.0
        with:
          browser: chrome
          working-directory: tests/end2end
          spec: cypress/integration/*-ghaction.js
          wait-on: http://localhost:8130

      - name: Save screenshots as artifacts if a test fails to ease debug
        uses: actions/upload-artifact@v3.1.1
        if: failure()
        with:
          name: cypress-screenshots
          path: |
            tests/end2end/cypress/screenshots
            tests/end2end/cypress/downloads

#  debug:
#    name: "Debug"
#    runs-on: ubuntu-latest
#    steps:
#      - name: Dump GitHub context
#        env:
#          GITHUB_CONTEXT: ${{ toJson(github) }}
#        run: |
#          echo "$GITHUB_CONTEXT"
<commit_msg>GH Action - Debug when it is a PR<commit_after>name: "🎳 End2end"
on:
  pull_request:
    types: [ labeled, opened, synchronize, reopened ]
    branches:
      - master
      - release_3_*
  workflow_dispatch:
  repository_dispatch:
    types: [ run-e2e-branches ]

jobs:
  end2end:
    # The first condition is triggered when we set the new label
    # The second one when we update the PR with new commits without changing labels
    # The third one when external workflow
    # The fourth one is for the manual button
    if: |
      github.event.label.name == 'run end2end' ||
      contains(github.event.pull_request.labels.*.name, 'run end2end') ||
      github.event_name == 'repository_dispatch' ||
      github.event_name == 'workflow_dispatch'
    name: "End-to-end"
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: tests
    env:
      CYPRESS_CI: TRUE
    steps:

#      - name: Debug
#        working-directory: .
#        run: |
#            echo ${{ github.event.pull_request.head.sha }}
#            echo ${{ github.head_ref }}
#            echo ${{ github.ref_name }}

      - name: Define branch name
        working-directory: .
        run: |
          if ${{ github.event_name == 'repository_dispatch' }}
          then
            # repository_dispatch is from an external workflow
            echo "Set branch from manual input: ${{ github.event.client_payload.branch }}"
            BRANCH="${{ github.event.client_payload.branch }}"
          elif ${{ github.event_name == 'workflow_dispatch' }}
          then
            # workflow_dispatch is the the button in UI
            echo "Set branch from default value: ${{ github.head_ref }}"
            BRANCH="${{ github.head_ref }}"
          else
            # From a PR
            echo "Set branch from default value : ${{ github.event.pull_request.head.sha }} "
            BRANCH="${{ github.event.pull_request.head.sha }}"
          fi
          echo "BRANCH=${BRANCH}" >> $GITHUB_ENV

      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ env.BRANCH }}
          token: ${{ secrets.BOT_HUB_TOKEN || github.token  }}  # Important to launch CI on a commit from a bot

      - name: Branch name
        run: echo running on branch ${GITHUB_REF##*/} with ${CYPRESS_CI}

      - name: Install Lizmap as a QGIS Server plugin
        run: make build-plugins

      - name: Show Lizmap environment and versions
        run: make show-qgis-server-versions && make env && cat .env

      - name: Pull docker images
        run: ./run-docker pull

      # In this step, this action saves a list of existing images,
      # the cache is created without them in the post run.
      # It also restores the cache if it exists.
      - uses: satackey/action-docker-layer-caching@v0.0.11
        # Ignore the failure of a step and avoid terminating the job.
        continue-on-error: true

      - name: Build and start docker images
        run: ./run-docker up --build -d

      - name: Wait docker images ready and install Lizmap
        run: sleep 30 && ./lizmap-ctl install

      - name: Load SQL data
        run: cd qgis-projects/tests && ./load_sql.sh

      # Playwright
      - uses: actions/setup-node@v3
      - name: Install dependencies
        run: cd end2end && npm ci

      - name: Install Playwright
        run: cd end2end && npx playwright install --with-deps chromium

      - name: Run Playwright tests
        run: cd end2end && npx playwright test --project=chromium

      - name: Upload test results
        if: failure()
        uses: actions/upload-artifact@v3.1.1
        with:
          name: playwright-report
          path: playwright-report

      # Install NPM dependencies, cache them correctly
      # and run all Cypress tests
      - name: Cypress run
        uses: cypress-io/github-action@v4.2.0
        with:
          browser: chrome
          working-directory: tests/end2end
          spec: cypress/integration/*-ghaction.js
          wait-on: http://localhost:8130

      - name: Save screenshots as artifacts if a test fails to ease debug
        uses: actions/upload-artifact@v3.1.1
        if: failure()
        with:
          name: cypress-screenshots
          path: |
            tests/end2end/cypress/screenshots
            tests/end2end/cypress/downloads

#  debug:
#    name: "Debug"
#    runs-on: ubuntu-latest
#    steps:
#      - name: Dump GitHub context
#        env:
#          GITHUB_CONTEXT: ${{ toJson(github) }}
#        run: |
#          echo "$GITHUB_CONTEXT"

{'fraction_non_alphanumeric': 0.09838747370881047, 'fraction_numerical': 0.007712082262210797, 'mean_word_length': 2.0158562367864694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4016154', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2460, 'n_words': 770}