Skip to content

Commit 9e58c93

Browse files
feat(amazonq): store the findings locally and include a path to them in ChatParams
1 parent 51f72b0 commit 9e58c93

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

packages/amazonq/src/lsp/chat/messages.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import {
7272
CodeWhispererSettings,
7373
initSecurityScanRender,
7474
ReferenceLogViewProvider,
75+
SecurityIssueProvider,
7576
SecurityIssueTreeViewProvider,
7677
CodeWhispererConstants,
7778
} from 'aws-core-vscode/codewhisperer'
@@ -89,6 +90,7 @@ import { decryptResponse, encryptRequest } from '../encryption'
8990
import { getCursorState } from '../utils'
9091
import { focusAmazonQPanel } from './commands'
9192
import { ChatMessage } from '@aws/language-server-runtimes/server-interface'
93+
import path from 'path'
9294

9395
export function registerActiveEditorChangeListener(languageClient: LanguageClient) {
9496
let debounceTimer: NodeJS.Timeout | undefined
@@ -285,6 +287,17 @@ export function registerMessageListeners(
285287
if (editor) {
286288
chatParams.cursorState = getCursorState(editor.selections)
287289
chatParams.textDocument = { uri: editor.document.uri.toString() }
290+
chatParams.findingsPath = path.join(
291+
__dirname,
292+
'..',
293+
'..',
294+
'..',
295+
'..',
296+
'..',
297+
'..',
298+
'findings',
299+
`SecurityIssues-${SecurityIssueProvider.instance.id}.json`
300+
)
288301
}
289302

290303
const chatRequest = await encryptRequest<ChatParams>(chatParams, encryptionKey)

packages/core/src/codewhisperer/service/diagnosticsProvider.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { CodeScanIssue, AggregatedCodeScanIssue } from '../models/model'
88
import { CodeAnalysisScope, codewhispererDiagnosticSourceLabel } from '../models/constants'
99
import { SecurityIssueTreeViewProvider } from './securityIssueTreeViewProvider'
1010
import { SecurityIssueProvider } from './securityIssueProvider'
11+
import fs = require('fs')
12+
import path from 'path'
1113

1214
export interface SecurityDiagnostic extends vscode.Diagnostic {
1315
findingId?: string
@@ -39,6 +41,15 @@ export function initSecurityScanRender(
3941
updateSecurityIssuesForProviders(securityRecommendation, scope === CodeAnalysisScope.FILE_AUTO)
4042
}
4143
securityScanRender.initialized = true
44+
const issuesJson = JSON.stringify(SecurityIssueProvider.instance.issues)
45+
const filePath = path.join(__dirname, '..', '..', '..', '..', '..', '..', 'findings')
46+
fs.existsSync(filePath) || fs.mkdirSync(filePath)
47+
fs.writeFileSync(
48+
path.join(filePath, `SecurityIssues-${SecurityIssueProvider.instance.id}.json`),
49+
issuesJson,
50+
'utf8'
51+
)
52+
cleanOldFiles(filePath)
4253
}
4354

4455
function updateSecurityIssuesForProviders(securityRecommendation: AggregatedCodeScanIssue, isAutoScope?: boolean) {
@@ -56,6 +67,26 @@ function updateSecurityIssuesForProviders(securityRecommendation: AggregatedCode
5667
SecurityIssueTreeViewProvider.instance.refresh()
5768
}
5869

70+
function cleanOldFiles(dirPath: string, maxfiles = 100) {
71+
const files = fs.readdirSync(dirPath)
72+
if (files.length > maxfiles) {
73+
type Stat = { fileName: string; mtime: number }
74+
const stats: Stat[] = []
75+
for (const file of files) {
76+
const stat = fs.statSync(path.join(dirPath, file[0]))
77+
stats.push({
78+
fileName: file[0],
79+
mtime: stat.mtime.getTime(),
80+
})
81+
}
82+
const sortedStats = stats.sort((a: Stat, b: Stat) => a.mtime - b.mtime)
83+
const numberToDelete = files.length - maxfiles
84+
for (let i = 0; i < numberToDelete; i++) {
85+
fs.rmSync(path.join(dirPath, sortedStats[i].fileName))
86+
}
87+
}
88+
}
89+
5990
export function updateSecurityDiagnosticCollection(securityRecommendation: AggregatedCodeScanIssue) {
6091
const filePath = securityRecommendation.filePath
6192
const uri = vscode.Uri.file(filePath)

packages/core/src/codewhisperer/service/securityIssueProvider.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as vscode from 'vscode'
77
import { AggregatedCodeScanIssue, CodeScanIssue, SuggestedFix } from '../models/model'
8+
import { randomUUID } from '../../shared/crypto'
89
export class SecurityIssueProvider {
910
static #instance: SecurityIssueProvider
1011
public static get instance() {
@@ -20,6 +21,15 @@ export class SecurityIssueProvider {
2021
this._issues = issues
2122
}
2223

24+
private _id: string = randomUUID()
25+
public get id() {
26+
return this._id
27+
}
28+
29+
public set id(id: string) {
30+
this._id = id
31+
}
32+
2333
public handleDocumentChange(event: vscode.TextDocumentChangeEvent) {
2434
// handleDocumentChange function may be triggered while testing by our own code generation.
2535
if (!event.contentChanges || event.contentChanges.length === 0) {

packages/core/src/shared/settings-toolkit.gen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ export const toolkitSettings = {
4444
"jsonResourceModification": {},
4545
"amazonqLSP": {},
4646
"amazonqLSPInline": {},
47-
"amazonqLSPInlineChat": {},
48-
"amazonqChatLSP": {}
47+
"amazonqChatLSP": {},
48+
"amazonqLSPInlineChat": {}
4949
},
5050
"aws.resources.enabledResources": {},
5151
"aws.lambda.recentlyUploaded": {},

0 commit comments

Comments
 (0)