Skip to content
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

ID:FPCO-21845;DONE:100;HOURS:40;Mixmaster changes for partner cli tok… #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/helper/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ export const ALLOWD_ENV = {
fyndx5: 'https://partners.fyndx5.de',
fynd: 'https://partners.fynd.com',
};

export const CLI_EXT_VER_FOR_PTOKEN = '1.9.9'
77 changes: 65 additions & 12 deletions src/lib/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import which from 'which';
import Partner from './Partner';
import Spinner from '../helper/spinner';
import CommandError, { ErrorCodes } from './CommandError';
import ExtensionService from './api/services/extension.service';
import ExtensionService, { RegisterExtensionPayloadNew } from './api/services/extension.service';

import {
Object,
Expand All @@ -20,13 +20,15 @@ import {
} from '../helper/extension_utils';

import { createDirectory, writeFile, readFile } from '../helper/file.utils';
import configStore, { CONFIG_KEYS } from './Config';
import ConfigStore, { CONFIG_KEYS } from './Config';
import { getBaseURL } from './api/services/url';
import {
installNpmPackages,
installJavaPackages,
installPythonDependencies,
} from '../helper/utils';
import semver from 'semver';
import { CLI_EXT_VER_FOR_PTOKEN } from '../helper/constants';

export const NODE_VUE = 'Node + Vue.js';
export const NODE_REACT = 'Node + React.js';
Expand Down Expand Up @@ -158,8 +160,9 @@ export default class Extension {
spinner.fail();
throw new CommandError(error.message);
}

if (isRegisterExtension) {
const notPartnerTokenRemovalVersion = answers.notPartnerTokenPltfmVersion || (await Extension.isPartnerTokenCliVersion()).notPartnerTokenPltfmVersion;
// This variable is just condition matching to these will be only used until v1.10.0 is deployed on all cluster after that will be needed to remove. currently using just for backward compatibility
if (isRegisterExtension && notPartnerTokenRemovalVersion) {
spinner = new Spinner('Registering Extension');
try {
spinner.start();
Expand All @@ -181,6 +184,35 @@ export default class Extension {
throw new CommandError(error.message);
}
}
else if (isRegisterExtension && !notPartnerTokenRemovalVersion){
spinner = new Spinner('Registering Extension');
const data: RegisterExtensionPayloadNew = {
name: answers.name,
base_url: 'http://localdev.fynd.com',
// We are just passing this url as temporary when preview url is called it gets updated with the ngrok url
extention_type: answers.type.toLowerCase(),
}
const { current_user: user } = ConfigStore.get(
CONFIG_KEYS.AUTH_TOKEN,
);
const activeEmail =
user.emails.find((e) => e.active && e.primary)?.email;
data.developed_by_name = `${user.first_name} ${user.last_name}`;
if(activeEmail){
data.contact_email = activeEmail;
}
try{
let extension_data: Object = await ExtensionService.registerExtensionPartners(data);
answers.extension_api_key = extension_data.client_id;
answers.extension_api_secret = extension_data.secret;
answers.base_url = extension_data.launch_url;
spinner.succeed();
}
catch(err){
spinner.fail();
throw new CommandError(err.message);
}
}

spinner = new Spinner('Installing Dependencies');
try {
Expand Down Expand Up @@ -272,6 +304,7 @@ export default class Extension {
public static async initExtensionHandler(options: Object) {
try {
let partner_access_token = getPartnerAccessToken();
const {notPartnerTokenPltfmVersion, platformVersion} = await Extension.isPartnerTokenCliVersion();

let answers: Object = {};

Expand Down Expand Up @@ -359,7 +392,7 @@ export default class Extension {

Extension.checkDependencies(prompt_answers.project_type);

if (!partner_access_token) {
if (!partner_access_token && notPartnerTokenPltfmVersion) {
partner_access_token = (
await Partner.connectHandler({ readOnly: true, ...options })
).partner_access_token;
Expand All @@ -372,6 +405,7 @@ export default class Extension {
...answers,
...prompt_answers,
};
answers.platform_version = platformVersion;

await Extension.createExtension(answers, true);
} catch (error) {
Expand All @@ -383,6 +417,7 @@ export default class Extension {
public static async setupExtensionHandler(options) {
try {
let partner_access_token = getPartnerAccessToken();
const {notPartnerTokenPltfmVersion,platformVersion} = await Extension.isPartnerTokenCliVersion();
let answers: Object;

let questions = [
Expand Down Expand Up @@ -427,12 +462,17 @@ export default class Extension {
},
];

if(!notPartnerTokenPltfmVersion){
questions.splice(1,1);
// Removing the api specret question as it will not be required for platform version greater than v1.10.0
}

answers = { ...answers, ...(await inquirer.prompt(questions)) };
answers.project_url = PROJECT_REPOS[answers.project_type];

Extension.checkDependencies(answers.project_type)

if (!partner_access_token) {
if (!partner_access_token && notPartnerTokenPltfmVersion) {
partner_access_token = (
await Partner.connectHandler({ readOnly: true, ...options })
).partner_access_token;
Expand All @@ -442,11 +482,17 @@ export default class Extension {
let spinner = new Spinner('Verifying API Keys');
try {
spinner.start();
extension_data = await ExtensionService.getExtensionData(
answers.extension_api_key,
answers.extension_api_secret,
partner_access_token,
);
if(notPartnerTokenPltfmVersion){
// This is just been kept for backward compatibility as of now once v1.10.0 gets deployed on all cluster please remove this
extension_data = await ExtensionService.getExtensionData(
answers.extension_api_key,
answers.extension_api_secret,
partner_access_token,
);
}
else{
extension_data = await ExtensionService.getExtensionDataPartners(answers.extension_api_key);
}
if (!extension_data) {
throw new Error();
}
Expand Down Expand Up @@ -480,10 +526,17 @@ export default class Extension {
);
}
}

answers.platform_version = platformVersion;
answers.notPartnerTokenPltfmVersion = notPartnerTokenPltfmVersion;
await Extension.createExtension(answers, false);
} catch (error) {
throw new CommandError(error.message, error.code);
}
}

public static async isPartnerTokenCliVersion () {
const platformVersion = await ExtensionService.getFyndPlatformVersion();
const notPartnerTokenPltfmVersion = semver.lte(platformVersion?.version || '1.0.0', CLI_EXT_VER_FOR_PTOKEN);
return {notPartnerTokenPltfmVersion,platformVersion};
}
}
89 changes: 78 additions & 11 deletions src/lib/ExtensionLaunchURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,37 @@ import ExtensionService from './api/services/extension.service';
import CommandError from './CommandError';
import Spinner from '../helper/spinner';
import path from 'path';
import semver from "semver";
import { CLI_EXT_VER_FOR_PTOKEN } from '../helper/constants';
import Extension from './Extension';

export default class ExtensionLaunchURL {
public static async setLaunchURLHandler(options) {
try {
let partner_access_token = getPartnerAccessToken();
const { notPartnerTokenPltfmVersion } = await Extension.isPartnerTokenCliVersion();

if (!partner_access_token) {
if (!partner_access_token && notPartnerTokenPltfmVersion) {
partner_access_token = (
await Partner.connectHandler({ readOnly: true, ...options })
).partner_access_token;
}

ExtensionLaunchURL.updateLaunchURL(
options.apiKey,
partner_access_token,
options.url,
);

if(notPartnerTokenPltfmVersion){
ExtensionLaunchURL.updateLaunchURL(
options.apiKey,
partner_access_token,
options.url,
);
}
else{
ExtensionLaunchURL.updateLaunchURLPartners(options.apiKey,options.url);
}
} catch (error) {
throw new CommandError(error.message, error.code);
}
}

// This is just been kept for backward compatibility as of now once v1.10.0 gets deployed on all cluster please remove this
public static async updateLaunchURL(
extension_api_key: string,
partner_access_token: string,
Expand Down Expand Up @@ -90,11 +99,64 @@ export default class ExtensionLaunchURL {
}
}

public static async updateLaunchURLPartners(extension_api_key: string,launch_url: string):Promise<void>{
try {
let java_env_file_path = path.join('src', 'main', 'resources', 'application.yml');
let spinner = new Spinner('Updating Launch URL');
try {
spinner.start();
let manualUpdateRequired = false;
await ExtensionService.updateLaunchURLPartners(
extension_api_key,
{ base_url: launch_url },
);

if (fs.existsSync('./.env')) {
let envData = readFile('./.env');
envData = replaceContent(
envData,
`EXTENSION_BASE_URL=.*[\n]`,
`EXTENSION_BASE_URL="${launch_url}"\n`,
);
writeFile('./.env', envData);
}
else if (fs.existsSync(java_env_file_path)) {
let envData = readFile(java_env_file_path);
envData = replaceContent(
envData,
`base_url.*[\n]`,
`base_url: '${launch_url}'\n`,
);
writeFile(java_env_file_path, envData);
}
else {
manualUpdateRequired = true;
}
spinner.succeed();
console.log(
chalk.greenBright(
`Launch url set successfully${
manualUpdateRequired
? '. Please update launch url in your code.'
: ''
}`,
),
);
} catch (error) {
spinner.fail();
throw new CommandError(error.message);
}
} catch (error) {
throw new CommandError(error.message, error.code);
}
}

public static async getLaunchURLHandler(options: Object) {
try {
const {notPartnerTokenPltfmVersion} = await Extension.isPartnerTokenCliVersion();
let partner_access_token = getPartnerAccessToken();

if (!partner_access_token) {
if (!partner_access_token && notPartnerTokenPltfmVersion) {
partner_access_token = (
await Partner.connectHandler({ readOnly: true, ...options })
).partner_access_token;
Expand All @@ -103,11 +165,16 @@ export default class ExtensionLaunchURL {
let spinner = new Spinner('Fetching Launch URL');
try {
spinner.start();
let extension_data =
await ExtensionService.getExtensionDataUsingToken(
let extension_data;
if(notPartnerTokenPltfmVersion){
extension_data = await ExtensionService.getExtensionDataUsingToken(
options.apiKey,
partner_access_token,
);
}
else{
extension_data = await ExtensionService.getExtensionDataPartners(options.apiKey);
}
let launchURL: string = extension_data.base_url;

spinner.succeed();
Expand Down
33 changes: 26 additions & 7 deletions src/lib/ExtensionPreviewURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
} from '../helper/extension_utils';
import Spinner from '../helper/spinner';
import CommandError, { ErrorCodes } from './CommandError';
import semver from "semver";
import { CLI_EXT_VER_FOR_PTOKEN } from '../helper/constants';
import open from 'open';
import Extension from './Extension';

export default class ExtensionPreviewURL {
organizationInfo: Object;
Expand All @@ -32,9 +36,13 @@ export default class ExtensionPreviewURL {
// initialize class instance
const extension = new ExtensionPreviewURL();
extension.options = options;
const {notPartnerTokenPltfmVersion} = await Extension.isPartnerTokenCliVersion();

// get the companyId
extension.organizationInfo = await extension.getOrganizationInfo();
// This is just been kept for backward compatibility as of now once v1.10.0 gets deployed on all cluster please remove this
if(notPartnerTokenPltfmVersion){
extension.organizationInfo = await extension.getOrganizationInfo();
}
if (!extension.options.companyId) {
extension.options.companyId = await extension.getCompanyId();
}
Expand Down Expand Up @@ -63,15 +71,25 @@ export default class ExtensionPreviewURL {
}

// update launch url on partners panel
await ExtensionLaunchURL.updateLaunchURL(
extension.options.apiKey,
extension.organizationInfo.partner_access_token,
extension.publicNgrokURL,
);

// This is just been kept for backward compatibility as of now once v1.10.0 gets deployed on all cluster please remove this
if(notPartnerTokenPltfmVersion){
await ExtensionLaunchURL.updateLaunchURL(
extension.options.apiKey,
extension.organizationInfo.partner_access_token,
extension.publicNgrokURL,
);
}
else{
await ExtensionLaunchURL.updateLaunchURLPartners(
extension.options.apiKey,
extension.publicNgrokURL
);
}
// get preview URL
const previewURL = extension.getPreviewURL();

await open(previewURL);

console.log(
boxen(
chalk.bold.black(
Expand All @@ -93,6 +111,7 @@ export default class ExtensionPreviewURL {
),
);
} catch (error) {
console.log(error, "Error");
throw new CommandError(error.message, error.code);
}
}
Expand Down
Loading
Loading