Skip to content

Commit cd3a355

Browse files
committed
Revert "Revert "feat(Global Environments): Introduce a new top level file type called global environments. (#7511)""
This reverts commit 8a0f455.
1 parent 8a0f455 commit cd3a355

17 files changed

Lines changed: 1069 additions & 474 deletions

packages/insomnia-smoke-test/tests/smoke/after-response-script-features.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ test.describe('after-response script features tests', async () => {
4646
await expect(statusTag).toContainText('200 OK');
4747

4848
// verify persisted environment
49-
await page.getByLabel('Manage Environments').click();
49+
await page.getByRole('button', { name: 'Manage Environments' }).click();
50+
await page.getByRole('button', { name: 'Manage collection environments' }).click();
5051
const responseBody = page.getByRole('dialog').getByTestId('CodeEditor').locator('.CodeMirror-line');
5152
const rows = await responseBody.allInnerTexts();
5253
const bodyJson = JSON.parse(rows.join(' '));

packages/insomnia-smoke-test/tests/smoke/environment-editor-interactions.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ test.describe('Environment Editor', async () => {
1717
test('create a new environment', async ({ page }) => {
1818
// Create the environment (which will become active on creation)
1919
// await page.getByLabel("Select an environment").click();
20-
await page.getByLabel('Manage Environments').click();
20+
await page.getByRole('button', { name: 'Manage Environments' }).click();
21+
await page.getByRole('button', { name: 'Manage collection environments' }).click();
2122
await page.getByTestId('CreateEnvironmentDropdown').click();
2223
await page.getByRole('menuitemradio', { name: 'Shared Environment' }).press('Enter');
2324
await page.getByRole('row', { name: 'New Environment' }).click();
2425
await page.getByRole('button', { name: 'Close' }).click();
2526

26-
await page.getByRole('button', { name: 'Select an environment' }).press('Enter');
2727
await page.getByRole('option', { name: 'New Environment' }).press('Enter');
28+
await page.getByRole('option', { name: 'New Environment' }).press('Escape');
2829

2930
// Send a request check variables defaulted to base env since new env is empty
3031
await page.getByLabel('Request Collection').getByTestId('New Request').press('Enter');
@@ -37,15 +38,16 @@ test.describe('Environment Editor', async () => {
3738
// rename an existing environment
3839
test('Rename an existing environment', async ({ page }) => {
3940
// Rename the environment
40-
await page.getByLabel('Manage Environments').click();
41+
await page.getByRole('button', { name: 'Manage Environments' }).click();
42+
await page.getByRole('button', { name: 'Manage collection environments' }).click();
4143
await page.getByRole('row', { name: 'ExampleB' }).getByRole('button', { name: 'name' }).dblclick();
4244
await page.getByRole('row', { name: 'ExampleB' }).locator('input').fill('Gandalf');
4345
await page.getByRole('row', { name: 'ExampleB' }).locator('input').press('Enter');
4446

4547
await page.getByRole('button', { name: 'Close' }).click();
4648

47-
await page.getByRole('button', { name: 'Select an environment' }).press('Enter');
4849
await page.getByRole('option', { name: 'Gandalf' }).press('Enter');
50+
await page.getByRole('option', { name: 'Gandalf' }).press('Escape');
4951

5052
// Send a request check variables defaulted to base env since new env is empty
5153
await page.getByLabel('Request Collection').getByTestId('New Request').press('Enter');
@@ -58,7 +60,8 @@ test.describe('Environment Editor', async () => {
5860

5961
test('Add new variables to an existing environment', async ({ page }) => {
6062
// Rename the environment
61-
await page.getByLabel('Manage Environments').click();
63+
await page.getByRole('button', { name: 'Manage Environments' }).click();
64+
await page.getByRole('button', { name: 'Manage collection environments' }).click();
6265
// add a new string environment variable
6366
await page.locator('pre').filter({ hasText: '"exampleNumber": 1111,' }).click();
6467
await page.getByTestId('CodeEditor').getByRole('textbox').press('Enter');
@@ -70,6 +73,7 @@ test.describe('Environment Editor', async () => {
7073

7174
// Open request
7275
await page.getByRole('button', { name: 'Close' }).click();
76+
await page.getByLabel('Manage collection environments').press('Escape');
7377
await page.getByLabel('Request Collection').getByTestId('New Request').press('Enter');
7478

7579
// Add number variable to request body

packages/insomnia-smoke-test/tests/smoke/pre-request-script-features.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ test.describe('pre-request features tests', async () => {
421421
await expect(statusTag).toContainText('200 OK');
422422

423423
// verify persisted environment
424-
await page.getByLabel('Manage Environments').click();
424+
await page.getByRole('button', { name: 'Manage Environments' }).click();
425+
await page.getByRole('button', { name: 'Manage collection environments' }).click();
425426
const responseBody = page.getByRole('dialog').getByTestId('CodeEditor').locator('.CodeMirror-line');
426427
const rows = await responseBody.allInnerTexts();
427428
const bodyJson = JSON.parse(rows.join(' '));

packages/insomnia/src/common/render.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,38 @@ export async function buildRenderContext(
6060
ancestors,
6161
rootEnvironment,
6262
subEnvironment,
63+
rootGlobalEnvironment,
64+
subGlobalEnvironment,
6365
baseContext = {},
6466
}: {
6567
ancestors?: RenderContextAncestor[];
6668
rootEnvironment?: Environment;
6769
subEnvironment?: Environment;
70+
rootGlobalEnvironment?: Environment | null;
71+
subGlobalEnvironment?: Environment | null;
6872
baseContext?: Record<string, any>;
6973
},
7074
) {
7175
const envObjects: Record<string, any>[] = [];
7276

77+
if (rootGlobalEnvironment) {
78+
const ordered = orderedJSON.order(
79+
rootGlobalEnvironment.data,
80+
rootGlobalEnvironment.dataPropertyOrder,
81+
JSON_ORDER_SEPARATOR,
82+
);
83+
envObjects.push(ordered);
84+
}
85+
86+
if (subGlobalEnvironment) {
87+
const ordered = orderedJSON.order(
88+
subGlobalEnvironment.data,
89+
subGlobalEnvironment.dataPropertyOrder,
90+
JSON_ORDER_SEPARATOR,
91+
);
92+
envObjects.push(ordered);
93+
}
94+
7395
// Get root environment keys in correct order
7496
// Then get sub environment keys in correct order
7597
// Then get ancestor (folder) environment keys in correct order
@@ -300,6 +322,8 @@ interface RenderRequest<T extends Request | GrpcRequest | WebSocketRequest> {
300322
interface BaseRenderContextOptions {
301323
environment?: string | Environment;
302324
baseEnvironment?: Environment;
325+
rootGlobalEnvironment?: Environment;
326+
subGlobalEnvironment?: Environment;
303327
purpose?: RenderPurpose;
304328
extraInfo?: ExtraRenderInfo;
305329
ignoreUndefinedEnvVariable?: boolean;
@@ -326,6 +350,29 @@ export async function getRenderContext(
326350
throw new Error('Failed to render. Could not find workspace');
327351
}
328352

353+
const workspaceMeta = await models.workspaceMeta.getByParentId(workspace._id);
354+
355+
let rootGlobalEnvironment: Environment | null = null;
356+
let subGlobalEnvironment: Environment | null = null;
357+
358+
if (workspaceMeta?.activeGlobalEnvironmentId) {
359+
const activeGlobalEnvironment = await models.environment.getById(workspaceMeta.activeGlobalEnvironmentId);
360+
361+
if (activeGlobalEnvironment) {
362+
if (activeGlobalEnvironment?.parentId.startsWith('wrk_')) {
363+
rootGlobalEnvironment = activeGlobalEnvironment;
364+
} else {
365+
subGlobalEnvironment = activeGlobalEnvironment;
366+
367+
const baseGlobalEnvironment = await models.environment.getById(activeGlobalEnvironment.parentId);
368+
369+
if (baseGlobalEnvironment) {
370+
rootGlobalEnvironment = baseGlobalEnvironment;
371+
}
372+
}
373+
}
374+
}
375+
329376
const rootEnvironment = baseEnvironment || await models.environment.getOrCreateForParentId(
330377
workspace ? workspace._id : 'n/a',
331378
);
@@ -361,6 +408,15 @@ export async function getRenderContext(
361408
}
362409

363410
const inKey = templating.NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME;
411+
412+
if (rootGlobalEnvironment) {
413+
getKeySource(rootGlobalEnvironment.data || {}, inKey, 'rootGlobal');
414+
}
415+
416+
if (subGlobalEnvironment) {
417+
getKeySource(subGlobalEnvironment.data || {}, inKey, 'subGlobal');
418+
}
419+
364420
// Get Keys from root environment
365421
getKeySource((rootEnvironment || {}).data, inKey, 'root');
366422

@@ -408,7 +464,14 @@ export async function getRenderContext(
408464
};
409465

410466
// Generate the context we need to render
411-
return buildRenderContext({ ancestors, rootEnvironment, subEnvironment: subEnvironment || undefined, baseContext });
467+
return buildRenderContext({
468+
ancestors,
469+
rootGlobalEnvironment,
470+
subGlobalEnvironment,
471+
rootEnvironment,
472+
subEnvironment: subEnvironment || undefined,
473+
baseContext,
474+
});
412475
}
413476
interface BaseRenderContext {
414477
getMeta: () => {};

packages/insomnia/src/models/workspace-meta.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const canSync = false;
1010
export interface BaseWorkspaceMeta {
1111
activeActivity: string | null;
1212
activeEnvironmentId: string | null;
13+
activeGlobalEnvironmentId: string | null;
1314
activeRequestId: string | null;
1415
activeUnitTestSuiteId: string | null;
1516
cachedGitLastAuthor: string | null;
@@ -30,6 +31,7 @@ export function init(): BaseWorkspaceMeta {
3031
return {
3132
activeActivity: null,
3233
activeEnvironmentId: null,
34+
activeGlobalEnvironmentId: null,
3335
activeRequestId: null,
3436
activeUnitTestSuiteId: null,
3537
cachedGitLastAuthor: null,

packages/insomnia/src/models/workspace.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface BaseWorkspace {
1717
name: string;
1818
description: string;
1919
certificates?: any; // deprecated
20-
scope: 'design' | 'collection' | 'mock-server';
20+
scope: 'design' | 'collection' | 'mock-server' | 'environment';
2121
}
2222

2323
export type WorkspaceScope = BaseWorkspace['scope'];
@@ -26,6 +26,7 @@ export const WorkspaceScopeKeys = {
2626
design: 'design',
2727
collection: 'collection',
2828
mockServer: 'mock-server',
29+
environment: 'environment',
2930
} as const;
3031

3132
export type Workspace = BaseModel & BaseWorkspace;
@@ -46,6 +47,10 @@ export const isMockServer = (workspace: Pick<Workspace, 'scope'>) => (
4647
workspace.scope === WorkspaceScopeKeys.mockServer
4748
);
4849

50+
export const isEnvironment = (workspace: Pick<Workspace, 'scope'>) => (
51+
workspace.scope === WorkspaceScopeKeys.environment
52+
);
53+
4954
export const init = (): BaseWorkspace => ({
5055
name: `New ${strings.collection.singular}`,
5156
description: '',
@@ -143,7 +148,8 @@ type MigrationWorkspace = Merge<Workspace, { scope: OldScopeTypes | Workspace['s
143148
function _migrateScope(workspace: MigrationWorkspace) {
144149
if (workspace.scope === WorkspaceScopeKeys.design
145150
|| workspace.scope === WorkspaceScopeKeys.collection
146-
|| workspace.scope === WorkspaceScopeKeys.mockServer) {
151+
|| workspace.scope === WorkspaceScopeKeys.mockServer
152+
|| workspace.scope === WorkspaceScopeKeys.environment) {
147153
return workspace as Workspace;
148154
}
149155
// designer and spec => design, unset => collection
@@ -175,6 +181,8 @@ export const scopeToActivity = (scope: WorkspaceScope) => {
175181
return ACTIVITY_SPEC;
176182
case WorkspaceScopeKeys.mockServer:
177183
return 'mock-server';
184+
case WorkspaceScopeKeys.environment:
185+
return 'environment';
178186
default:
179187
return ACTIVITY_DEBUG;
180188
}

0 commit comments

Comments
 (0)