name: 'dhis2: nightly'

# This workflow runs the e2e tests on the default branch against dev

on:
    # schedule:
    # - cron: '10 4 * * 2'
    workflow_dispatch:

concurrency:
    group: ${{ github.workflow}}-${{ github.ref }}
    cancel-in-progress: true

defaults:
    run:
        shell: bash

jobs:
    e2e-dev:
        runs-on: ubuntu-latest

        strategy:
            matrix:
                containers: [1, 2, 3, 4]

        steps:
            - uses: actions/checkout@v4
            - uses: actions/setup-node@v4
              with:
                  node-version: 18.x

            - name: Install
              run: yarn install --frozen-lockfile

            - name: End-to-End tests
              uses: cypress-io/github-action@v2
              with:
                  record: true
                  parallel: true
                  start: ${{ env.SERVER_START_CMD }}
                  wait-on: ${{ env.SERVER_URL }}
                  wait-on-timeout: 300
                  group: 'e2e'
                  tag: ${{ github.event_name }}
              env:
                  CI: true
                  CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
                  SERVER_START_CMD: 'yarn cypress:start'
                  SERVER_URL: 'http://localhost:3000'
                  cypress_dhis2_api_stub_mode: 'DISABLED'
                  REACT_APP_DHIS2_BASE_URL: ${{ secrets.CYPRESS_DHIS2_BASE_URL_DEV }}
                  cypress_dhis2_base_url: ${{ secrets.CYPRESS_DHIS2_BASE_URL_DEV}}
                  cypress_dhis2_username: ${{ secrets.CYPRESS_DHIS2_USERNAME }}
                  cypress_dhis2_password: ${{ secrets.CYPRESS_DHIS2_PASSWORD }}
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    send-slack-message:
        runs-on: ubuntu-latest
        needs: e2e-dev
        if: |
            failure() &&
            !cancelled()
        steps:
            - name: Send failure message to analytics-internal-kfmt slack channel
              id: slack
              uses: slackapi/slack-github-action@v1.27.0
              with:
                  channel-id: ${{ secrets.SLACK_CHANNEL_ID }}
                  slack-message: ':dashboard-app: Dashboard-app e2e nightly run <https://cloud.cypress.io/projects/5fk191/runs?branches=[{"label":"dev","suggested":false,"value":"dev"}]|failed>'
              env:
                  SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
