name: "Build and Test"

on:
  push:
    branches:
      - main
      - stable
      - oldstable
  pull_request:
    branches:
      - main
      - stable
      - oldstable

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

jobs:
  check-format:
    name: Check formatting
    runs-on: ubuntu-latest
    steps:
      - name: Check out gvmd
        uses: actions/checkout@v6
      - name: Check Source Format
        run: |
          clang-format -i -style=file src/gmp_{base,delete,get,tickets}.h \
          src/{gmp,gmpd,iterator,sql,types,utils,manage_utils}.h
          git diff --exit-code

  scan-build:
    name: scan-build (clang static analyzer)
    runs-on: ubuntu-latest
    container: ghcr.io/greenbone/gvm-libs:stable
    steps:
      - name: Check out gvmd
        uses: actions/checkout@v6
      - name: Install clang tools
        run: |
          apt update
          apt install --no-install-recommends -y clang clang-format clang-tools
      - name: Install build dependencies
        run: sh .github/install-dependencies.sh .github/build-dependencies.list
      - name: Configure scan build
        run: |
          scan-build cmake -B build -DCMAKE_BUILD_TYPE=Debug
          scan-build -o ~/scan-build-report cmake --build build
      - name: Upload scan-build report
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: scan-build-report
          path: ~/scan-build-report/
          retention-days: 7

  build-gvmd:
    name: Build gvmd (${{ matrix.build.name }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - build:
              name: stable
              container: stable
          - build:
              name: oldstable
              container: oldstable
          - build:
              name: testing
              container: testing
          - build:
              name: "with openvasd"
              container: stable
              flags: "-DENABLE_OPENVASD=1"
          - build:
              name: "with container scanning"
              container: stable
              flags: "-DENABLE_OPENVASD=1 -DENABLE_CONTAINER_SCANNING=1"
    container: ghcr.io/greenbone/gvm-libs:${{ matrix.build.container }}
    steps:
      - uses: actions/checkout@v6
      - name: Install build dependencies
        run: sh .github/install-dependencies.sh .github/build-dependencies.list
      - name: Configure and compile gvmd
        run: |
          rm -rf .git
          cmake -B build -DCMAKE_BUILD_TYPE=Release ${{ matrix.build.flags || '' }}
          cmake --build build -j $(nproc) -- install

  test-units:
    name: Unit Tests
    runs-on: ubuntu-latest
    container: ghcr.io/greenbone/gvm-libs:stable
    steps:
      - name: Check out gvmd
        uses: actions/checkout@v6
      - name: Install git for Codecov uploader
        run: |
          apt update
          apt install --no-install-recommends -y ca-certificates git
      - name: Install build dependencies
        run: sh .github/install-dependencies.sh .github/build-dependencies.list
      - name: "Instal cgreen"
        uses: greenbone/actions/install-cgreen@v3
      - name: Set git safe.directory
        run: git config --global --add safe.directory '*'
      - name: Build gvmd
        run: |
          cmake -B build -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=1
          cmake --build build
      - name: Configure and run tests
        run: CTEST_OUTPUT_ON_FAILURE=1 cmake --build build -- tests test
      - name: Upload test coverage to Codecov
        if: github.repository == 'greenbone/gvmd'
        uses: codecov/codecov-action@v6
        with:
          files: build/coverage/coverage.xml
          token: ${{ secrets.CODECOV_TOKEN }}
          flags: unittests

  cmake-format-check:
    name: Check CMake Formatting
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v6
      - uses: greenbone/actions/uv@v3
        with:
          install: gersemi
      - name: Check CMake Format
        id: check
        run: |
          gersemi --check . cmake
      - name: Report Diff
        if: ${{ failure() && steps.check.outcome == 'failure' }}
        run: |
          echo "## CMake Format Check" >> $GITHUB_STEP_SUMMARY
          gersemi --check --no-warn-about-unknown-commands . cmake >> $GITHUB_STEP_SUMMARY 2>&1 || true
          echo "## CMake Format Diff" >> $GITHUB_STEP_SUMMARY
          echo '```diff' >> $GITHUB_STEP_SUMMARY
          gersemi --diff . cmake >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY
