name: PVS-Studio analysis

# If the CI job fails with 404-not-found, then PVS studio has updated the release.
# Get the new DEB version from: https://pvs-studio.com/en/pvs-studio/download-all/

on: push

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

jobs:
  pvs_studio_analyzer:
    name: PVS-Studio static analyzer
    runs-on: ubuntu-latest
    env:
      debfile: pvs-studio-7.29.79138.387-amd64.deb
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - run:  sudo apt-get update

      - name: Log and setup environment
        run: |
          ./scripts/log-env.sh
          mkdir -p pvs-package

      - uses: actions/cache@v3.3.2
        id: cache-pvs
        with:
          path: pvs-package
          key: ${{ env.debfile }}

      - name:  Fetch PVS-Studio package
        if:    steps.cache-pvs.outputs.cache-hit != 'true'
        run:   wget "https://cdn.pvs-studio.com/${debfile}" -O "pvs-package/pvs.deb"

      - name: Install dependencies
        run: |
          set -xeu
          sudo apt-get install -y strace $(cat packages/ubuntu-20.04-apt.txt)
          sudo pip3 install --upgrade meson ninja
          sudo dpkg -i "pvs-package/pvs.deb"
          pvs-studio-analyzer credentials "${{ secrets.PvsStudioName }}" "${{ secrets.PvsStudioKey }}"

      - name: Cache subprojects
        id:   cache-subprojects
        uses: actions/cache@v3.3.2
        with:
          path: subprojects.tar
          key: subprojects-${{ hashFiles('subprojects/*.wrap') }}-1
          enableCrossOsArchive: true

      - if:   steps.cache-subprojects.outputs.cache-hit != 'true'
        name: Generate subprojects cache
        run:  scripts/fetch-and-tar-subprojects.sh

      - name: Extract subprojects cache
        run:  scripts/extract-subprojects-tar.sh

      - name: Build
        run: |
          set -xeu
          meson setup -Dbuildtype=debug -Dunit_tests=disabled --native-file=.github/meson/native-clang-15.ini build
          pvs-studio-analyzer trace -- meson compile -C build

      - name: Analyze
        run: |
          set -xeu
          log="pvs-report.log"
          general_criteria="GA:1,2;64:1;OP:1,2,3;CS:1"
          stamp="$(date +'%Y-%m-%d_T%H%M')-${GITHUB_SHA:0:8}"
          reportdir="pvs-report/pvs-report-${stamp}"
          disable_warnings="V002,V801,V802,V813,V826,V1042,V1071,V2008,V2010"

          mkdir -p "${reportdir}"

          pvs-studio-analyzer analyze \
            -a 63 \
            -e subprojects \
            -s .pvs-suppress \
            -j "$(nproc)" \
            -o "${log}"

          plog-converter \
            -p dosbox-staging \
            -v "${GITHUB_SHA:0:8}" \
            -a "${general_criteria}" \
            -d "${disable_warnings}" \
            -t fullhtml,csv \
            -o "${reportdir}" \
            "${log}"

          pvs-studio-analyzer suppress \
            -a "${general_criteria}" \
            -o "${reportdir}/supressible-list.json" \
            "${log}"

          ln -sf "${reportdir}/pvs-report.csv" .
          mv -f "${reportdir}/pvs-report.fullhtml"/* "${reportdir}/"
          rm -rf "${reportdir}/pvs-report.fullhtml"

      - name: Upload report
        uses: actions/upload-artifact@v4.3.1
        with:
          name: pvs-analysis-report
          path: pvs-report
          overwrite: true

      - name: Summarize report
        env:
          MAX_BUGS: 109
        run: |
          echo "Full report is included in build Artifacts"
          echo
          ./scripts/count-pvs-bugs.py "pvs-report.csv" "${MAX_BUGS}"
