name: {{ name }}

on:
  push:
    branches:
      - {{ branch }}
  pull_request:
    branches:
      - {{ branch }}

jobs:
  reuse:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: REUSE Compliance Check
      uses: fsfe/reuse-action@v1

  clang-format:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Run clang-format style check for C/C++/Protobuf programs
      uses: jidicula/clang-format-action@v4.10.2
      with:
        clang-format-version: '13'
        check-path: 'src'
        # The fallback style for clang-format.
        # To be used when no .clang-format file exists in a repository.
        fallback-style: 'llvm'

  static-code-analysis:

    env:
      RCA_LINK: https://github.com/mozilla/rust-code-analysis/releases/download
      RCA_VERSION: v0.0.25

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Install rust-code-analysis
      run: |
        mkdir -p $HOME/.local/bin
        curl -L "$RCA_LINK/$RCA_VERSION/rust-code-analysis-linux-cli-x86_64.tar.gz" |
        tar xz -C $HOME/.local/bin
        echo "$HOME/.local/bin" >> $GITHUB_PATH

    - name: Run rust-code-analysis
      run: |
        mkdir $HOME/rca-json
        rust-code-analysis-cli --metrics -O json --pr -o "$HOME/rca-json" -p $PWD

    - name: Upload rust-code-analysis json
      uses: actions/upload-artifact@v3
      with:
        name: rca-json
        path: ~/rca-json

  tests:

    needs: [reuse, clang-format, static-code-analysis]

    strategy:
      matrix:
        platform: [ubuntu-latest, macos-latest]

    runs-on: {{ '${{ matrix.platform }}' }}

    steps:
    - uses: actions/checkout@v4

    - name: Set up last Python version
      uses: actions/setup-python@v2

    - name: Install meson, ninja and gcovr
      run: |
        pip install meson ninja gcovr

    - name: Create build directory and configure settings
      run: |
        meson setup --buildtype release .build-directory

    - name: Build the project
      run: |
        meson compile -C .build-directory

    - name: Create a test and code coverage directory
      run: |
        meson setup -Db_coverage=true .build-directory-coverage

    - name: Run code coverage tests
      run: |
        meson test -C .build-directory-coverage

    - name: Export the code coverage as xml file
      run: |
        ninja coverage -C .build-directory-coverage

    - name: Create an address sanitizer instrumented build directory
      run: |
        meson setup --buildtype release -Db_sanitize=address -Db_lundef=false .build-directory-asan

    - name: Run address sanitizer instrumented tests
      run: |
        meson test -C .build-directory-asan

    - name: Update coverage.info
      uses: actions/upload-artifact@v3
      with:
        name: coverage
        path: |
          .build-directory-coverage/meson-logs/coverage.info

  docker-compose-tests:

    needs: [reuse, clang-format, static-code-analysis]

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Run test suite with docker-compose
      run: |
        docker-compose up