@jonathan0823/pi-sonarqube 0.1.3 → 0.2.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/README.md +25 -3
- package/dist/api.d.ts +39 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +443 -0
- package/dist/api.js.map +1 -0
- package/dist/commands.d.ts +69 -0
- package/dist/commands.d.ts.map +1 -0
- package/dist/commands.js +248 -0
- package/dist/commands.js.map +1 -0
- package/dist/config.d.ts +38 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +275 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +2 -54
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +315 -975
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +92 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/ui.d.ts +40 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +234 -0
- package/dist/ui.js.map +1 -0
- package/package.json +1 -1
- package/src/api.ts +587 -0
- package/src/commands.ts +314 -0
- package/src/config.ts +336 -0
- package/src/index.ts +477 -1262
- package/src/types.ts +112 -0
- package/src/ui.ts +320 -0
package/dist/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
3
|
+
import { basename } from "node:path";
|
|
1
4
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
|
-
import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { Key, matchesKey, Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
4
|
-
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
5
|
-
import { basename, dirname, relative, resolve } from "node:path";
|
|
6
5
|
import { Type } from "typebox";
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
import { slugify, normalizeServerUrl, sonarProjectPropertiesPath, projectConfigPath, resolveInitTarget, loadProjectConfig, saveProjectConfig, ensureDefaultSonarProjectProperties, saveWorkspaceRegistry, resolveConfig, } from "./config.js";
|
|
7
|
+
import { runScanner, readReportTask, waitForAnalysis, normalizeIssueFilters, fetchIssues, fetchDuplicationMeasures, fetchIssueSeverityCounts, fetchFileDuplications, fetchFileDuplicationBlocks, createAnalysisState, issueFilterLabel, } from "./api.js";
|
|
8
|
+
import { STATE_TYPE, sonarArgumentCompletions, parseCommandArgs, splitSonarArgumentContext, mergeAutocompleteSuggestions, formatIssue, formatSummary, formatMetricsOutput, formatDuplicationsList, formatDuplicationBlockDetail, formatReport, helpText, targetLabel, sonarErrorMessage, } from "./commands.js";
|
|
9
|
+
import { startAnalysisUi, showIssueBrowser, buildIssuePreview, openIssuePreview, resolveTargetState, showDuplicationBrowser, } from "./ui.js";
|
|
9
10
|
const SonarToolParams = Type.Object({
|
|
10
|
-
action: StringEnum(["analyze", "issues", "open"]),
|
|
11
|
+
action: StringEnum(["analyze", "issues", "open", "metrics", "duplications"]),
|
|
11
12
|
path: Type.Optional(Type.String({ description: "Target alias or project directory to analyze or inspect" })),
|
|
12
13
|
issueIndex: Type.Optional(Type.Number({ description: "1-based issue index to open" })),
|
|
13
14
|
severities: Type.Optional(Type.Array(Type.String({ description: "Issue severity to fetch (e.g. CRITICAL)" }))),
|
|
@@ -15,616 +16,7 @@ const SonarToolParams = Type.Object({
|
|
|
15
16
|
types: Type.Optional(Type.Array(Type.String({ description: "Issue type to fetch (e.g. BUG)" }))),
|
|
16
17
|
rules: Type.Optional(Type.Array(Type.String({ description: "Rule keys to fetch" }))),
|
|
17
18
|
});
|
|
18
|
-
|
|
19
|
-
{ value: "analyze", label: "analyze", description: "run analysis and fetch issues" },
|
|
20
|
-
{ value: "issues", label: "issues", description: "browse the latest issues" },
|
|
21
|
-
{ value: "open", label: "open", description: "preview a specific issue" },
|
|
22
|
-
{ value: "init", label: "init", description: "configure a project target" },
|
|
23
|
-
];
|
|
24
|
-
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
25
|
-
function nowIso() {
|
|
26
|
-
return new Date().toISOString();
|
|
27
|
-
}
|
|
28
|
-
function slugify(input) {
|
|
29
|
-
return (input
|
|
30
|
-
.toLowerCase()
|
|
31
|
-
.replace(/[^a-z0-9]+/g, "-")
|
|
32
|
-
.replace(/^-+|-+$/g, "")
|
|
33
|
-
.slice(0, 80) || "sonarqube");
|
|
34
|
-
}
|
|
35
|
-
function normalizePath(input, cwd) {
|
|
36
|
-
const trimmed = input.trim().replace(/^@/, "");
|
|
37
|
-
return resolve(cwd, trimmed);
|
|
38
|
-
}
|
|
39
|
-
function parseProperties(text) {
|
|
40
|
-
const props = {};
|
|
41
|
-
for (const rawLine of text.split(/\r?\n/)) {
|
|
42
|
-
const line = rawLine.trim();
|
|
43
|
-
if (!line || line.startsWith("#") || line.startsWith("!"))
|
|
44
|
-
continue;
|
|
45
|
-
const sep = line.indexOf("=");
|
|
46
|
-
if (sep < 0)
|
|
47
|
-
continue;
|
|
48
|
-
const key = line.slice(0, sep).trim();
|
|
49
|
-
const value = line.slice(sep + 1).trim();
|
|
50
|
-
if (key)
|
|
51
|
-
props[key] = value;
|
|
52
|
-
}
|
|
53
|
-
return props;
|
|
54
|
-
}
|
|
55
|
-
function normalizeServerUrl(url) {
|
|
56
|
-
const fallback = "http://localhost:9000";
|
|
57
|
-
if (!url?.trim())
|
|
58
|
-
return fallback;
|
|
59
|
-
return url.trim().replace(/\/+$/, "");
|
|
60
|
-
}
|
|
61
|
-
const SONAR_SEVERITIES = ["BLOCKER", "CRITICAL", "MAJOR", "MINOR", "INFO"];
|
|
62
|
-
const SONAR_STATUSES = ["OPEN", "CONFIRMED", "REOPENED", "RESOLVED", "CLOSED"];
|
|
63
|
-
const SONAR_TYPES = ["BUG", "VULNERABILITY", "CODE_SMELL"];
|
|
64
|
-
function createAutocompleteItem(value, description) {
|
|
65
|
-
return description ? { value, label: value, description } : { value, label: value };
|
|
66
|
-
}
|
|
67
|
-
function filterAutocompleteItems(items, prefix) {
|
|
68
|
-
const query = prefix.trim().toLowerCase();
|
|
69
|
-
const filtered = query
|
|
70
|
-
? items.filter((item) => item.value.toLowerCase().startsWith(query) || item.label.toLowerCase().startsWith(query))
|
|
71
|
-
: [...items];
|
|
72
|
-
return filtered.map((item) => ({ value: item.value, label: item.label, description: item.description }));
|
|
73
|
-
}
|
|
74
|
-
function mergeAutocompleteSuggestions(prefix, current, extraItems) {
|
|
75
|
-
const merged = new Map();
|
|
76
|
-
for (const item of current?.items ?? [])
|
|
77
|
-
merged.set(item.value, item);
|
|
78
|
-
for (const item of extraItems)
|
|
79
|
-
merged.set(item.value, item);
|
|
80
|
-
const items = [...merged.values()];
|
|
81
|
-
return items.length > 0 ? { prefix, items } : current;
|
|
82
|
-
}
|
|
83
|
-
function splitSonarArgumentContext(argumentText) {
|
|
84
|
-
const trimmedLeft = argumentText.replace(/^\s+/, "");
|
|
85
|
-
if (!trimmedLeft)
|
|
86
|
-
return { command: "", current: "", tokens: [] };
|
|
87
|
-
const tokens = trimmedLeft.split(/\s+/);
|
|
88
|
-
const current = /\s$/.test(argumentText) ? "" : tokens.pop() ?? "";
|
|
89
|
-
return { command: tokens[0] ?? current, current, tokens: tokens.length > 0 ? tokens : current ? [current] : [] };
|
|
90
|
-
}
|
|
91
|
-
function createFilterCompletionList() {
|
|
92
|
-
return [
|
|
93
|
-
...SONAR_SEVERITIES.flatMap((value) => [
|
|
94
|
-
createAutocompleteItem(`severity:${value}`, "severity"),
|
|
95
|
-
createAutocompleteItem(value, "severity"),
|
|
96
|
-
]),
|
|
97
|
-
...SONAR_STATUSES.flatMap((value) => [
|
|
98
|
-
createAutocompleteItem(`status:${value}`, "status"),
|
|
99
|
-
createAutocompleteItem(value, "status"),
|
|
100
|
-
]),
|
|
101
|
-
...SONAR_TYPES.flatMap((value) => [
|
|
102
|
-
createAutocompleteItem(`type:${value}`, "type"),
|
|
103
|
-
createAutocompleteItem(value, "type"),
|
|
104
|
-
]),
|
|
105
|
-
];
|
|
106
|
-
}
|
|
107
|
-
function sonarArgumentCompletions(argumentText, issues) {
|
|
108
|
-
const { command, current, tokens } = splitSonarArgumentContext(argumentText);
|
|
109
|
-
const lowerCommand = command.toLowerCase();
|
|
110
|
-
const commandMatches = SONAR_COMMANDS.filter((item) => item.value.startsWith(lowerCommand));
|
|
111
|
-
if (!command || commandMatches.length > 1 || !SONAR_COMMANDS.some((item) => item.value === lowerCommand)) {
|
|
112
|
-
return commandMatches.length > 0 ? commandMatches.map((item) => createAutocompleteItem(item.value, item.description)) : null;
|
|
113
|
-
}
|
|
114
|
-
if (lowerCommand === "open") {
|
|
115
|
-
const issueItems = (issues ?? []).slice(0, 10).map((issue, index) => createAutocompleteItem(String(index + 1), `${issue.severity} ${issue.filePath}${issue.line ? `:${issue.line}` : ""}`));
|
|
116
|
-
const suggestions = [...issueItems, ...createFilterCompletionList()];
|
|
117
|
-
const filtered = filterAutocompleteItems(suggestions, current);
|
|
118
|
-
return filtered.length > 0 ? filtered : null;
|
|
119
|
-
}
|
|
120
|
-
if (lowerCommand === "analyze" || lowerCommand === "issues") {
|
|
121
|
-
const suggestions = createFilterCompletionList();
|
|
122
|
-
const filtered = filterAutocompleteItems(suggestions, current);
|
|
123
|
-
return filtered.length > 0 ? filtered : null;
|
|
124
|
-
}
|
|
125
|
-
if (lowerCommand === "init") {
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
return tokens.length === 0 ? filterAutocompleteItems(SONAR_COMMANDS, current) : null;
|
|
129
|
-
}
|
|
130
|
-
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
131
|
-
function resolveProjectKey(baseDir, props) {
|
|
132
|
-
const fromProps = props["sonar.projectKey"]?.trim();
|
|
133
|
-
if (fromProps)
|
|
134
|
-
return fromProps;
|
|
135
|
-
return slugify(basename(baseDir));
|
|
136
|
-
}
|
|
137
|
-
async function readOptionalText(path) {
|
|
138
|
-
try {
|
|
139
|
-
return await readFile(path, "utf8");
|
|
140
|
-
}
|
|
141
|
-
catch {
|
|
142
|
-
return undefined;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
async function readOptionalJson(path) {
|
|
146
|
-
try {
|
|
147
|
-
const text = await readFile(path, "utf8");
|
|
148
|
-
return JSON.parse(text);
|
|
149
|
-
}
|
|
150
|
-
catch {
|
|
151
|
-
return undefined;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
function projectConfigPath(baseDir) {
|
|
155
|
-
return resolve(baseDir, CONFIG_DIR_NAME, "sonarqube.json");
|
|
156
|
-
}
|
|
157
|
-
function sonarqubeConfigDir(baseDir) {
|
|
158
|
-
return resolve(baseDir, CONFIG_DIR_NAME);
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Load project-local config from `.pi/sonarqube.json`.
|
|
162
|
-
* Returns undefined when the file doesn't exist or is invalid.
|
|
163
|
-
*/
|
|
164
|
-
async function loadProjectConfig(baseDir) {
|
|
165
|
-
return readOptionalJson(projectConfigPath(baseDir));
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Save project-local config to `.pi/sonarqube.json`.
|
|
169
|
-
*/
|
|
170
|
-
async function saveProjectConfig(baseDir, config) {
|
|
171
|
-
const dir = sonarqubeConfigDir(baseDir);
|
|
172
|
-
await mkdir(dir, { recursive: true });
|
|
173
|
-
await writeFile(projectConfigPath(baseDir), JSON.stringify(config, null, 2) + "\n", "utf8");
|
|
174
|
-
}
|
|
175
|
-
const WORKSPACE_REGISTRY_FILE = "sonarqube.workspaces.json";
|
|
176
|
-
function workspaceRegistryPath(repoRoot) {
|
|
177
|
-
return resolve(repoRoot, CONFIG_DIR_NAME, WORKSPACE_REGISTRY_FILE);
|
|
178
|
-
}
|
|
179
|
-
async function hasGitRootMarker(dir) {
|
|
180
|
-
try {
|
|
181
|
-
const entry = await stat(resolve(dir, ".git"));
|
|
182
|
-
return entry.isDirectory() || entry.isFile();
|
|
183
|
-
}
|
|
184
|
-
catch {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
async function findRepoRoot(startDir) {
|
|
189
|
-
let dir = resolve(startDir);
|
|
190
|
-
for (;;) {
|
|
191
|
-
if (await hasGitRootMarker(dir))
|
|
192
|
-
return dir;
|
|
193
|
-
const parent = dirname(dir);
|
|
194
|
-
if (parent === dir)
|
|
195
|
-
return resolve(startDir);
|
|
196
|
-
dir = parent;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
async function loadWorkspaceRegistry(startDir) {
|
|
200
|
-
const repoRoot = await findRepoRoot(startDir);
|
|
201
|
-
const raw = await readOptionalJson(workspaceRegistryPath(repoRoot));
|
|
202
|
-
const workspaces = raw?.workspaces && typeof raw.workspaces === "object" ? { ...raw.workspaces } : {};
|
|
203
|
-
return { repoRoot, registry: { version: 1, workspaces: workspaces } };
|
|
204
|
-
}
|
|
205
|
-
async function saveWorkspaceRegistry(startDir, alias, targetDir) {
|
|
206
|
-
const { repoRoot, registry } = await loadWorkspaceRegistry(startDir);
|
|
207
|
-
registry.workspaces[alias] = relative(repoRoot, targetDir) || ".";
|
|
208
|
-
await mkdir(sonarqubeConfigDir(repoRoot), { recursive: true });
|
|
209
|
-
await writeFile(workspaceRegistryPath(repoRoot), JSON.stringify(registry, null, 2) + "\n", "utf8");
|
|
210
|
-
}
|
|
211
|
-
function looksLikePath(token) {
|
|
212
|
-
return (token === "." ||
|
|
213
|
-
token === ".." ||
|
|
214
|
-
token.startsWith("./") ||
|
|
215
|
-
token.startsWith("../") ||
|
|
216
|
-
token.startsWith("/") ||
|
|
217
|
-
token.startsWith("~") ||
|
|
218
|
-
token.includes("/") ||
|
|
219
|
-
token.includes("\\"));
|
|
220
|
-
}
|
|
221
|
-
function knownTargets(registry) {
|
|
222
|
-
return Object.keys(registry.workspaces).sort();
|
|
223
|
-
}
|
|
224
|
-
async function resolveTarget(ctx, targetInput) {
|
|
225
|
-
const { repoRoot, registry } = await loadWorkspaceRegistry(ctx.cwd);
|
|
226
|
-
if (!targetInput) {
|
|
227
|
-
return { baseDir: ctx.cwd, repoRoot };
|
|
228
|
-
}
|
|
229
|
-
const aliasTarget = registry.workspaces[targetInput];
|
|
230
|
-
if (aliasTarget) {
|
|
231
|
-
const baseDir = resolve(repoRoot, aliasTarget);
|
|
232
|
-
const baseDirStat = await stat(baseDir).catch(() => undefined);
|
|
233
|
-
if (!baseDirStat?.isDirectory()) {
|
|
234
|
-
throw new Error(`SonarQube target "${targetInput}" points to a missing directory: ${baseDir}`);
|
|
235
|
-
}
|
|
236
|
-
return { baseDir, repoRoot, alias: targetInput };
|
|
237
|
-
}
|
|
238
|
-
if (looksLikePath(targetInput)) {
|
|
239
|
-
const baseDir = normalizePath(targetInput, ctx.cwd);
|
|
240
|
-
const baseDirStat = await stat(baseDir).catch(() => undefined);
|
|
241
|
-
if (!baseDirStat?.isDirectory()) {
|
|
242
|
-
throw new Error(`SonarQube target path not found: ${baseDir}`);
|
|
243
|
-
}
|
|
244
|
-
return { baseDir, repoRoot };
|
|
245
|
-
}
|
|
246
|
-
const known = knownTargets(registry);
|
|
247
|
-
if (known.length > 0) {
|
|
248
|
-
throw new Error(`Unknown SonarQube target "${targetInput}". Known targets: ${known.join(", ")}. Use /sonarqube init <alias> <path> to add one.`);
|
|
249
|
-
}
|
|
250
|
-
throw new Error(`Unknown SonarQube target "${targetInput}". Use /sonarqube init <alias> <path> to add one.`);
|
|
251
|
-
}
|
|
252
|
-
async function resolveInitTarget(ctx, alias, targetInput) {
|
|
253
|
-
if (targetInput) {
|
|
254
|
-
const resolved = await resolveTarget(ctx, targetInput);
|
|
255
|
-
return { ...resolved, alias };
|
|
256
|
-
}
|
|
257
|
-
const { repoRoot } = await loadWorkspaceRegistry(ctx.cwd);
|
|
258
|
-
return { baseDir: ctx.cwd, repoRoot, alias };
|
|
259
|
-
}
|
|
260
|
-
export { projectConfigPath, loadProjectConfig, saveProjectConfig, sonarqubeConfigDir };
|
|
261
|
-
// ── Config resolution (env > .pi/sonarqube.json > sonar-project.properties) ────
|
|
262
|
-
async function resolveConfig(ctx, inputPath) {
|
|
263
|
-
const { baseDir } = await resolveTarget(ctx, inputPath);
|
|
264
|
-
const propertiesPath = resolve(baseDir, "sonar-project.properties");
|
|
265
|
-
const propertiesText = await readOptionalText(propertiesPath);
|
|
266
|
-
const props = propertiesText ? parseProperties(propertiesText) : {};
|
|
267
|
-
const projectCfg = await loadProjectConfig(baseDir);
|
|
268
|
-
const serverUrl = normalizeServerUrl(process.env.SONARQUBE_URL ??
|
|
269
|
-
process.env.SONAR_HOST_URL ??
|
|
270
|
-
projectCfg?.serverUrl?.trim() ??
|
|
271
|
-
props["sonar.host.url"]);
|
|
272
|
-
const projectKey = process.env.SONAR_PROJECT_KEY?.trim() ||
|
|
273
|
-
projectCfg?.projectKey?.trim() ||
|
|
274
|
-
props["sonar.projectKey"]?.trim() ||
|
|
275
|
-
resolveProjectKey(baseDir, props);
|
|
276
|
-
const token = process.env.SONARQUBE_TOKEN?.trim() ||
|
|
277
|
-
process.env.SONAR_TOKEN?.trim() ||
|
|
278
|
-
projectCfg?.token?.trim() ||
|
|
279
|
-
props["sonar.token"]?.trim();
|
|
280
|
-
return {
|
|
281
|
-
baseDir,
|
|
282
|
-
serverUrl,
|
|
283
|
-
projectKey,
|
|
284
|
-
token: token || undefined,
|
|
285
|
-
hasProperties: Boolean(propertiesText),
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
// ── Auth / API helpers ────────────────────────────────────────────────────────
|
|
289
|
-
function authHeader(token) {
|
|
290
|
-
if (!token)
|
|
291
|
-
return undefined;
|
|
292
|
-
return `Basic ${Buffer.from(`${token}:`).toString("base64")}`;
|
|
293
|
-
}
|
|
294
|
-
async function fetchJson(url, token, signal) {
|
|
295
|
-
const headers = { Accept: "application/json" };
|
|
296
|
-
const auth = authHeader(token);
|
|
297
|
-
if (auth)
|
|
298
|
-
headers.Authorization = auth;
|
|
299
|
-
const response = await fetch(url, { headers, signal });
|
|
300
|
-
if (!response.ok) {
|
|
301
|
-
const body = await response.text().catch(() => "");
|
|
302
|
-
const suffix = body.trim() ? `: ${body.trim().slice(0, 240)}` : "";
|
|
303
|
-
throw new Error(`SonarQube API request failed (${response.status} ${response.statusText}) for ${url}${suffix}`);
|
|
304
|
-
}
|
|
305
|
-
return (await response.json());
|
|
306
|
-
}
|
|
307
|
-
function toIssuePath(component, projectKey) {
|
|
308
|
-
const prefix = `${projectKey}:`;
|
|
309
|
-
if (component.startsWith(prefix))
|
|
310
|
-
return component.slice(prefix.length);
|
|
311
|
-
const colon = component.indexOf(":");
|
|
312
|
-
return colon >= 0 ? component.slice(colon + 1) : component;
|
|
313
|
-
}
|
|
314
|
-
function normalizeIssueList(values, uppercase = false) {
|
|
315
|
-
const cleaned = values
|
|
316
|
-
?.map((value) => value.trim())
|
|
317
|
-
.filter(Boolean)
|
|
318
|
-
.map((value) => (uppercase ? value.toUpperCase() : value));
|
|
319
|
-
if (!cleaned?.length)
|
|
320
|
-
return undefined;
|
|
321
|
-
return [...new Set(cleaned)];
|
|
322
|
-
}
|
|
323
|
-
function normalizeIssueFilters(filters) {
|
|
324
|
-
const normalized = {
|
|
325
|
-
severities: normalizeIssueList(filters?.severities, true),
|
|
326
|
-
statuses: normalizeIssueList(filters?.statuses, true),
|
|
327
|
-
types: normalizeIssueList(filters?.types, true),
|
|
328
|
-
rules: normalizeIssueList(filters?.rules),
|
|
329
|
-
};
|
|
330
|
-
return normalized.severities || normalized.statuses || normalized.types || normalized.rules ? normalized : undefined;
|
|
331
|
-
}
|
|
332
|
-
function mergeIssueFilters(...filters) {
|
|
333
|
-
const merged = {};
|
|
334
|
-
for (const filter of filters) {
|
|
335
|
-
if (!filter)
|
|
336
|
-
continue;
|
|
337
|
-
if (filter.severities?.length)
|
|
338
|
-
merged.severities = [...(merged.severities ?? []), ...filter.severities];
|
|
339
|
-
if (filter.statuses?.length)
|
|
340
|
-
merged.statuses = [...(merged.statuses ?? []), ...filter.statuses];
|
|
341
|
-
if (filter.types?.length)
|
|
342
|
-
merged.types = [...(merged.types ?? []), ...filter.types];
|
|
343
|
-
if (filter.rules?.length)
|
|
344
|
-
merged.rules = [...(merged.rules ?? []), ...filter.rules];
|
|
345
|
-
}
|
|
346
|
-
return normalizeIssueFilters(merged);
|
|
347
|
-
}
|
|
348
|
-
function splitFilterValues(value) {
|
|
349
|
-
return value.split(",").map((part) => part.trim()).filter(Boolean);
|
|
350
|
-
}
|
|
351
|
-
function parseIssueFilterToken(token) {
|
|
352
|
-
const trimmed = token.trim();
|
|
353
|
-
if (!trimmed)
|
|
354
|
-
return undefined;
|
|
355
|
-
const explicit = trimmed.match(/^([^:=]+)[:=](.+)$/);
|
|
356
|
-
if (explicit) {
|
|
357
|
-
const key = explicit[1].trim().toLowerCase();
|
|
358
|
-
const values = splitFilterValues(explicit[2]);
|
|
359
|
-
if (values.length === 0)
|
|
360
|
-
return undefined;
|
|
361
|
-
if (key === "severity" || key === "severities")
|
|
362
|
-
return { severities: values.map((value) => value.toUpperCase()) };
|
|
363
|
-
if (key === "status" || key === "statuses")
|
|
364
|
-
return { statuses: values.map((value) => value.toUpperCase()) };
|
|
365
|
-
if (key === "type" || key === "types")
|
|
366
|
-
return { types: values.map((value) => value.toUpperCase()) };
|
|
367
|
-
if (key === "rule" || key === "rules")
|
|
368
|
-
return { rules: values };
|
|
369
|
-
return undefined;
|
|
370
|
-
}
|
|
371
|
-
const upper = trimmed.toUpperCase();
|
|
372
|
-
if (SONAR_SEVERITIES.includes(upper))
|
|
373
|
-
return { severities: [upper] };
|
|
374
|
-
if (SONAR_STATUSES.includes(upper))
|
|
375
|
-
return { statuses: [upper] };
|
|
376
|
-
if (SONAR_TYPES.includes(upper))
|
|
377
|
-
return { types: [upper] };
|
|
378
|
-
return undefined;
|
|
379
|
-
}
|
|
380
|
-
function parseSonarIssueArgs(tokens, allowIssueIndex = false) {
|
|
381
|
-
const filters = [];
|
|
382
|
-
let targetInput;
|
|
383
|
-
let issueIndex;
|
|
384
|
-
for (const token of tokens) {
|
|
385
|
-
if (!token)
|
|
386
|
-
continue;
|
|
387
|
-
if (allowIssueIndex && issueIndex === undefined && /^\d+$/.test(token)) {
|
|
388
|
-
issueIndex = Number(token);
|
|
389
|
-
continue;
|
|
390
|
-
}
|
|
391
|
-
const parsedFilter = parseIssueFilterToken(token);
|
|
392
|
-
if (parsedFilter) {
|
|
393
|
-
filters.push(parsedFilter);
|
|
394
|
-
continue;
|
|
395
|
-
}
|
|
396
|
-
if (!targetInput) {
|
|
397
|
-
targetInput = token;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
return {
|
|
401
|
-
targetInput,
|
|
402
|
-
issueIndex,
|
|
403
|
-
filters: mergeIssueFilters(...filters),
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
function issueFilterLabel(filters) {
|
|
407
|
-
if (!filters)
|
|
408
|
-
return "";
|
|
409
|
-
const parts = [
|
|
410
|
-
filters.severities?.length ? `severities=${filters.severities.join(",")}` : "",
|
|
411
|
-
filters.statuses?.length ? `statuses=${filters.statuses.join(",")}` : "",
|
|
412
|
-
filters.types?.length ? `types=${filters.types.join(",")}` : "",
|
|
413
|
-
filters.rules?.length ? `rules=${filters.rules.join(",")}` : "",
|
|
414
|
-
].filter(Boolean);
|
|
415
|
-
return parts.join(" • ");
|
|
416
|
-
}
|
|
417
|
-
function severitySortRank(severity) {
|
|
418
|
-
switch (severity.toUpperCase()) {
|
|
419
|
-
case "BLOCKER":
|
|
420
|
-
return 0;
|
|
421
|
-
case "CRITICAL":
|
|
422
|
-
return 1;
|
|
423
|
-
case "MAJOR":
|
|
424
|
-
return 2;
|
|
425
|
-
case "MINOR":
|
|
426
|
-
return 3;
|
|
427
|
-
case "INFO":
|
|
428
|
-
return 4;
|
|
429
|
-
default:
|
|
430
|
-
return 5;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
function compareIssuesForContext(left, right) {
|
|
434
|
-
const severityDiff = severitySortRank(left.severity) - severitySortRank(right.severity);
|
|
435
|
-
if (severityDiff !== 0)
|
|
436
|
-
return severityDiff;
|
|
437
|
-
const fileDiff = left.filePath.localeCompare(right.filePath);
|
|
438
|
-
if (fileDiff !== 0)
|
|
439
|
-
return fileDiff;
|
|
440
|
-
const leftLine = left.line ?? Number.POSITIVE_INFINITY;
|
|
441
|
-
const rightLine = right.line ?? Number.POSITIVE_INFINITY;
|
|
442
|
-
if (leftLine !== rightLine)
|
|
443
|
-
return leftLine - rightLine;
|
|
444
|
-
return left.message.localeCompare(right.message);
|
|
445
|
-
}
|
|
446
|
-
// ── Issue helpers ─────────────────────────────────────────────────────────────
|
|
447
|
-
function buildIssuePreview(baseDir, issue, radius = 3) {
|
|
448
|
-
return (async () => {
|
|
449
|
-
const filePath = resolve(baseDir, issue.filePath);
|
|
450
|
-
const content = await readFile(filePath, "utf8");
|
|
451
|
-
const lines = content.split(/\r?\n/);
|
|
452
|
-
const target = issue.line ?? 1;
|
|
453
|
-
const start = Math.max(1, target - radius);
|
|
454
|
-
const end = Math.min(lines.length, target + radius);
|
|
455
|
-
const width = String(end).length;
|
|
456
|
-
const rendered = [];
|
|
457
|
-
for (let line = start; line <= end; line++) {
|
|
458
|
-
const marker = line === target ? ">" : " ";
|
|
459
|
-
const num = String(line).padStart(width, " ");
|
|
460
|
-
rendered.push(`${marker} ${num} | ${lines[line - 1] ?? ""}`);
|
|
461
|
-
}
|
|
462
|
-
return rendered.join("\n");
|
|
463
|
-
})();
|
|
464
|
-
}
|
|
465
|
-
function formatIssue(issue, index) {
|
|
466
|
-
const loc = issue.line ? `${issue.filePath}:${issue.line}` : issue.filePath;
|
|
467
|
-
const prefix = index !== undefined ? `${String(index).padStart(2, "0")}. ` : "";
|
|
468
|
-
const rule = issue.ruleName ? `${issue.rule} (${issue.ruleName})` : issue.rule;
|
|
469
|
-
return `${prefix}${issue.severity} ${loc} — ${rule} — ${issue.message}`;
|
|
470
|
-
}
|
|
471
|
-
function formatSummary(state) {
|
|
472
|
-
const issueCount = state.totalIssues;
|
|
473
|
-
const noIssues = issueCount === 0;
|
|
474
|
-
const filterLabel = state.filters ? ` (${issueFilterLabel(state.filters)})` : "";
|
|
475
|
-
return noIssues
|
|
476
|
-
? `SonarQube: no issues found for ${state.projectKey}${filterLabel}`
|
|
477
|
-
: `SonarQube: ${issueCount} issue${issueCount === 1 ? "" : "s"} found for ${state.projectKey}${filterLabel}`;
|
|
478
|
-
}
|
|
479
|
-
function formatReport(state) {
|
|
480
|
-
const lines = [
|
|
481
|
-
`Project: ${state.projectKey}`,
|
|
482
|
-
`Server: ${state.serverUrl}`,
|
|
483
|
-
`Base dir: ${state.baseDir}`,
|
|
484
|
-
`Issues: ${state.totalIssues}`,
|
|
485
|
-
];
|
|
486
|
-
if (state.filters) {
|
|
487
|
-
lines.push(`Filters: ${issueFilterLabel(state.filters)}`);
|
|
488
|
-
}
|
|
489
|
-
if (state.issues.length === 0) {
|
|
490
|
-
lines.push("No open issues found.");
|
|
491
|
-
return lines.join("\n");
|
|
492
|
-
}
|
|
493
|
-
lines.push("");
|
|
494
|
-
for (const [index, issue] of state.issues.slice(0, 10).entries()) {
|
|
495
|
-
lines.push(formatIssue(issue, index + 1));
|
|
496
|
-
}
|
|
497
|
-
if (state.issues.length > 10) {
|
|
498
|
-
lines.push(`... ${state.issues.length - 10} more`);
|
|
499
|
-
}
|
|
500
|
-
return lines.join("\n");
|
|
501
|
-
}
|
|
502
|
-
function severityColor(theme, severity) {
|
|
503
|
-
switch (severity.toLowerCase()) {
|
|
504
|
-
case "blocker":
|
|
505
|
-
case "critical":
|
|
506
|
-
return theme.fg("error", severity);
|
|
507
|
-
case "major":
|
|
508
|
-
return theme.fg("warning", severity);
|
|
509
|
-
case "minor":
|
|
510
|
-
return theme.fg("accent", severity);
|
|
511
|
-
default:
|
|
512
|
-
return theme.fg("muted", severity);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
const ANALYSIS_SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
516
|
-
const ANALYSIS_WIDGET_KEY = "sonarqube-analysis";
|
|
517
|
-
function startAnalysisUi(ctx, projectKey) {
|
|
518
|
-
let frameIndex = 0;
|
|
519
|
-
let phase = "Starting...";
|
|
520
|
-
const render = () => {
|
|
521
|
-
const frame = ANALYSIS_SPINNER[frameIndex];
|
|
522
|
-
ctx.ui.setWidget(ANALYSIS_WIDGET_KEY, [`${frame} SonarQube ${projectKey}`, phase]);
|
|
523
|
-
};
|
|
524
|
-
ctx.ui.setWorkingMessage(`Analyzing ${projectKey}...`);
|
|
525
|
-
ctx.ui.setWorkingIndicator({ frames: ANALYSIS_SPINNER, intervalMs: 80 });
|
|
526
|
-
render();
|
|
527
|
-
const timer = setInterval(() => {
|
|
528
|
-
frameIndex = (frameIndex + 1) % ANALYSIS_SPINNER.length;
|
|
529
|
-
render();
|
|
530
|
-
}, 80);
|
|
531
|
-
return {
|
|
532
|
-
setPhase(nextPhase) {
|
|
533
|
-
phase = nextPhase;
|
|
534
|
-
render();
|
|
535
|
-
},
|
|
536
|
-
stop() {
|
|
537
|
-
clearInterval(timer);
|
|
538
|
-
ctx.ui.setWidget(ANALYSIS_WIDGET_KEY, undefined);
|
|
539
|
-
ctx.ui.setWorkingMessage();
|
|
540
|
-
ctx.ui.setWorkingIndicator();
|
|
541
|
-
},
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
// ── Issue Browser UI component ────────────────────────────────────────────────
|
|
545
|
-
class IssueBrowser {
|
|
546
|
-
state;
|
|
547
|
-
theme;
|
|
548
|
-
done;
|
|
549
|
-
selected = 0;
|
|
550
|
-
cachedWidth;
|
|
551
|
-
cachedLines;
|
|
552
|
-
constructor(state, theme, done) {
|
|
553
|
-
this.state = state;
|
|
554
|
-
this.theme = theme;
|
|
555
|
-
this.done = done;
|
|
556
|
-
}
|
|
557
|
-
invalidate() {
|
|
558
|
-
this.cachedWidth = undefined;
|
|
559
|
-
this.cachedLines = undefined;
|
|
560
|
-
}
|
|
561
|
-
handleInput(data) {
|
|
562
|
-
if (matchesKey(data, Key.escape) || matchesKey(data, "ctrl+c")) {
|
|
563
|
-
this.done(null);
|
|
564
|
-
return;
|
|
565
|
-
}
|
|
566
|
-
if (matchesKey(data, Key.up)) {
|
|
567
|
-
this.selected = Math.max(0, this.selected - 1);
|
|
568
|
-
this.invalidate();
|
|
569
|
-
return;
|
|
570
|
-
}
|
|
571
|
-
if (matchesKey(data, Key.down)) {
|
|
572
|
-
this.selected = Math.min(this.state.issues.length - 1, this.selected + 1);
|
|
573
|
-
this.invalidate();
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
if (matchesKey(data, Key.enter)) {
|
|
577
|
-
this.done(this.selected);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
render(width) {
|
|
581
|
-
if (this.cachedLines && this.cachedWidth === width) {
|
|
582
|
-
return this.cachedLines;
|
|
583
|
-
}
|
|
584
|
-
const lines = [];
|
|
585
|
-
const title = this.theme.fg("accent", this.theme.bold(" SonarQube Issues "));
|
|
586
|
-
const subtitleText = this.state.filters
|
|
587
|
-
? `${this.state.projectKey} • ${this.state.totalIssues} issue(s) • ${issueFilterLabel(this.state.filters)}`
|
|
588
|
-
: `${this.state.projectKey} • ${this.state.totalIssues} issue(s)`;
|
|
589
|
-
const subtitle = this.theme.fg("dim", subtitleText);
|
|
590
|
-
lines.push(truncateToWidth(title, width));
|
|
591
|
-
lines.push(truncateToWidth(subtitle, width));
|
|
592
|
-
lines.push("");
|
|
593
|
-
const pageSize = Math.min(20, this.state.issues.length);
|
|
594
|
-
const halfWindow = Math.floor(pageSize / 2);
|
|
595
|
-
const maxStart = Math.max(0, this.state.issues.length - pageSize);
|
|
596
|
-
const start = Math.max(0, Math.min(this.selected - halfWindow, maxStart));
|
|
597
|
-
const end = Math.min(this.state.issues.length, start + pageSize);
|
|
598
|
-
const visibleIssues = this.state.issues.slice(start, end);
|
|
599
|
-
if (visibleIssues.length === 0) {
|
|
600
|
-
lines.push(truncateToWidth(this.theme.fg("success", "No open issues found."), width));
|
|
601
|
-
}
|
|
602
|
-
else {
|
|
603
|
-
if (start > 0) {
|
|
604
|
-
lines.push(truncateToWidth(this.theme.fg("dim", `... ${start} more above`), width));
|
|
605
|
-
}
|
|
606
|
-
visibleIssues.forEach((issue, index) => {
|
|
607
|
-
const issueIndex = start + index;
|
|
608
|
-
const selected = issueIndex === this.selected;
|
|
609
|
-
const marker = selected ? this.theme.fg("accent", ">") : this.theme.fg("dim", " ");
|
|
610
|
-
const location = issue.line ? `${issue.filePath}:${issue.line}` : issue.filePath;
|
|
611
|
-
const rule = issue.ruleName ? `${issue.rule} (${issue.ruleName})` : issue.rule;
|
|
612
|
-
const severity = severityColor(this.theme, issue.severity);
|
|
613
|
-
const summary = `${marker} ${String(issueIndex + 1).padStart(2, "0")}. ${severity} ${this.theme.fg("accent", location)} ${this.theme.fg("muted", rule)} ${this.theme.fg("text", `— ${issue.message}`)}`;
|
|
614
|
-
lines.push(truncateToWidth(summary, width));
|
|
615
|
-
});
|
|
616
|
-
if (end < this.state.issues.length) {
|
|
617
|
-
lines.push(truncateToWidth(this.theme.fg("dim", `... ${this.state.issues.length - end} more below`), width));
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
lines.push("");
|
|
621
|
-
lines.push(truncateToWidth(this.theme.fg("dim", "Up/Down to move, Enter to preview, Esc to close"), width));
|
|
622
|
-
this.cachedWidth = width;
|
|
623
|
-
this.cachedLines = lines;
|
|
624
|
-
return lines;
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
// ── State restoration ─────────────────────────────────────────────────────────
|
|
19
|
+
// ── State restoration ───────────────────────────────────────────────────────
|
|
628
20
|
async function restoreState(pi, ctx, statesByBaseDir) {
|
|
629
21
|
statesByBaseDir.clear();
|
|
630
22
|
let latest;
|
|
@@ -637,195 +29,7 @@ async function restoreState(pi, ctx, statesByBaseDir) {
|
|
|
637
29
|
ctx.ui.setStatus("sonarqube", undefined);
|
|
638
30
|
return latest;
|
|
639
31
|
}
|
|
640
|
-
// ──
|
|
641
|
-
function extractScannerHint(output, config) {
|
|
642
|
-
const lines = output.toLowerCase();
|
|
643
|
-
if (/status code 401|status 401|unauthorized/i.test(lines)) {
|
|
644
|
-
const defaultHint = config.serverUrl === "http://localhost:9000"
|
|
645
|
-
? " (default: http://localhost:9000)"
|
|
646
|
-
: "";
|
|
647
|
-
return [
|
|
648
|
-
`Authentication failed for ${config.serverUrl}${defaultHint}.`,
|
|
649
|
-
"Run `/sonarqube init` to set up your server URL and token, then retry.",
|
|
650
|
-
"Or set SONARQUBE_TOKEN in your environment.",
|
|
651
|
-
].join(" ");
|
|
652
|
-
}
|
|
653
|
-
if (/status code 403|status 403|forbidden/i.test(lines)) {
|
|
654
|
-
return [
|
|
655
|
-
`Access denied by ${config.serverUrl}.`,
|
|
656
|
-
'Your token may not have the required permissions ("Execute Analysis").',
|
|
657
|
-
"Run `/sonarqube init` to update the token or check server permissions.",
|
|
658
|
-
].join(" ");
|
|
659
|
-
}
|
|
660
|
-
if (/connect econnrefused|connect refused|connect timeout|enotfound|econnreset/i.test(lines)) {
|
|
661
|
-
return [
|
|
662
|
-
`Cannot reach SonarQube server at ${config.serverUrl}.`,
|
|
663
|
-
"Is the server running? Run `/sonarqube init` to check the server URL.",
|
|
664
|
-
"Default: http://localhost:9000",
|
|
665
|
-
].join(" ");
|
|
666
|
-
}
|
|
667
|
-
if (/not found|unknown url/i.test(lines) && (config.serverUrl === "http://localhost:9000" || /localhost/i.test(config.serverUrl))) {
|
|
668
|
-
return [
|
|
669
|
-
`SonarQube API at ${config.serverUrl} returned "not found".`,
|
|
670
|
-
"The default server URL is http://localhost:9000. Run `/sonarqube init` to configure.",
|
|
671
|
-
].join(" ");
|
|
672
|
-
}
|
|
673
|
-
return "";
|
|
674
|
-
}
|
|
675
|
-
// ── SonarScanner interaction ─────────────────────────────────────────────────
|
|
676
|
-
async function runScanner(pi, config, signal) {
|
|
677
|
-
const args = [
|
|
678
|
-
`-Dsonar.projectKey=${config.projectKey}`,
|
|
679
|
-
`-Dsonar.projectBaseDir=${config.baseDir}`,
|
|
680
|
-
`-Dsonar.host.url=${config.serverUrl}`,
|
|
681
|
-
...(config.hasProperties ? [] : [`-Dsonar.sources=.`]),
|
|
682
|
-
`-Dsonar.scm.disabled=true`,
|
|
683
|
-
];
|
|
684
|
-
if (config.token) {
|
|
685
|
-
args.push(`-Dsonar.token=${config.token}`);
|
|
686
|
-
}
|
|
687
|
-
const result = await pi.exec("sonar-scanner", args, {
|
|
688
|
-
cwd: config.baseDir,
|
|
689
|
-
signal,
|
|
690
|
-
timeout: 30 * 60 * 1000,
|
|
691
|
-
});
|
|
692
|
-
if (result.code !== 0) {
|
|
693
|
-
const output = [result.stdout, result.stderr]
|
|
694
|
-
.filter(Boolean)
|
|
695
|
-
.join("\n")
|
|
696
|
-
.trim();
|
|
697
|
-
const hint = extractScannerHint(output, config);
|
|
698
|
-
const detail = output ? `\n\n${output.slice(-4000)}` : "";
|
|
699
|
-
const message = hint
|
|
700
|
-
? `SonarQube scan failed for ${config.projectKey}. ${hint}`
|
|
701
|
-
: `SonarQube scan failed for ${config.projectKey}${detail}`;
|
|
702
|
-
throw new Error(message);
|
|
703
|
-
}
|
|
704
|
-
return result.stdout || result.stderr;
|
|
705
|
-
}
|
|
706
|
-
async function readReportTask(baseDir) {
|
|
707
|
-
const path = resolve(baseDir, ".scannerwork", "report-task.txt");
|
|
708
|
-
const text = await readFile(path, "utf8");
|
|
709
|
-
return parseProperties(text);
|
|
710
|
-
}
|
|
711
|
-
async function waitForAnalysis(serverUrl, token, ceTaskUrl, signal) {
|
|
712
|
-
for (let attempt = 0; attempt < 120; attempt++) {
|
|
713
|
-
const result = await fetchJson(ceTaskUrl, token, signal);
|
|
714
|
-
const status = result.task.status.toUpperCase();
|
|
715
|
-
if (status === "SUCCESS")
|
|
716
|
-
return result.task.analysisId;
|
|
717
|
-
if (status === "FAILED") {
|
|
718
|
-
throw new Error(`SonarQube analysis failed: ${result.task.errorMessage || "unknown error"}`);
|
|
719
|
-
}
|
|
720
|
-
if (status === "CANCELED") {
|
|
721
|
-
throw new Error("SonarQube analysis was canceled by the server");
|
|
722
|
-
}
|
|
723
|
-
await new Promise((resolveWait) => setTimeout(resolveWait, 2000));
|
|
724
|
-
}
|
|
725
|
-
throw new Error(`Timed out waiting for SonarQube analysis at ${serverUrl}`);
|
|
726
|
-
}
|
|
727
|
-
async function fetchRuleName(serverUrl, token, ruleKey, signal) {
|
|
728
|
-
try {
|
|
729
|
-
const result = await fetchJson(`${serverUrl}/api/rules/show?key=${encodeURIComponent(ruleKey)}`, token, signal);
|
|
730
|
-
return result.rule.name;
|
|
731
|
-
}
|
|
732
|
-
catch {
|
|
733
|
-
return undefined;
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
async function fetchIssues(serverUrl, token, projectKey, signal, filters) {
|
|
737
|
-
const normalizedFilters = normalizeIssueFilters(filters);
|
|
738
|
-
const issues = [];
|
|
739
|
-
const ruleNames = new Map();
|
|
740
|
-
let page = 1;
|
|
741
|
-
let total = 0;
|
|
742
|
-
for (;;) {
|
|
743
|
-
const url = new URL("/api/issues/search", serverUrl);
|
|
744
|
-
url.searchParams.set("projects", projectKey);
|
|
745
|
-
url.searchParams.set("resolved", "false");
|
|
746
|
-
url.searchParams.set("ps", "100");
|
|
747
|
-
url.searchParams.set("p", String(page));
|
|
748
|
-
if (normalizedFilters?.severities?.length) {
|
|
749
|
-
url.searchParams.set("severities", normalizedFilters.severities.join(","));
|
|
750
|
-
}
|
|
751
|
-
if (normalizedFilters?.statuses?.length) {
|
|
752
|
-
url.searchParams.set("statuses", normalizedFilters.statuses.join(","));
|
|
753
|
-
}
|
|
754
|
-
if (normalizedFilters?.types?.length) {
|
|
755
|
-
url.searchParams.set("types", normalizedFilters.types.join(","));
|
|
756
|
-
}
|
|
757
|
-
if (normalizedFilters?.rules?.length) {
|
|
758
|
-
url.searchParams.set("rules", normalizedFilters.rules.join(","));
|
|
759
|
-
}
|
|
760
|
-
const result = await fetchJson(url.toString(), token, signal);
|
|
761
|
-
total = result.total;
|
|
762
|
-
for (const raw of result.issues) {
|
|
763
|
-
if (!ruleNames.has(raw.rule)) {
|
|
764
|
-
ruleNames.set(raw.rule, await fetchRuleName(serverUrl, token, raw.rule, signal));
|
|
765
|
-
}
|
|
766
|
-
issues.push({
|
|
767
|
-
key: raw.key,
|
|
768
|
-
rule: raw.rule,
|
|
769
|
-
ruleName: ruleNames.get(raw.rule),
|
|
770
|
-
severity: raw.severity,
|
|
771
|
-
message: raw.message,
|
|
772
|
-
filePath: toIssuePath(raw.component, projectKey),
|
|
773
|
-
line: typeof raw.line === "number" ? raw.line : null,
|
|
774
|
-
status: raw.status,
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
if (issues.length >= total || result.issues.length === 0)
|
|
778
|
-
break;
|
|
779
|
-
page += 1;
|
|
780
|
-
}
|
|
781
|
-
if (normalizedFilters && issues.length > 1) {
|
|
782
|
-
issues.sort(compareIssuesForContext);
|
|
783
|
-
}
|
|
784
|
-
return issues;
|
|
785
|
-
}
|
|
786
|
-
function createAnalysisState(config, issues, extras = {}) {
|
|
787
|
-
return {
|
|
788
|
-
version: 1,
|
|
789
|
-
analyzedAt: nowIso(),
|
|
790
|
-
baseDir: config.baseDir,
|
|
791
|
-
serverUrl: config.serverUrl,
|
|
792
|
-
projectKey: config.projectKey,
|
|
793
|
-
dashboardUrl: extras.dashboardUrl,
|
|
794
|
-
ceTaskUrl: extras.ceTaskUrl,
|
|
795
|
-
analysisId: extras.analysisId,
|
|
796
|
-
filters: extras.filters,
|
|
797
|
-
totalIssues: issues.length,
|
|
798
|
-
issues,
|
|
799
|
-
};
|
|
800
|
-
}
|
|
801
|
-
async function hasProjectAnalyses(serverUrl, token, projectKey, signal) {
|
|
802
|
-
try {
|
|
803
|
-
const result = await fetchJson(`${serverUrl}/api/project_analyses/search?project=${encodeURIComponent(projectKey)}&ps=1`, token, signal);
|
|
804
|
-
return (result.analyses?.length ?? 0) > 0;
|
|
805
|
-
}
|
|
806
|
-
catch {
|
|
807
|
-
return false;
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
async function loadProjectIssuesFromApi(ctx, inputPath, filters) {
|
|
811
|
-
const config = await resolveConfig(ctx, inputPath);
|
|
812
|
-
const normalizedFilters = normalizeIssueFilters(filters);
|
|
813
|
-
const issues = await fetchIssues(config.serverUrl, config.token, config.projectKey, ctx.signal, normalizedFilters);
|
|
814
|
-
return createAnalysisState(config, issues, { filters: normalizedFilters });
|
|
815
|
-
}
|
|
816
|
-
async function resolveTargetState(ctx, statesByBaseDir, targetInput, filters) {
|
|
817
|
-
const resolvedTarget = await resolveTarget(ctx, targetInput);
|
|
818
|
-
try {
|
|
819
|
-
return await loadProjectIssuesFromApi(ctx, targetInput, filters);
|
|
820
|
-
}
|
|
821
|
-
catch (error) {
|
|
822
|
-
const cached = statesByBaseDir.get(resolvedTarget.baseDir);
|
|
823
|
-
if (cached)
|
|
824
|
-
return cached;
|
|
825
|
-
throw error;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
// ── Analysis orchestration ────────────────────────────────────────────────────
|
|
32
|
+
// ── Analysis orchestration ──────────────────────────────────────────────────
|
|
829
33
|
async function analyzeProject(pi, ctx, inputPath, filters) {
|
|
830
34
|
const config = await resolveConfig(ctx, inputPath);
|
|
831
35
|
const baseDirStat = await stat(config.baseDir).catch(() => undefined);
|
|
@@ -841,16 +45,20 @@ async function analyzeProject(pi, ctx, inputPath, filters) {
|
|
|
841
45
|
const ceTaskUrl = report["ceTaskUrl"];
|
|
842
46
|
const dashboardUrl = report["dashboardUrl"];
|
|
843
47
|
const analysisId = ceTaskUrl
|
|
844
|
-
? (analysisUi.setPhase("Waiting for SonarQube analysis..."),
|
|
48
|
+
? (analysisUi.setPhase("Waiting for SonarQube analysis..."),
|
|
49
|
+
await waitForAnalysis(config.serverUrl, config.token, ceTaskUrl, ctx.signal))
|
|
845
50
|
: undefined;
|
|
846
51
|
analysisUi.setPhase("Fetching issues...");
|
|
847
52
|
const normalizedFilters = normalizeIssueFilters(filters);
|
|
848
53
|
const issues = await fetchIssues(config.serverUrl, config.token, config.projectKey, ctx.signal, normalizedFilters);
|
|
54
|
+
analysisUi.setPhase("Fetching metrics...");
|
|
55
|
+
const measures = await fetchDuplicationMeasures(config.serverUrl, config.token, config.projectKey, ctx.signal);
|
|
849
56
|
const state = createAnalysisState(config, issues, {
|
|
850
57
|
dashboardUrl,
|
|
851
58
|
ceTaskUrl,
|
|
852
59
|
analysisId,
|
|
853
60
|
filters: normalizedFilters,
|
|
61
|
+
measures,
|
|
854
62
|
});
|
|
855
63
|
pi.appendEntry(STATE_TYPE, state);
|
|
856
64
|
return state;
|
|
@@ -862,114 +70,104 @@ async function analyzeProject(pi, ctx, inputPath, filters) {
|
|
|
862
70
|
analysisUi.stop();
|
|
863
71
|
}
|
|
864
72
|
}
|
|
865
|
-
// ──
|
|
866
|
-
async function
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
if (ctx.
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
}
|
|
882
|
-
async function initCommandHandler(ctx, options = {}) {
|
|
883
|
-
const { baseDir, repoRoot } = await resolveInitTarget(ctx, options.alias, options.targetInput);
|
|
884
|
-
const existing = await loadProjectConfig(baseDir);
|
|
885
|
-
if (existing) {
|
|
886
|
-
const ok = await ctx.ui.confirm("Overwrite?", `SonarQube config already exists at ${baseDir}:\n Server: ${existing.serverUrl}\n Project: ${existing.projectKey}\n\nOverwrite?`);
|
|
887
|
-
if (!ok) {
|
|
888
|
-
if (ctx.hasUI)
|
|
889
|
-
ctx.ui.notify("Init cancelled.", "info");
|
|
890
|
-
return;
|
|
891
|
-
}
|
|
892
|
-
}
|
|
73
|
+
// ── Init command helpers (extracted to reduce cognitive complexity) ─────────
|
|
74
|
+
async function confirmOverwrite(ctx, baseDir, existing) {
|
|
75
|
+
const ok = await ctx.ui.confirm("Overwrite?", [
|
|
76
|
+
`SonarQube config already exists at ${baseDir}:`,
|
|
77
|
+
` Server: ${existing.serverUrl}`,
|
|
78
|
+
` Project: ${existing.projectKey}`,
|
|
79
|
+
"",
|
|
80
|
+
"Overwrite?",
|
|
81
|
+
].join("\n"));
|
|
82
|
+
if (ok)
|
|
83
|
+
return true;
|
|
84
|
+
if (ctx.hasUI)
|
|
85
|
+
ctx.ui.notify("Init cancelled.", "info");
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
async function collectInitInputs(ctx, existing, baseDir) {
|
|
893
89
|
const serverUrlInput = await ctx.ui.editor("SonarQube server URL", existing?.serverUrl ?? "http://localhost:9000");
|
|
894
90
|
if (serverUrlInput === undefined)
|
|
895
|
-
return;
|
|
91
|
+
return undefined;
|
|
896
92
|
const serverUrl = normalizeServerUrl(serverUrlInput || existing?.serverUrl || "http://localhost:9000");
|
|
897
93
|
const projectKeyInput = await ctx.ui.input("SonarQube project key", existing?.projectKey ?? basename(baseDir));
|
|
898
94
|
if (projectKeyInput === undefined)
|
|
899
|
-
return;
|
|
95
|
+
return undefined;
|
|
900
96
|
const projectKey = projectKeyInput || existing?.projectKey || slugify(basename(baseDir));
|
|
901
97
|
const tokenInput = await ctx.ui.input("SonarQube token (optional, press Enter to skip)", existing?.token ?? "");
|
|
902
98
|
if (tokenInput === undefined)
|
|
99
|
+
return undefined;
|
|
100
|
+
return { serverUrl, projectKey, token: tokenInput.trim() || undefined };
|
|
101
|
+
}
|
|
102
|
+
async function initCommandHandler(ctx, options = {}) {
|
|
103
|
+
const { baseDir, repoRoot } = await resolveInitTarget(ctx, options.alias, options.targetInput);
|
|
104
|
+
const existing = await loadProjectConfig(baseDir);
|
|
105
|
+
const confirmed = existing ? await confirmOverwrite(ctx, baseDir, existing) : true;
|
|
106
|
+
if (!confirmed)
|
|
903
107
|
return;
|
|
904
|
-
const
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
});
|
|
108
|
+
const inputs = await collectInitInputs(ctx, existing, baseDir);
|
|
109
|
+
if (!inputs)
|
|
110
|
+
return;
|
|
111
|
+
await saveProjectConfig(baseDir, inputs);
|
|
112
|
+
const propertiesState = await ensureDefaultSonarProjectProperties(baseDir);
|
|
910
113
|
if (options.alias) {
|
|
911
114
|
await saveWorkspaceRegistry(repoRoot, options.alias, baseDir);
|
|
912
115
|
}
|
|
913
116
|
if (ctx.hasUI) {
|
|
914
|
-
|
|
915
|
-
ctx.ui.notify(`SonarQube config saved${targetLabel} to ${projectConfigPath(baseDir)}`, "info");
|
|
117
|
+
notifyInitResult(ctx, baseDir, propertiesState, options.alias);
|
|
916
118
|
}
|
|
917
119
|
}
|
|
918
|
-
function
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
"Filters:",
|
|
929
|
-
" /sonarqube issues be CRITICAL",
|
|
930
|
-
" /sonarqube issues be severity:CRITICAL status:OPEN",
|
|
931
|
-
"",
|
|
932
|
-
"Autocomplete:",
|
|
933
|
-
" type /sonarqube and press Tab to complete the subcommand or filters",
|
|
934
|
-
"",
|
|
935
|
-
"Defaults:",
|
|
936
|
-
" config is stored in .pi/sonarqube.json",
|
|
937
|
-
" use project paths directly (no alias needed)",
|
|
938
|
-
].join("\n");
|
|
120
|
+
function notifyInitResult(ctx, baseDir, propertiesState, alias) {
|
|
121
|
+
const label = alias ? ` (${alias})` : "";
|
|
122
|
+
let note = "";
|
|
123
|
+
if (propertiesState === "created") {
|
|
124
|
+
note = ` and created ${sonarProjectPropertiesPath(baseDir)}`;
|
|
125
|
+
}
|
|
126
|
+
else if (propertiesState === "updated") {
|
|
127
|
+
note = ` and merged ${sonarProjectPropertiesPath(baseDir)}`;
|
|
128
|
+
}
|
|
129
|
+
ctx.ui.notify(`SonarQube config saved${label} to ${projectConfigPath(baseDir)}${note}`, "info");
|
|
939
130
|
}
|
|
940
|
-
|
|
941
|
-
|
|
131
|
+
// ── Tool result rendering helpers (reduces renderResult complexity) ─────────
|
|
132
|
+
function renderErrorResult(errorMsg, theme) {
|
|
133
|
+
return new Text(theme.fg("error", errorMsg), 0, 0);
|
|
942
134
|
}
|
|
943
|
-
function
|
|
944
|
-
return
|
|
135
|
+
function renderEmptyResult(summary, theme) {
|
|
136
|
+
return new Text(theme.fg("success", summary), 0, 0);
|
|
945
137
|
}
|
|
946
|
-
function
|
|
947
|
-
const
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
? { action: "init", targetInput: tokens[1] }
|
|
957
|
-
: { action: "init", alias: tokens[1] };
|
|
958
|
-
}
|
|
959
|
-
return { action: "init", alias: tokens[1], targetInput: tokens[2] };
|
|
138
|
+
function renderIssueResult(issue, contentText, expanded, theme) {
|
|
139
|
+
const text = contentText ?? formatIssue(issue);
|
|
140
|
+
const preview = expanded ? text.split("\n").slice(1).join("\n") : "";
|
|
141
|
+
const label = theme.fg("accent", formatIssue(issue));
|
|
142
|
+
return new Text(preview ? `${label}\n${preview}` : label, 0, 0);
|
|
143
|
+
}
|
|
144
|
+
function renderIssueListResult(state, expanded, theme) {
|
|
145
|
+
const summary = formatSummary(state);
|
|
146
|
+
if (state.issues.length === 0 && !state.measures) {
|
|
147
|
+
return new Text(theme.fg("success", summary), 0, 0);
|
|
960
148
|
}
|
|
961
|
-
|
|
962
|
-
|
|
149
|
+
const lines = [];
|
|
150
|
+
lines.push(theme.fg("accent", summary));
|
|
151
|
+
if (state.measures) {
|
|
152
|
+
const density = state.measures.duplicatedLinesDensity.toFixed(1);
|
|
153
|
+
const detail = state.measures.duplicatedBlocks > 0
|
|
154
|
+
? `Duplication: ${density}% lines=${state.measures.duplicatedLines} blocks=${state.measures.duplicatedBlocks} files=${state.measures.duplicatedFiles}`
|
|
155
|
+
: `Duplication: ${density}% (no duplications detected)`;
|
|
156
|
+
lines.push(theme.fg("dim", detail));
|
|
963
157
|
}
|
|
964
|
-
if (
|
|
965
|
-
return
|
|
158
|
+
if (state.issues.length === 0) {
|
|
159
|
+
return new Text(lines.join("\n"), 0, 0);
|
|
966
160
|
}
|
|
967
|
-
|
|
968
|
-
|
|
161
|
+
const visible = expanded ? state.issues : state.issues.slice(0, 5);
|
|
162
|
+
for (const [i, issue] of visible.entries()) {
|
|
163
|
+
lines.push(theme.fg("muted", formatIssue(issue, i + 1)));
|
|
969
164
|
}
|
|
970
|
-
|
|
165
|
+
if (!expanded && state.issues.length > visible.length) {
|
|
166
|
+
lines.push(theme.fg("dim", `... ${state.issues.length - visible.length} more`));
|
|
167
|
+
}
|
|
168
|
+
return new Text(lines.join("\n"), 0, 0);
|
|
971
169
|
}
|
|
972
|
-
// ── Extension entrypoint
|
|
170
|
+
// ── Extension entrypoint ────────────────────────────────────────────────────
|
|
973
171
|
export default function sonarqube(pi) {
|
|
974
172
|
const statesByBaseDir = new Map();
|
|
975
173
|
let latestState;
|
|
@@ -987,10 +185,9 @@ export default function sonarqube(pi) {
|
|
|
987
185
|
async getSuggestions(lines, cursorLine, cursorCol, options) {
|
|
988
186
|
const line = lines[cursorLine] ?? "";
|
|
989
187
|
const beforeCursor = line.slice(0, cursorCol);
|
|
990
|
-
const match =
|
|
991
|
-
if (!match)
|
|
188
|
+
const match = /^\/sonarqube(?:\s+(.*))?$/.exec(beforeCursor);
|
|
189
|
+
if (!match)
|
|
992
190
|
return current.getSuggestions(lines, cursorLine, cursorCol, options);
|
|
993
|
-
}
|
|
994
191
|
const argumentText = match[1] ?? "";
|
|
995
192
|
const { current: currentToken } = splitSonarArgumentContext(argumentText);
|
|
996
193
|
const extraItems = sonarArgumentCompletions(argumentText, latestState?.issues) ?? [];
|
|
@@ -1031,11 +228,12 @@ export default function sonarqube(pi) {
|
|
|
1031
228
|
if (params.action === "analyze") {
|
|
1032
229
|
const state = await analyzeProject(pi, ctx, params.path, filters);
|
|
1033
230
|
rememberState(state);
|
|
1034
|
-
return {
|
|
1035
|
-
content: [{ type: "text", text: formatReport(state) }],
|
|
1036
|
-
details: state,
|
|
1037
|
-
};
|
|
231
|
+
return { content: [{ type: "text", text: formatReport(state) }], details: state };
|
|
1038
232
|
}
|
|
233
|
+
if (params.action === "duplications")
|
|
234
|
+
return toolDuplications(ctx, params.path);
|
|
235
|
+
if (params.action === "metrics")
|
|
236
|
+
return toolMetrics(ctx, params.path);
|
|
1039
237
|
const targetState = await resolveTargetState(ctx, statesByBaseDir, params.path, filters);
|
|
1040
238
|
if (!targetState) {
|
|
1041
239
|
return {
|
|
@@ -1045,10 +243,7 @@ export default function sonarqube(pi) {
|
|
|
1045
243
|
}
|
|
1046
244
|
rememberState(targetState);
|
|
1047
245
|
if (params.action === "issues") {
|
|
1048
|
-
return {
|
|
1049
|
-
content: [{ type: "text", text: formatReport(targetState) }],
|
|
1050
|
-
details: targetState,
|
|
1051
|
-
};
|
|
246
|
+
return { content: [{ type: "text", text: formatReport(targetState) }], details: targetState };
|
|
1052
247
|
}
|
|
1053
248
|
const index = params.issueIndex ?? 1;
|
|
1054
249
|
const issue = targetState.issues[index - 1];
|
|
@@ -1065,8 +260,8 @@ export default function sonarqube(pi) {
|
|
|
1065
260
|
};
|
|
1066
261
|
}
|
|
1067
262
|
catch (error) {
|
|
1068
|
-
const
|
|
1069
|
-
return { content: [{ type: "text", text:
|
|
263
|
+
const msg = sonarErrorMessage(error);
|
|
264
|
+
return { content: [{ type: "text", text: msg }], details: { error: msg } };
|
|
1070
265
|
}
|
|
1071
266
|
},
|
|
1072
267
|
renderCall(args, theme, _context) {
|
|
@@ -1081,29 +276,20 @@ export default function sonarqube(pi) {
|
|
|
1081
276
|
},
|
|
1082
277
|
renderResult(result, { expanded }, theme, _context) {
|
|
1083
278
|
const state = result.details;
|
|
1084
|
-
if (state
|
|
1085
|
-
|
|
279
|
+
if (!state) {
|
|
280
|
+
const text = result.content[0];
|
|
281
|
+
return new Text(text?.type === "text" ? text.text : "", 0, 0);
|
|
282
|
+
}
|
|
283
|
+
if ("error" in state && state.error) {
|
|
284
|
+
return renderErrorResult(state.error, theme);
|
|
1086
285
|
}
|
|
1087
|
-
if (
|
|
286
|
+
if ("selectedIssue" in state && state.selectedIssue) {
|
|
1088
287
|
const issue = state.selectedIssue;
|
|
1089
|
-
const
|
|
1090
|
-
|
|
1091
|
-
return new Text(`${theme.fg("accent", formatIssue(issue))}${preview ? `\n${preview}` : ""}`, 0, 0);
|
|
288
|
+
const contentText = result.content[0]?.type === "text" ? result.content[0].text : undefined;
|
|
289
|
+
return renderIssueResult(issue, contentText, expanded, theme);
|
|
1092
290
|
}
|
|
1093
|
-
if (
|
|
1094
|
-
|
|
1095
|
-
if (state.issues.length === 0) {
|
|
1096
|
-
return new Text(theme.fg("success", summary), 0, 0);
|
|
1097
|
-
}
|
|
1098
|
-
const visible = expanded ? state.issues : state.issues.slice(0, 5);
|
|
1099
|
-
const lines = [theme.fg("accent", summary)];
|
|
1100
|
-
visible.forEach((issue, index) => {
|
|
1101
|
-
lines.push(theme.fg("muted", formatIssue(issue, index + 1)));
|
|
1102
|
-
});
|
|
1103
|
-
if (!expanded && state.issues.length > visible.length) {
|
|
1104
|
-
lines.push(theme.fg("dim", `... ${state.issues.length - visible.length} more`));
|
|
1105
|
-
}
|
|
1106
|
-
return new Text(lines.join("\n"), 0, 0);
|
|
291
|
+
if ("issues" in state) {
|
|
292
|
+
return renderIssueListResult(state, expanded, theme);
|
|
1107
293
|
}
|
|
1108
294
|
const text = result.content[0];
|
|
1109
295
|
return new Text(text?.type === "text" ? text.text : "", 0, 0);
|
|
@@ -1115,60 +301,27 @@ export default function sonarqube(pi) {
|
|
|
1115
301
|
handler: async (args, ctx) => {
|
|
1116
302
|
try {
|
|
1117
303
|
const parsed = parseCommandArgs(args);
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
if (ctx.hasUI) {
|
|
1121
|
-
ctx.ui.notify(helpText(), "info");
|
|
1122
|
-
}
|
|
1123
|
-
return;
|
|
1124
|
-
}
|
|
1125
|
-
// --- Init ---
|
|
1126
|
-
if (parsed.action === "init") {
|
|
1127
|
-
await initCommandHandler(ctx, { alias: parsed.alias, targetInput: parsed.targetInput });
|
|
1128
|
-
return;
|
|
1129
|
-
}
|
|
1130
|
-
// --- Analyze ---
|
|
1131
|
-
if (parsed.action === "analyze") {
|
|
1132
|
-
const state = await analyzeProject(pi, ctx, parsed.targetInput, parsed.filters);
|
|
1133
|
-
rememberState(state);
|
|
1134
|
-
if (ctx.hasUI) {
|
|
1135
|
-
const scope = targetLabel(parsed.targetInput);
|
|
1136
|
-
ctx.ui.notify(`${formatSummary(state)}. Use /sonarqube issues${scope} to browse.`, state.issues.length === 0 ? "info" : "warning");
|
|
1137
|
-
}
|
|
1138
|
-
return;
|
|
1139
|
-
}
|
|
1140
|
-
const targetState = await resolveTargetState(ctx, statesByBaseDir, parsed.targetInput, parsed.filters);
|
|
1141
|
-
if (!targetState) {
|
|
1142
|
-
if (ctx.hasUI) {
|
|
1143
|
-
ctx.ui.notify(`No SonarQube analysis found for this target. Run /sonarqube analyze${targetLabel(parsed.targetInput)} first.`, "warning");
|
|
1144
|
-
}
|
|
1145
|
-
return;
|
|
1146
|
-
}
|
|
1147
|
-
rememberState(targetState);
|
|
1148
|
-
// --- Issues ---
|
|
1149
|
-
if (parsed.action === "issues") {
|
|
1150
|
-
if (targetState.issues.length === 0) {
|
|
304
|
+
switch (parsed.action) {
|
|
305
|
+
case "help":
|
|
1151
306
|
if (ctx.hasUI)
|
|
1152
|
-
ctx.ui.notify(
|
|
307
|
+
ctx.ui.notify(helpText(), "info");
|
|
308
|
+
return;
|
|
309
|
+
case "init":
|
|
310
|
+
await initCommandHandler(ctx, { alias: parsed.alias, targetInput: parsed.targetInput });
|
|
311
|
+
return;
|
|
312
|
+
case "analyze": {
|
|
313
|
+
await commandAnalyze(pi, ctx, parsed.targetInput, parsed.filters, rememberState);
|
|
1153
314
|
return;
|
|
1154
315
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
// --- Open ---
|
|
1164
|
-
const index = parsed.issueIndex ?? 1;
|
|
1165
|
-
const issue = targetState.issues[index - 1];
|
|
1166
|
-
if (!issue) {
|
|
1167
|
-
if (ctx.hasUI)
|
|
1168
|
-
ctx.ui.notify(`Issue #${index} was not found.`, "error");
|
|
1169
|
-
return;
|
|
316
|
+
case "metrics":
|
|
317
|
+
await commandMetrics(ctx, parsed.targetInput);
|
|
318
|
+
return;
|
|
319
|
+
case "duplications":
|
|
320
|
+
await commandDuplications(pi, ctx, parsed.targetInput, parsed.issueIndex);
|
|
321
|
+
return;
|
|
322
|
+
default:
|
|
323
|
+
await commandIssuesOrOpen(pi, ctx, statesByBaseDir, parsed, rememberState);
|
|
1170
324
|
}
|
|
1171
|
-
await openIssuePreview(ctx, targetState, issue);
|
|
1172
325
|
}
|
|
1173
326
|
catch (error) {
|
|
1174
327
|
if (ctx.hasUI) {
|
|
@@ -1180,4 +333,191 @@ export default function sonarqube(pi) {
|
|
|
1180
333
|
},
|
|
1181
334
|
});
|
|
1182
335
|
}
|
|
336
|
+
async function commandAnalyze(pi, ctx, targetInput, filters, rememberState) {
|
|
337
|
+
const state = await analyzeProject(pi, ctx, targetInput, filters);
|
|
338
|
+
rememberState(state);
|
|
339
|
+
if (ctx.hasUI) {
|
|
340
|
+
const scope = targetLabel(targetInput);
|
|
341
|
+
ctx.ui.notify(`${formatSummary(state)}. Use /sonarqube issues${scope} to browse.`, state.issues.length === 0 ? "info" : "warning");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
async function commandMetrics(ctx, targetInput) {
|
|
345
|
+
const config = await resolveConfig(ctx, targetInput);
|
|
346
|
+
const [measures, issueCounts] = await Promise.all([
|
|
347
|
+
fetchDuplicationMeasures(config.serverUrl, config.token, config.projectKey, ctx.signal),
|
|
348
|
+
fetchIssueSeverityCounts(config.serverUrl, config.token, config.projectKey, ctx.signal),
|
|
349
|
+
]);
|
|
350
|
+
if (!measures && !issueCounts) {
|
|
351
|
+
if (ctx.hasUI) {
|
|
352
|
+
ctx.ui.notify(`Project "${config.projectKey}" has not been analyzed yet. Run /sonarqube analyze first.`, "warning");
|
|
353
|
+
}
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const text = formatMetricsOutput({ projectKey: config.projectKey, measures, issueCounts });
|
|
357
|
+
if (ctx.hasUI) {
|
|
358
|
+
ctx.ui.notify(text, "info");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async function commandDuplications(pi, ctx, targetInput, fileIndex) {
|
|
362
|
+
const config = await resolveConfig(ctx, targetInput);
|
|
363
|
+
let files;
|
|
364
|
+
try {
|
|
365
|
+
files = await fetchFileDuplications(config.serverUrl, config.token, config.projectKey, ctx.signal);
|
|
366
|
+
}
|
|
367
|
+
catch (error) {
|
|
368
|
+
const msg = sonarErrorMessage(error);
|
|
369
|
+
if (msg.includes("403") || msg.includes("Insufficient privileges")) {
|
|
370
|
+
if (ctx.hasUI) {
|
|
371
|
+
ctx.ui.notify("SonarQube token needs 'See Source Code' permission to list duplicated files. Update the token permissions in SonarQube and run /sonarqube init to update the token.", "warning");
|
|
372
|
+
}
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
throw error;
|
|
376
|
+
}
|
|
377
|
+
if (files.length === 0) {
|
|
378
|
+
// Fallback: check project-level metrics for confirmation
|
|
379
|
+
const projectMeasures = await fetchDuplicationMeasures(config.serverUrl, config.token, config.projectKey, ctx.signal).catch(() => undefined);
|
|
380
|
+
if (projectMeasures && projectMeasures.duplicatedFiles > 0) {
|
|
381
|
+
if (ctx.hasUI) {
|
|
382
|
+
ctx.ui.notify(`Cannot list individual files — your SonarQube token lacks "See Source Code" permission. Grant it in SonarQube project permissions, then run /sonarqube init to update the token.`, "warning");
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else if (ctx.hasUI) {
|
|
386
|
+
ctx.ui.notify("No duplications found.", "info");
|
|
387
|
+
}
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (fileIndex !== undefined) {
|
|
391
|
+
await showDuplicationDrillDown(ctx, config, files, fileIndex);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
await showDuplicationListOrBrowser(ctx, config, files);
|
|
395
|
+
}
|
|
396
|
+
async function showDuplicationDrillDown(ctx, config, files, fileIndex) {
|
|
397
|
+
const file = files[fileIndex - 1];
|
|
398
|
+
if (!file) {
|
|
399
|
+
if (ctx.hasUI)
|
|
400
|
+
ctx.ui.notify(`File #${fileIndex} not found.`, "error");
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const groups = await fetchFileDuplicationBlocks(config.serverUrl, config.token, file.fileKey, config.projectKey, ctx.signal);
|
|
404
|
+
const text = formatDuplicationBlockDetail(file.filePath, groups);
|
|
405
|
+
if (ctx.mode === "tui") {
|
|
406
|
+
await ctx.ui.editor(`Duplications in ${file.filePath}`, text);
|
|
407
|
+
}
|
|
408
|
+
else if (ctx.hasUI) {
|
|
409
|
+
ctx.ui.notify(text, "info");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
async function showDuplicationListOrBrowser(ctx, config, files) {
|
|
413
|
+
if (ctx.mode !== "tui") {
|
|
414
|
+
ctx.ui.notify(formatDuplicationsList(files), "info");
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const choice = await showDuplicationBrowser(ctx, files);
|
|
418
|
+
if (choice == null)
|
|
419
|
+
return;
|
|
420
|
+
const file = files[choice];
|
|
421
|
+
const groups = await fetchFileDuplicationBlocks(config.serverUrl, config.token, file.fileKey, config.projectKey, ctx.signal);
|
|
422
|
+
await ctx.ui.editor(`Duplications in ${file.filePath}`, formatDuplicationBlockDetail(file.filePath, groups));
|
|
423
|
+
}
|
|
424
|
+
async function commandIssuesOrOpen(pi, ctx, statesByBaseDir, parsed, rememberState) {
|
|
425
|
+
const targetState = await resolveTargetState(ctx, statesByBaseDir, parsed.targetInput, parsed.filters);
|
|
426
|
+
if (!targetState) {
|
|
427
|
+
if (ctx.hasUI) {
|
|
428
|
+
ctx.ui.notify(`No SonarQube analysis found for this target. Run /sonarqube analyze${targetLabel(parsed.targetInput)} first.`, "warning");
|
|
429
|
+
}
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
rememberState(targetState);
|
|
433
|
+
if (parsed.action === "issues") {
|
|
434
|
+
await commandShowIssues(ctx, targetState);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
// "open"
|
|
438
|
+
const index = parsed.issueIndex ?? 1;
|
|
439
|
+
const issue = targetState.issues[index - 1];
|
|
440
|
+
if (!issue) {
|
|
441
|
+
if (ctx.hasUI)
|
|
442
|
+
ctx.ui.notify(`Issue #${index} was not found.`, "error");
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
await openIssuePreview(ctx, targetState, issue);
|
|
446
|
+
}
|
|
447
|
+
async function commandShowIssues(ctx, targetState) {
|
|
448
|
+
if (targetState.issues.length === 0) {
|
|
449
|
+
if (ctx.hasUI)
|
|
450
|
+
ctx.ui.notify(formatSummary(targetState), "info");
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
const choice = await showIssueBrowser(ctx, targetState);
|
|
454
|
+
if (choice != null) {
|
|
455
|
+
const issue = targetState.issues[choice];
|
|
456
|
+
if (issue)
|
|
457
|
+
await openIssuePreview(ctx, targetState, issue);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// ── Tool helper functions ────────────────────────────────────────────────────
|
|
461
|
+
async function toolDuplications(ctx, path) {
|
|
462
|
+
let config;
|
|
463
|
+
try {
|
|
464
|
+
config = await resolveConfig(ctx, path);
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
467
|
+
return {
|
|
468
|
+
content: [{ type: "text", text: "Project not configured. Run /sonarqube init first." }],
|
|
469
|
+
details: { error: "Project not configured." },
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
let files;
|
|
473
|
+
try {
|
|
474
|
+
files = await fetchFileDuplications(config.serverUrl, config.token, config.projectKey, ctx.signal);
|
|
475
|
+
}
|
|
476
|
+
catch (error) {
|
|
477
|
+
const msg = sonarErrorMessage(error);
|
|
478
|
+
if (msg.includes("403") || msg.includes("Insufficient privileges")) {
|
|
479
|
+
return {
|
|
480
|
+
content: [{ type: "text", text: "SonarQube token needs 'See Source Code' permission to list duplicated files. Grant it in SonarQube project permissions, then update the token via /sonarqube init." }],
|
|
481
|
+
details: { error: "Token missing 'See Source Code' permission." },
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
content: [{ type: "text", text: msg }],
|
|
486
|
+
details: { error: msg },
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
if (files.length === 0) {
|
|
490
|
+
return {
|
|
491
|
+
content: [{ type: "text", text: `No duplications found for ${config.projectKey}.` }],
|
|
492
|
+
details: { error: `No duplications found for ${config.projectKey}.` },
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
const text = formatDuplicationsList(files);
|
|
496
|
+
return { content: [{ type: "text", text }], details: { projectKey: config.projectKey, files } };
|
|
497
|
+
}
|
|
498
|
+
async function toolMetrics(ctx, path) {
|
|
499
|
+
let config;
|
|
500
|
+
try {
|
|
501
|
+
config = await resolveConfig(ctx, path);
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
return {
|
|
505
|
+
content: [{ type: "text", text: "Project not configured. Run /sonarqube init first." }],
|
|
506
|
+
details: { error: "Project not configured." },
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
const [measures, issueCounts] = await Promise.all([
|
|
510
|
+
fetchDuplicationMeasures(config.serverUrl, config.token, config.projectKey, ctx.signal),
|
|
511
|
+
fetchIssueSeverityCounts(config.serverUrl, config.token, config.projectKey, ctx.signal),
|
|
512
|
+
]);
|
|
513
|
+
if (!measures && !issueCounts) {
|
|
514
|
+
return {
|
|
515
|
+
content: [{ type: "text", text: `Project "${config.projectKey}" has not been analyzed yet. Run /sonarqube analyze first.` }],
|
|
516
|
+
details: { error: `Project "${config.projectKey}" has not been analyzed yet. Run /sonarqube analyze first.` },
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
const text = formatMetricsOutput({ projectKey: config.projectKey, measures, issueCounts });
|
|
520
|
+
return { content: [{ type: "text", text }], details: { projectKey: config.projectKey, measures, issueCounts } };
|
|
521
|
+
}
|
|
522
|
+
export { projectConfigPath, loadProjectConfig, saveProjectConfig } from "./config.js";
|
|
1183
523
|
//# sourceMappingURL=index.js.map
|