name: "CodeQL"

on:
  push:
    branches: [main]
  pull_request:
  schedule:
    - cron: "15 21 * * 6"

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  analyze:
    name: Analyze ${{ matrix.language }}
    runs-on: ubuntu-latest
    permissions:
      actions: read
      contents: read
      security-events: write

    strategy:
      fail-fast: false
      matrix:
        language: [python, cpp]

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Install Dependencies (python)
        if: ${{ matrix.language == 'python' }}
        run: |
          sudo apt-get update
          sudo apt-get install --yes python3-pip
          pip3 install qiskit-terra

      - name: Configure CMake
        if: ${{ matrix.language == 'cpp' }}
        run: cmake -S . -B build -DBUILD_DDSIM_TESTS=ON -DBUILD_QFR_TESTS=ON -DBUILD_DD_PACKAGE_TESTS=ON -DBINDINGS=ON

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v2
        with:
          languages: ${{ matrix.language }}
          config-file: .github/codeql-config.yml

      - name: Autobuild
        uses: github/codeql-action/autobuild@v2
        if: ${{ matrix.language == 'python' }}

      - name: Build cpp
        if: ${{ matrix.language == 'cpp' }}
        run: cmake --build build

      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v2
        with:
          upload: False
          output: sarif-results

      - name: filter-sarif
        uses: advanced-security/filter-sarif@main
        with:
          patterns: |
            -**/extern/**
          input: sarif-results/${{ matrix.language }}.sarif
          output: sarif-results/${{ matrix.language }}.sarif

      - name: Upload SARIF
        uses: github/codeql-action/upload-sarif@v2
        with:
          sarif_file: sarif-results/${{ matrix.language }}.sarif
