name: PVS-Studio analysis

on:
  push:
    paths-ignore:
      - '.clang-format'
      - '.mdl-styles'
      - '*.md'
      - 'docs/**'
      - 'licenses/**'
      - 'website/**'

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

jobs:
  pvs_studio_analyzer:
    name: PVS-Studio static analyzer
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - name: Configure PVS-Studio repository
        run: |
          set -xeu
          wget -q -O - https://files.pvs-studio.com/etc/pubkey.txt | sudo apt-key add -
          sudo wget -O /etc/apt/sources.list.d/viva64.list https://files.pvs-studio.com/etc/viva64.list

      - name: Install dependencies
        run: |
          set -xeu
          sudo apt-get update
          sudo apt-get install -y strace $(cat packages/ubuntu-22.04-apt.txt)
          sudo pip3 install --upgrade meson ninja
          sudo apt-get install pvs-studio

      - name: Configure PVS-Studio license
        run: |
          set -xeu
          pvs-studio-analyzer credentials "${{ secrets.PvsStudioName }}" "${{ secrets.PvsStudioKey }}"

      - name: Cache subprojects
        id:   cache-subprojects
        uses: actions/cache@v4
        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: 87
        run: |
          echo "Full report is included in build Artifacts"
          echo
          ./scripts/count-pvs-bugs.py "pvs-report.csv" "${MAX_BUGS}"
