-
Notifications
You must be signed in to change notification settings - Fork 42
GH#3711: Fix critical quality-debt in codacy-collector-helper.sh #4067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,9 +38,11 @@ readonly CODACY_PAGE_SIZE=100 | |
| readonly CODACY_MAX_PAGES=50 | ||
| readonly CODACY_RATE_LIMIT_WAIT=60 | ||
| readonly CODACY_RETRY_BACKOFF_BASE=2 | ||
| # Config file paths (working config preferred, template as fallback) | ||
| readonly AUDIT_CONFIG="configs/code-audit-config.json" | ||
| readonly AUDIT_CONFIG_TEMPLATE="configs/code-audit-config.json.txt" | ||
| # Config file paths anchored to repo root (not CWD-dependent) | ||
| REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" | ||
| readonly REPO_ROOT | ||
| readonly AUDIT_CONFIG="${REPO_ROOT}/configs/code-audit-config.json" | ||
| readonly AUDIT_CONFIG_TEMPLATE="${REPO_ROOT}/configs/code-audit-config.json.txt" | ||
|
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anchoring |
||
|
|
||
| # ============================================================================= | ||
| # Logging: uses shared log_* from shared-constants.sh with CODACY prefix | ||
|
|
@@ -228,13 +230,17 @@ codacy_api_request() { | |
| local attempt=0 | ||
| local response="" | ||
| local http_code="" | ||
| local tmp_response tmp_headers | ||
|
|
||
| tmp_response=$(mktemp) | ||
| tmp_headers=$(mktemp) | ||
| local tmp_response | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| local tmp_dir="${HOME}/.aidevops/.agent-workspace/tmp" | ||
| mkdir -p "$tmp_dir" 2>/dev/null || true | ||
| tmp_response=$(mktemp "${tmp_dir}/codacy-resp.XXXXXX") | ||
|
Comment on lines
+234
to
+236
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a dedicated temporary directory References
|
||
| _save_cleanup_scope | ||
| trap '_run_cleanups' RETURN | ||
| push_cleanup "rm -f '${tmp_response}' '${tmp_headers}'" | ||
| push_cleanup "rm -f '${tmp_response}'" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| # Rate-limit retries use a separate budget so 429s don't consume error retries | ||
| local rate_limit_retries=0 | ||
| local max_rate_limit_retries=5 | ||
|
Comment on lines
+241
to
+243
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Introducing |
||
|
|
||
| while [[ $attempt -lt ${MAX_RETRIES:-3} ]]; do | ||
| attempt=$((attempt + 1)) | ||
|
|
@@ -243,7 +249,6 @@ codacy_api_request() { | |
| -s | ||
| -w "%{http_code}" | ||
| -o "$tmp_response" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| -D "$tmp_headers" | ||
| -H "api-token: ${CODACY_TOKEN}" | ||
| -H "Accept: application/json" | ||
| ) | ||
|
|
@@ -270,10 +275,17 @@ codacy_api_request() { | |
| return 0 | ||
| ;; | ||
| 429) | ||
| # Rate limited — extract retry-after or use default wait | ||
| # Rate limited — separate budget so 429s don't consume error retries | ||
| rate_limit_retries=$((rate_limit_retries + 1)) | ||
| if [[ $rate_limit_retries -ge $max_rate_limit_retries ]]; then | ||
| log_error "Rate limited ${rate_limit_retries} times — giving up: ${endpoint}" | ||
| return 1 | ||
| fi | ||
|
Comment on lines
+279
to
+283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new logic to check |
||
| local retry_after="$CODACY_RATE_LIMIT_WAIT" | ||
| log_warn "Rate limited (429). Waiting ${retry_after}s before retry (attempt ${attempt}/${MAX_RETRIES})..." | ||
| log_warn "Rate limited (429). Waiting ${retry_after}s (rate-limit retry ${rate_limit_retries}/${max_rate_limit_retries})..." | ||
| sleep "$retry_after" | ||
| # Don't consume the error retry budget for rate limits | ||
| attempt=$((attempt - 1)) | ||
|
Comment on lines
+287
to
+288
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decrementing the main |
||
| continue | ||
| ;; | ||
| 401) | ||
|
|
@@ -387,12 +399,13 @@ cmd_collect() { | |
| while [[ "$has_more" == "true" && $page -lt $CODACY_MAX_PAGES ]]; do | ||
| page=$((page + 1)) | ||
|
|
||
| # Build request body with cursor-based pagination | ||
| # Build request body with cursor-based pagination (jq for safe JSON escaping) | ||
| local request_body | ||
| if [[ -n "$cursor" ]]; then | ||
| request_body="{\"limit\": ${CODACY_PAGE_SIZE}, \"cursor\": \"${cursor}\"}" | ||
| request_body=$(jq -nc --argjson limit "$CODACY_PAGE_SIZE" --arg cursor "$cursor" \ | ||
| '{limit: $limit, cursor: $cursor}') | ||
| else | ||
| request_body="{\"limit\": ${CODACY_PAGE_SIZE}}" | ||
| request_body=$(jq -nc --argjson limit "$CODACY_PAGE_SIZE" '{limit: $limit}') | ||
| fi | ||
|
Comment on lines
+405
to
409
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using References
|
||
|
|
||
| local endpoint="/analysis/organizations/gh/${org}/repositories/${repo_name}/issues/search" | ||
|
|
@@ -463,8 +476,10 @@ insert_findings() { | |
| local response="$2" | ||
|
|
||
| local sql_file jq_filter_file | ||
| sql_file=$(mktemp) | ||
| jq_filter_file=$(mktemp) | ||
| local tmp_dir="${HOME}/.aidevops/.agent-workspace/tmp" | ||
| mkdir -p "$tmp_dir" 2>/dev/null || true | ||
| sql_file=$(mktemp "${tmp_dir}/codacy-sql.XXXXXX") | ||
| jq_filter_file=$(mktemp "${tmp_dir}/codacy-jq.XXXXXX") | ||
|
Comment on lines
+479
to
+482
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to References
|
||
| _save_cleanup_scope | ||
| trap '_run_cleanups' RETURN | ||
| push_cleanup "rm -f '${sql_file}'" | ||
|
|
@@ -512,7 +527,13 @@ JQ_EOF | |
|
|
||
| local count=0 | ||
| if [[ -s "$sql_file" ]]; then | ||
| db "$AUDIT_DB" <"$sql_file" 2>/dev/null || log_warn "Some Codacy inserts may have failed" | ||
| # Wrap in transaction for atomicity and performance (single fsync) | ||
| { | ||
| echo "BEGIN TRANSACTION;" | ||
| cat "$sql_file" | ||
| echo "COMMIT;" | ||
| } | | ||
|
Comment on lines
+531
to
+535
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrapping the batch INSERT statements in |
||
| db "$AUDIT_DB" 2>/dev/null || log_warn "Some Codacy inserts may have failed" | ||
| count=$(wc -l <"$sql_file" | tr -d ' ') | ||
| fi | ||
|
|
||
|
|
@@ -597,7 +618,12 @@ cmd_query() { | |
| log_error "Missing value for --limit" | ||
| return 1 | ||
| } | ||
| limit="$2" | ||
| if [[ "$2" =~ ^[0-9]+$ ]] && [[ "$2" -gt 0 && "$2" -le 10000 ]]; then | ||
| limit="$2" | ||
| else | ||
| log_error "Invalid limit value: $2 (must be a positive integer, max 10000)" | ||
| return 1 | ||
|
Comment on lines
+621
to
+625
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The added input validation for the References
|
||
| fi | ||
| shift 2 | ||
| ;; | ||
| *) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
REPO_ROOTvariable is correctly declared and then madereadonlyin separate steps. This adheres to the ShellCheck SC2155 recommendation, which suggests separating declaration and assignment forreadonlyvariables to avoid potential issues in some shell environments. This is a good practice for maintainability and robustness.References
localdeclaration from the assignment if command substitution is involved, to ensure exit code safety. For consistency, this pattern should be maintained even for simple assignments.