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

Fixed trouble with environment variables when make a export #4

Merged
merged 3 commits into from
Jun 27, 2024
Merged
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
93 changes: 88 additions & 5 deletions src/commands/profile/export/services/analysis-export.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,98 @@
import axios from "axios";
import prompts from "prompts";
import zlib from "zlib";

import { Account } from "@tago-io/sdk";
import axios from "axios";
import { IExportHolder } from "../types";
import { AnalysisInfo } from "@tago-io/sdk/out/modules/Account/analysis.types";

import { infoMSG } from "../../../../lib/messages";
import { replaceObj } from "../../../../lib/replace-obj";
import { IExportHolder } from "../types";

/**
* Choose one of the values for your Environment Variable
*/
async function choose_variable(key: string, values: string[]) {
const choices: { title: string }[] = [];

for (const value of values) {
choices.push({ title: value });
}

const { variable } = await prompts({
message: `Choice one value of Environment Variable - key "${key}":`,
name: "variable",
type: "autocomplete",
choices,
});

return variable;
}

function separate_variable_with_duplicate_values(export_analysis: AnalysisInfo[], import_analysis: AnalysisInfo[]) {
const values_by_keys: any = {};
for (const item of [...export_analysis, ...import_analysis]) {
if (!item.variables) {
continue;
}
for (const variable of item.variables as unknown as { key: string; value: any }[]) {
// eslint-disable-next-line no-prototype-builtins
if (!values_by_keys.hasOwnProperty(variable.key)) {
values_by_keys[variable.key] = [];
}
if (!values_by_keys[variable.key].includes(variable.value)) {
values_by_keys[variable.key].push(variable.value);
}
}
}

return Object.keys(values_by_keys).map((key) => {
if (values_by_keys[key].length > 1) {
return { key: key, value: values_by_keys[key] };
}
});
}

async function fixEnvironmentVariables(
import_account: Account,
export_analysis: AnalysisInfo[],
import_analysis: AnalysisInfo[],
analysis_info: { id: string; variables: { key: string; value: string } }[]
) {
const variables_with_duplicate_values = separate_variable_with_duplicate_values(export_analysis, import_analysis);

for (const variable of variables_with_duplicate_values) {
if (!variable) {
continue;
}
const variable_value = await choose_variable(variable.key, variable.value);
variable.value = variable_value;
}

for (const analyze_info of analysis_info) {
for (const variable of variables_with_duplicate_values) {
if (!variable) {
continue;
}
(analyze_info.variables as unknown as { key: string; value: any }[]).find((data) => data.key === variable.key)!.value = variable.value;
}
await import_account.analysis.edit(analyze_info.id, {
variables: analyze_info.variables,
});
}
}

async function analysisExport(account: Account, import_account: Account, export_holder: IExportHolder) {
infoMSG("Exporting analysis: started");

const list = await account.analysis
// @ts-expect-error we are looking only for keys
.list({ amount: 99, fields: ["id", "name", "tags", "variables"], filter: { tags: [{ key: "export_id" }] } })
.then((r) => r.reverse());
// @ts-expect-error we are looking only for keys
const list = await account.analysis.list({ amount: 99, fields: ["id", "name", "tags"], filter: { tags: [{ key: "export_id" }] } }).then((r) => r.reverse());
// @ts-expect-error we are looking only for keys
const import_list = await import_account.analysis.list({ amount: 99, fields: ["id", "tags"], filter: { tags: [{ key: "export_id" }] } });
const import_list = await import_account.analysis.list({ amount: 99, fields: ["id", "tags", "variables"], filter: { tags: [{ key: "export_id" }] } });

const analysis_info = [];
for (const { id: analysis_id, name } of list) {
console.info(`Exporting analysis ${name}...`);
const analysis = await account.analysis.info(analysis_id);
Expand All @@ -30,6 +110,7 @@ async function analysisExport(account: Account, import_account: Account, export_
active: new_analysis.active,
variables: new_analysis.variables,
});
analysis_info.push({ id: target_id, variables: new_analysis.variables });
}
const script = await account.analysis.downloadScript(analysis_id);
const script_base64 = await axios
Expand All @@ -43,6 +124,8 @@ async function analysisExport(account: Account, import_account: Account, export_
export_holder.analysis[analysis_id] = target_id;
}

await fixEnvironmentVariables(import_account, list, import_list, analysis_info);

infoMSG("Exporting analysis: finished");
return export_holder;
}
Expand Down