Skip to content

Commit 4d56741

Browse files
dhasani23David Hasaniashishrp-awslaileni-aws
authored
fix(amazonq): validate yaml file for required keys (#7818)
## Problem Sometimes a `.yaml` file QCT asks for from the customer is malformed and users don't see what exactly is wrong. ## Solution Validate the file and provide a more specific error message. --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Co-authored-by: David Hasani <[email protected]> Co-authored-by: invictus <[email protected]> Co-authored-by: Laxman Reddy <[email protected]>
1 parent 76a1cd0 commit 4d56741

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

packages/core/src/amazonqGumby/chat/controller/controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,14 @@ export class GumbyController {
580580
return
581581
}
582582
const fileContents = await fs.readFileText(fileUri[0].fsPath)
583-
const isValidFile = await validateCustomVersionsFile(fileContents)
583+
const missingKey = await validateCustomVersionsFile(fileContents)
584584

585-
if (!isValidFile) {
586-
this.messenger.sendUnrecoverableErrorResponse('invalid-custom-versions-file', message.tabID)
585+
if (missingKey) {
586+
this.messenger.sendMessage(
587+
CodeWhispererConstants.invalidCustomVersionsFileMessage(missingKey),
588+
message.tabID,
589+
'ai-prompt'
590+
)
587591
return
588592
}
589593
this.messenger.sendMessage(CodeWhispererConstants.receivedValidConfigFileMessage, message.tabID, 'ai-prompt')

packages/core/src/amazonqGumby/chat/controller/messenger/messenger.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export type UnrecoverableErrorType =
5050
| 'job-start-failed'
5151
| 'unsupported-source-db'
5252
| 'unsupported-target-db'
53-
| 'invalid-custom-versions-file'
5453
| 'error-parsing-sct-file'
5554
| 'invalid-zip-no-sct-file'
5655
| 'invalid-from-to-jdk'
@@ -453,9 +452,6 @@ export class Messenger {
453452
case 'unsupported-target-db':
454453
message = CodeWhispererConstants.invalidMetadataFileUnsupportedTargetDB
455454
break
456-
case 'invalid-custom-versions-file':
457-
message = CodeWhispererConstants.invalidCustomVersionsFileMessage
458-
break
459455
case 'error-parsing-sct-file':
460456
message = CodeWhispererConstants.invalidMetadataFileErrorParsing
461457
break

packages/core/src/codewhisperer/models/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ export const invalidMetadataFileUnsupportedSourceDB =
582582
export const invalidMetadataFileUnsupportedTargetDB =
583583
'I can only convert SQL for migrations to Aurora PostgreSQL or Amazon RDS for PostgreSQL target databases. The provided .sct file indicates another target database for this migration.'
584584

585-
export const invalidCustomVersionsFileMessage =
586-
"I wasn't able to parse the dependency upgrade file. Check that it's configured properly and try again. For an example of the required dependency upgrade file format, see the [documentation](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/code-transformation.html#dependency-upgrade-file)."
585+
export const invalidCustomVersionsFileMessage = (missingKey: string) =>
586+
`The dependency upgrade file provided is missing required field \`${missingKey}\`. Check that it is configured properly and try again. For an example of the required dependency upgrade file format, see the [documentation](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/code-transformation.html#dependency-upgrade-file).`
587587

588588
export const invalidMetadataFileErrorParsing =
589589
"It looks like the .sct file you provided isn't valid. Make sure that you've uploaded the .zip file you retrieved from your schema conversion in AWS DMS."

packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ export async function parseBuildFile() {
117117
return undefined
118118
}
119119

120+
// return the first missing key in the custom versions file, or undefined if all required keys are present
120121
export async function validateCustomVersionsFile(fileContents: string) {
121-
const requiredKeys = ['dependencyManagement:', 'identifier:', 'targetVersion:']
122+
const requiredKeys = ['dependencyManagement', 'identifier', 'targetVersion', 'originType']
122123
for (const key of requiredKeys) {
123124
if (!fileContents.includes(key)) {
124125
getLogger().info(`CodeTransformation: .YAML file is missing required key: ${key}`)
125-
return false
126+
return key
126127
}
127128
}
128-
return true
129+
return undefined
129130
}
130131

131132
export async function validateSQLMetadataFile(fileContents: string, message: any) {

packages/core/src/test/codewhisperer/commands/transformByQ.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,14 @@ dependencyManagement:
571571
})
572572

573573
it(`WHEN validateCustomVersionsFile on fully valid .yaml file THEN passes validation`, async function () {
574-
const isValidFile = await validateCustomVersionsFile(validCustomVersionsFile)
575-
assert.strictEqual(isValidFile, true)
574+
const missingKey = await validateCustomVersionsFile(validCustomVersionsFile)
575+
assert.strictEqual(missingKey, undefined)
576576
})
577577

578578
it(`WHEN validateCustomVersionsFile on invalid .yaml file THEN fails validation`, async function () {
579579
const invalidFile = validCustomVersionsFile.replace('dependencyManagement', 'invalidKey')
580-
const isValidFile = await validateCustomVersionsFile(invalidFile)
581-
assert.strictEqual(isValidFile, false)
580+
const missingKey = await validateCustomVersionsFile(invalidFile)
581+
assert.strictEqual(missingKey, 'dependencyManagement')
582582
})
583583

584584
it(`WHEN validateMetadataFile on fully valid .sct file THEN passes validation`, async function () {

0 commit comments

Comments
 (0)