-
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
Open
naman-contentstack
wants to merge
4
commits into
feat/AM2.0
Choose a base branch
from
feat/DX-4976
base: feat/AM2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
100 changes: 100 additions & 0 deletions
100
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,100 @@ | ||
| 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 { PROCESS_NAMES, PROCESS_STATUS } from '../constants/index'; | ||
| import { runInBatches } from '../utils/concurrent-batch'; | ||
|
|
||
| const STRIP_KEYS = ['created_at', 'created_by', 'updated_at', 'updated_by', 'is_system', 'category', 'preview_image_url', 'category_detail']; | ||
|
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. we already have this defined in the config |
||
|
|
||
| /** | ||
| * 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 dir = this.getAssetTypesDir(); | ||
| const items = await this.readAllChunkedJson<Record<string, unknown>>(dir, 'asset-types.json'); | ||
|
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. we should use our fs utility |
||
|
|
||
| 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> }; | ||
| 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, STRIP_KEYS); | ||
| const existingClean = omit(existing, STRIP_KEYS); | ||
| 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, STRIP_KEYS) 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all these should be part of the import/export config passed from the export or import command, we can have defaults here