name: ATM Check Queries Run

env:
  DB_PATH: test_db
  ATM_MODEL_PACK: javascript/ql/experimental/adaptivethreatmodeling/src
  QUERY_SUITE: codeql-suites/javascript-atm-code-scanning.qls

on:
  pull_request:
    paths:
      - ".github/workflows/atm-check-queries-run.yml"
      - "javascript/ql/experimental/adaptivethreatmodeling/**"
  workflow_dispatch:

jobs:
  run-atm-queries:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Setup CodeQL
        uses: ./.github/actions/fetch-codeql
        with:
          channel: release

      - name: Install ATM model pack
        run: |
          set -exu
          
          # Install ATM model pack
          codeql pack install ${ATM_MODEL_PACK}

          # Retrieve model checksum
          model_checksum=$(codeql resolve extensions ${ATM_MODEL_PACK}/${QUERY_SUITE} | jq -r '.models[0].checksum')

          # Trust the model so that we can use it in the ATM boosted queries
          mkdir -p "$HOME/.config/codeql"
          echo "--insecurely-execute-ml-model-checksums ${model_checksum}" >> "$HOME/.config/codeql/config"

      - name: Create test DB
        run: |
          codeql database create ${RUNNER_TEMP}/${DB_PATH} --source-root config/atm/ --language javascript 

      - name: Run ATM query suite
        run: |
          codeql database run-queries -vv -- ${RUNNER_TEMP}/${DB_PATH} ${ATM_MODEL_PACK}/${QUERY_SUITE}
      

name: ATM Check Queries Run

env:
  DB_PATH: test_db
  QUERY_PACK: javascript/ql/experimental/adaptivethreatmodeling/src
  QUERY_SUITE: codeql-suites/javascript-atm-code-scanning.qls

on:
  pull_request:
    paths:
      - ".github/workflows/atm-check-queries-run.yml"
      - "javascript/ql/experimental/adaptivethreatmodeling/**"
  workflow_dispatch:

jobs:
  run-atm-queries:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Setup CodeQL
        uses: ./.github/actions/fetch-codeql
        with:
          channel: release

      - name: Install ATM model
        run: |
          set -exu
          
          # Install dependencies of ATM query pack
          codeql pack install ${QUERY_PACK}

          # Retrieve model checksum
          model_checksum=$(codeql resolve extensions ${QUERY_PACK}/${QUERY_SUITE} | jq -r '.models[0].checksum')

          # Trust the model so that we can use it in the ATM boosted queries
          mkdir -p "$HOME/.config/codeql"
          echo "--insecurely-execute-ml-model-checksums ${model_checksum}" >> "$HOME/.config/codeql/config"

      - name: Create test DB
        run: |
          codeql database create ${RUNNER_TEMP}/${DB_PATH} --source-root config/atm/ --language javascript 

      - name: Run ATM query suite
        run: |
          codeql database run-queries -vv -- ${RUNNER_TEMP}/${DB_PATH} ${QUERY_PACK}/${QUERY_SUITE}
      

Fix naming of query pack
ATM: Fix naming of query pack
