codeant-cli 0.2.3 → 0.2.5

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.
@@ -1,405 +0,0 @@
1
- import React, { useEffect, useState } from 'react';
2
- import { Box, Text, useApp } from 'ink';
3
- import { getConfigValue } from '../utils/config.js';
4
- import { fetchApi } from '../utils/fetchApi.js';
5
- import StaticAnalysisApiHelper from '../utils/staticAnalysisApiHelper.js';
6
- import fs from 'fs/promises';
7
- import path from 'path';
8
-
9
- export default function StaticAnalysis({ scanType = 'staged-only', failOn = 'CRITICAL', include = [], exclude = [], autoFix = false, lastNCommits = 1 }) {
10
- const { exit } = useApp();
11
- const [status, setStatus] = useState('initializing');
12
- const [issues, setIssues] = useState([]);
13
- const [error, setError] = useState(null);
14
- const [fileCount, setFileCount] = useState(0);
15
- const [fixedFiles, setFixedFiles] = useState([]);
16
- const apiKey = getConfigValue('apiKey');
17
-
18
- // Handle not logged in state
19
- useEffect(() => {
20
- if (!apiKey) {
21
- const id = setTimeout(() => exit(new Error('Not logged in')), 0);
22
- return () => clearTimeout(id);
23
- }
24
- }, [apiKey, exit]);
25
-
26
- // Check if logged in
27
- if (!apiKey) {
28
- return React.createElement(
29
- Box,
30
- { flexDirection: 'column', padding: 1 },
31
- React.createElement(Text, { color: 'red' }, '✗ Not logged in.'),
32
- React.createElement(Text, { color: 'gray' }, 'Run "codeant login" to authenticate.'),
33
- );
34
- }
35
-
36
- // Helper to check if an issue should cause failure based on failOn level
37
- const shouldFailOn = (issueType) => {
38
- const type = issueType?.toUpperCase();
39
- const failOnUpper = failOn?.toUpperCase();
40
- if (!failOnUpper || failOnUpper === 'NONE') return false;
41
- if (failOnUpper === 'BLOCKER') return type === 'BLOCKER';
42
- if (failOnUpper === 'CRITICAL') return type === 'BLOCKER' || type === 'CRITICAL';
43
- if (failOnUpper === 'MAJOR') return type === 'BLOCKER' || type === 'CRITICAL' || type === 'MAJOR';
44
- if (failOnUpper === 'MINOR') return type === 'BLOCKER' || type === 'CRITICAL' || type === 'MAJOR' || type === 'MINOR';
45
- return true; // 'INFO' or 'ALL' - fail on any issue
46
- };
47
-
48
- useEffect(() => {
49
- let cancelled = false;
50
-
51
- async function scanCode() {
52
- try {
53
- if (cancelled) return;
54
- setStatus('scanning');
55
-
56
- // Initialize git helper and get files
57
- const helper = new StaticAnalysisApiHelper(process.cwd());
58
- await helper.init();
59
-
60
- if (cancelled) return;
61
- const requestBody = await helper.buildStaticAnalysisApiRequest(scanType, include, exclude, { lastNCommits });
62
- setFileCount(requestBody.filesData.length);
63
-
64
- if (requestBody.filesData.length === 0) {
65
- if (!cancelled) setStatus('no_files');
66
- return;
67
- }
68
-
69
- // Call the static analysis API
70
- const response = await fetchApi(
71
- '/extension/prReview/static-analysis',
72
- 'POST',
73
- requestBody,
74
- );
75
-
76
- if (cancelled) return;
77
- const codeSuggestions = response.codeSuggestions || [];
78
-
79
- // Filter to only include files with actual issues
80
- const filesWithIssues = codeSuggestions.filter(
81
- file => file.issues && file.issues.length > 0,
82
- );
83
-
84
- // Auto-fix issues if enabled
85
- const fixedFilesList = [];
86
- if (autoFix && filesWithIssues.length > 0) {
87
- const gitRoot = helper.getGitRoot();
88
-
89
- for (const fileData of filesWithIssues) {
90
- const fixableIssues = fileData.issues.filter(
91
- issue => issue.fixAvailable && issue.fix?.inputFileEdits?.length > 0
92
- );
93
-
94
- if (fixableIssues.length > 0) {
95
- try {
96
- const filePath = path.join(gitRoot, fileData.file);
97
- let fileContent = await fs.readFile(filePath, 'utf8');
98
- const lines = fileContent.split('\n');
99
-
100
- // Collect all text edits from all issues
101
- const allEdits = [];
102
- for (const issue of fixableIssues) {
103
- for (const fileEdit of issue.fix.inputFileEdits) {
104
- if (fileEdit.target === fileData.file) {
105
- for (const textEdit of fileEdit.textEdits) {
106
- allEdits.push({
107
- textRange: textEdit.textRange,
108
- newText: textEdit.newText,
109
- });
110
- }
111
- }
112
- }
113
- }
114
-
115
- // Sort edits by position (descending) to avoid offset issues
116
- allEdits.sort((a, b) => {
117
- if (b.textRange.startLine !== a.textRange.startLine) {
118
- return b.textRange.startLine - a.textRange.startLine;
119
- }
120
- if (b.textRange.startLineOffset !== a.textRange.startLineOffset) {
121
- return b.textRange.startLineOffset - a.textRange.startLineOffset;
122
- }
123
- if (b.textRange.endLine !== a.textRange.endLine) {
124
- return b.textRange.endLine - a.textRange.endLine;
125
- }
126
- return b.textRange.endLineOffset - a.textRange.endLineOffset;
127
- });
128
-
129
- // Apply each edit
130
- for (const edit of allEdits) {
131
- const { startLine, startLineOffset, endLine, endLineOffset } = edit.textRange;
132
-
133
- if (startLine === endLine) {
134
- // Single line edit
135
- const line = lines[startLine - 1];
136
- lines[startLine - 1] =
137
- line.substring(0, startLineOffset) +
138
- edit.newText +
139
- line.substring(endLineOffset);
140
- } else {
141
- // Multi-line edit
142
- const firstLine = lines[startLine - 1];
143
- const lastLine = lines[endLine - 1];
144
-
145
- const newContent =
146
- firstLine.substring(0, startLineOffset) +
147
- edit.newText +
148
- lastLine.substring(endLineOffset);
149
-
150
- // Replace the lines
151
- lines.splice(startLine - 1, endLine - startLine + 1, newContent);
152
- }
153
- }
154
-
155
- await fs.writeFile(filePath, lines.join('\n'), 'utf8');
156
- fixedFilesList.push({
157
- file: fileData.file,
158
- fixedCount: fixableIssues.length,
159
- });
160
- } catch (err) {
161
- console.error(`Failed to fix ${fileData.file}: ${err.message}`);
162
- }
163
- }
164
- }
165
- }
166
-
167
- if (!cancelled) {
168
- setFixedFiles(fixedFilesList);
169
- setIssues(filesWithIssues);
170
- setStatus('done');
171
- }
172
- } catch (err) {
173
- if (!cancelled) {
174
- setError(err.message);
175
- setStatus('error');
176
- }
177
- }
178
- }
179
-
180
- scanCode();
181
-
182
- return () => {
183
- cancelled = true;
184
- };
185
- }, [scanType, include, exclude, autoFix, lastNCommits]);
186
-
187
- // Handle exit after status changes
188
- useEffect(() => {
189
- if (status === 'done') {
190
- // Check if any issues should cause failure based on failOn level
191
- const hasBlockingIssues = issues.some(file =>
192
- file.issues.some(issue => shouldFailOn(issue.type)),
193
- );
194
-
195
- if (hasBlockingIssues) {
196
- setTimeout(() => {
197
- process.exitCode = 1;
198
- exit(new Error('Code issues detected'));
199
- }, 100);
200
- } else {
201
- setTimeout(() => exit(), 100);
202
- }
203
- } else if (status === 'no_files') {
204
- setTimeout(() => exit(), 100);
205
- } else if (status === 'error') {
206
- setTimeout(() => exit(new Error(error)), 100);
207
- }
208
- }, [status, issues, failOn, exit, shouldFailOn, error]);
209
-
210
- // Render: Initializing
211
- if (status === 'initializing') {
212
- return React.createElement(
213
- Box,
214
- { flexDirection: 'column', padding: 1 },
215
- React.createElement(Text, { color: 'cyan' }, 'Initializing...'),
216
- );
217
- }
218
-
219
- // Render: Scanning
220
- if (status === 'scanning') {
221
- return React.createElement(
222
- Box,
223
- { flexDirection: 'column', padding: 1 },
224
- React.createElement(Text, { color: 'cyan' }, `Analyzing ${fileCount} file(s)...`),
225
- );
226
- }
227
-
228
- // Render: No files to scan
229
- if (status === 'no_files') {
230
- const noFilesMessages = {
231
- 'staged-only': 'Stage some changes first with "git add".',
232
- 'branch-diff': 'No changes found compared to the base branch.',
233
- 'uncommitted': 'No uncommitted changes found.',
234
- 'last-commit': 'No files found in the last commit.',
235
- };
236
-
237
- return React.createElement(
238
- Box,
239
- { flexDirection: 'column', padding: 1 },
240
- React.createElement(Text, { color: 'yellow' }, 'No files to scan.'),
241
- React.createElement(Text, { color: 'gray' }, noFilesMessages[scanType] || 'No files found.'),
242
- );
243
- }
244
-
245
- // Render: Error
246
- if (status === 'error') {
247
- return React.createElement(
248
- Box,
249
- { flexDirection: 'column', padding: 1 },
250
- React.createElement(Text, { color: 'red' }, '✗ Error: ', error),
251
- );
252
- }
253
-
254
- // Render: Done with issues found
255
- if (status === 'done' && issues.length > 0) {
256
- const allIssues = issues.flatMap(file =>
257
- file.issues.map(i => ({ ...i, file_path: file.file })),
258
- );
259
-
260
- const blockingIssues = allIssues.filter(i => shouldFailOn(i.type));
261
- const nonBlockingIssues = allIssues.filter(i => !shouldFailOn(i.type));
262
-
263
- const hasBlockingIssues = blockingIssues.length > 0;
264
-
265
- // Group by file for display
266
- const groupByFile = (issuesList) => {
267
- const grouped = Object.create(null);
268
- issuesList.forEach(i => {
269
- if (!grouped[i.file_path]) grouped[i.file_path] = [];
270
- grouped[i.file_path].push(i);
271
- });
272
- return grouped;
273
- };
274
-
275
- const blockingByFile = groupByFile(blockingIssues);
276
- const nonBlockingByFile = groupByFile(nonBlockingIssues);
277
-
278
- // Helper to get color based on issue type
279
- const getIssueColor = (type) => {
280
- const t = type?.toUpperCase();
281
- if (t === 'BLOCKER') return 'red';
282
- if (t === 'CRITICAL') return 'red';
283
- if (t === 'MAJOR') return 'red';
284
- if (t === 'MINOR') return 'yellow';
285
- return 'gray';
286
- };
287
-
288
- const elements = [
289
- // Header
290
- React.createElement(
291
- Text,
292
- { key: 'header', color: hasBlockingIssues ? 'red' : 'yellow', bold: true },
293
- hasBlockingIssues
294
- ? `✗ ${blockingIssues.length} issue(s) found!`
295
- : `⚠ ${nonBlockingIssues.length} issue(s) found (not blocking)`,
296
- ),
297
- React.createElement(Text, { key: 'spacer1' }, ''),
298
- ];
299
-
300
- // Blocking issues
301
- if (blockingIssues.length > 0) {
302
- Object.entries(blockingByFile).forEach(([filePath, fileIssues]) => {
303
- elements.push(
304
- React.createElement(Text, { key: `file-${filePath}`, color: 'yellow', bold: true }, filePath),
305
- );
306
- fileIssues.forEach((issue, idx) => {
307
- const color = getIssueColor(issue.type);
308
- elements.push(
309
- React.createElement(
310
- Text,
311
- { key: `issue-${filePath}-${idx}`, color },
312
- ` Line ${issue.line_number}: ${issue.issue_text} (${issue.type})`,
313
- ),
314
- );
315
- // Show fix suggestion if available
316
- if (issue.fixAvailable && issue.fix?.message) {
317
- elements.push(
318
- React.createElement(
319
- Text,
320
- { key: `fix-${filePath}-${idx}`, color: 'green', dimColor: true },
321
- ` 💡 ${issue.fix.message}`,
322
- ),
323
- );
324
- }
325
- });
326
- });
327
- }
328
-
329
- // Non-blocking issues
330
- if (nonBlockingIssues.length > 0) {
331
- if (blockingIssues.length > 0) {
332
- elements.push(React.createElement(Text, { key: 'spacer2' }, ''));
333
- }
334
- elements.push(
335
- React.createElement(Text, { key: 'nb-header', color: 'gray' }, 'Additional issues (not blocking):')
336
- );
337
- Object.entries(nonBlockingByFile).forEach(([filePath, fileIssues]) => {
338
- elements.push(
339
- React.createElement(Text, { key: `nb-file-${filePath}`, color: 'gray' }, ` ${filePath}`)
340
- );
341
- fileIssues.forEach((issue, idx) => {
342
- elements.push(
343
- React.createElement(
344
- Text,
345
- { key: `nb-issue-${filePath}-${idx}`, color: 'gray', dimColor: true },
346
- ` Line ${issue.line_number}: ${issue.issue_text} (${issue.type})`
347
- )
348
- );
349
- });
350
- });
351
- }
352
-
353
- elements.push(React.createElement(Text, { key: 'spacer3' }, ''));
354
-
355
- // Show auto-fix summary
356
- if (fixedFiles.length > 0) {
357
- const totalFixed = fixedFiles.reduce((sum, f) => sum + f.fixedCount, 0);
358
- elements.push(
359
- React.createElement(Text, { key: 'autofix', color: 'green' }, `✓ Auto-fixed ${totalFixed} issue(s) in ${fixedFiles.length} file(s)`)
360
- );
361
- elements.push(React.createElement(Text, { key: 'spacer4' }, ''));
362
- }
363
-
364
- if (hasBlockingIssues) {
365
- // TODO: change message based on whether auto-fix fixed some issues or not
366
- const message = (fixedFiles.length > 0)
367
- ? `✓ Fixed issues automatically. Please review changes before committing.`
368
- : `Fix issues before committing.`;
369
- elements.push(
370
- React.createElement(Text, { key: 'footer', color: 'red' }, message)
371
- );
372
- } else {
373
- elements.push(
374
- React.createElement(Text, { key: 'footer', color: 'green' }, '✓ Commit allowed (no blocking issues)')
375
- );
376
- }
377
-
378
- return React.createElement(
379
- Box,
380
- { flexDirection: 'column', padding: 1 },
381
- ...elements
382
- );
383
- }
384
-
385
- // Render: Done with no issues
386
- const elements = [];
387
-
388
- if (fixedFiles.length > 0) {
389
- const totalFixed = fixedFiles.reduce((sum, f) => sum + f.fixedCount, 0);
390
- elements.push(
391
- React.createElement(Text, { key: 'autofix', color: 'green' }, `✓ Auto-fixed ${totalFixed} issue(s) in ${fixedFiles.length} file(s)`)
392
- );
393
- elements.push(React.createElement(Text, { key: 'spacer' }, ''));
394
- }
395
-
396
- elements.push(
397
- React.createElement(Text, { key: 'no-issues', color: 'green' }, '✓ No issues found')
398
- );
399
-
400
- return React.createElement(
401
- Box,
402
- { flexDirection: 'column', padding: 1 },
403
- ...elements
404
- );
405
- }
@@ -1,119 +0,0 @@
1
- import CommonApiHelper from './commonApiHelper.js';
2
- import { minimatch } from 'minimatch';
3
-
4
- /**
5
- * Transforms git diff data into the format expected by the static analysis API
6
- *
7
- * API Input Format:
8
- * {
9
- * "filesData": [
10
- * {
11
- * "file": str,
12
- * "code": str
13
- * }
14
- * ]
15
- * }
16
- */
17
- class SecurityAnalysisApiHelper extends CommonApiHelper {
18
- /**
19
- * Transform diff info array to API format
20
- * Groups by file (without diff ranges for static analysis)
21
- */
22
- _transformDiffsToApiFormat(diffs) {
23
- // Group diffs by filename
24
- const fileMap = new Map();
25
-
26
- for (const diff of diffs) {
27
- const filePath = diff.filename_str;
28
-
29
- // Skip diffs with missing or invalid filename
30
- if (!filePath) {
31
- continue;
32
- }
33
-
34
- if (!fileMap.has(filePath)) {
35
- fileMap.set(filePath, {
36
- file_path: filePath,
37
- code: diff.head_file_str || ''
38
- });
39
- }
40
- }
41
-
42
- return Array.from(fileMap.values());
43
- }
44
-
45
- /**
46
- * Filter files based on include and exclude glob patterns
47
- * @param {Array} files - Array of file objects with 'file' property
48
- * @param {Array} includePatterns - Array of glob pattern strings to include
49
- * @param {Array} excludePatterns - Array of glob pattern strings to exclude
50
- */
51
- _filterFiles(files, includePatterns = [], excludePatterns = []) {
52
- return files.filter(fileObj => {
53
- const filePath = fileObj.file;
54
-
55
- // If include patterns are specified, file must match at least one
56
- if (includePatterns.length > 0) {
57
- const matchesInclude = includePatterns.some(pattern => {
58
- try {
59
- if (pattern instanceof RegExp) {
60
- return pattern.test(filePath);
61
- }
62
- return minimatch(filePath, pattern, { matchBase: true });
63
- } catch (e) {
64
- console.warn(`Invalid include pattern: ${pattern}`, e.message);
65
- return false;
66
- }
67
- });
68
-
69
- if (!matchesInclude) {
70
- return false;
71
- }
72
- }
73
-
74
- // If exclude patterns are specified, file must not match any
75
- if (excludePatterns.length > 0) {
76
- const matchesExclude = excludePatterns.some(pattern => {
77
- try {
78
- if (pattern instanceof RegExp) {
79
- return pattern.test(filePath);
80
- }
81
- return minimatch(filePath, pattern, { matchBase: true });
82
- } catch (e) {
83
- console.warn(`Invalid exclude pattern: ${pattern}`, e.message);
84
- return false;
85
- }
86
- });
87
-
88
- if (matchesExclude) {
89
- return false;
90
- }
91
- }
92
-
93
- return true;
94
- });
95
- }
96
-
97
- /**
98
- * Build the complete request body for the static analysis API
99
- * @param {string} type - Type of scan (staged-only, branch-diff, etc.)
100
- * @param {Array} includePatterns - Optional array of glob patterns to include files
101
- * @param {Array} excludePatterns - Optional array of glob patterns to exclude files
102
- * @param {Object} options - Additional options like lastNCommits
103
- */
104
- async buildSecurityAnalysisApiRequest(type = 'staged-only', includePatterns = [], excludePatterns = [], options = {}) {
105
- let files = await this.getFilesForType(type, options);
106
-
107
- // Handle null/undefined return
108
- if (!files || !Array.isArray(files)) {
109
- files = [];
110
- }
111
-
112
- // Apply include/exclude filters
113
- files = this._filterFiles(files, includePatterns, excludePatterns);
114
-
115
- return { files };
116
- }
117
- }
118
-
119
- export default SecurityAnalysisApiHelper;
@@ -1,119 +0,0 @@
1
- import CommonApiHelper from './commonApiHelper.js';
2
- import { minimatch } from 'minimatch';
3
-
4
- /**
5
- * Transforms git diff data into the format expected by the static analysis API
6
- *
7
- * API Input Format:
8
- * {
9
- * "filesData": [
10
- * {
11
- * "file": str,
12
- * "code": str
13
- * }
14
- * ]
15
- * }
16
- */
17
- class StaticAnalysisApiHelper extends CommonApiHelper {
18
- /**
19
- * Transform diff info array to API format
20
- * Groups by file (without diff ranges for static analysis)
21
- */
22
- _transformDiffsToApiFormat(diffs) {
23
- // Group diffs by filename
24
- const fileMap = new Map();
25
-
26
- for (const diff of diffs) {
27
- const filePath = diff.filename_str;
28
-
29
- // Skip diffs with missing or invalid filename
30
- if (!filePath) {
31
- continue;
32
- }
33
-
34
- if (!fileMap.has(filePath)) {
35
- fileMap.set(filePath, {
36
- file: filePath,
37
- code: diff.head_file_str || ''
38
- });
39
- }
40
- }
41
-
42
- return Array.from(fileMap.values());
43
- }
44
-
45
- /**
46
- * Filter files based on include and exclude glob patterns
47
- * @param {Array} files - Array of file objects with 'file' property
48
- * @param {Array} includePatterns - Array of glob pattern strings to include
49
- * @param {Array} excludePatterns - Array of glob pattern strings to exclude
50
- */
51
- _filterFiles(files, includePatterns = [], excludePatterns = []) {
52
- return files.filter(fileObj => {
53
- const filePath = fileObj.file;
54
-
55
- // If include patterns are specified, file must match at least one
56
- if (includePatterns.length > 0) {
57
- const matchesInclude = includePatterns.some(pattern => {
58
- try {
59
- if (pattern instanceof RegExp) {
60
- return pattern.test(filePath);
61
- }
62
- return minimatch(filePath, pattern, { matchBase: true });
63
- } catch (e) {
64
- console.warn(`Invalid include pattern: ${pattern}`, e.message);
65
- return false;
66
- }
67
- });
68
-
69
- if (!matchesInclude) {
70
- return false;
71
- }
72
- }
73
-
74
- // If exclude patterns are specified, file must not match any
75
- if (excludePatterns.length > 0) {
76
- const matchesExclude = excludePatterns.some(pattern => {
77
- try {
78
- if (pattern instanceof RegExp) {
79
- return pattern.test(filePath);
80
- }
81
- return minimatch(filePath, pattern, { matchBase: true });
82
- } catch (e) {
83
- console.warn(`Invalid exclude pattern: ${pattern}`, e.message);
84
- return false;
85
- }
86
- });
87
-
88
- if (matchesExclude) {
89
- return false;
90
- }
91
- }
92
-
93
- return true;
94
- });
95
- }
96
-
97
- /**
98
- * Build the complete request body for the static analysis API
99
- * @param {string} type - Type of scan (staged-only, branch-diff, etc.)
100
- * @param {Array} includePatterns - Optional array of glob patterns to include files
101
- * @param {Array} excludePatterns - Optional array of glob patterns to exclude files
102
- * @param {Object} options - Additional options like lastNCommits
103
- */
104
- async buildStaticAnalysisApiRequest(type = 'staged-only', includePatterns = [], excludePatterns = [], options = {}) {
105
- let files = await this.getFilesForType(type, options);
106
-
107
- // Handle null/undefined return
108
- if (!files || !Array.isArray(files)) {
109
- files = [];
110
- }
111
-
112
- // Apply include/exclude filters
113
- files = this._filterFiles(files, includePatterns, excludePatterns);
114
-
115
- return { filesData: files };
116
- }
117
- }
118
-
119
- export default StaticAnalysisApiHelper;