Skip to content

Commit df810aa

Browse files
authored
Merge pull request aws#6659 from zuoyaofu/ignore-bug
fix(amazonq): ignored lines should not show up in /review scan issues
2 parents 71650e5 + 5e82026 commit df810aa

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "/review: ignored lines should not show up in scan issues"
4+
}

packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { timeoutUtils } from 'aws-core-vscode/shared'
2020
import assert from 'assert'
2121
import sinon from 'sinon'
2222
import * as vscode from 'vscode'
23-
import fs from 'fs' // eslint-disable-line no-restricted-imports
23+
import path from 'path'
2424

2525
const buildRawCodeScanIssue = (params?: Partial<RawCodeScanIssue>): RawCodeScanIssue => ({
2626
filePath: 'workspaceFolder/python3.7-plain-sam-app/hello_world/app.py',
@@ -65,17 +65,18 @@ const buildMockListCodeScanFindingsResponse = (
6565
nextToken: nextToken ? 'nextToken' : undefined,
6666
})
6767

68+
function getWorkspaceFolder(): string {
69+
return (
70+
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ??
71+
path.join(__dirname, '../../../../../../core/src/testFixtures/workspaceFolder')
72+
)
73+
}
74+
6875
describe('securityScanHandler', function () {
6976
describe('listScanResults', function () {
7077
let mockClient: Stub<DefaultCodeWhispererClient>
7178
beforeEach(function () {
7279
mockClient = stub(DefaultCodeWhispererClient)
73-
sinon.stub(fs, 'existsSync').returns(true)
74-
sinon.stub(fs, 'statSync').returns({ isFile: () => true } as fs.Stats)
75-
})
76-
77-
afterEach(function () {
78-
sinon.restore()
7980
})
8081

8182
it('should make ListCodeScanFindings request and aggregate findings by file path', async function () {
@@ -85,14 +86,13 @@ describe('securityScanHandler', function () {
8586
mockClient,
8687
'jobId',
8788
'codeScanFindingsSchema',
88-
['projectPath'],
89+
[getWorkspaceFolder()],
8990
CodeAnalysisScope.PROJECT,
9091
undefined
9192
)
9293

93-
assert.equal(aggregatedCodeScanIssueList.length, 2)
94+
assert.equal(aggregatedCodeScanIssueList.length, 1)
9495
assert.equal(aggregatedCodeScanIssueList[0].issues.length, 1)
95-
assert.equal(aggregatedCodeScanIssueList[1].issues.length, 1)
9696
})
9797

9898
it('should handle ListCodeScanFindings request with paginated response', async function () {
@@ -123,12 +123,12 @@ describe('securityScanHandler', function () {
123123
mockClient,
124124
'jobId',
125125
'codeScanFindingsSchema',
126-
['projectPath'],
126+
[getWorkspaceFolder()],
127127
CodeAnalysisScope.PROJECT,
128128
undefined
129129
)
130130

131-
assert.equal(aggregatedCodeScanIssueList.length, 2)
131+
assert.equal(aggregatedCodeScanIssueList.length, 1)
132132
assert.equal(aggregatedCodeScanIssueList[0].issues.length, 3)
133133
})
134134

@@ -145,7 +145,7 @@ describe('securityScanHandler', function () {
145145
mockClient,
146146
'jobId',
147147
'codeScanFindingsSchema',
148-
['projectPath'],
148+
[getWorkspaceFolder()],
149149
scope,
150150
undefined
151151
)

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,20 @@ export async function listScanResults(
8282
// Do not use .. in between because there could be multiple project paths in the same parent dir.
8383
const filePath = path.join(projectPath, key.split('/').slice(1).join('/'))
8484
if (existsSync(filePath) && statSync(filePath).isFile()) {
85+
const document = await vscode.workspace.openTextDocument(filePath)
8586
const aggregatedCodeScanIssue: AggregatedCodeScanIssue = {
8687
filePath: filePath,
87-
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, editor, jobId, scope)),
88+
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, document, jobId, scope)),
8889
}
8990
aggregatedCodeScanIssueList.push(aggregatedCodeScanIssue)
9091
}
9192
}
9293
const maybeAbsolutePath = `/${key}`
9394
if (existsSync(maybeAbsolutePath) && statSync(maybeAbsolutePath).isFile()) {
95+
const document = await vscode.workspace.openTextDocument(maybeAbsolutePath)
9496
const aggregatedCodeScanIssue: AggregatedCodeScanIssue = {
9597
filePath: maybeAbsolutePath,
96-
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, editor, jobId, scope)),
98+
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, document, jobId, scope)),
9799
}
98100
aggregatedCodeScanIssueList.push(aggregatedCodeScanIssue)
99101
}
@@ -103,18 +105,20 @@ export async function listScanResults(
103105

104106
function mapRawToCodeScanIssue(
105107
issue: RawCodeScanIssue,
106-
editor: vscode.TextEditor | undefined,
108+
document: vscode.TextDocument,
107109
jobId: string,
108110
scope: CodeWhispererConstants.CodeAnalysisScope
109111
): CodeScanIssue {
110112
const isIssueTitleIgnored = CodeWhispererSettings.instance.getIgnoredSecurityIssues().includes(issue.title)
111-
const isSingleIssueIgnored =
112-
editor &&
113-
detectCommentAboveLine(editor.document, issue.startLine - 1, CodeWhispererConstants.amazonqIgnoreNextLine)
114-
const language = editor
115-
? runtimeLanguageContext.getLanguageContext(editor.document.languageId, path.extname(editor.document.fileName))
116-
.language
117-
: 'plaintext'
113+
const isSingleIssueIgnored = detectCommentAboveLine(
114+
document,
115+
issue.startLine - 1,
116+
CodeWhispererConstants.amazonqIgnoreNextLine
117+
)
118+
const language = runtimeLanguageContext.getLanguageContext(
119+
document.languageId,
120+
path.extname(document.fileName)
121+
).language
118122
return {
119123
startLine: issue.startLine - 1 >= 0 ? issue.startLine - 1 : 0,
120124
endLine: issue.endLine,

0 commit comments

Comments
 (0)