Skip to content

Commit 426b2e0

Browse files
committed
first try codeql cli integration
1 parent 8d4ef3b commit 426b2e0

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

integration_test.sh

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ LOG_DIR=${LOG_DIR:-_logs/logs}
1212
SUMMARY_FILE=${SUMMARY_FILE:-_logs/build_summary.md}
1313
KNOWN_GOOD_FILE=""
1414

15+
# Codeql
16+
17+
CODEQL_WORK_DIR="./codeql_analysis_results"
18+
CODEQL_DATABASES_DIR="${CODEQL_WORK_DIR}/databases"
19+
CODEQL_SARIF_DIR="${CODEQL_WORK_DIR}/sarif"
20+
CODEQL_LANGUAGE="cpp"
21+
CODEQL_QUERY_PACKS="codeql/cpp-queries,codeql/misra-cpp-coding-standards" # Add more packs as needed
22+
CODEQL_CLI_VERSION="v2.23.6" # Use the latest stable version
23+
CODEQL_PLATFORM="linux64" # e.g., linux64, macos, win64
24+
CODEQL_BUNDLE="codeql-${CODEQL_PLATFORM}.zip"
25+
CODEQL_URL="https://github.com/github/codeql-cli-binaries/releases/download/${CODEQL_CLI_VERSION}/${CODEQL_BUNDLE}"
26+
#https://github.com/github/codeql-cli-binaries/releases/download/v2.23.6/codeql-linux64.zip
27+
1528
# maybe move this to known_good.json or a config file later
1629
declare -A BUILD_TARGET_GROUPS=(
1730
[score_baselibs]="@score_baselibs//score/..."
@@ -23,6 +36,8 @@ declare -A BUILD_TARGET_GROUPS=(
2336
[score_feo]="@score_feo//..."
2437
)
2538

39+
40+
2641
# Parse command line arguments
2742
while [[ $# -gt 0 ]]; do
2843
case $1 in
@@ -107,19 +122,70 @@ overall_depr_total=0
107122

108123
# Track if any build group failed
109124
any_failed=0
125+
binary_path="${CODEQL_WORK_DIR}/codeql-cli/codeql/codeql"
126+
127+
if [ -x "${binary_path}" ]; then
128+
echo "Local CodeQL CLI found at ${binary_path}. Adding to PATH."
129+
export PATH="$(pwd)/${CODEQL_WORK_DIR}/codeql-cli/codeql:${PATH}"
130+
else
131+
echo "CodeQL CLI not found. Downloading..."
132+
mkdir -p "${CODEQL_WORK_DIR}/codeql-cli"
133+
curl -L "${CODEQL_URL}" -o "${CODEQL_WORK_DIR}/${CODEQL_BUNDLE}"
134+
unzip "${CODEQL_WORK_DIR}/${CODEQL_BUNDLE}" -d "${CODEQL_WORK_DIR}/codeql-cli"
135+
export PATH="$(pwd)/${CODEQL_WORK_DIR}/codeql-cli/codeql:${PATH}"
136+
echo "CodeQL CLI downloaded and added to PATH."
137+
fi
138+
139+
# Verify CodeQL CLI is now available
140+
if ! command -v codeql &> /dev/null; then
141+
echo "Error: CodeQL CLI could not be set up. Exiting."
142+
exit 1
143+
else
144+
echo "codeql found in path"
145+
fi
146+
147+
148+
mkdir -p "${CODEQL_DATABASES_DIR}"
149+
mkdir -p "${CODEQL_SARIF_DIR}"
110150

111151
for group in "${!BUILD_TARGET_GROUPS[@]}"; do
112152
targets="${BUILD_TARGET_GROUPS[$group]}"
113153
log_file="${LOG_DIR}/${group}.log"
154+
155+
db_path="${CODEQL_DATABASES_DIR}/${group}_db"
156+
sarif_output="${CODEQL_SARIF_DIR}/${group}.sarif"
114157

158+
# 1. Clean Bazel to ensure a fresh build for CodeQL tracing
159+
echo "Running 'bazel clean --expunge' and 'bazel shutdown'..."
160+
bazel clean --expunge || { echo "Bazel clean failed for ${group}"; exit 1; }
161+
bazel shutdown || { echo "Bazel shutdown failed for ${group}"; exit 1; }
162+
115163
# Log build group banner only to stdout/stderr (not into summary table file)
116164
echo "--- Building group: ${group} ---"
117165
start_ts=$(date +%s)
118166
echo "bazel build --config "${CONFIG}" ${targets} --verbose_failures"
119167
# GitHub Actions log grouping start
120168
echo "::group::Bazel build (${group})"
121169
set +e
122-
bazel build --config "${CONFIG}" ${targets} --verbose_failures 2>&1 | tee "$log_file"
170+
171+
build_command="bazel build \
172+
--config '${CONFIG}' \
173+
${targets} \
174+
--verbose_failures \
175+
--spawn_strategy=local \
176+
--nouse_action_cache \
177+
--noremote_accept_cached \
178+
--noremote_upload_local_results \
179+
--disk_cache= \
180+
2>&1 | tee \\\"${log_file}\\\""
181+
182+
codeql database create "${db_path}" \
183+
--language="${CODEQL_LANGUAGE}" \
184+
--command="bash -c \"${build_command}\"" \
185+
--overwrite \
186+
|| { echo "CodeQL database creation failed for ${group}"; exit 1; }
187+
188+
123189
build_status=${PIPESTATUS[0]}
124190
# Track if any build group failed
125191
if [[ ${build_status} -ne 0 ]]; then
@@ -133,6 +199,24 @@ for group in "${!BUILD_TARGET_GROUPS[@]}"; do
133199
d_count=$(depr_count "$log_file")
134200
overall_warn_total=$(( overall_warn_total + w_count ))
135201
overall_depr_total=$(( overall_depr_total + d_count ))
202+
203+
# Shutdown Bazel again after the traced build
204+
echo "Running 'bazel shutdown' after CodeQL database creation..."
205+
bazel shutdown || { echo "Bazel shutdown failed after tracing for ${group}"; exit 1; }
206+
207+
# 4. Analyze the created database
208+
echo "Analyzing CodeQL database for ${group}..."
209+
codeql database analyze "${DB_PATH}" \
210+
--format=sarifv2.1.0 \
211+
--output="${SARIF_OUTPUT}" \
212+
--sarif-category="${group}-${CODEQL_LANGUAGE}" \
213+
--packs "${CODEQL_QUERY_PACKS}" \
214+
|| { echo "CodeQL analysis failed for ${group}"; exit 1; }
215+
216+
echo "CodeQL analysis for ${group} complete. Results saved to: ${SARIF_OUTPUT}"
217+
echo ""
218+
219+
136220
# Append as a markdown table row (duration without trailing 's')
137221
if [[ ${build_status} -eq 0 ]]; then
138222
status_symbol=""

0 commit comments

Comments
 (0)