| 1 | # Fetch a large Codacy PR issue list |
| 2 | |
| 3 | Use this when `pr-issues.sh <PR>` fails locally with: |
| 4 | |
| 5 | ```text |
| 6 | jq: Argument list too long |
| 7 | ``` |
| 8 | |
| 9 | ## Why it happens |
| 10 | |
| 11 | `codacyaudit_pr_issues` can return a large JSON array. Passing that array to |
| 12 | `jq --argjson data "$issues_array"` sends the whole payload through the shell's |
| 13 | argument vector and can exceed the OS limit before `jq` starts. |
| 14 | |
| 15 | ## Correct pattern |
| 16 | |
| 17 | Write the JSON array to a temporary file and load it through `jq --slurpfile`: |
| 18 | |
| 19 | ```bash |
| 20 | issues_tmp="$(mktemp "${TMPDIR:-/tmp}/codacy-pr-issues-XXXXXX")" |
| 21 | trap 'rm -f "$issues_tmp"' EXIT |
| 22 | printf '%s' "$issues_array" > "$issues_tmp" |
| 23 | |
| 24 | jq -n \ |
| 25 | --arg pr "$PR" \ |
| 26 | --arg fetched_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ |
| 27 | --argjson total "$total" \ |
| 28 | --slurpfile data "$issues_tmp" \ |
| 29 | '{pr: ($pr | tonumber), fetched_at: $fetched_at, total: $total, data: $data[0]}' |
| 30 | ``` |
| 31 | |
| 32 | This keeps token bytes out of stdout and avoids shell argument-size limits. |