# yamllint disable rule:comments-indentation
name: BackportPR

env:
  # Force the stdout and stderr streams to be unbuffered
  PYTHONUNBUFFERED: 1

on: # yamllint disable-line rule:truthy
  push:
    branches:
      - 'backport/**'

# Cancel the previous wf run in PRs.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  RunConfig:
    runs-on: [self-hosted, style-checker-aarch64]
    outputs:
      data: ${{ steps.runconfig.outputs.CI_DATA }}
    steps:
      - name: Check out repository code
        uses: ClickHouse/checkout@v1
        with:
          clear-repository: true # to ensure correct digests
          fetch-depth: 0 # to get version
          filter: tree:0
      - name: Debug Info
        uses: ./.github/actions/debug
      - name: Labels check
        run: |
          cd "$GITHUB_WORKSPACE/tests/ci"
          python3 run_check.py
      - name: Python unit tests
        run: |
          cd "$GITHUB_WORKSPACE/tests/ci"
          echo "Testing the main ci directory"
          python3 -m unittest discover -s . -p 'test_*.py'
      - name: PrepareRunConfig
        id: runconfig
        run: |
            echo "::group::configure CI run"
            python3 "$GITHUB_WORKSPACE/tests/ci/ci.py" --configure --outfile ${{ runner.temp }}/ci_run_data.json
            echo "::endgroup::"

            echo "::group::CI run configure results"
            python3 -m json.tool ${{ runner.temp }}/ci_run_data.json
            echo "::endgroup::"

            {
              echo 'CI_DATA<<EOF'
              cat  ${{ runner.temp }}/ci_run_data.json
              echo 'EOF'
            } >> "$GITHUB_OUTPUT"
      - name: Re-create GH statuses for skipped jobs if any
        run: |
            python3 "$GITHUB_WORKSPACE/tests/ci/ci.py" --infile ${{ runner.temp }}/ci_run_data.json --update-gh-statuses
  BuildDockers:
    needs: [RunConfig]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/docker_test_images.yml
    with:
      data: ${{ needs.RunConfig.outputs.data }}
  CompatibilityCheckX86:
    needs: [RunConfig, BuilderDebRelease]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Compatibility check (release)
      runner_type: style-checker
      data: ${{ needs.RunConfig.outputs.data }}
  CompatibilityCheckAarch64:
    needs: [RunConfig, BuilderDebAarch64]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Compatibility check (aarch64)
      runner_type: style-checker
      data: ${{ needs.RunConfig.outputs.data }}
#########################################################################################
#################################### ORDINARY BUILDS ####################################
#########################################################################################
  BuilderDebRelease:
    needs: [RunConfig, BuildDockers]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_build.yml
    with:
      build_name: package_release
      checkout_depth: 0
      data: ${{ needs.RunConfig.outputs.data }}
  BuilderDebAarch64:
    needs: [RunConfig, BuildDockers]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_build.yml
    with:
      build_name: package_aarch64
      checkout_depth: 0
      data: ${{ needs.RunConfig.outputs.data }}
  BuilderDebAsan:
    needs: [RunConfig, BuildDockers]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_build.yml
    with:
      build_name: package_asan
      data: ${{ needs.RunConfig.outputs.data }}
  BuilderDebTsan:
    needs: [RunConfig, BuildDockers]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_build.yml
    with:
      build_name: package_tsan
      data: ${{ needs.RunConfig.outputs.data }}
  BuilderDebDebug:
    needs: [RunConfig, BuildDockers]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_build.yml
    with:
      build_name: package_debug
      data: ${{ needs.RunConfig.outputs.data }}
  BuilderBinDarwin:
    needs: [RunConfig, BuildDockers]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_build.yml
    with:
      build_name: binary_darwin
      data: ${{ needs.RunConfig.outputs.data }}
      checkout_depth: 0
  BuilderBinDarwinAarch64:
    needs: [RunConfig, BuildDockers]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_build.yml
    with:
      build_name: binary_darwin_aarch64
      data: ${{ needs.RunConfig.outputs.data }}
      checkout_depth: 0
############################################################################################
##################################### Docker images  #######################################
############################################################################################
  DockerServerImage:
    needs: [RunConfig, BuilderDebRelease, BuilderDebAarch64]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Docker server image
      runner_type: style-checker
      data: ${{ needs.RunConfig.outputs.data }}
  DockerKeeperImage:
    needs: [RunConfig, BuilderDebRelease, BuilderDebAarch64]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Docker keeper image
      runner_type: style-checker
      data: ${{ needs.RunConfig.outputs.data }}
