Skip to content
Back to skills

Sonar

ASecurity

Check the SonarCloud quality gate for a PR (or the current branch) and drive it to zero. Reports GREEN, or the exact PR-scoped new issues/hotspots/coverage gaps to fix.

  • 1,509 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 2, 2026
ai-agentsjavascriptrustgojavabashsqlgitapisecurity

Works with

  • cli
  • api

Security analysis

A100/100

Scanned December 5, 2026

npx -y skills add ffroliva/gflow-cli --skill sonar --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Sonar?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Sonar
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ffroliva-sonar/badge)](https://www.skillsdirectory.com/skills/ffroliva-sonar)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: sonar
version: "1.0"
description: >
  Check the SonarCloud quality gate for a PR (or the current branch) and drive it to zero. Reports GREEN, or the exact PR-scoped new issues/hotspots/coverage gaps to fix.
---

# `/gflow:sonar [PR#]` — SonarCloud quality gate

The reusable Sonar primitive. `/gflow:check` is **local** and cannot see Sonar (it
is server-side, post-push); `/gflow:pr-council-review` is an LLM diff review, not the
gate verdict. This command answers one question: **is the SonarCloud gate green
(zero new issues) for this PR, and if not, exactly what must be fixed?**

`$ARGUMENTS` is the PR number. If empty, resolve it from the current branch
(`gh pr view --json number -q .number`). **`main` or `develop`** means the branch gate:
swap `&pullRequest=<N>` for `&branch=<name>` in every call below, and look for the CI run
with `gh run list --branch <name> --workflow ci.yml --commit <sha> --limit 1` (the commit
you are checking, not just the latest run). A branch can go red with
no code change: if the last analysis has a `QUALITY_PROFILE` event
(`GET /api/project_analyses/search?project=ffroliva_gflow-cli&branch=<name>`), SonarSource
changed the rules, and the new issues sit on unchanged code. Mark an issue false positive
only when the code is provably valid (2026-11-11: 55 x S6663 on `sqlite3.Row["col"]`), via
`POST /api/issues/bulk_change` with `do_transition=falsepositive` and the reason in `comment`.
The transition does not repaint the red check, so re-run the Sonar job afterwards
(`gh run rerun <run-id> --job <job-id>`) and read step 1 again.

Project: key `ffroliva_gflow-cli`, org `ffroliva-github` (see `sonar-project.properties`).
Quality gate: custom `Pristine (AAA, 90% coverage)` (gate ID `168256`) — **90% hard floor**
on both `new_coverage` and overall `coverage` (**target 95%**), AAA ratings (new and
overall), duplicated lines ≤ 3%, and 100% new security hotspots reviewed.
The CI scan sets `sonar.qualitygate.wait=true`, so a **green `SonarCloud analysis`
check means the gate genuinely passed** — but the gate **API is stale until that
check finishes**, so always check the GitHub check FIRST.

## Steps

**1. Check the GitHub check first (never trust the gate API while it's pending).**

```bash
gh pr checks <N> --json name,state,bucket | \
  jq -r '.[] | select(.name=="SonarCloud analysis") | "\(.bucket) \(.state)"'
```

For a branch there is no PR check: read the job of the run found above instead,
`gh run view <run-id> --json jobs -q '.jobs[] | select(.name=="SonarCloud analysis")'`.

- `pass` → GREEN **only if the `SonarCloud scan` step ran**. When SonarCloud is
  unreachable, CI skips that step and the job still passes with a warning. Check
  `gh run view <run-id> --json jobs -q '.jobs[] | select(.name=="SonarCloud analysis")
  | .steps[] | select(.name=="SonarCloud scan") | .conclusion'` is `success` (for a PR,
  the run id is in the check's `link`); `skipped`
  (or a cancelled or missing job) means no gate result exists for this commit, so report
  that, not GREEN.
- `pending` → the SonarCloud job (~50s) runs *after* the ~3.5min test matrix; wait
  (`gh pr checks <N> --watch`; for a branch, `gh run watch <run-id>`) before reading the API, or the API returns the
  previous commit's verdict.
- `fail` → continue to step 2 to enumerate the exact failing conditions.

**2. Enumerate the failing conditions (PR-scoped — this is the #1 gotcha).**

New-code issues live on the PR branch, NOT `main`. **Scope every call with
`&pullRequest=<N>`** or the API reports 0 and you chase phantoms. Token is in
`.env.local` as `SONAR_TOKEN` — read it inside a sandbox so it never lands in chat.
`curl` may be blocked by the context-mode hook → use `ctx_execute` (javascript
`fetch` with `Authorization: Basic base64(token+":")`).

```bash
# Which gate conditions are ERROR:
GET /api/qualitygates/project_status?projectKey=ffroliva_gflow-cli&pullRequest=<N>
# Actual coverage against the 90% floor and 95% target (swap &branch=<name> on branches):
GET /api/measures/component?component=ffroliva_gflow-cli&pullRequest=<N>&metricKeys=new_coverage,coverage
# New issues (bugs/smells) with file:line + rule + creationDate:
GET /api/issues/search?componentKeys=ffroliva_gflow-cli&pullRequest=<N>&resolved=false
# Unreviewed security hotspots:
GET /api/hotspots/search?projectKey=ffroliva_gflow-cli&pullRequest=<N>&status=TO_REVIEW
```

**3. Fix by condition — no gaming.**

- **`new_coverage < 90%` or `coverage < 90%`** (hard floor; **target 95%**) →
  - Below 90% fails the quality gate: add **real** unit tests for the uncovered lines
    (usually a new orchestration/`_run_*`/helper). There is NO mark-safe shortcut and
    NEVER widen `sonar.coverage.exclusions` to dodge it.
  - Between 90.0% and 94.9% clears the hard gate, but is below the **95% target**: report
    the gap explicitly in the verdict and close uncovered testable branches where
    practical without fake tests or exclusion widening.
- **`new_code_smells` / `new_maintainability_rating` (e.g. S1192 duplicated literal,
  S3776 cognitive complexity)** → fix at source. For a duplicated literal your diff
  merely *touched*, collapse it to one module-scope alias so it falls under the
  threshold (pyright/tests prove it's safe). For S7497, re-raise swallowed
  `asyncio.CancelledError`.
- **`new_security_hotspots_reviewed < 100%`** → only for a **genuine** false positive:
  `POST /api/hotspots/change_status` form body
  `hotspot=<key>&status=REVIEWED&resolution=SAFE&comment=<justification>` (204 = ok),
  then **re-run the SonarCloud job** so it reposts the GitHub status
  (`gh run rerun <run-id> --job <sonar-job-id>` — the coverage artifact is reused).
  Never mark a real hotspot SAFE.

**4. Push fixes, then re-verify from step 1** (the check must read `pass`).

## Output

- Verdict: **GREEN** (gate passed, zero new issues) or **RED** with the exact failing
  conditions and a fix list (`file:line · rule · what to do`).
- Measured `new_coverage` and overall `coverage` against the **90% hard floor** and
  **95% target** (call out any 90.0%–94.9% gap below the 95% target).
- After any fix: re-confirm via step 1 — green check = gate passed.

## Pipeline Continuation (Next Step Handoff)

Upon completing SonarCloud Quality Gate:
1. **Gate 🟢 GREEN:** Proactively announce: **"SonarCloud quality gate passed (zero new issues). Next step: Phase 10 Release Pipeline (`/gflow:release`) or merge PR."**
2. **Gate 🔴 RED:** Fix identified code smells/coverage gaps, push, and re-run `/gflow:sonar <PR#>`.

## See also

- `docs/GITHUB.md` § SonarCloud Quality Gate — full policy + coverage-exclusion rationale.
- `/gflow:check` — the local pre-commit gates (coverage floor pre-empts `new_coverage`).
- Memory: [[sonarcloud-pr-issues-scope-s7497]], [[sonarcloud-new-code-gotchas]],
  [[sonarcloud-hotspot-review-workflow]], [[sonarcloud-setup]].

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.