opencode-sonarqube 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +409 -0
- package/dist/index.js +20701 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Manuel Guttmann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
# opencode-sonarqube
|
|
2
|
+
|
|
3
|
+
OpenCode Plugin for SonarQube integration - Enterprise-level code quality from the start.
|
|
4
|
+
|
|
5
|
+
[](https://sonarqube.example.com)
|
|
6
|
+
[](https://sonarqube.example.com)
|
|
7
|
+
[](https://sonarqube.example.com)
|
|
8
|
+
[](./LICENSE)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Automatic Analysis**: Triggers SonarQube analysis when the AI agent becomes idle
|
|
13
|
+
- **15 Tool Actions**: Comprehensive SonarQube integration for AI agents
|
|
14
|
+
- **Clean as You Code**: Focus on new code issues with `newissues` action
|
|
15
|
+
- **Custom Command**: Use `/sonarqube` command for quick analysis
|
|
16
|
+
- **Security Hotspots**: Review and track security hotspots requiring manual review
|
|
17
|
+
- **Quality Gate Integration**: Shows pass/fail status with detailed metrics
|
|
18
|
+
- **Git Integration**: Detects git operations and suggests quality checks
|
|
19
|
+
- **Pre-commit Validation**: Warns about blockers before commit (enterprise mode)
|
|
20
|
+
- **System Prompt Injection**: AI always knows current quality status
|
|
21
|
+
- **Toast Notifications**: Visual feedback on analysis completion
|
|
22
|
+
- **Session Compaction**: Preserves analysis state across session compaction
|
|
23
|
+
- **Multi-Language Support**: Works with any language SonarQube supports
|
|
24
|
+
- **Auto-Fix Mode**: Optionally let the agent fix issues automatically
|
|
25
|
+
- **Enterprise Levels**: Configure strictness (enterprise/standard/relaxed/off)
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
### One-Line Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# Interactive installation (recommended)
|
|
33
|
+
bash <(curl -fsSL https://raw.githubusercontent.com/mguttmann/opencode-sonarqube/main/scripts/install.sh)
|
|
34
|
+
|
|
35
|
+
# Or download and run:
|
|
36
|
+
curl -fsSL https://raw.githubusercontent.com/mguttmann/opencode-sonarqube/main/scripts/install.sh -o install.sh
|
|
37
|
+
chmod +x install.sh && ./install.sh
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
For CI/automation (non-interactive):
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
export SONAR_URL=https://your-sonarqube-server.com
|
|
44
|
+
export SONAR_USER=admin
|
|
45
|
+
export SONAR_PASSWORD=your-password
|
|
46
|
+
curl -fsSL https://raw.githubusercontent.com/mguttmann/opencode-sonarqube/main/scripts/install.sh | bash
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The installer will:
|
|
50
|
+
1. Check prerequisites (Bun/npm)
|
|
51
|
+
2. Ask for SonarQube server URL, username, and password
|
|
52
|
+
3. Test the connection
|
|
53
|
+
4. Configure `opencode.json`
|
|
54
|
+
5. Set up environment variables
|
|
55
|
+
|
|
56
|
+
**Important: Restart OpenCode after installation!**
|
|
57
|
+
|
|
58
|
+
### Manual Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
# Install package
|
|
62
|
+
bun add opencode-sonarqube
|
|
63
|
+
|
|
64
|
+
# Or with npm
|
|
65
|
+
npm install opencode-sonarqube
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Add to your `opencode.json`:
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"plugin": ["opencode-sonarqube"]
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Important:** Configuration is done via environment variables (NOT in opencode.json):
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
export SONAR_HOST_URL="https://your-sonarqube-server.com"
|
|
80
|
+
export SONAR_USER="admin"
|
|
81
|
+
export SONAR_PASSWORD="your-password"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Add these to your `~/.zshrc` or `~/.bashrc` to make them permanent.
|
|
85
|
+
|
|
86
|
+
## Configuration
|
|
87
|
+
|
|
88
|
+
### Using the Configuration Script
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
./scripts/configure.sh
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This interactive script allows you to:
|
|
95
|
+
- Update SonarQube URL
|
|
96
|
+
- Change credentials
|
|
97
|
+
- Test connection
|
|
98
|
+
- Reset project state
|
|
99
|
+
|
|
100
|
+
### Environment Variables (Required)
|
|
101
|
+
|
|
102
|
+
| Variable | Description |
|
|
103
|
+
|----------|-------------|
|
|
104
|
+
| `SONAR_HOST_URL` | SonarQube server URL (e.g., `https://sonarqube.example.com`) |
|
|
105
|
+
| `SONAR_USER` | Username for authentication |
|
|
106
|
+
| `SONAR_PASSWORD` | Password for authentication |
|
|
107
|
+
|
|
108
|
+
### Default Behavior
|
|
109
|
+
|
|
110
|
+
The plugin uses these defaults (configurable in future versions):
|
|
111
|
+
|
|
112
|
+
| Setting | Default | Description |
|
|
113
|
+
|---------|---------|-------------|
|
|
114
|
+
| Level | `enterprise` | Strictest quality requirements |
|
|
115
|
+
| Auto-Analyze | `true` | Analyze when AI becomes idle |
|
|
116
|
+
| Auto-Fix | `false` | Don't auto-fix issues |
|
|
117
|
+
| Sources | `src` | Source directory |
|
|
118
|
+
|
|
119
|
+
### Strictness Levels
|
|
120
|
+
|
|
121
|
+
| Level | Behavior |
|
|
122
|
+
|-------|----------|
|
|
123
|
+
| `enterprise` | All rules active, blocks on blocker/critical/major, requires 80%+ coverage |
|
|
124
|
+
| `standard` | Major+ rules, blocks on blocker/critical |
|
|
125
|
+
| `relaxed` | Only blocker/critical, blocks on blocker |
|
|
126
|
+
| `off` | Plugin disabled |
|
|
127
|
+
|
|
128
|
+
## Tool Actions (15 total)
|
|
129
|
+
|
|
130
|
+
The plugin adds a `sonarqube` tool with these actions:
|
|
131
|
+
|
|
132
|
+
### Setup & Analysis
|
|
133
|
+
|
|
134
|
+
| Action | Description |
|
|
135
|
+
|--------|-------------|
|
|
136
|
+
| `setup` / `init` | Initialize project (auto-creates on SonarQube if needed) |
|
|
137
|
+
| `analyze` | Run full analysis, return issues |
|
|
138
|
+
|
|
139
|
+
### Issue Investigation
|
|
140
|
+
|
|
141
|
+
| Action | Description |
|
|
142
|
+
|--------|-------------|
|
|
143
|
+
| `issues` | Get all current issues |
|
|
144
|
+
| `newissues` | Get only issues in NEW code (Clean as You Code) |
|
|
145
|
+
| `worstfiles` | Show files with most issues (prioritize refactoring) |
|
|
146
|
+
| `hotspots` | Get security hotspots that need manual review |
|
|
147
|
+
| `duplications` | Find code duplications across the project |
|
|
148
|
+
|
|
149
|
+
### Status & Validation
|
|
150
|
+
|
|
151
|
+
| Action | Description |
|
|
152
|
+
|--------|-------------|
|
|
153
|
+
| `status` | Get quality gate status and metrics |
|
|
154
|
+
| `validate` | Check if project meets enterprise quality standards |
|
|
155
|
+
| `metrics` | Show detailed code metrics with trends |
|
|
156
|
+
|
|
157
|
+
### Information
|
|
158
|
+
|
|
159
|
+
| Action | Description |
|
|
160
|
+
|--------|-------------|
|
|
161
|
+
| `rule` | Explain a specific SonarQube rule (requires `ruleKey`) |
|
|
162
|
+
| `history` | Show past analysis history |
|
|
163
|
+
| `profile` | Show quality profile configuration |
|
|
164
|
+
| `branches` | Show branch analysis status |
|
|
165
|
+
|
|
166
|
+
### Tool Options
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
sonarqube({
|
|
170
|
+
action: "analyze" | "issues" | "newissues" | "worstfiles" | "status" |
|
|
171
|
+
"validate" | "hotspots" | "duplications" | "rule" | "history" |
|
|
172
|
+
"profile" | "branches" | "metrics" | "setup",
|
|
173
|
+
scope: "all" | "new" | "changed", // What to analyze
|
|
174
|
+
severity: "blocker" | "critical" | "major" | "minor" | "info" | "all",
|
|
175
|
+
fix: true | false, // Include fix suggestions
|
|
176
|
+
projectKey: "override-key", // Optional override
|
|
177
|
+
force: true | false, // Force re-initialization
|
|
178
|
+
ruleKey: "typescript:S1234", // For "rule" action
|
|
179
|
+
branch: "feature-branch" // For multi-branch analysis
|
|
180
|
+
})
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Automatic Behaviors
|
|
184
|
+
|
|
185
|
+
The plugin automatically handles many scenarios without user intervention:
|
|
186
|
+
|
|
187
|
+
### Session Start
|
|
188
|
+
- Checks for existing issues
|
|
189
|
+
- Injects quality status into system prompt
|
|
190
|
+
- AI is immediately aware of code quality state
|
|
191
|
+
|
|
192
|
+
### File Changes
|
|
193
|
+
- Tracks all edited files during the session
|
|
194
|
+
- When agent becomes idle, runs automatic analysis (if `autoAnalyze: true`)
|
|
195
|
+
- Notifies of any new issues introduced
|
|
196
|
+
|
|
197
|
+
### Git Operations
|
|
198
|
+
|
|
199
|
+
| Operation | Automatic Behavior |
|
|
200
|
+
|-----------|-------------------|
|
|
201
|
+
| `git pull` / `git merge` | Suggests checking for new issues |
|
|
202
|
+
| `git checkout` (with changes) | Suggests running analysis |
|
|
203
|
+
| `git commit` (enterprise mode) | Warns if BLOCKER/CRITICAL issues exist |
|
|
204
|
+
| `git push` | Shows notification that code was pushed |
|
|
205
|
+
|
|
206
|
+
### System Prompt Injection
|
|
207
|
+
- Quality gate status is injected into every conversation
|
|
208
|
+
- AI always knows: issue counts, quality gate status, failed conditions
|
|
209
|
+
- No need to manually check - AI proactively addresses quality issues
|
|
210
|
+
|
|
211
|
+
### Session Compaction
|
|
212
|
+
- Quality context is preserved when session is compacted
|
|
213
|
+
- Long conversations maintain awareness of code quality state
|
|
214
|
+
|
|
215
|
+
## Usage Examples
|
|
216
|
+
|
|
217
|
+
### Via AI Agent Tool
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
User: "Implement user authentication"
|
|
221
|
+
|
|
222
|
+
Agent: I'll implement user authentication. Let me first check the current code quality.
|
|
223
|
+
[Uses sonarqube({ action: "status" })]
|
|
224
|
+
|
|
225
|
+
Agent: Quality gate is passing. Now implementing the feature...
|
|
226
|
+
[Writes code in src/auth/...]
|
|
227
|
+
|
|
228
|
+
Agent: Let me analyze the new code.
|
|
229
|
+
[Uses sonarqube({ action: "newissues" })]
|
|
230
|
+
|
|
231
|
+
Result:
|
|
232
|
+
## New Code Issues (Clean as You Code)
|
|
233
|
+
**Issues in New Code:** 2
|
|
234
|
+
|
|
235
|
+
### CRITICAL (1)
|
|
236
|
+
- **src/auth/login.ts:15** - Hardcoded password detected
|
|
237
|
+
Rule: `typescript:S2068`
|
|
238
|
+
|
|
239
|
+
Agent: I found a critical issue. Let me fix it...
|
|
240
|
+
[Fixes the hardcoded password]
|
|
241
|
+
[Uses sonarqube({ action: "analyze" })]
|
|
242
|
+
|
|
243
|
+
Result:
|
|
244
|
+
## SonarQube Analysis Results
|
|
245
|
+
**Quality Gate: [PASS] OK**
|
|
246
|
+
No issues in new code!
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Common Workflows
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
// Initialize project (first run)
|
|
253
|
+
sonarqube({ action: "setup" })
|
|
254
|
+
|
|
255
|
+
// Run analysis with fix suggestions
|
|
256
|
+
sonarqube({ action: "analyze", fix: true })
|
|
257
|
+
|
|
258
|
+
// Check only YOUR recent changes (Clean as You Code)
|
|
259
|
+
sonarqube({ action: "newissues" })
|
|
260
|
+
|
|
261
|
+
// Find files that need most attention
|
|
262
|
+
sonarqube({ action: "worstfiles" })
|
|
263
|
+
|
|
264
|
+
// Get only critical issues
|
|
265
|
+
sonarqube({ action: "issues", severity: "critical" })
|
|
266
|
+
|
|
267
|
+
// Understand a rule
|
|
268
|
+
sonarqube({ action: "rule", ruleKey: "typescript:S3776" })
|
|
269
|
+
|
|
270
|
+
// Enterprise validation before release
|
|
271
|
+
sonarqube({ action: "validate" })
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## CLI Usage
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
# Initialize project (first run)
|
|
278
|
+
bun run src/index.ts --setup
|
|
279
|
+
|
|
280
|
+
# Run analysis
|
|
281
|
+
bun run src/index.ts --analyze
|
|
282
|
+
|
|
283
|
+
# Check quality gate status
|
|
284
|
+
bun run src/index.ts --status
|
|
285
|
+
|
|
286
|
+
# View current issues
|
|
287
|
+
bun run src/index.ts --issues
|
|
288
|
+
|
|
289
|
+
# View security hotspots
|
|
290
|
+
bun run src/index.ts --hotspots
|
|
291
|
+
|
|
292
|
+
# Override project key
|
|
293
|
+
bun run src/index.ts --status --project-key=my-project
|
|
294
|
+
|
|
295
|
+
# Force re-initialize
|
|
296
|
+
bun run src/index.ts --setup --force
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Programmatic API
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import {
|
|
303
|
+
createSonarQubeAPI,
|
|
304
|
+
loadConfig,
|
|
305
|
+
getProjectState,
|
|
306
|
+
runAnalysis,
|
|
307
|
+
bootstrap
|
|
308
|
+
} from "opencode-sonarqube";
|
|
309
|
+
|
|
310
|
+
// Create API client
|
|
311
|
+
const config = loadConfig();
|
|
312
|
+
const state = await getProjectState("./");
|
|
313
|
+
const api = createSonarQubeAPI(config, state);
|
|
314
|
+
|
|
315
|
+
// Health check
|
|
316
|
+
const health = await api.healthCheck();
|
|
317
|
+
console.log("Healthy:", health.healthy);
|
|
318
|
+
|
|
319
|
+
// Get issues
|
|
320
|
+
const issues = await api.issues.getFormattedIssues({
|
|
321
|
+
projectKey: "my-project",
|
|
322
|
+
severities: ["BLOCKER", "CRITICAL"],
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// Get quality gate status
|
|
326
|
+
const status = await api.qualityGate.getStatus("my-project");
|
|
327
|
+
console.log("Status:", status.projectStatus.status);
|
|
328
|
+
|
|
329
|
+
// Get new code issues only
|
|
330
|
+
const newIssues = await api.issues.search({
|
|
331
|
+
projectKey: "my-project",
|
|
332
|
+
inNewCode: true,
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
// Get worst files for refactoring
|
|
336
|
+
const worstFiles = await api.components.getWorstFiles("my-project", 10);
|
|
337
|
+
|
|
338
|
+
// Run full analysis
|
|
339
|
+
const result = await runAnalysis(config, state, { projectKey: "my-project" }, "./");
|
|
340
|
+
console.log("Quality Gate:", result.qualityGateStatus);
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
See [API Documentation](./docs/API.md) for complete reference.
|
|
344
|
+
|
|
345
|
+
## Documentation
|
|
346
|
+
|
|
347
|
+
| Document | Description |
|
|
348
|
+
|----------|-------------|
|
|
349
|
+
| [API Reference](./docs/API.md) | Complete API documentation |
|
|
350
|
+
| [Architecture](./docs/ARCHITECTURE.md) | System architecture and design |
|
|
351
|
+
| [SonarQube Setup](./docs/SONARQUBE_SETUP.md) | Server installation guide |
|
|
352
|
+
| [Contributing](./CONTRIBUTING.md) | Development guidelines |
|
|
353
|
+
| [Changelog](./CHANGELOG.md) | Version history |
|
|
354
|
+
|
|
355
|
+
## API Modules
|
|
356
|
+
|
|
357
|
+
The plugin provides 12 API modules for SonarQube interaction:
|
|
358
|
+
|
|
359
|
+
| Module | Purpose |
|
|
360
|
+
|--------|---------|
|
|
361
|
+
| `ProjectsAPI` | Create, search, delete projects |
|
|
362
|
+
| `IssuesAPI` | Search issues, get counts, format for display |
|
|
363
|
+
| `QualityGateAPI` | Check status, validate enterprise quality |
|
|
364
|
+
| `RulesAPI` | Get rule details and explanations |
|
|
365
|
+
| `SourcesAPI` | Fetch source code context for issues |
|
|
366
|
+
| `DuplicationsAPI` | Find code duplications |
|
|
367
|
+
| `ComputeEngineAPI` | Track analysis task status |
|
|
368
|
+
| `ProjectAnalysesAPI` | Get analysis history |
|
|
369
|
+
| `QualityProfilesAPI` | Get active quality profiles |
|
|
370
|
+
| `BranchesAPI` | Multi-branch analysis management |
|
|
371
|
+
| `MetricsAPI` | Get detailed metrics with period comparison |
|
|
372
|
+
| `ComponentsAPI` | Get files/directories with issue counts |
|
|
373
|
+
|
|
374
|
+
## Requirements
|
|
375
|
+
|
|
376
|
+
- SonarQube server 9.9+ (tested with 26.1)
|
|
377
|
+
- Node.js 18+ or Bun
|
|
378
|
+
- OpenCode with plugin support
|
|
379
|
+
|
|
380
|
+
## Quality Metrics
|
|
381
|
+
|
|
382
|
+
This project maintains enterprise-level quality:
|
|
383
|
+
|
|
384
|
+
| Metric | Value |
|
|
385
|
+
|--------|-------|
|
|
386
|
+
| Test Coverage | 100% |
|
|
387
|
+
| Tests | 626 |
|
|
388
|
+
| Bugs | 0 |
|
|
389
|
+
| Vulnerabilities | 0 |
|
|
390
|
+
| Code Smells | 0 |
|
|
391
|
+
| Duplications | 0% |
|
|
392
|
+
| Quality Gate | Passed |
|
|
393
|
+
| Reliability Rating | A |
|
|
394
|
+
| Security Rating | A |
|
|
395
|
+
| Maintainability Rating | A |
|
|
396
|
+
|
|
397
|
+
## License
|
|
398
|
+
|
|
399
|
+
MIT
|
|
400
|
+
|
|
401
|
+
## Author
|
|
402
|
+
|
|
403
|
+
Manuel Guttmann
|
|
404
|
+
|
|
405
|
+
## Links
|
|
406
|
+
|
|
407
|
+
- [GitHub Repository](https://github.com/mguttmann/opencode-sonarqube)
|
|
408
|
+
- [Issue Tracker](https://github.com/mguttmann/opencode-sonarqube/issues)
|
|
409
|
+
- [SonarQube Documentation](https://docs.sonarqube.org/)
|