# Validate on default OSes.
name: Main

on:
  workflow_dispatch:
  pull_request:

jobs:
  src_checkers:
    name: Source checkers
    runs-on: ubuntu-latest
    permissions:
      issues: read
    steps:
      - name: Clone the git repo
        uses: actions/checkout@v4
        with:
          # required for `make check-license` to work properly
          fetch-depth: 50

      - name: Check changelog
        # Skip for pmem/pmdk/master and stable-* branches
        if: |
          !(github.repository == 'pmem/pmdk' &&
            (github.ref_name == 'master' || startsWith(github.ref_name, 'stable-')))
        uses: Zomzog/changelog-checker@v1.3.0
        with:
          fileName: ChangeLog
          noChangelogLabel: no changelog # the default
          checkNotification: Simple
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Install dependencies
        run: |
          sudo pip install flake8
          sudo apt-get install clang-format-14

      - name: Check license
        id: check_license
        continue-on-error: true
        env:
          NDCTL_ENABLE: n # just to speed up the job
          PMEMOBJ_IGNORE_DIRTY_SHUTDOWN: y # not recommended for production
          PMEMOBJ_IGNORE_BAD_BLOCKS: y # not recommended for production
        run: |
          make -j$(nproc) check-license || true
          git diff > /tmp/check-license.diff
          [ $(cat /tmp/check-license.diff | wc -l ) -gt 0 ] && exit 1
          exit 0

      - name: Upload check license diff
        if: steps.check_license.outcome != 'success'
        uses: actions/upload-artifact@v4
        with:
          name: check-license.diff
          path: /tmp/check-license.diff

      - name: Check license - Exit code
        run: |
          [ "${{steps.check_license.outcome}}" != "success" ] && exit 1
          exit 0

      - name: Check style
        env:
          CSTYLE_FAIL_IF_CLANG_FORMAT_MISSING: 1
          NDCTL_ENABLE: n # just to speed up the job
          PMEMOBJ_IGNORE_DIRTY_SHUTDOWN: y # not recommended for production
          PMEMOBJ_IGNORE_BAD_BLOCKS: y # not recommended for production
        run: make -j$(nproc) cstyle


  basic_build:
    name: Basic build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - CC: gcc
            CXX: g++
          - CC: clang
            CXX: clang++
    steps:
      - name: Clone the git repo
        uses: actions/checkout@v4

      - name: Install dependencies
        run: sudo apt-get -y install pandoc

      - name: Build sources
        env:
          NDCTL_ENABLE: n # just to speed up the job
          PMEMOBJ_IGNORE_DIRTY_SHUTDOWN: y # not recommended for production
          PMEMOBJ_IGNORE_BAD_BLOCKS: y # not recommended for production
          CC: ${{ matrix.CC }}
          CXX: ${{ matrix.CXX }}
        run: make -j$(nproc) test


  call-ubuntu:
    needs: [src_checkers, basic_build]
    uses: ./.github/workflows/ubuntu.yml
    name: Ubuntu

  coverage_scan:
    needs: [src_checkers, basic_build]
    uses: ./.github/workflows/scan_coverage.yml
    secrets:
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
    name: Coverage
