name: PR E2E

on:
  pull_request_review:
    types: [submitted]

concurrency:
  group: e2e-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  get-metadata:
    name: Get Metadata
    runs-on: ubuntu-latest
    steps:
      - name: Check out current commit
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 2

      - name: Determine changed files
        uses: tomi/paths-filter-action@v3.0.2
        id: changed
        with:
          filters: |
            not_ignored:
              - '!.devcontainer/**'
              - '!.github/*'
              - '!.github/scripts/*'
              - '!.github/workflows/benchmark-*'
              - '!.github/workflows/check-*'
              - '!.vscode/**'
              - '!docker/**'
              - '!packages/@n8n/benchmark/**'
              - '!**/*.md'
          predicate-quantifier: 'every'

    outputs:
      # The workflow should run when:
      # - It has changes to files that are not ignored
      # - It is not a community PR
      # - It is targeting master or a release branch
      should_run: ${{ steps.changed.outputs.not_ignored == 'true' && !contains(github.event.pull_request.labels.*.name, 'community') && (github.event.pull_request.base.ref == 'master' || startsWith(github.event.pull_request.base.ref, 'release/')) }}

  run-e2e-tests:
    name: E2E [Electron/Node 18]
    uses: ./.github/workflows/e2e-reusable.yml
    needs: [get-metadata]
    if: ${{ github.event.review.state == 'approved' && needs.get-metadata.outputs.should_run == 'true' }}
    with:
      pr_number: ${{ github.event.pull_request.number }}
      user: ${{ github.event.pull_request.user.login || 'PR User' }}
    secrets:
      CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}

  post-e2e-tests:
    runs-on: ubuntu-latest
    name: E2E [Electron/Node 18] - Checks
    needs: [get-metadata, run-e2e-tests]
    if: always()
    steps:
      - name: E2E success comment
        if: ${{ needs.get-metadata.outputs.should_run == 'true' && needs.run-e2e-tests.outputs.tests_passed == 'true' }}
        uses: peter-evans/create-or-update-comment@v4.0.0
        with:
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            :white_check_mark: All Cypress E2E specs passed
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: E2E fail comment
        if: needs.run-e2e-tests.result == 'failure'
        uses: peter-evans/create-or-update-comment@v4.0.0
        with:
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            :warning: Some Cypress E2E specs are failing, please fix them before merging
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Success job if community PR
        if: ${{ contains(github.event.pull_request.labels.*.name, 'community') }}
        run: exit 0

      - name: Fail job if run-e2e-tests failed
        if: ${{ (github.event.review.state != 'approved' && github.event.review.state != 'commented') || needs.run-e2e-tests.result == 'failure' }}
        run: exit 1