| 1 | --- |
| 2 | name: codacy-audit |
| 3 | description: Codacy Cloud workflow for this repository -- run Codacy's analyzers locally before `git push` (mirrors what Codacy CI runs), and fetch/cluster Codacy issues for any PR via the v3 API. Use when the user mentions Codacy, "codacy analysis", `codacy-analysis-cli`, "codacy issues on PR", "fix codacy CI", "codacy markdownlint findings", or any Codacy gate failing on a netdata-org PR. Ships scripts analyze-local.sh (docker/binary runner for codacy-analysis-cli) and pr-issues.sh (paginated v3 issue fetch + group-by tool/pattern/severity/file). Token-safe -- CODACY_TOKEN never reaches assistant-visible stdout. Read-only by design; write actions (mark FP, mark fixed) require a GitHub issue or branch-local SOW. |
| 4 | --- |
| 5 | |
| 6 | # Codacy audit skill |
| 7 | |
| 8 | Drives Codacy Cloud for `netdata/netdata`: |
| 9 | |
| 10 | 1. **Pre-push prevention** -- run the same analyzers Codacy CI runs, locally, before `git push`. Collapses the "push -> wait minutes -> see findings -> fix -> push again" loop into one push. |
| 11 | 2. **Read-only PR triage** -- list Codacy issues for any PR, cluster by tool / pattern / severity / file, drop the JSON dump under `<repo>/.local/audits/codacy/`. |
| 12 | |
| 13 | This skill is the fourth in the static-analysis triage family in this repo: |
| 14 | `coverity-audit/`, `sonarqube-audit/`, `graphql-audit/`, `codacy-audit/`. Same shape, same conventions, same artifact directory. |
| 15 | |
| 16 | ## MANDATORY -- keep this skill alive |
| 17 | |
| 18 | If you (the assistant) discover a new pattern, gotcha, working flow, correction, or any operational knowledge while running this skill -- update this `SKILL.md` AND commit it BEFORE proceeding. Knowledge that isn't committed is lost. |
| 19 | |
| 20 | Examples worth capturing: |
| 21 | - New v3 API endpoint or response-shape detail learned the hard way |
| 22 | - Codacy-side rate-limit signals |
| 23 | - A pattern Codacy mismodels for this codebase (so the next assistant can add a path exclusion or mark it FP) |
| 24 | - A new tool the local CLI gained / lost |
| 25 | - Auth-failure surface (e.g. token type mismatch, expired token signs) |
| 26 | |
| 27 | ## MANDATORY -- live how-tos catalog |
| 28 | |
| 29 | Each concrete question that requires non-trivial analysis (multiple wrapper calls, jq pipelines, cross-referencing other skills) MUST become a how-to under `how-tos/<slug>.md` AND get an entry in `how-tos/INDEX.md` BEFORE the task is reported complete. Skipping this means the next assistant repeats the analysis from scratch. |
| 30 | |
| 31 | ## Scope |
| 32 | |
| 33 | In scope: |
| 34 | |
| 35 | - Local pre-push analysis via `codacy-analysis-cli` (auto-detects local binary, falls back to docker). |
| 36 | - Read-only PR-issue queries against the v3 API. |
| 37 | - Token-safe wrappers (sentinel-driven no-leak self-test). |
| 38 | |
| 39 | Out of scope until a real use case creates a GitHub issue or branch-local SOW: |
| 40 | |
| 41 | - Write actions (mark issue as false-positive, mark as fixed, modify ignore-patterns). |
| 42 | - Master-backlog triage on the 31,425+ open issues. |
| 43 | - Cross-repo aggregation across the netdata org. |
| 44 | |
| 45 | ## Required env keys |
| 46 | |
| 47 | | Key | Required for | |
| 48 | |---|---| |
| 49 | | `CODACY_TOKEN` | Account API token, header `api-token: <value>`. Required by `pr-issues.sh` and any wrapper that calls `_codacyaudit_run`. NOT required by `analyze-local.sh` (the CLI runs anonymously). | |
| 50 | | `CODACY_HOST` | Defaults to `https://api.codacy.com`. Override only if Codacy moves the API host. | |
| 51 | | `CODACY_PROVIDER` | Defaults to `gh` (GitHub). | |
| 52 | | `CODACY_ORG` | Defaults to `netdata`. | |
| 53 | | `CODACY_REPO` | Defaults to `netdata`. | |
| 54 | |
| 55 | All values live in `<repo>/.env` (gitignored). See `<repo>/.agents/ENV.md` for setup (where each value comes from, sample formats, common mistakes). |
| 56 | |
| 57 | ## Scripts (in scripts/) |
| 58 | |
| 59 | | Script | Purpose | |
| 60 | |---|---| |
| 61 | | `_lib.sh` | Helpers (`codacyaudit_*` prefix). Token-safe; ships `codacyaudit_selftest_no_token_leak`. | |
| 62 | | `analyze-local.sh` | Run `codacy-analysis-cli` locally; auto-pick local-binary or docker; write JSON dump under `.local/audits/codacy/`. | |
| 63 | | `pr-issues.sh` | Fetch all Codacy issues for a PR via the v3 API; cluster summary on stdout; full JSON dump on disk. | |
| 64 | |
| 65 | ## Workflow -- pre-push prevention |
| 66 | |
| 67 | ``` |
| 68 | $ .agents/skills/codacy-audit/scripts/analyze-local.sh |
| 69 | [analyze-local] runner=docker format=json dir=<repo> |
| 70 | [analyze-local] wrote 0 finding(s) to <repo>/.local/audits/codacy/local-<ts>.json |
| 71 | ``` |
| 72 | |
| 73 | Run this before `git push`. If it returns 0 findings, the Codacy gate on the PR will be green (modulo Codacy server-side patterns the local CLI doesn't bundle). If it returns findings, fix them locally first. |
| 74 | |
| 75 | Operational gotcha: when the Dockerized Codacy CLI fails before a tool can emit |
| 76 | results, the output file may have a `.json` suffix but contain tool-runner logs |
| 77 | instead of JSON. Always verify with `jq empty <dump>` before treating a local |
| 78 | dump as finding evidence. If GitHub check-run annotations are empty too, use |
| 79 | `pr-issues.sh` with `CODACY_TOKEN`; without that token, record the evidence gap |
| 80 | and re-check after the next push. |
| 81 | |
| 82 | Operational gotcha: the public Codacy v3 analysis endpoint can expose PR issue |
| 83 | details even when GitHub check-run annotations are empty and no `CODACY_TOKEN` |
| 84 | is available: |
| 85 | |
| 86 | ``` |
| 87 | curl -fsS \ |
| 88 | "https://api.codacy.com/api/v3/analysis/organizations/gh/netdata/repositories/netdata/pull-requests/<PR>/issues?limit=100" |
| 89 | ``` |
| 90 | |
| 91 | Filter for `.data[] | select(.deltaType == "Added")` to identify the issues |
| 92 | that still block the PR. Treat `commitInfo` fields as sensitive operational |
| 93 | metadata; do not copy names or email addresses into committed artifacts. |
| 94 | |
| 95 | To restrict to a single tool (matches what Codacy reported on a CI run): |
| 96 | |
| 97 | ``` |
| 98 | $ .agents/skills/codacy-audit/scripts/analyze-local.sh --tool markdownlint |
| 99 | ``` |
| 100 | |
| 101 | ## Workflow -- PR triage |
| 102 | |
| 103 | ``` |
| 104 | $ .agents/skills/codacy-audit/scripts/pr-issues.sh 22423 |
| 105 | [pr-issues] fetching issues for PR #22423 ... |
| 106 | [pr-issues] wrote 0 issue(s) to <repo>/.local/audits/codacy/pr-22423-<ts>.json |
| 107 | |
| 108 | No issues on PR #22423. |
| 109 | ``` |
| 110 | |
| 111 | For a PR with findings, the script emits a clustered TSV summary. Default grouping is `--by pattern`; switch to `--by tool`, `--by severity`, `--by file`, or `--by category` for other angles. The JSON dump under `.local/audits/codacy/` carries the full issue payload for follow-up jq queries. |
| 112 | |
| 113 | Operational note: large Codacy PR issue arrays must be passed to `jq` via a |
| 114 | temporary file and `--slurpfile`, not `--argjson`, because shell argument-size |
| 115 | limits can fail before `jq` starts. |
| 116 | |
| 117 | ## Path discipline |
| 118 | |
| 119 | This skill follows `<repo>/.agents/sow/specs/sensitive-data-discipline.md`: |
| 120 | |
| 121 | - Repo files: repo-relative (`<repo>/src/...`). |
| 122 | - Codacy account / org / repo identifiers: env-keyed. |
| 123 | - `CODACY_TOKEN`: NEVER literal in any committed file; ALWAYS via `${CODACY_TOKEN}` and the `_lib.sh` wrappers. |
| 124 | - Audit dumps: gitignored under `<repo>/.local/audits/codacy/`. |
| 125 | |
| 126 | ## Related skills |
| 127 | |
| 128 | - `.agents/skills/coverity-audit/` -- Coverity Scan (same triage shape). |
| 129 | - `.agents/skills/sonarqube-audit/` -- SonarCloud (same triage shape). |
| 130 | - `.agents/skills/graphql-audit/` -- GitHub Code Scanning / CodeQL (same triage shape). |
| 131 | |
| 132 | ## Token-safe self-test |
| 133 | |
| 134 | Before trusting wrappers in a long-running session, run the self-test: |
| 135 | |
| 136 | ``` |
| 137 | $ source .agents/skills/codacy-audit/scripts/_lib.sh |
| 138 | $ codacyaudit_load_env |
| 139 | $ codacyaudit_selftest_no_token_leak |
| 140 | PASS: codacyaudit_selftest_no_token_leak |
| 141 | ``` |
| 142 | |
| 143 | The self-test sets `CODACY_TOKEN` to a sentinel UUID, drives every public wrapper, captures stdout, and asserts the sentinel never appears. Run after editing `_lib.sh` or any wrapper. |