# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
  push:
    branches: [ master, develop ]
    paths:
      - '**/CMakeLists.txt'
      - 'pom.xml'
      - 'src/main/**'
      - 'src/resources/**'
      - '.github/resources/codeql-config.yml'
      - '.github/workflows/codeql.yml'
  pull_request:
    branches: [ master, develop ]
    paths:
      - '**/CMakeLists.txt'
      - 'pom.xml'
      - 'src/main/**'
      - 'src/resources/**'
      - '.github/resources/codeql-config.yml'
      - '.github/workflows/codeql.yml'

env:
  PREREQS_ENV: ${{github.workspace}}/prereqs.sh
  PREREQS_INSTALL_DIR: ${{github.workspace}}/prereqs
  CMAKE_INSTALL_PREFIX: ${{github.workspace}}/install
  GENOMICSDB_BUILD_DIR: ${{github.workspace}}/build

jobs:
  analyze:
    name: Analyze
    runs-on: ubuntu-20.04
    permissions:
      actions: read
      contents: read
      security-events: write

    strategy:
      fail-fast: false
      # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
      # Learn more about CodeQL language support at https://git.io/codeql-language-support

    steps:
    - uses: actions/checkout@v4

    - uses: actions/setup-java@v4
      with:
        distribution: 'temurin'
        java-package: jdk
        java-version: '17'

    # Initializes the CodeQL tools for scanning.
    - name: Initialize CodeQL
      uses: github/codeql-action/init@v3
      with:
        config-file: ./.github/resources/codeql-config.yml
        languages: cpp, java
        # If you wish to specify custom queries, you can do so here or in a config file.
        # By default, queries listed here will override any specified in a config file.
        # Prefix the list here with "+" to use these queries and those in the config file.
        # queries: ./path/to/local/query, your-org/your-repo/queries@main

    - name: Cache built prerequisites for ubuntu
      uses: actions/cache@v4
      with:
        path: |
          ${{env.PREREQS_INSTALL_DIR}}
          ~/.m2/repository
          ~/awssdk-install
          ~/gcssdk-install
          ~/protobuf-install
        key: ubuntu-codeql-cache-prereqs-v1

    - name: Build GenomicsDB
      shell: bash
      run: |
        $GITHUB_WORKSPACE/.github/scripts/cleanup_hosts.sh
        echo "Install Prerequisites for Linux.."
        pushd $GITHUB_WORKSPACE/scripts/prereqs
        sudo INSTALL_PREFIX=$PREREQS_INSTALL_DIR PREREQS_ENV=$PREREQS_ENV ./install_prereqs.sh
        popd
        echo "Install prerequisites for Linux DONE"
        source $PREREQS_ENV
        cmake $GITHUB_WORKSPACE -DCMAKE_PREFIX_PATH=$PREREQS_INSTALL_DIR -DBUILD_JAVA=1 -DCMAKE_INSTALL_PREFIX=$CMAKE_INSTALL_PREFIX
        make -j4
        make install
        rm -fr dependencies/ src/test/ test/

    # Autobuild attempts to build any compiled languages  (C/C++, C#, or Java).
    # If this step fails, then you should remove it and run the build manually (see below)
    # - name: Autobuild
    #  uses: github/codeql-action/autobuild@v1

    - name: Perform CodeQL Analysis
      uses: github/codeql-action/analyze@v3
