name: Checks
on:
  pull_request:

jobs:
  cppcheck:
    name: Cppcheck
    runs-on: ubuntu-22.04
    timeout-minutes: 5

    steps:
      - name: Checkout sources
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          path: src/pg_stat_monitor

      - name: Checkout cppcheck sources
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          repository: "danmar/cppcheck"
          ref: "2.13.4"
          path: src/cppcheck

      - name: Build and install cppcheck
        working-directory: src/cppcheck
        run: |
          mkdir build
          cd build
          cmake ..
          cmake --build .
          sudo cmake --install .

      - name: Execute linter check with cppcheck
        working-directory: src/pg_stat_monitor
        run: |
          set -x
          cppcheck --enable=all --inline-suppr --template='{file}:{line},{severity},{id},{message}' --error-exitcode=1 --suppress=missingIncludeSystem --suppress=missingInclude --suppress=unmatchedSuppression:pg_stat_monitor.c --check-config .

  format:
    name: Format
    runs-on: ubuntu-22.04
    timeout-minutes: 5

    steps:
      - name: Clone postgres repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          repository: 'postgres/postgres'
          ref: 'REL_17_STABLE'

      - name: Checkout sources
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          path: 'contrib/pg_stat_monitor'

      - name: Configure postgres
        run: ./configure

      - name: Install perltidy
        run: sudo cpan -T SHANCOCK/Perl-Tidy-20230309.tar.gz

      - name: Install pg_bsd_indent
        working-directory: src/tools/pg_bsd_indent
        run: sudo make install

      - name: Add pg_bsd_indent and pgindent to path
        run: |
          echo "/usr/local/pgsql/bin" >> $GITHUB_PATH
          echo "${{ github.workspace }}/src/tools/pgindent" >> $GITHUB_PATH

      - name: Format sources
        working-directory: contrib/pg_stat_monitor
        run: |
          make update-typedefs
          make indent

      - name: Check files are formatted and no source code changes
        working-directory: contrib/pg_stat_monitor
        run: |
          git status
          git diff --exit-code

  license:
    name: License
    runs-on: ubuntu-22.04
    timeout-minutes: 5

    steps:
      - name: Checkout sources
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

      - name: Check license headers
        uses: apache/skywalking-eyes/header@61275cc80d0798a405cb070f7d3a8aaf7cf2c2c1 # v0.8.0
        with:
          token: "" # Prevent comments
