name: Cypress repeat

on:
  workflow_dispatch:
    inputs:
      spec:
        required: false
        description: 'Spec to run. Leave empty if you prefer to filter tests using grep'
      grep:
        required: false
        description: 'Run all tests which titles contain text. Leave empty if you prefer to filter tests using spec.'
      dashboard_url:
        required: true
        default: 'https://automation-dashboard.staging.saleor.cloud/dashboard'
        description: "Dashboard url"
      API_url:
        required: true
        default: 'https://automation-dashboard.staging.saleor.cloud/graphql/'
        description: "API url"
      repeat:
        required: true
        default: "10"
        description: "How many times run tests"

jobs:

  run-tests:
    if: ${{ github.event.inputs.spec }} || ${{ github.event.inputs.grep }} ## Do not run if spec or grep not provided
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version-file: ".nvmrc"
          
      - name: Package
        run: npm ci
          
      - name: Install cypress-repeat
        run: npm i -D cypress-repeat

      - name: Cypress run
        id: cypress
        uses: cypress-io/github-action@v4
        env:
          API_URI: ${{ github.event.inputs.API_url}}
          APP_MOUNT_URI: ${{ secrets.APP_MOUNT_URI }}
          CYPRESS_baseUrl: ${{ github.event.inputs.dashboard_url}}
          CYPRESS_USER_NAME: ${{ secrets.CYPRESS_USER_NAME }}
          CYPRESS_SECOND_USER_NAME: ${{ secrets.CYPRESS_SECOND_USER_NAME }}
          CYPRESS_USER_PASSWORD: ${{ secrets.CYPRESS_USER_PASSWORD }}
          CYPRESS_PERMISSIONS_USERS_PASSWORD: ${{ secrets.CYPRESS_PERMISSIONS_USERS_PASSWORD }}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CYPRESS_mailHogUrl: ${{ secrets.CYPRESS_MAILHOG }}
          COMMIT_INFO_MESSAGE: Cypress repeat with - ${{ github.event.inputs.spec }} ${{ github.event.inputs.grep }} ${{ github.event.inputs.dashboard_url}}
          CYPRESS_grep: ${{ github.event.inputs.grep }}
          STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_KEY }}
          STRIPE_PUBLIC_KEY: ${{ secrets.STRIPE_PUBLIC_KEY }}
        with:
          parallel: true
          group: 'UI - Chrome'
          record: true
          tag: Repeat
          spec: ${{ github.event.inputs.spec }}
          command: npx cypress-repeat -n ${{ github.event.inputs.repeat}}