############################################################################################
##################################### BUILD REPORTER #######################################
############################################################################################
  Builds_Report:
    # run report check for failed builds to indicate the CI error
    if: ${{ !cancelled() && needs.RunConfig.result == 'success' && contains(fromJson(needs.RunConfig.outputs.data).jobs_data.jobs_to_do, 'Builds') }}
    needs: [RunConfig, BuilderDebAarch64, BuilderDebAsan, BuilderDebDebug, BuilderDebRelease, BuilderDebTsan, BuilderBinDarwin, BuilderBinDarwinAarch64]
    runs-on: [self-hosted, style-checker-aarch64]
    steps:
      - name: Check out repository code
        uses: ClickHouse/checkout@v1
      - name: Download reports
        run: |
          python3 "$GITHUB_WORKSPACE/tests/ci/ci.py" --infile ${{ toJson(needs.RunConfig.outputs.data) }} --pre --job-name Builds
      - name: Builds report
        run: |
          cd "$GITHUB_WORKSPACE/tests/ci"
          python3 ./build_report_check.py --reports package_release package_aarch64 package_asan package_tsan package_debug binary_darwin binary_darwin_aarch64
      - name: Set status
        run: |
          python3 "$GITHUB_WORKSPACE/tests/ci/ci.py" --infile ${{ toJson(needs.RunConfig.outputs.data) }} --post --job-name Builds
############################################################################################
#################################### INSTALL PACKAGES ######################################
############################################################################################
  InstallPackagesTestRelease:
    needs: [RunConfig, BuilderDebRelease]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Install packages (release)
      runner_type: style-checker
      data: ${{ needs.RunConfig.outputs.data }}
      run_command: |
        python3 install_check.py "$CHECK_NAME"
  InstallPackagesTestAarch64:
    needs: [RunConfig, BuilderDebAarch64]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Install packages (aarch64)
      runner_type: style-checker-aarch64
      data: ${{ needs.RunConfig.outputs.data }}
      run_command: |
        python3 install_check.py "$CHECK_NAME"
##############################################################################################
########################### FUNCTIONAl STATELESS TESTS #######################################
##############################################################################################
  FunctionalStatelessTestAsan:
    needs: [RunConfig, BuilderDebAsan]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Stateless tests (asan)
      runner_type: func-tester
      data: ${{ needs.RunConfig.outputs.data }}
##############################################################################################
############################ FUNCTIONAl STATEFUL TESTS #######################################
##############################################################################################
  FunctionalStatefulTestDebug:
    needs: [RunConfig, BuilderDebDebug]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Stateful tests (debug)
      runner_type: func-tester
      data: ${{ needs.RunConfig.outputs.data }}
##############################################################################################
######################################### STRESS TESTS #######################################
##############################################################################################
  StressTestTsan:
    needs: [RunConfig, BuilderDebTsan]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Stress test (tsan)
      runner_type: func-tester
      data: ${{ needs.RunConfig.outputs.data }}
#############################################################################################
############################# INTEGRATION TESTS #############################################
#############################################################################################
  IntegrationTestsAsanOldAnalyzer:
    needs: [RunConfig, BuilderDebAsan]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Integration tests (asan, old analyzer)
      runner_type: func-tester
      data: ${{ needs.RunConfig.outputs.data }}
  IntegrationTestsTsan:
    needs: [RunConfig, BuilderDebTsan]
    if: ${{ !failure() && !cancelled() }}
    uses: ./.github/workflows/reusable_test.yml
    with:
      test_name: Integration tests (tsan)
      runner_type: func-tester
      data: ${{ needs.RunConfig.outputs.data }}
  FinishCheck:
    if: ${{ !cancelled() }}
    needs:
      - RunConfig
      - Builds_Report
      - FunctionalStatelessTestAsan
      - FunctionalStatefulTestDebug
      - StressTestTsan
      - IntegrationTestsTsan
      - IntegrationTestsAsanOldAnalyzer
      - CompatibilityCheckX86
      - CompatibilityCheckAarch64
    runs-on: [self-hosted, style-checker]
    steps:
      - name: Check out repository code
        uses: ClickHouse/checkout@v1
        with:
          clear-repository: true
      - name: Finish label
        if: ${{ !failure() }}
        run: |
          export WORKFLOW_RESULT_FILE="/tmp/workflow_results.json"
          cat > "$WORKFLOW_RESULT_FILE" << 'EOF'
          ${{ toJson(needs) }}
          EOF
          cd "$GITHUB_WORKSPACE/tests/ci"
          # update mergeable check
          python3 merge_pr.py --set-ci-status
          # update overall ci report
          python3 finish_check.py --wf-status ${{ contains(needs.*.result, 'failure') && 'failure' || 'success' }}
          python3 merge_pr.py
      - name: Check Workflow results
        if: ${{ !cancelled() }}
        run: |
          export WORKFLOW_RESULT_FILE="/tmp/workflow_results.json"
          cat > "$WORKFLOW_RESULT_FILE" << 'EOF'
          ${{ toJson(needs) }}
          EOF
          python3 ./tests/ci/ci_buddy.py --check-wf-status