-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add support for AM in import module #41
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 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
64c6e36
feat: add support for AM in import module
a01a6fc
chore: update implementation for AM import
de7d9bb
Merge branch 'feat/AM2.0' into feat/DX-4976
bfb990b
Merge branch 'feat/AM2.0' into feat/DX-4976
cbd6d7f
chore: made implementation consistent with other modules
93c7444
Merge branch 'feat/AM2.0' into feat/DX-4976
2ef8e3a
chore: updated implementation for AM import
4f99b26
chore: optimise the code
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
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
101 changes: 101 additions & 0 deletions
101
packages/contentstack-asset-management/src/import/asset-types.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,101 @@ | ||
| import omit from 'lodash/omit'; | ||
| import isEqual from 'lodash/isEqual'; | ||
| import { log } from '@contentstack/cli-utilities'; | ||
|
|
||
| import type { AssetManagementAPIConfig, ImportContext } from '../types/asset-management-api'; | ||
| import { AssetManagementImportAdapter } from './base'; | ||
| import { FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS, PROCESS_NAMES, PROCESS_STATUS } from '../constants/index'; | ||
| import { runInBatches } from '../utils/concurrent-batch'; | ||
| import { readChunkedJsonItems } from '../utils/chunked-json-read'; | ||
|
|
||
| /** | ||
| * Reads shared asset types from `spaces/asset_types/asset-types.json` and POSTs | ||
| * each to the target org-level AM endpoint (`POST /api/asset_types`). | ||
| * | ||
| * Strategy: Fetch → Diff → Create only missing, warn on conflict | ||
| * 1. Fetch asset types that already exist in the target org. | ||
| * 2. Skip entries where is_system=true (platform-owned, cannot be created via API). | ||
| * 3. If uid already exists and definition differs → warn and skip. | ||
| * 4. If uid already exists and definition matches → silently skip. | ||
| * 5. Strip read-only/computed keys from the POST body before creating new asset types. | ||
| */ | ||
| export default class ImportAssetTypes extends AssetManagementImportAdapter { | ||
| constructor(apiConfig: AssetManagementAPIConfig, importContext: ImportContext) { | ||
| super(apiConfig, importContext); | ||
| } | ||
|
|
||
| async start(): Promise<void> { | ||
| await this.init(); | ||
|
|
||
| const stripKeys = this.importContext.assetTypesImportInvalidKeys ?? [...FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS]; | ||
| const dir = this.getAssetTypesDir(); | ||
| const indexName = this.importContext.assetTypesFileName ?? 'asset-types.json'; | ||
| const items = await readChunkedJsonItems<Record<string, unknown>>(dir, indexName, this.importContext.context); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using fs utility, we are already have a way to read to this, no need of this implementation at all "readChunkedJsonItems" |
||
|
|
||
| if (items.length === 0) { | ||
| log.debug('No shared asset types to import', this.importContext.context); | ||
| return; | ||
| } | ||
|
|
||
| // Fetch existing asset types from the target org keyed by uid for diff comparison. | ||
| // Asset types are org-level; the spaceUid param in getWorkspaceAssetTypes is unused in the path. | ||
| const existingByUid = new Map<string, Record<string, unknown>>(); | ||
| try { | ||
| const existing = await this.getWorkspaceAssetTypes(''); | ||
| for (const at of existing.asset_types ?? []) { | ||
| existingByUid.set(at.uid, at as Record<string, unknown>); | ||
| } | ||
| log.debug(`Target org has ${existingByUid.size} existing asset type(s)`, this.importContext.context); | ||
| } catch (e) { | ||
| log.debug(`Could not fetch existing asset types, will attempt to create all: ${e}`, this.importContext.context); | ||
| } | ||
|
|
||
| this.updateStatus(PROCESS_STATUS[PROCESS_NAMES.AM_IMPORT_ASSET_TYPES].IMPORTING, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); | ||
|
|
||
| type ToCreate = { uid: string; payload: Record<string, unknown> }; | ||
|
naman-contentstack marked this conversation as resolved.
Outdated
|
||
| const toCreate: ToCreate[] = []; | ||
|
|
||
| for (const assetType of items) { | ||
| const uid = assetType.uid as string; | ||
|
|
||
| if (assetType.is_system) { | ||
| log.debug(`Skipping system asset type: ${uid}`, this.importContext.context); | ||
| continue; | ||
| } | ||
|
|
||
| const existing = existingByUid.get(uid); | ||
| if (existing) { | ||
| const exportedClean = omit(assetType, stripKeys); | ||
| const existingClean = omit(existing, stripKeys); | ||
| if (!isEqual(exportedClean, existingClean)) { | ||
| log.warn( | ||
| `Asset type "${uid}" already exists in the target org with a different definition. Skipping — to apply the exported definition, delete the asset type from the target org first.`, | ||
| this.importContext.context, | ||
| ); | ||
| } else { | ||
| log.debug(`Asset type "${uid}" already exists with matching definition, skipping`, this.importContext.context); | ||
| } | ||
| this.tick(true, `asset-type: ${uid} (skipped, already exists)`, null, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); | ||
| continue; | ||
| } | ||
|
|
||
| toCreate.push({ uid, payload: omit(assetType, stripKeys) as Record<string, unknown> }); | ||
| } | ||
|
|
||
| await runInBatches(toCreate, this.apiConcurrency, async ({ uid, payload }) => { | ||
| try { | ||
| await this.createAssetType(payload as any); | ||
| this.tick(true, `asset-type: ${uid}`, null, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); | ||
| log.debug(`Imported asset type: ${uid}`, this.importContext.context); | ||
| } catch (e) { | ||
| this.tick( | ||
| false, | ||
| `asset-type: ${uid}`, | ||
| (e as Error)?.message ?? PROCESS_STATUS[PROCESS_NAMES.AM_IMPORT_ASSET_TYPES].FAILED, | ||
| PROCESS_NAMES.AM_IMPORT_ASSET_TYPES, | ||
| ); | ||
| log.debug(`Failed to import asset type ${uid}: ${e}`, this.importContext.context); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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.