name: Coverity
"on":
  schedule:
    # run at 22:00 on every saturday
    - cron: '0 22 * * TUE,SAT'
  push:
    branches:
      - coverity_scan
      - trigger/coverity
  pull_request:
    paths: .github/workflows/coverity.yaml
  workflow_dispatch:
jobs:

  coverity:
    name: Coverity ${{ matrix.pg }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        # run only on the 3 latest PG versions as we have rate limit on coverity
        pg: [16, 17, 18]
        os: [ubuntu-22.04]
    steps:
    - name: Install Dependencies
      run: |
        sudo apt-get update
        sudo apt-get install gnupg systemd-coredump gdb postgresql-common
        yes | sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
        sudo apt-get update
        sudo apt-get install postgresql-${{ matrix.pg }} postgresql-server-dev-${{ matrix.pg }}

    - name: Checkout TimescaleDB
      uses: actions/checkout@v4

    - name: Coverity tools
      run: |
        wget https://scan.coverity.com/download/linux64 \
          --post-data "token=${{ secrets.COVERITY_TOKEN }}&project=timescale%2Ftimescaledb" \
          -O coverity_tool.tgz -q
        tar xf coverity_tool.tgz
        mv cov-analysis-linux64-* coverity

    - name: Build TimescaleDB
      run: |
        PATH="$GITHUB_WORKSPACE/coverity/bin:/usr/lib/postgresql/${{ matrix.pg }}/bin:$PATH"
        ./bootstrap -DCMAKE_BUILD_TYPE=Release
        cov-build --dir cov-int make -C build

    - name: Upload report
      env:
        FORM_EMAIL: --form email=ci@timescale.com
        FORM_FILE: --form file=@timescaledb.tgz
        FORM_DESC: --form description="CI"
        FORM_TOKEN: --form token="${{ secrets.COVERITY_TOKEN }}"
        COVERITY_URL: https://scan.coverity.com/builds?project=timescale%2Ftimescaledb
      run: |
        tar czf timescaledb.tgz cov-int
        curl $FORM_TOKEN $FORM_EMAIL $FORM_DESC $FORM_FILE \
          --form version="$(grep '^version' version.config | cut -b11-)-${{ matrix.pg }}" $COVERITY_URL
