sonar-workflow
Fix SonarQube code quality issues for a pull request. Auto-fixes trivial issues (formatting, unused imports) and requests approval for complex issues (security, bugs). Integrates with commit-workflow and pr-workflow skills.
- License 许可
- MIT
- 最近更新
- 2026-09-20
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- sonar-workflow
- description
- Fix SonarQube code quality issues for a pull request. Auto-fixes trivial issues (formatting, unused imports) and requests approval for complex issues (security, bugs). Integrates with commit-workflow and pr-workflow skills.
- allowed-tools
- Bash
03 SKILL.md 正文
SonarQube Quality Workflow
This skill helps fix SonarQube code quality issues for a pull request by prioritizing issues, auto-fixing trivial problems, and requesting approval for complex changes.
When to Use
- After creating a PR when SonarQube analysis is available
- When asked to "fix SonarQube issues" or "improve code quality"
- When quality gate is failing on a PR
Prerequisites
- PR must exist for current branch
- SonarQube project configured in
sonar-project.properties - SonarQube analysis completed for the PR
Issue Classification
Trivial (Auto-fix without approval)
- Severity:
INFOorLOW - Type: Code smell only
- Single-file changes
- Impact:
MAINTAINABILITYonly - Keywords: "unused", "console.log", "formatting", "whitespace", "empty", "import order"
Complex (User approval required)
- All security hotspots
- Severity:
HIGH,BLOCKER,CRITICAL - Type: Bug, Vulnerability
- Coverage gaps (requires test writing)
- Code duplications (requires refactoring)
- Medium severity affecting multiple files
Workflow
1. Verify PR Exists
Check if PR exists for current branch:
gh pr list --head "$(git branch --show-current)" --state open
Exit if no PR found with message: "No PR found for current branch. Create PR first using pr-workflow skill."
Extract PR number from output.
2. Get PR Details
gh pr view <pr-number> --json number,headRefName,baseRefName
Extract: PR number, branch name, base branch.
3. Get SonarQube Project Key
grep "^sonar.projectKey=" sonar-project.properties | cut -d'=' -f2
Use the extracted project key for all SonarQube operations.
Verify project exists using search_my_sonarqube_projects.
4. Fetch Issues by Priority
Process in strict priority order:
a. Security Hotspots (Highest Priority)
# Tool: search_security_hotspots(projectKey, pullRequest, status=["TO_REVIEW"])
# For each: show_security_hotspot(hotspotKey)
Classification: Always complex (require approval).
b. BLOCKER/HIGH Issues
# Tool: search_sonar_issues_in_projects(projects=[projectKey], pullRequestId, severities=["BLOCKER","HIGH"])
# For each: show_rule(ruleKey) for remediation guidance
Classification: Always complex (require approval).
c. Coverage Issues
# Tool: search_files_by_coverage(projectKey, pullRequest, maxCoverage=70)
# For each: get_file_coverage_details(fileKey, pullRequest)
Classification: Always complex (requires test writing).
d. Code Duplications
# Tool: search_duplicated_files(projectKey, pullRequest)
# For each with >5% duplication: get_duplications(fileKey, pullRequest)
Classification: Always complex (requires refactoring).
e. MEDIUM/LOW/INFO Issues
# Tool: search_sonar_issues_in_projects(projects=[projectKey], pullRequestId, severities=["MEDIUM","LOW","INFO"])
# For each: show_rule(ruleKey)
Classification: Apply trivial vs complex logic based on rule keywords and type.
5. Categorize Issues
For each issue:
- Check severity + type + rule keywords
- Assign to
trivial_issues[]orcomplex_issues[] - Store: key, file, line, rule, description, fix_guidance
6. Auto-Fix Trivial Issues
For each trivial issue:
- Read file with issue
- Apply automated fix based on rule type:
- Unused imports: Remove import statement
console.log: Remove or comment out- Formatting: Apply suggested formatting
- Empty blocks: Add TODO comment or remove
- Verify fix:
analyze_code_snippet(projectKey, fileContent, language, scope) - If new issues introduced, rollback and move to complex queue
- If successful, add to
fixed_issues[]
7. Fix Complex Issues with Approval
For each complex issue (in priority order):
- Present to user:
- Issue type, severity, file:line
- Rule description and remediation guidance
- Code context (show relevant lines)
- Ask: "Fix this issue? [yes/no/skip remaining]"
- If yes:
- Read file
- Apply fix based on rule guidance
- Verify with
analyze_code_snippet - If successful, add to
fixed_issues[] - If failed, report and continue
- If "skip remaining", break loop
8. Commit Changes
Group fixes by type for logical commits using commit-workflow skill:
# Security fixes
Skill(skill="commit-workflow")
# Use format: fix(security): <description>
# Bug fixes
Skill(skill="commit-workflow")
# Use format: fix(<scope>): <description>
# Code smell fixes
Skill(skill="commit-workflow")
# Use format: refactor(<scope>): <description>
Commit Message Format:
<type>(<scope>): <description>
WHY: SonarQube rule <rule-key> - <rule reason>
Example:
fix(auth): remove unused import in login handler
WHY: SonarQube rule javascript:S1128 - unused imports increase bundle size
9. Mark Issues Resolved
For each fixed issue:
# Trivial code smells
change_sonar_issue_status(key, status=["accept"])
# Security hotspots
change_security_hotspot_status(hotspotKey, status=["REVIEWED"], resolution=["FIXED"])
# Complex bugs/vulnerabilities
change_sonar_issue_status(key, status=["accept"])
10. Update PR Description
Add new section to PR template after "## Test Results":
## SonarQube Fixes
Fixed X issues across Y files:
- Security: Z hotspots reviewed and fixed
- Bugs: A issues fixed
- Code Smells: B issues fixed
- Coverage: C files improved
- Duplications: D blocks refactored
Invoke pr-workflow skill to update PR with enhanced description.
Safety Rules
- NEVER auto-fix security hotspots - always require approval
- NEVER auto-fix HIGH/BLOCKER severity - always require approval
- VERIFY after each fix - use
analyze_code_snippetto ensure no new issues - ROLLBACK failed fixes - revert file if verification fails
- ONE-BY-ONE for complex - never batch complex fixes
- PRESERVE functionality - only fix code quality, don't change behavior
- RESPECT exclusions - honor
sonar.exclusions(node_modules, .next, dist, etc.) - NEVER commit secrets - reject any rule fix that exposes credentials
Examples
Example 1: Auto-fixing Unused Imports (Trivial)
Issue: javascript:S1128 - Unused import in apps/web/lib/auth.ts:5
Action:
- Read file
- Remove unused import line
- Verify with
analyze_code_snippet - Commit:
refactor(auth): remove unused imports - Mark resolved:
change_sonar_issue_status(key, status=["accept"])
Example 2: Fixing Security Hotspot (Complex)
Issue: Security hotspot - "Make sure this CORS policy is safe here" in apps/web/api/route.ts:12
Action:
- Present to user with rule details
- Wait for approval
- If approved:
- Read file and context
- Restrict CORS origins to whitelist
- Verify fix
- Commit:
fix(api): restrict CORS to whitelisted origins
- Mark resolved:
change_security_hotspot_status(hotspotKey, status=["REVIEWED"], resolution=["FIXED"])
Example 3: Coverage Gap (Complex)
Issue: File apps/web/lib/utils.ts has 45% coverage
Action:
- Present coverage details to user
- Show uncovered lines
- Wait for approval
- If approved:
- Write unit tests for uncovered code paths
- Verify coverage improvement
- Commit:
test(utils): add unit tests for utility functions
- Note: Coverage improvements don't require marking issues resolved (metric-based)
Edge Cases
- No PR found: Exit with helpful message
- SonarQube project not found: Exit with error
- No issues found: Success message: "No SonarQube issues found for this PR. Quality gate passed!"
- Fix verification fails: Rollback file, log error, continue to next issue
- All fixes rejected: Exit gracefully: "No fixes applied. Issues remain in SonarQube."
- Multiple SonarQube projects: Use project key from
sonar-project.properties
04 同仓库更多 Skills
yes-no
Answer the user's question with a yes or no, optionally followed by a short explanation when the model sees fit. Use when the user types "/yes-no", asks a yes/no question and wants a decisive answer, or says "just yes or no".
short-answer
Answer the user's question as concisely as possible, a direct, to-the-point response with no preamble, filler, or unnecessary elaboration. Use when the user types "/short-answer", asks for a brief/short/concise/TL;DR answer, or says "just the answer".
redash
Query a Redash instance over its REST API , list data sources and saved queries, read a saved query's SQL, run it, or run ad-hoc read-only SQL and get the rows back. Use when the user asks about Redash, a Redash query/dashboard/link, "run that query", "what does query 1234 return", or wants numbers out of an analytics data source rather than the application DB.
prod-db-postgres
暂无简介
prod-db-mysql-bastion
暂无简介
pr-workflow
Use this skill when asked to create or update a pull request (PR). It ensures all PRs follow the repository's established templates and standards. Handles both new PR creation and updating existing PRs with new commits, title changes, or description updates.