name: Reusable run e2e tests
on:
  workflow_dispatch:
    inputs:
      commit_tag:
        description: 'The commit tag to use'
        required: true
        type: string
      e2e-tests-with-ssh:
        description: Run end to end tests with ability to ssh into environment
        required: false
        type: boolean
        default: false
      e2e-tests-logs-dump:
        description: Dump logs after running end to end tests
        required: false
        type: boolean
        default: false
      e2e-tests-containers-list:
        description: List containers before starting end to end tests
        required: false
        type: boolean
        default: true
  workflow_call:
    inputs:
      commit_tag:
        required: true
        type: string
      e2e-tests-with-ssh:
        description: Run end to end tests with ability to ssh into environment
        required: false
        type: boolean
        default: false
      e2e-tests-logs-dump:
        description: Dump logs after running end to end tests
        required: false
        type: boolean
        default: false
      e2e-tests-containers-list:
        description: List containers before starting end to end tests
        required: false
        type: boolean
        default: true
      has-changes-requiring-e2e-testing:
        type: string
    outputs:
      tests_outcome:
        value: ${{ jobs.run-e2e-tests.outputs.tests_outcome }}
    secrets:
      DOCKERHUB_USERNAME:
        required: false
      DOCKERHUB_TOKEN:
        required: false

jobs:
  # Required job
  run-e2e-tests:
    # We can only use conditionals, and not path filters to 'successfully' skip a required job - https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/troubleshooting-required-status-checks#handling-skipped-but-required-checks
    if: ${{ inputs.has-changes-requiring-e2e-testing == 'true' }}
    env:
      COORDINATOR_TAG: ${{ inputs.commit_tag }}
      POSTMAN_TAG: ${{ inputs.commit_tag }}
      PROVER_TAG: ${{ inputs.commit_tag }}
      TRANSACTION_EXCLUSION_API_TAG: ${{ inputs.commit_tag }}
      DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
      DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
    outputs:
      tests_outcome: ${{ steps.run_e2e_tests.outcome }}
    # xl saves ~0 mins
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-large
    steps:
      - name: Setup upterm session
        if: ${{ inputs.e2e-tests-with-ssh }}
        uses: lhotari/action-upterm@v1
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup nodejs environment
        uses: ./.github/actions/setup-nodejs
        with:
          pnpm-install-options: '-F contracts -F e2e --frozen-lockfile --prefer-offline'
      - name: Login to Docker Hub
        if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }}
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Create directory for conflated traces
        run: |
          mkdir -p tmp/local/traces/v2/conflated
          chmod -R a+rw tmp/local/
      - name: Pull all images with retry
        uses: nick-fields/retry@v3
        with:
          max_attempts: 10
          retry_on: error
          retry_wait_seconds: 30
          timeout_minutes: 10
          command: |
            make docker-pull-images-external-to-monorepo
      - name: Download local docker image artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: linea-*
      - name: Load Docker images
        run: |
          pwd && ls -la && echo "GITHUB_WORKSPACE=$GITHUB_WORKSPACE" &&
          gunzip -c $GITHUB_WORKSPACE/linea-coordinator/linea-coordinator-docker-image.tar.gz | docker load &&
          gunzip -c $GITHUB_WORKSPACE/linea-postman/linea-postman-docker-image.tar.gz | docker load &&
          gunzip -c $GITHUB_WORKSPACE/linea-prover/linea-prover-docker-image.tar.gz | docker load &&
          gunzip -c $GITHUB_WORKSPACE/linea-transaction-exclusion-api/linea-transaction-exclusion-api-docker-image.tar.gz | docker load
        shell: bash
      - name: Spin up fresh environment with besu tracing with retry
        uses: nick-fields/retry@v3
        with:
          max_attempts: 10
          retry_on: error
          retry_wait_seconds: 30
          timeout_minutes: 10
          command: |
            make start-env-with-tracing-v2-ci CLEAN_PREVIOUS_ENV=false
          on_retry_command: |
            make clean-environment
      - name: List docker containers/images
        if: ${{ always() && inputs.e2e-tests-containers-list }}
        continue-on-error: true
        run: |
          docker ps -la && docker images
          docker container ls -a
      - name: Run e2e tests
        id: run_e2e_tests
        timeout-minutes: 4
        run: |
          pnpm run -F e2e test:e2e:local
      - name: Show e2e tests result
        if: always()
        run: |
          echo "E2E_TESTS_RESULT: ${{ steps.run_e2e_tests.outcome }}"
      - name: Dump logs
        if: ${{ failure() && inputs.e2e-tests-logs-dump }}
        run: |
          mkdir -p docker_logs
          find tmp/local/ >> docker_logs/files_in_shared_dir.txt || true
          docker ps -a >> docker_logs/docker_ps.txt || true
          docker logs coordinator --since 1h &>> docker_logs/coordinator.txt || true
          docker logs prover-v3 --since 1h &>> docker_logs/prover-v3.txt || true
          docker logs shomei --since 1h &>> docker_logs/shomei.txt || true
          docker logs zkbesu-shomei --since 1h &>> docker_logs/zkbesu-shomei.txt || true
          docker logs shomei-frontend --since 1h &>> docker_logs/shomei-frontend.txt || true
          docker logs postman --since 1h &>> docker_logs/postman.txt || true
          docker logs traces-node --since 1h &>> docker_logs/traces-node.txt || true
          docker logs l2-node-besu --since 1h &>> docker_logs/l2-node-besu.txt || true
          docker logs transaction-exclusion-api --since 1h &>> docker_logs/transaction-exclusion-api.txt || true
          docker logs sequencer --since 1h &>> docker_logs/sequencer.txt || true
      - name: Archive debug logs
        uses: actions/upload-artifact@v4
        if: ${{ failure() && inputs.e2e-tests-logs-dump }}
        with:
          name: end-2-end-debug-logs
          if-no-files-found: error
          path: |
            docker_logs/**/*
