feat(gen2-migration): detecting service type to distinguish between cdk and cfn custom resources#14410
feat(gen2-migration): detecting service type to distinguish between cdk and cfn custom resources#14410sanjanaravikumar-az wants to merge 9 commits intogen2-migrationfrom
Conversation
|
|
||
| type CustomResourceServiceType = 'customCDK' | 'customCloudformation'; | ||
|
|
||
| const getCustomResourcesWithType = (): Map<string, CustomResourceServiceType> => { |
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 4 months ago
To fix the problem, remove the unused function getCustomResourcesWithType. This eliminates the unused binding and any dead logic inside it, without affecting existing functionality, because nothing currently depends on it.
Concretely:
- In
packages/amplify-cli/src/commands/gen2-migration/codegen-generate/src/codegen-head/command-handlers.ts, delete the entiregetCustomResourcesWithTypedeclaration (lines 327–338 in the snippet), keeping the surroundingtype CustomResourceServiceTypeandgetCustomResourcesdefinitions intact. - No imports, exports, or other definitions need to be added or changed, since the function is local and not referenced.
| @@ -324,19 +324,6 @@ | ||
|
|
||
| type CustomResourceServiceType = 'customCDK' | 'customCloudformation'; | ||
|
|
||
| const getCustomResourcesWithType = (): Map<string, CustomResourceServiceType> => { | ||
| const meta = stateManager.getMeta(); | ||
| const customCategory = meta?.custom; | ||
| const resourceMap = new Map<string, CustomResourceServiceType>(); | ||
|
|
||
| if (customCategory) { | ||
| Object.entries(customCategory).forEach(([name, config]: [string, { service: CustomResourceServiceType }]) => { | ||
| resourceMap.set(name, config.service); | ||
| }); | ||
| } | ||
| return resourceMap; | ||
| }; | ||
|
|
||
| const getCustomResources = (): string[] => { | ||
| const meta = stateManager.getMeta(); | ||
| const customCategory = meta?.custom; |
There was a problem hiding this comment.
Can you address this as either true positive or false positive?
iankhou
left a comment
There was a problem hiding this comment.
Looks good, but difficult for me to verify functionality by looking at it. How was it tested?
We are making a lot of changes to transformations here. I think it would be good to have unit tests that cover functionality introduced by the new parameter resourceDependencies.
|
|
||
| type CustomResourceServiceType = 'customCDK' | 'customCloudformation'; | ||
|
|
||
| const getCustomResourcesWithType = (): Map<string, CustomResourceServiceType> => { |
There was a problem hiding this comment.
Can you address this as either true positive or false positive?
| for (const resource of customResources) { | ||
| const cdkStackFilePath = path.join(destinationCustomResourcePath, resource, 'cdk-stack.ts'); | ||
|
|
||
| try { | ||
| const cdkStackContent = await fs.readFile(cdkStackFilePath, { encoding: 'utf-8' }); | ||
| const dependencies: string[] = []; | ||
|
|
||
| // Parse for AmplifyHelpers.addResourceDependency calls | ||
| const dependencyRegex = /AmplifyHelpers\.addResourceDependency\s*\([^,]+,[^,]+,[^,]+,\s*\[([^\]]+)\]/g; | ||
| let match; | ||
|
|
||
| while ((match = dependencyRegex.exec(cdkStackContent)) !== null) { | ||
| const dependenciesArray = match[1]; | ||
|
|
||
| // Extract category values from dependency objects | ||
| const categoryRegex = /category:\s*['"]([^'"]+)['"]/g; | ||
| let categoryMatch; | ||
|
|
||
| while ((categoryMatch = categoryRegex.exec(dependenciesArray)) !== null) { | ||
| const category = categoryMatch[1]; | ||
| if (!dependencies.includes(category)) { | ||
| dependencies.push(category); | ||
| } | ||
| } |
There was a problem hiding this comment.
For this kind of logic I would generally like to see some kind of test, since regex is not verifiable by reading it (at least not by me). A unit test may be sufficient for now.
| `throw new Error('Follow https://docs.amplify.aws/react/start/migrate-to-gen2/ to update the resource dependency');\n\nexport class`, | ||
| ); | ||
| } | ||
| // AmplifyHelpers.addResourceDependency is now supported by the transformer |
Overview
This PR adds service type detection for custom resources during Gen1 to Gen2 migration. It introduces the ability to distinguish between CDK-based (customCDK) and CloudFormation-based (customCloudformation) custom resources.
Key Changes
CustomResourceServiceTypetype to distinguish between customCDK and customCloudformation resourcesgetCustomResourcesWithType()function that reads from amplify-meta.json and returns a map of resource names to their service typesGraphQLAPIKeyOutputmapping back tocfnResources.cfnGraphqlApi.attrApiKeyBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.