name: Build

on:
  push:
    branches: [master, release/*, develop]
  pull_request:
    branches: [master, release/*, develop]
  workflow_dispatch:

concurrency:
  # Only cancel in-progress jobs or runs for the current workflow - matches against branch & tags
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build-and-test:
    name: Build and Test

    strategy:
      fail-fast: false
      matrix:
        os: [heavy]
        conan_profile: [gcc, clang]
        build_type: [Release, Debug]
        container: ['{ "image": "ghcr.io/xrplf/clio-ci:latest" }']
        static: [true]

        include:
          - os: macos15
            conan_profile: default_apple_clang
            build_type: Release
            container: ""
            static: false

    uses: ./.github/workflows/build_and_test.yml
    with:
      runs_on: ${{ matrix.os }}
      container: ${{ matrix.container }}
      conan_profile: ${{ matrix.conan_profile }}
      build_type: ${{ matrix.build_type }}
      static: ${{ matrix.static }}
      run_unit_tests: true
      run_integration_tests: false
      upload_clio_server: true

  code_coverage:
    name: Run Code Coverage

    uses: ./.github/workflows/build_impl.yml
    with:
      runs_on: heavy
      container: '{ "image": "ghcr.io/xrplf/clio-ci:latest" }'
      conan_profile: gcc
      build_type: Debug
      disable_cache: false
      code_coverage: true
      static: true
      upload_clio_server: false
      targets: all
      sanitizer: "false"
      analyze_build_time: false
    secrets:
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

  check_config:
    name: Check Config Description
    needs: build-and-test
    runs-on: heavy
    container:
      image: ghcr.io/xrplf/clio-ci:latest

    steps:
      - uses: actions/checkout@v4

      - uses: actions/download-artifact@v4
        with:
          name: clio_server_Linux_Release_gcc

      - name: Compare Config Description
        shell: bash
        run: |
          repoConfigFile=docs/config-description.md
          if ! [ -f "${repoConfigFile}" ]; then
            echo "Config Description markdown file is missing in docs folder"
            exit 1
          fi

          chmod +x ./clio_server
          configDescriptionFile=config_description_new.md
          ./clio_server -d "${configDescriptionFile}"

          configDescriptionHash=$(sha256sum "${configDescriptionFile}" | cut -d' ' -f1)
          repoConfigHash=$(sha256sum "${repoConfigFile}" | cut -d' ' -f1)

          if [ "${configDescriptionHash}" != "${repoConfigHash}" ]; then
            echo "Markdown file is not up to date"
            diff -u "${repoConfigFile}" "${configDescriptionFile}"
            rm -f "${configDescriptionFile}"
            exit 1
          fi
          rm -f "${configDescriptionFile}"
          exit 0
