name: Continuous Integration

on: [push, pull_request]

jobs:
  get_fedora_releases:
    name: Get Fedora Releases
    runs-on: ubuntu-latest
    steps:
      - name: Query Fedora
        id: releases
        uses: sgallagher/get-fedora-releases-action@v1
    outputs:
      stable: ${{ steps.releases.outputs.stable }}
      development: ${{ steps.releases.outputs.development }}

  unit_tests_fedora_stable:
    name: Unit Tests (Stable Fedora)
    needs: get_fedora_releases
    runs-on: ubuntu-latest
    continue-on-error: false
    strategy:
      matrix:
        release: ${{ fromJson(needs.get_fedora_releases.outputs.stable) }}
    container:
      image: quay.io/fedora/fedora:${{ matrix.release }}-x86_64

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install dependencies
        run: $GITHUB_WORKSPACE/.ci/fedora/get_fedora_deps.sh


      - name: Set up the build directory
        run: meson setup --buildtype=debugoptimized ci $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci

      - name: Run CI tests
        run: meson test -C ci --suite ci --print-errorlogs -t 5

      - name: Set up the valgrind build directory
        run:
          meson setup --buildtype=debugoptimized -Dverbose_tests=false
                      -Drpmio=disabled
                      ci_valgrind $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci_valgrind

      - name: Run valgrind tests
        run:
          meson test -C ci_valgrind --suite ci_valgrind --print-errorlogs -t 10
            --wrap=$GITHUB_WORKSPACE/contrib/valgrind/valgrind_wrapper.sh

      - name: Run clang static analysis tests
        run: |
          meson setup --buildtype=debug -Dskip_introspection=true -Dwith_py3=false ci_scanbuild
          ninja -C ci_scanbuild scan-build; if [ $? -ne 0 ]; then elinks -dump ci_scanbuild/meson-logs/scanbuild/*/index.html; fi


  rpm_tests:
    name: Installed RPM Tests (Stable Fedora)
    needs: get_fedora_releases
    runs-on: ubuntu-latest
    continue-on-error: false
    strategy:
      matrix:
        arch:
          - x86_64
        release: ${{ fromJson(needs.get_fedora_releases.outputs.stable) }}
    container:
      image: quay.io/fedora/fedora:${{ matrix.release }}-x86_64

    steps:
      - name: Install git
        run: dnf -y install git-core

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install dependencies
        run: $GITHUB_WORKSPACE/.ci/fedora/get_fedora_deps.sh

      - name: Set up the RPM output directory
        run: mkdir -p $GITHUB_WORKSPACE/rpmbuild/RPMS

      - name: Build the RPM repository
        working-directory: ${{github.workspace}}/rpmbuild/RPMS
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          packit --debug build locally ../..
          createrepo_c x86_64

      - name: Install the packages
        working-directory: ${{github.workspace}}/rpmbuild/RPMS
        run:
          $GITHUB_WORKSPACE/.ci/retry-command.sh
          dnf -y install --nogpgcheck
                         --allowerasing
                         --repofrompath libmodulemd-ci,x86_64
                         x86_64/python3-libmodulemd*.rpm
                         x86_64/libmodulemd-devel*.rpm

      - name: Set up the build directory
        run:
          meson --buildtype=release -Dtest_installed_lib=true
                installed_lib_tests $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C installed_lib_tests

      - name: Run tests on the installed RPMs
        run:
          meson test --suite ci
                     -C installed_lib_tests
                     --print-errorlogs
                     -t 5

  unit_tests_fedora_devel:
    name: Unit Tests (Devel Fedora)
    needs: get_fedora_releases
    runs-on: ubuntu-latest
    continue-on-error: true
    strategy:
      matrix:
        release: ${{ fromJson(needs.get_fedora_releases.outputs.development) }}
    container:
      image: quay.io/fedora/fedora:${{ matrix.release }}-x86_64

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install dependencies
        run: $GITHUB_WORKSPACE/.ci/fedora/get_fedora_deps.sh


      - name: Set up the build directory
        run: meson setup --buildtype=debugoptimized ci $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci

      - name: Run CI tests
        run: meson test -C ci --suite ci --print-errorlogs -t 5

      - name: Set up the valgrind build directory
        run:
          meson setup --buildtype=debugoptimized -Dverbose_tests=false
                      ci_valgrind $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci_valgrind

      - name: Run valgrind tests
        run:
          meson test -C ci_valgrind --suite ci_valgrind --print-errorlogs -t 10
            --wrap=$GITHUB_WORKSPACE/contrib/valgrind/valgrind_wrapper.sh

      - name: Run clang static analysis tests
        run: |
          meson setup --buildtype=debug -Dskip_introspection=true -Dwith_py3=false ci_scanbuild
          ninja -C ci_scanbuild scan-build; if [ $? -ne 0 ]; then elinks -dump ci_scanbuild/meson-logs/scanbuild/*/index.html; fi


  rpm_tests_devel:
    name: Installed RPM Tests (Devel Fedora)
    needs: get_fedora_releases
    runs-on: ubuntu-latest
    continue-on-error: true
    strategy:
      matrix:
        arch:
          - x86_64
        release: ${{ fromJson(needs.get_fedora_releases.outputs.development) }}
    container:
      image: quay.io/fedora/fedora:${{ matrix.release }}-x86_64

    steps:
      - name: Install git
        run: dnf -y install git-core

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install dependencies
        run: $GITHUB_WORKSPACE/.ci/fedora/get_fedora_deps.sh

      - name: Set up the RPM output directory
        run: mkdir -p $GITHUB_WORKSPACE/rpmbuild/RPMS

      - name: Build the RPM repository
        working-directory: ${{github.workspace}}/rpmbuild/RPMS
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          packit --debug build locally ../..
          createrepo_c x86_64

      - name: Install the packages
        working-directory: ${{github.workspace}}/rpmbuild/RPMS
        run:
          $GITHUB_WORKSPACE/.ci/retry-command.sh
          dnf -y install --nogpgcheck
                         --allowerasing
                         --repofrompath libmodulemd-ci,x86_64
                         x86_64/python3-libmodulemd*.rpm
                         x86_64/libmodulemd-devel*.rpm

      - name: Set up the build directory
        run:
          meson --buildtype=release -Dtest_installed_lib=true
                installed_lib_tests $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C installed_lib_tests

      - name: Run tests on the installed RPMs
        run:
          meson test --suite ci
                     -C installed_lib_tests
                     --print-errorlogs
                     -t 5

  archlinux:
    name: Arch Linux
    runs-on: ubuntu-latest
    continue-on-error: true
    container:
      image: docker.io/library/archlinux:base

    steps:
      - name: Disable excluding documenation in pacman configuration
        run: sed -i -e '\| usr/share/gtk-doc/|d' -e '\| usr/share/doc/|d' /etc/pacman.conf

      - name: Install dependencies
        run: pacman -Syu --needed --noconfirm base-devel file git glib2 glib2-devel glib2-docs gobject-introspection gtk-doc jq libyaml meson python-gobject python-six valgrind

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up the build directory
        run:
          meson setup --buildtype=debugoptimized -Drpmio=disabled
                      ci $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci

      - name: Run CI tests
        run: meson test -C ci --suite ci --print-errorlogs -t 5

      - name: Set up the valgrind build directory
        run:
          meson setup --buildtype=debugoptimized -Dverbose_tests=false
                      -Drpmio=disabled
                      ci_valgrind $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci_valgrind

      - name: Run valgrind tests
        # Run profile scripts to set debuginfo environment variable,
        # <https://bugs.archlinux.org/task/74755>
        shell: bash --login -eo pipefail {0}
        run:
          meson test -C ci_valgrind --suite ci_valgrind --print-errorlogs -t 10
            --wrap=$GITHUB_WORKSPACE/contrib/valgrind/valgrind_wrapper.sh

  mageia_8:
    name: Mageia 8
    runs-on: ubuntu-latest
    continue-on-error: true
    container:
      image: docker.io/library/mageia:8

    steps:
      - name: Install dependencies
        run:
          dnf -y --setopt=install_weak_deps=False --setopt=tsflags='' install
              clang
              clang-analyzer
              createrepo_c
              curl
              elinks
              gcc
              gcc-c++
              git-core
              glib2-devel
              /usr/share/gtk-doc/html/glib/index.html
              gobject-introspection-devel
              gtk-doc
              jq
              libyaml-devel
              meson
              ninja-build
              openssl
              pkgconf
              popt-devel
              python3-six
              python3-autopep8
              python3-devel
              python3-gitpython
              python3-gobject3
              python3-pycodestyle
              python3-rpm-macros
              rpm-mageia-setup-build
              rpm-build
              rpm-devel
              rpmdevtools
              ruby
              "rubygem(json)"
              rubygems
              sudo
              valgrind
              wget

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up the build directory
        run: meson setup --buildtype=debugoptimized ci $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci

      - name: Run CI tests
        run: meson test -C ci --suite ci --print-errorlogs -t 5

      - name: Set up the valgrind build directory
        run:
          meson setup --buildtype=debugoptimized -Dverbose_tests=false
                      ci_valgrind $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci_valgrind

      - name: Run valgrind tests
        run:
          meson test -C ci_valgrind --suite ci_valgrind --print-errorlogs -t 10
            --wrap=$GITHUB_WORKSPACE/contrib/valgrind/valgrind_wrapper.sh

  opensuse_tw:
    name: openSUSE Tumbleweed
    runs-on: ubuntu-latest
    continue-on-error: true
    container:
      image: registry.opensuse.org/opensuse/tumbleweed
    steps:
      - name: Enable docs in container
        run:
          sed -i /etc/zypp/zypp.conf
              -e "s/rpm.install.excludedocs = yes/rpm.install.excludedocs = no/"

      - name: Install dependencies
        run:
          zypper install --no-confirm --no-recommends --capability
                 clang
                 clang-tools
                 createrepo_c
                 elinks
                 gcc
                 gcc-c++
                 git-core
                 glib2-devel
                 glib2-doc
                 gobject-introspection-devel
                 gtk-doc
                 jq
                 libyaml-devel
                 meson
                 ninja
                 pkgconf
                 python3-autopep8
                 python3-devel
                 python3-GitPython
                 python3-gobject
                 python3-pycodestyle
                 python-rpm-macros
                 rpm-build
                 rpm-devel
                 rpmdevtools
                 sudo
                 valgrind

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up the build directory
        run: meson setup --buildtype=debugoptimized ci $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci

      - name: Run CI tests
        run: meson test -C ci --suite ci --print-errorlogs -t 5

      - name: Set up the valgrind build directory
        run:
          meson setup --buildtype=debugoptimized -Dverbose_tests=false
                      ci_valgrind $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci_valgrind

      - name: Run valgrind tests
        run:
          meson test -C ci_valgrind --suite ci_valgrind --print-errorlogs -t 10
            --wrap=$GITHUB_WORKSPACE/contrib/valgrind/valgrind_wrapper.sh

  mandriva:
    name: OpenMandriva Cooker
    runs-on: ubuntu-latest
    continue-on-error: true
    container:
      image: docker.io/openmandriva/cooker
    outputs:
      meson_version: ${{ steps.scanbuild.outputs.available }}
    steps:
      - name: Install Dependencies
        run:
          dnf -y --setopt=install_weak_deps=False --setopt=tsflags='' install
              clang
              clang-analyzer
              cmake
              jq
              meson
              ninja
              rpmdevtools
              sudo
              valgrind
              wget
              task-devel
              'pkgconfig(glib-2.0)'
              'pkgconfig(gobject-introspection-1.0)'
              'pkgconfig(yaml-0.1)'
              'python3dist(autopep8)'
              'python3dist(pygobject)'
              'pkgconfig(rpm)'
              git-core

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up the build directory
        env:
          CC: clang
          CXX: clang++
        run:
          meson setup --buildtype=debugoptimized -Dwith_docs=false
                      ci $GITHUB_WORKSPACE

      - name: Compile the code
        run: ninja -C ci

      - name: Run CI tests
        run: meson test -C ci --suite ci --print-errorlogs -t 5

      # Do not run "ninja scan-build" on this platform because glib2.0
      # HTML documentation does not exist and -Dwith_docs=false is needed.
      # The internal scan-build target ignores build options
      # <https://github.com/mesonbuild/meson/issues/1167> and the build would
      # fail.
