Extract base environment (#149)

This commit is contained in:
Gregory Schier
2024-12-21 05:44:55 -08:00
committed by GitHub
parent ecabe9b6ef
commit dd8ccfe21f
28 changed files with 425 additions and 387 deletions

View File

@@ -7233,19 +7233,15 @@ function pluginHookImport(ctx, contents) {
};
const workspacesToImport = parsed.resources.filter(isWorkspace);
for (const workspaceToImport of workspacesToImport) {
const baseEnvironment = parsed.resources.find(
(r) => isEnvironment(r) && r.parentId === workspaceToImport._id
);
resources.workspaces.push({
id: convertId(workspaceToImport._id),
createdAt: new Date(workspacesToImport.created ?? Date.now()).toISOString().replace("Z", ""),
updatedAt: new Date(workspacesToImport.updated ?? Date.now()).toISOString().replace("Z", ""),
model: "workspace",
name: workspaceToImport.name,
variables: baseEnvironment ? parseVariables(baseEnvironment.data) : []
name: workspaceToImport.name
});
const environmentsToImport = parsed.resources.filter(
(r) => isEnvironment(r) && r.parentId === baseEnvironment?._id
(r) => isEnvironment(r)
);
resources.environments.push(
...environmentsToImport.map((r) => importEnvironment(r, workspaceToImport._id))
@@ -7394,13 +7390,6 @@ function importHttpRequest(r, workspaceId, sortPriority = 0) {
})).filter(({ name, value }) => name !== "" || value !== "")
};
}
function parseVariables(data) {
return Object.entries(data).map(([name, value]) => ({
enabled: true,
name,
value: `${value}`
}));
}
function convertSyntax(variable) {
if (!isJSString(variable)) return variable;
return variable.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}");

View File

@@ -145878,13 +145878,20 @@ function pluginHookImport(_ctx, contents) {
model: "workspace",
id: generateId("workspace"),
name: info.name || "Postman Import",
description: info.description?.content ?? info.description ?? "",
description: info.description?.content ?? info.description ?? ""
};
exportResources.workspaces.push(workspace);
const environment = {
model: "environment",
id: generateId("environment"),
name: "Global Variables",
workspaceId: workspace.id,
variables: root.variable?.map((v) => ({
name: v.key,
value: v.value
})) ?? []
};
exportResources.workspaces.push(workspace);
exportResources.environments.push(environment);
const importItem = (v, folderId = null) => {
if (typeof v.name === "string" && Array.isArray(v.item)) {
const folder = {

View File

@@ -45,13 +45,20 @@ function pluginHookImport(_ctx, contents) {
model: "workspace",
id: generateId("workspace"),
name: info.name || "Postman Import",
description: info.description?.content ?? info.description ?? "",
description: info.description?.content ?? info.description ?? ""
};
exportResources.workspaces.push(workspace);
const environment = {
model: "environment",
id: generateId("environment"),
name: "Global Variables",
workspaceId: workspace.id,
variables: root.variable?.map((v) => ({
name: v.key,
value: v.value
})) ?? []
};
exportResources.workspaces.push(workspace);
exportResources.environments.push(environment);
const importItem = (v, folderId = null) => {
if (typeof v.name === "string" && Array.isArray(v.item)) {
const folder = {

View File

@@ -41,6 +41,24 @@ function pluginHookImport(_ctx, contents) {
parsed.resources.httpRequests = parsed.resources.requests;
delete parsed.resources["requests"];
}
for (const workspace of parsed.resources.workspaces ?? []) {
if ("variables" in workspace) {
const baseEnvironment = {
id: `GENERATE_ID::base_env_${workspace["id"]}`,
name: "Global Variables",
variables: workspace.variables,
workspaceId: workspace.id
};
parsed.resources.environments = parsed.resources.environments ?? [];
parsed.resources.environments.push(baseEnvironment);
delete workspace.variables;
for (const environment of parsed.resources.environments) {
if (environment.workspaceId === workspace.id && environment.id !== baseEnvironment.id) {
environment.environmentId = baseEnvironment.id;
}
}
}
}
return { resources: parsed.resources };
}
function isJSObject(obj) {