Skip to content

Commit 1562c38

Browse files
dhasani23David Hasani
andauthored
fix(amazonq): update YAML validation logic (#8146)
## Problem We need to validate dependency IDs differently from plugin IDs ## Solution Update validation logic --- - 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]>
1 parent 2accbd5 commit 1562c38

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ export function validateCustomVersionsFile(fileContents: string) {
139139
getLogger().info('CodeTransformation: .YAML file must contain at least dependencies or plugins')
140140
return `YAML file must contain at least \`dependencies\` or \`plugins\` under \`dependencyManagement\``
141141
}
142-
for (const item of dependenciesAndPlugins) {
143-
const errorMessage = validateItem(item)
142+
for (const item of dependencies) {
143+
const errorMessage = validateItem(item, false)
144+
if (errorMessage) {
145+
return errorMessage
146+
}
147+
}
148+
for (const item of plugins) {
149+
const errorMessage = validateItem(item, true)
144150
if (errorMessage) {
145151
return errorMessage
146152
}
@@ -153,10 +159,15 @@ export function validateCustomVersionsFile(fileContents: string) {
153159
}
154160

155161
// return an error message, or undefined if item is valid
156-
function validateItem(item: any, validOriginTypes: string[] = ['FIRST_PARTY', 'THIRD_PARTY']) {
157-
if (!/^[^\s:]+:[^\s:]+$/.test(item.identifier)) {
162+
function validateItem(item: any, isPlugin: boolean) {
163+
const validOriginTypes = ['FIRST_PARTY', 'THIRD_PARTY']
164+
if (!isPlugin && !/^[^\s:]+:[^\s:]+$/.test(item.identifier)) {
158165
getLogger().info(`CodeTransformation: Invalid identifier format: ${item.identifier}`)
159-
return `Invalid identifier format: \`${item.identifier}\`. Must be in format \`groupId:artifactId\` without spaces`
166+
return `Invalid dependency identifier format: \`${item.identifier}\`. Must be in format \`groupId:artifactId\` without spaces`
167+
}
168+
if (isPlugin && !item.identifier?.trim()) {
169+
getLogger().info('CodeTransformation: Missing identifier in plugin')
170+
return 'Missing `identifier` in plugin'
160171
}
161172
if (!validOriginTypes.includes(item.originType)) {
162173
getLogger().info(`CodeTransformation: Invalid originType: ${item.originType}`)

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ dependencyManagement:
6565
targetVersion: "3.0.0"
6666
originType: "THIRD_PARTY"
6767
plugins:
68-
- identifier: "com.example:plugin"
68+
- identifier: "plugin.id"
6969
targetVersion: "1.2.0"
7070
versionProperty: "plugin.version" # Optional
7171
originType: "FIRST_PARTY" # or "THIRD_PARTY"`
@@ -582,15 +582,21 @@ dependencyManagement:
582582
assert.strictEqual(errorMessage, `Missing required key: \`dependencyManagement\``)
583583
})
584584

585-
it(`WHEN validateCustomVersionsFile on .yaml file with invalid identifier format THEN fails validation`, function () {
585+
it(`WHEN validateCustomVersionsFile on .yaml file with invalid dependency identifier format THEN fails validation`, function () {
586586
const invalidFile = validCustomVersionsFile.replace('com.example:library1', 'com.example-library1')
587587
const errorMessage = validateCustomVersionsFile(invalidFile)
588588
assert.strictEqual(
589589
errorMessage,
590-
`Invalid identifier format: \`com.example-library1\`. Must be in format \`groupId:artifactId\` without spaces`
590+
`Invalid dependency identifier format: \`com.example-library1\`. Must be in format \`groupId:artifactId\` without spaces`
591591
)
592592
})
593593

594+
it(`WHEN validateCustomVersionsFile on .yaml file with missing plugin identifier format THEN fails validation`, function () {
595+
const invalidFile = validCustomVersionsFile.replace('plugin.id', '')
596+
const errorMessage = validateCustomVersionsFile(invalidFile)
597+
assert.strictEqual(errorMessage, 'Missing `identifier` in plugin')
598+
})
599+
594600
it(`WHEN validateCustomVersionsFile on .yaml file with invalid originType THEN fails validation`, function () {
595601
const invalidFile = validCustomVersionsFile.replace('FIRST_PARTY', 'INVALID_TYPE')
596602
const errorMessage = validateCustomVersionsFile(invalidFile)

0 commit comments

Comments
 (0)