name: Tests cloud

permissions: write-all

on:
  push:
    branches:
      - main
  workflow_dispatch:
  pull_request_target:
    branches:
      - main
    types: [labeled]

jobs:
  test:
    if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request_target' && github.event.label.name == 'tests:run-cloud')
    runs-on: [self-hosted, style-checker-aarch64]
    defaults:
      run:
        shell: bash
    strategy:
      matrix:
        go:
          - "1.25"
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.ref }}

      - name: Install Go ${{ matrix.go }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go }}

      - name: Tests
        env:
          CLICKHOUSE_USE_DOCKER: false
          CLICKHOUSE_USE_SSL: true
          CLICKHOUSE_HOST: ${{ secrets.INTEGRATIONS_TEAM_TESTS_CLOUD_HOST_SMT_PROD }}
          CLICKHOUSE_PASSWORD: ${{ secrets.INTEGRATIONS_TEAM_TESTS_CLOUD_PASSWORD_SMT_PROD }}
          CLICKHOUSE_JWT: ${{ secrets.INTEGRATIONS_TEAM_TESTS_CLOUD_JWT_DESERT_VM_43_PROD }}
          TESTCONTAINERS_RYUK_DISABLED: "true"
          GOTOOLCHAIN: "local"
        run: |
          CLICKHOUSE_DIAL_TIMEOUT=20 CLICKHOUSE_TEST_TIMEOUT=600s CLICKHOUSE_QUORUM_INSERT=2 make test

      - name: Remove tests:run-cloud label
        if: always() && github.event_name == 'pull_request_target'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh api repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/tests%3Arun-cloud -X DELETE || true
