-
Notifications
You must be signed in to change notification settings - Fork 0
fix(import): support typed CDS and RAP imports #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae05795
fix(import): support typed CDS and RAP imports
ThePlenkov 8785884
refactor(import): isolate object selection
ThePlenkov 86ab17a
refactor(import): split object resolution helpers
ThePlenkov ec1f057
refactor(import): isolate object selection utility
ThePlenkov e0ca135
refactor(import): group resolution context
ThePlenkov 4cb3ee3
docs(srvb): align v1 media-type references with v2 implementation
ThePlenkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
packages/adt-cli/src/lib/services/import/object-selection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| export type SearchObject = { | ||
| name?: string; | ||
| type?: string; | ||
| uri?: string; | ||
| description?: string; | ||
| packageName?: string; | ||
| }; | ||
|
|
||
| export type ObjectSearchOptions = { | ||
| objectName: string; | ||
| objectType?: string; | ||
| }; | ||
|
|
||
| type SearchObjectResolutionContext = { | ||
| objects: SearchObject[]; | ||
| options: ObjectSearchOptions; | ||
| exactMatches: SearchObject[]; | ||
| availableExactTypes: string[]; | ||
| requestedObjectType: string | undefined; | ||
| }; | ||
|
|
||
| function normalizeObjectType(type: string): string { | ||
| return type.toUpperCase().split('/')[0] ?? ''; | ||
| } | ||
|
|
||
| function getExactSearchMatches( | ||
| objects: SearchObject[], | ||
| objectName: string, | ||
| ): SearchObject[] { | ||
| const normalizedName = objectName.toUpperCase(); | ||
| return objects.filter( | ||
| (obj) => String(obj.name || '').toUpperCase() === normalizedName, | ||
| ); | ||
| } | ||
|
|
||
| function getSearchObjectTypes(objects: SearchObject[]): string[] { | ||
| return [ | ||
| ...new Set( | ||
| objects.map((obj) => normalizeObjectType(String(obj.type || ''))), | ||
| ), | ||
| ].filter(Boolean); | ||
| } | ||
|
|
||
| function findExactSearchMatch( | ||
| exactMatches: SearchObject[], | ||
| requestedObjectType: string | undefined, | ||
| availableExactTypes: string[], | ||
| ): SearchObject | undefined { | ||
| if (requestedObjectType) { | ||
| return exactMatches.find( | ||
| (obj) => | ||
| normalizeObjectType(String(obj.type || '')) === requestedObjectType, | ||
| ); | ||
| } | ||
|
|
||
| return availableExactTypes.length <= 1 ? exactMatches[0] : undefined; | ||
| } | ||
|
|
||
| function createObjectNotFoundError( | ||
| objects: SearchObject[], | ||
| objectName: string, | ||
| ): Error { | ||
| const similar = objects | ||
| .filter((obj) => | ||
| String(obj.name || '') | ||
| .toUpperCase() | ||
| .includes(objectName.toUpperCase()), | ||
| ) | ||
| .slice(0, 5); | ||
| const similarList = similar | ||
| .map((obj) => ` • ${obj.name} (${obj.type}) – ${obj.packageName}`) | ||
| .join('\n'); | ||
| const hint = | ||
| similar.length > 0 ? `\n💡 Similar objects:\n${similarList}` : ''; | ||
|
|
||
| return new Error(`Object '${objectName}' not found in the system.${hint}`); | ||
| } | ||
|
|
||
| function createSearchObjectResolutionError( | ||
| context: SearchObjectResolutionContext, | ||
| ): Error { | ||
| const { | ||
| objects, | ||
| options, | ||
| exactMatches, | ||
| availableExactTypes, | ||
| requestedObjectType, | ||
| } = context; | ||
|
|
||
| if (requestedObjectType && exactMatches.length > 0) { | ||
| return new Error( | ||
| `Object '${options.objectName}' with type '${requestedObjectType}' was not found. Available types: ${availableExactTypes.join(', ') || 'none'}.`, | ||
| ); | ||
| } | ||
|
|
||
| if (!requestedObjectType && availableExactTypes.length > 1) { | ||
| return new Error( | ||
| `Object '${options.objectName}' is ambiguous. Use --object-type to select one of: ${availableExactTypes.join(', ')}.`, | ||
| ); | ||
| } | ||
|
|
||
| return createObjectNotFoundError(objects, options.objectName); | ||
| } | ||
|
|
||
| export function selectSearchObject( | ||
| objects: SearchObject[], | ||
| options: ObjectSearchOptions, | ||
| ): SearchObject { | ||
| const exactMatches = getExactSearchMatches(objects, options.objectName); | ||
| const availableExactTypes = getSearchObjectTypes(exactMatches); | ||
| const requestedObjectType = options.objectType | ||
| ? normalizeObjectType(options.objectType.trim()) | ||
| : undefined; | ||
| const exactMatch = findExactSearchMatch( | ||
| exactMatches, | ||
| requestedObjectType, | ||
| availableExactTypes, | ||
| ); | ||
|
|
||
| if (exactMatch) { | ||
| return exactMatch; | ||
| } | ||
|
|
||
| throw createSearchObjectResolutionError({ | ||
| objects, | ||
| options, | ||
| exactMatches, | ||
| availableExactTypes, | ||
| requestedObjectType, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const factoryGet = vi.fn(); | ||
| const formatImport = vi.fn(); | ||
|
|
||
| vi.mock('@abapify/adk', () => ({ | ||
| AdkPackage: { | ||
| get: vi.fn(async () => ({ superPackage: undefined })), | ||
| }, | ||
| AdkTransport: { get: vi.fn() }, | ||
| MergedTransportView: class {}, | ||
| matchesSelector: vi.fn(), | ||
| createAdkFactory: vi.fn(() => ({ get: factoryGet })), | ||
| getGlobalContext: vi.fn(() => ({ | ||
| client: { | ||
| adt: { | ||
| repository: { | ||
| informationsystem: { | ||
| search: { | ||
| quickSearch: vi.fn(async () => ({ | ||
| objectReference: [ | ||
| { | ||
| name: 'Z_SHARED_NAME', | ||
| type: 'BDEF/BDO', | ||
| packageName: 'ZPKG', | ||
| }, | ||
| { | ||
| name: 'Z_SHARED_NAME', | ||
| type: 'DDLX', | ||
| packageName: 'ZPKG', | ||
| }, | ||
| ], | ||
| })), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| })), | ||
| })); | ||
|
|
||
| vi.mock('../../../src/lib/utils/format-loader', () => ({ | ||
| loadFormatPlugin: vi.fn(async () => ({ | ||
| name: 'abapGit', | ||
| description: 'abapGit format plugin', | ||
| instance: { | ||
| registry: { isSupported: vi.fn(() => true) }, | ||
| format: { import: formatImport }, | ||
| hooks: {}, | ||
| }, | ||
| })), | ||
| parseFormatSpec: vi.fn(() => ({ package: '@abapify/adt-plugin-abapgit' })), | ||
| })); | ||
|
|
||
| vi.mock('../../../src/lib/utils/destinations', () => ({ | ||
| getConfig: vi.fn(async () => ({ raw: {} })), | ||
| })); | ||
|
|
||
| describe('ImportService.importObject()', () => { | ||
| beforeEach(() => { | ||
| factoryGet.mockReset(); | ||
| formatImport.mockReset(); | ||
| factoryGet.mockImplementation((_name, type) => ({ | ||
| type, | ||
| load: vi.fn(async () => undefined), | ||
| })); | ||
| formatImport.mockResolvedValue({ success: true, filesCreated: [] }); | ||
| }); | ||
|
|
||
| it('selects the requested type when the same name belongs to multiple object types', async () => { | ||
| const { ImportService } = | ||
| await import('../../../src/lib/services/import/service'); | ||
|
|
||
| const result = await new ImportService().importObject({ | ||
| objectName: 'Z_SHARED_NAME', | ||
| objectType: 'DDLX', | ||
| outputPath: '/tmp/import-object', | ||
| format: 'abapgit', | ||
| }); | ||
|
|
||
| expect(factoryGet).toHaveBeenCalledWith('Z_SHARED_NAME', 'DDLX'); | ||
| expect(result.objectType).toBe('DDLX'); | ||
| }); | ||
|
|
||
| it('requires an explicit type when the same name belongs to multiple object types', async () => { | ||
| const { ImportService } = | ||
| await import('../../../src/lib/services/import/service'); | ||
|
|
||
| await expect( | ||
| new ImportService().importObject({ | ||
| objectName: 'Z_SHARED_NAME', | ||
| outputPath: '/tmp/import-object', | ||
| format: 'abapgit', | ||
| }), | ||
| ).rejects.toThrow( | ||
| "Object 'Z_SHARED_NAME' is ambiguous. Use --object-type to select one of: BDEF, DDLX.", | ||
| ); | ||
| expect(factoryGet).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.