Skip to content

Commit 7a479f1

Browse files
authored
fix(codewhisperer): codescan name is required by api aws#4760
Problem CreateUploadUrl and CreateCodeScan APIs now require CodeScanName field Solution Pass a random UUID to the APIs
1 parent 282939d commit 7a479f1

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

packages/core/src/codewhisperer/client/user-service-2.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,13 @@
414414
"type": "string",
415415
"enum": ["Completed", "Pending", "Failed"]
416416
},
417+
"CodeAnalysisUploadContext": {
418+
"type": "structure",
419+
"required": ["codeScanName"],
420+
"members": {
421+
"codeScanName": { "shape": "CodeScanName" }
422+
}
423+
},
417424
"CodeCoverageEvent": {
418425
"type": "structure",
419426
"required": ["programmingLanguage", "acceptedCharacterCount", "totalCharacterCount", "timestamp"],
@@ -461,6 +468,12 @@
461468
"max": 128,
462469
"min": 1
463470
},
471+
"CodeScanName": {
472+
"type": "string",
473+
"documentation": "<p>Code analysis scan name</p>",
474+
"max": 128,
475+
"min": 1
476+
},
464477
"CodeScanRemediationsEvent": {
465478
"type": "structure",
466479
"members": {
@@ -1243,7 +1256,8 @@
12431256
"clientToken": {
12441257
"shape": "StartCodeAnalysisRequestClientTokenString",
12451258
"idempotencyToken": true
1246-
}
1259+
},
1260+
"codeScanName": { "shape": "CodeScanName" }
12471261
}
12481262
},
12491263
"StartCodeAnalysisRequestClientTokenString": {
@@ -1570,7 +1584,8 @@
15701584
"UploadContext": {
15711585
"type": "structure",
15721586
"members": {
1573-
"taskAssistPlanningUploadContext": { "shape": "TaskAssistPlanningUploadContext" }
1587+
"taskAssistPlanningUploadContext": { "shape": "TaskAssistPlanningUploadContext" },
1588+
"codeAnalysisUploadContext": { "shape": "CodeAnalysisUploadContext" }
15741589
},
15751590
"union": true
15761591
},

packages/core/src/codewhisperer/commands/startSecurityScan.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import path from 'path'
3232
import { ZipMetadata, ZipUtil } from '../util/zipUtil'
3333
import { debounce } from 'lodash'
3434
import { once } from '../../shared/utilities/functionUtils'
35+
import { randomUUID } from '../../common/crypto'
3536

3637
const localize = nls.loadMessageBundle()
3738
export const stopScanButton = localize('aws.codewhisperer.stopscan', 'Stop Scan')
@@ -133,8 +134,9 @@ export async function startSecurityScan(
133134
throwIfCancelled(scope)
134135
let artifactMap: ArtifactMap = {}
135136
const uploadStartTime = performance.now()
137+
const scanName = randomUUID()
136138
try {
137-
artifactMap = await getPresignedUrlAndUpload(client, zipMetadata, scope)
139+
artifactMap = await getPresignedUrlAndUpload(client, zipMetadata, scope, scanName)
138140
} catch (error) {
139141
getLogger().error('Failed to upload code artifacts', error)
140142
throw error
@@ -148,7 +150,13 @@ export async function startSecurityScan(
148150
*/
149151
throwIfCancelled(scope)
150152
serviceInvocationStartTime = performance.now()
151-
const scanJob = await createScanJob(client, artifactMap, codeScanTelemetryEntry.codewhispererLanguage, scope)
153+
const scanJob = await createScanJob(
154+
client,
155+
artifactMap,
156+
codeScanTelemetryEntry.codewhispererLanguage,
157+
scope,
158+
scanName
159+
)
152160
if (scanJob.status === 'Failed') {
153161
throw new Error(scanJob.errorMessage)
154162
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ export async function createScanJob(
140140
client: DefaultCodeWhispererClient,
141141
artifactMap: codewhispererClient.ArtifactMap,
142142
languageId: string,
143-
scope: CodeWhispererConstants.CodeAnalysisScope
143+
scope: CodeWhispererConstants.CodeAnalysisScope,
144+
scanName: string
144145
) {
145146
getLogger().verbose(`Creating scan job...`)
146147
const req: codewhispererClient.CreateCodeScanRequest = {
@@ -149,8 +150,12 @@ export async function createScanJob(
149150
languageName: languageId,
150151
},
151152
scope: scope,
153+
codeScanName: scanName,
152154
}
153-
const resp = await client.createCodeScan(req)
155+
const resp = await client.createCodeScan(req).catch(err => {
156+
getLogger().error(`Failed creating scan job. Request id: ${err.requestId}`)
157+
throw err
158+
})
154159
getLogger().verbose(`Request id: ${resp.$response.requestId}`)
155160
TelemetryHelper.instance.sendCodeScanEvent(languageId, resp.$response.requestId)
156161
return resp
@@ -159,7 +164,8 @@ export async function createScanJob(
159164
export async function getPresignedUrlAndUpload(
160165
client: DefaultCodeWhispererClient,
161166
zipMetadata: ZipMetadata,
162-
scope: CodeWhispererConstants.CodeAnalysisScope
167+
scope: CodeWhispererConstants.CodeAnalysisScope,
168+
scanName: string
163169
) {
164170
if (zipMetadata.zipFilePath === '') {
165171
throw new Error("Zip failure: can't find valid source zip.")
@@ -168,9 +174,17 @@ export async function getPresignedUrlAndUpload(
168174
contentMd5: getMd5(zipMetadata.zipFilePath),
169175
artifactType: 'SourceCode',
170176
uploadIntent: getUploadIntent(scope),
177+
uploadContext: {
178+
codeAnalysisUploadContext: {
179+
codeScanName: scanName,
180+
},
181+
},
171182
}
172183
getLogger().verbose(`Prepare for uploading src context...`)
173-
const srcResp = await client.createUploadUrl(srcReq)
184+
const srcResp = await client.createUploadUrl(srcReq).catch(err => {
185+
getLogger().error(`Failed getting presigned url for uploading src context. Request id: ${err.requestId}`)
186+
throw err
187+
})
174188
getLogger().verbose(`Request id: ${srcResp.$response.requestId}`)
175189
getLogger().verbose(`Complete Getting presigned Url for uploading src context.`)
176190
getLogger().verbose(`Uploading src context...`)

packages/core/src/testE2E/codewhisperer/securityScan.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { makeTemporaryToolkitFolder } from '../../shared/filesystemUtilities'
2323
import { fsCommon } from '../../srcShared/fs'
2424
import { ZipUtil } from '../../codewhisperer/util/zipUtil'
25+
import { randomUUID } from '../../common/crypto'
2526

2627
const filePromptWithSecurityIssues = `from flask import app
2728
@@ -84,7 +85,7 @@ describe('CodeWhisperer security scan', async function () {
8485
Step 1: Generate context truncations
8586
Step 2: Get presigned Url and upload
8687
87-
returns artifactMap and projectPath
88+
returns artifactMap, projectPath and codeScanName
8889
*/
8990
async function securityJobSetup(editor: vscode.TextEditor) {
9091
const zipUtil = new ZipUtil()
@@ -93,16 +94,18 @@ describe('CodeWhisperer security scan', async function () {
9394
const projectPath = zipUtil.getProjectPath(editor.document.uri)
9495
const scope = CodeWhispererConstants.CodeAnalysisScope.PROJECT
9596
const zipMetadata = await zipUtil.generateZip(uri, scope)
97+
const codeScanName = randomUUID()
9698

9799
let artifactMap
98100
try {
99-
artifactMap = await getPresignedUrlAndUpload(client, zipMetadata, scope)
101+
artifactMap = await getPresignedUrlAndUpload(client, zipMetadata, scope, codeScanName)
100102
} finally {
101103
await zipUtil.removeTmpFiles(zipMetadata)
102104
}
103105
return {
104106
artifactMap: artifactMap,
105107
projectPath: projectPath,
108+
codeScanName: codeScanName,
106109
}
107110
}
108111

@@ -120,7 +123,13 @@ describe('CodeWhisperer security scan', async function () {
120123
const scope = CodeWhispererConstants.CodeAnalysisScope.PROJECT
121124

122125
//get job status and result
123-
const scanJob = await createScanJob(client, artifactMap, editor.document.languageId, scope)
126+
const scanJob = await createScanJob(
127+
client,
128+
artifactMap,
129+
editor.document.languageId,
130+
scope,
131+
securityJobSetupResult.codeScanName
132+
)
124133
const jobStatus = await pollScanJobStatus(client, scanJob.jobId, scope)
125134
const securityRecommendationCollection = await listScanResults(
126135
client,
@@ -146,7 +155,13 @@ describe('CodeWhisperer security scan', async function () {
146155
const securityJobSetupResult = await securityJobSetup(editor)
147156
const artifactMap = securityJobSetupResult.artifactMap
148157
const projectPath = securityJobSetupResult.projectPath
149-
const scanJob = await createScanJob(client, artifactMap, editor.document.languageId, scope)
158+
const scanJob = await createScanJob(
159+
client,
160+
artifactMap,
161+
editor.document.languageId,
162+
scope,
163+
securityJobSetupResult.codeScanName
164+
)
150165

151166
//get job status and result
152167
const jobStatus = await pollScanJobStatus(client, scanJob.jobId, scope)

0 commit comments

Comments
 (0)