name: Electron Playwright Tests

on:
  push:
    branches:
      - master
  pull_request:
    types:
      - labeled
  workflow_dispatch:
    inputs:
      version_name:
        type: string
        description: "Desktop Version name eg: 5.6"
        required: true
      job_name:
        type: choice
        description: "Job name"
        required: true
        default: "All"
        options:
          - "e2e-linux"
          - "e2e-macos"
          - "e2e-windows"
          - "All"

jobs:
  e2e-linux:
    if: ${{
      (
      (inputs.job_name == 'e2e-linux' || inputs.job_name == 'All')
      &&
      github.event_name == 'workflow_dispatch'
      ) ||
      (
      github.event_name == 'push'
      ) ||
      (
      github.event_name == 'pull_request' &&
      github.event.pull_request.labels &&
      contains(github.event.pull_request.labels.*.name, 'Run Desktop E2E Tests')
      )
      }}
    uses: ./.github/workflows/e2e-functional-template.yml
    secrets: inherit
    with:
      runs-on: ubuntu-22.04
      DESKTOP_VERSION: ${{ inputs.version_name || github.head_ref || github.ref }}

  e2e-macos:
    if: ${{
      (
      (inputs.job_name == 'e2e-macos' || inputs.job_name == 'All')
      &&
      github.event_name == 'workflow_dispatch'
      ) ||
      (
      github.event_name == 'push'
      ) ||
      (
      github.event_name == 'pull_request' &&
      github.event.pull_request.labels &&
      contains(github.event.pull_request.labels.*.name, 'Run Desktop E2E Tests')
      )
      }}
    uses: ./.github/workflows/e2e-functional-template.yml
    secrets: inherit
    with:
      runs-on: macos-13
      DESKTOP_VERSION: ${{ inputs.version_name || github.head_ref || github.ref }}

  e2e-windows:
    if: ${{
      (
      (inputs.job_name == 'e2e-windows' || inputs.job_name == 'All')
      &&
      github.event_name == 'workflow_dispatch'
      ) ||
      (
      github.event_name == 'push'
      ) ||
      (
      github.event_name == 'pull_request' &&
      github.event.pull_request.labels &&
      contains(github.event.pull_request.labels.*.name, 'Run Desktop E2E Tests')
      )
      }}
    uses: ./.github/workflows/e2e-functional-template.yml
    secrets: inherit
    with:
      runs-on: windows-2022
      DESKTOP_VERSION: ${{ inputs.version_name || github.head_ref || github.ref }}

  e2e-remove-label:
    if: ${{ always() && contains(github.event.pull_request.labels.*.name, 'Run Desktop E2E Tests') }}
    needs: [e2e-linux, e2e-macos, e2e-windows]
    runs-on: ubuntu-22.04
    steps:
      - name: e2e/unify-comments-in-single-comment
        run: |
          echo "PR_COMMENT<<EOF" >> "${GITHUB_ENV}"
          echo "Here are the test results below:" >> "${GITHUB_ENV}"
          echo "${{ needs.e2e-linux.outputs.COMMENT_BODY }}" >> "${GITHUB_ENV}"
          echo "${{ needs.e2e-macos.outputs.COMMENT_BODY }}" >> "${GITHUB_ENV}"
          echo "${{ needs.e2e-windows.outputs.COMMENT_BODY }}" >> "${GITHUB_ENV}"
          echo "EOF" >> "${GITHUB_ENV}"

      - name: e2e/send-comment-results-in-pr
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: process.env.PR_COMMENT,
            });

      - name: e2e/remove-label-from-pr
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        continue-on-error: true # Label might have been removed manually
        with:
          script: |
            github.rest.issues.removeLabel({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: 'Run Desktop E2E Tests',
            });
