Skip to content

Commit

Permalink
Merge pull request #1202 from contentstack/refactor/CS-42957
Browse files Browse the repository at this point in the history
refactor: marketplace apps HTTP adapter calls to SDK call migration on import command [CS-42957]
  • Loading branch information
antonyagustine authored Dec 6, 2023
2 parents e7a7908 + 25b41f0 commit eee2042
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = class ImportMarketplaceApps {
}

this.developerHubBaseUrl = this.config.developerHubBaseUrl || (await getDeveloperHubUrl(this.config));
this.config.developerHubBaseUrl = this.developerHubBaseUrl;
this.client = await managementSDKClient({ endpoint: this.developerHubBaseUrl });
this.appSdkAxiosInstance = await managementSDKClient({
host: this.developerHubBaseUrl.split('://').pop()
Expand Down Expand Up @@ -166,7 +167,7 @@ module.exports = class ImportMarketplaceApps {

// NOTE install all private apps which is not available for stack.
await this.handleAllPrivateAppsCreationProcess();
const installedApps = await getAllStackSpecificApps(this.developerHubBaseUrl, this.httpClient, this.config);
const installedApps = await getAllStackSpecificApps(this.config);

log(this.config, 'Starting marketplace app installation', 'success');

Expand All @@ -186,8 +187,7 @@ module.exports = class ImportMarketplaceApps {
const listOfNewMeta = [];
const listOfOldMeta = [];
const extensionUidMap = {};
const allInstalledApps =
(await getAllStackSpecificApps(this.developerHubBaseUrl, this.httpClient, this.config)) || [];
const allInstalledApps = (await getAllStackSpecificApps(this.config)) || [];

for (const app of this.marketplaceApps) {
listOfOldMeta.push(..._.map(app?.ui_location?.locations, 'meta').flat());
Expand Down
265 changes: 159 additions & 106 deletions packages/contentstack-import/src/import/modules/marketplace-apps.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/contentstack-import/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ export interface TermsConfig{

export { default as DefaultConfig } from './default-config';
export { default as ImportConfig } from './import-config';
export * from './marketplace-app'
65 changes: 65 additions & 0 deletions packages/contentstack-import/src/types/marketplace-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
type AppLocation =
| 'cs.cm.stack.config'
| 'cs.cm.stack.dashboard'
| 'cs.cm.stack.sidebar'
| 'cs.cm.stack.custom_field'
| 'cs.cm.stack.rte'
| 'cs.cm.stack.asset_sidebar'
| 'cs.org.config';

interface ExtensionMeta {
uid?: string;
name?: string;
description?: string;
path?: string;
signed: boolean;
extension_uid?: string;
data_type?: string;
enabled?: boolean;
width?: number;
blur?: boolean;
default_width?: 'full' | 'half';
}

interface Extension {
type: AppLocation;
meta: ExtensionMeta[];
}

interface LocationConfiguration {
signed: boolean;
base_url: string;
locations: Extension[];
}

interface AnyProperty {
[propName: string]: any;
}

type Manifest = {
uid: string;
name: string;
icon?: string;
hosting?: any;
version?: number;
description: string;
organization_uid: string;
framework_version?: string;
oauth?: any;
webhook?: any;
ui_location: LocationConfiguration;
target_type: 'stack' | 'organization';
visibility: 'private' | 'public' | 'public_unlisted';
} & AnyProperty;

type Installation = {
uid: string;
status: string;
manifest: Manifest;
configuration: any;
server_configuration: any;
target: { type: string; uid: string };
ui_location: LocationConfiguration;
} & AnyProperty;

export { Installation, Manifest };
3 changes: 0 additions & 3 deletions packages/contentstack-import/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ export {
getDeveloperHubUrl,
getOrgUid,
getConfirmationToCreateApps,
createPrivateApp,
handleNameConflict,
installApp,
makeRedirectUrlCall,
confirmToCloseProcess,
getAllStackSpecificApps,
ifAppAlreadyExist,
updateAppConfig,
} from './marketplace-app-helper';
export { schemaTemplate, suppressSchemaReference, removeReferenceFields } from './content-type-helper';
export { lookupExtension } from './extension-helper';
Expand Down
100 changes: 39 additions & 61 deletions packages/contentstack-import/src/utils/marketplace-app-helper.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
import isEmpty from 'lodash/isEmpty';
import chalk from 'chalk';
import map from 'lodash/map';
import omit from 'lodash/omit';
import omitBy from 'lodash/omitBy';
import isEmpty from 'lodash/isEmpty';
import includes from 'lodash/includes';
import chalk from 'chalk';
import { cliux, configHandler, HttpClient, ContentstackClient, managementSDKClient } from '@contentstack/cli-utilities';
import {
cliux,
HttpClient,
configHandler,
managementSDKClient,
marketplaceSDKClient,
} from '@contentstack/cli-utilities';

import { log } from './logger';
import { trace } from '../utils/log';
import { ImportConfig } from '../types';
import { ImportConfig, Installation } from '../types';
import { formatError } from '../utils';
import { askDeveloperHubUrl } from './interactive';
import { getAppName, askAppName, selectConfiguration } from '../utils/interactive';

export const getAllStackSpecificApps = async (
developerHubBaseUrl: string,
httpClient: HttpClient,
config: ImportConfig,
) => {
const appSdkAxiosInstance = await managementSDKClient({
host: developerHubBaseUrl.split('://').pop(),
skip = 0,
listOfApps: Installation[] = [],
): Promise<Installation[]> => {
const appSdk = await marketplaceSDKClient({
host: config.developerHubBaseUrl.split('://').pop(),
});
return await appSdkAxiosInstance.axiosInstance
.get(`${developerHubBaseUrl}/installations?target_uids=${config.target_stack}`, {
headers: {
organization_uid: config.org_uid,
},
})
.then(({ data }: any) => data.data)
.catch((error: any) => {
const collection = await appSdk
.marketplace(config.org_uid)
.installation()
.fetchAll({ target_uids: config.target_stack, skip })
.catch((error) => {
trace(error, 'error', true);
log(config, `Failed to export marketplace-apps ${formatError(error)}`, 'error');
});

if (collection) {
const { items: apps, count } = collection;
// NOTE Remove all the chain functions
const installation = map(apps, (app) =>
omitBy(app, (val, _key) => {
if (val instanceof Function) return true;
return false;
}),
) as unknown as Installation[];
listOfApps = listOfApps.concat(installation);

if (count - (skip + 50) > 0) {
return await getAllStackSpecificApps(config, skip + 50, listOfApps);
}
}

return listOfApps;
};

export const getDeveloperHubUrl = async (config: ImportConfig): Promise<string> => {
Expand Down Expand Up @@ -90,29 +111,6 @@ export const getConfirmationToCreateApps = async (privateApps: any, config: Impo
}
};

export const createPrivateApp = async (client: ContentstackClient, config: ImportConfig, app: any): Promise<any> => {
const privateApp = omit(app, ['uid']) as any;
return await client
.organization(config.org_uid)
.app()
.create(privateApp)
.catch((error: any) => error);
};

export const installApp = async (
client: ContentstackClient,
config: ImportConfig,
appManifestUid?: string,
mappedUid?: string,
): Promise<any> => {
const appUid = mappedUid || appManifestUid;
return await client
.organization(config.org_uid)
.app(appUid)
.install({ targetUid: config.target_stack, targetType: 'stack' })
.catch((error: any) => error);
};

export const handleNameConflict = async (app: any, appSuffix: number, config: ImportConfig) => {
const appName = config.forceStopMarketplaceAppsPrompt
? getAppName(app.name, appSuffix)
Expand Down Expand Up @@ -189,23 +187,3 @@ export const ifAppAlreadyExist = async (app: any, currentStackApp: any, config:

return updateParam;
};

export const updateAppConfig = async (
client: ContentstackClient,
config: ImportConfig,
app: any,
payload: { configuration: Record<string, unknown>; server_configuration: Record<string, unknown> },
): Promise<any> => {
let installation = client.organization(config.org_uid).app(app?.manifest?.uid).installation(app?.uid);

installation = Object.assign(installation, payload);
return await installation
.update()
.then((data: any) => {
log(config, `${app?.manifest?.name} app config updated successfully.!`, 'success');
})
.catch((error: any) => {
trace(error, 'error', true);
log(config, `Failed to update app config.${formatError(error)}`, 'error');
});
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Agent } from 'node:https';
import { App } from '@contentstack/marketplace-sdk/types/marketplace/app';
import { App, AppData } from '@contentstack/marketplace-sdk/types/marketplace/app';
import { client, ContentstackConfig, ContentstackClient, ContentstackToken } from '@contentstack/marketplace-sdk';

import authHandler from './auth-handler';
Expand Down Expand Up @@ -130,6 +130,13 @@ class MarketplaceSDKInitiator {
export const marketplaceSDKInitiator = new MarketplaceSDKInitiator();
const marketplaceSDKClient: typeof marketplaceSDKInitiator.createAppSDKClient =
marketplaceSDKInitiator.createAppSDKClient.bind(marketplaceSDKInitiator);
export { App, Installation, MarketplaceSDKInitiator, ContentstackMarketplaceConfig, ContentstackMarketplaceClient };
export {
App,
AppData,
Installation,
MarketplaceSDKInitiator,
ContentstackMarketplaceConfig,
ContentstackMarketplaceClient,
};

export default marketplaceSDKClient;
2 changes: 2 additions & 0 deletions packages/contentstack-utilities/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Logger from './logger';
import marketplaceSDKClient, {
App,
AppData,
Installation,
MarketplaceSDKInitiator,
marketplaceSDKInitiator,
Expand Down Expand Up @@ -33,6 +34,7 @@ export * from './add-locale';
// Marketplace SDK export
export {
App,
AppData,
Installation,
marketplaceSDKClient,
MarketplaceSDKInitiator,
Expand Down

0 comments on commit eee2042

Please sign in to comment.