From f9b298771676ec2935dc537386ea33702ade94dd Mon Sep 17 00:00:00 2001 From: "n8n-cat-bot[bot]" <283985454+n8n-cat-bot[bot]@users.noreply.github.com> Date: Thu, 24 Sep 2026 17:42:06 +0000 Subject: [PATCH] chore: Variable scope shows as global until refresh (#39479) Co-authored-by: n8n-cat-bot[bot] --- .../environments.ee/environments.store.ts | 27 ++++++++++-------- .../environments.ee/environments.test.ts | 28 +++++++++++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.store.ts b/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.store.ts index ea6680b88fb..dc3631e6719 100644 --- a/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.store.ts +++ b/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.store.ts @@ -44,14 +44,22 @@ export const useEnvironmentsStore = defineStore('environments', () => { return data; } + function setProjectScope(data: EnvironmentVariable, projectId?: string | null) { + if (!projectId) return; + + const project = + projectStore.availableProjects.find((p) => p.id === projectId) ?? + projectStore.myProjects.find((p) => p.id === projectId) ?? + (projectStore.personalProject?.id === projectId ? projectStore.personalProject : undefined) ?? + (projectStore.currentProject?.id === projectId ? projectStore.currentProject : undefined); + if (project) { + data.project = { id: project.id, name: project.name ?? '' }; + } + } + async function createVariable(variable: CreateEnvironmentVariable) { const data = await environmentsApi.createVariable(rootStore.restApiContext, variable); - if (variable.projectId) { - const project = projectStore.availableProjects?.find((p) => p.id === variable.projectId); - if (project) { - data.project = { ...project, name: project?.name ?? '' }; - } - } + setProjectScope(data, variable.projectId); allVariables.value.unshift(data); return data; @@ -59,12 +67,7 @@ export const useEnvironmentsStore = defineStore('environments', () => { async function updateVariable(variable: UpdateEnvironmentVariable) { const data = await environmentsApi.updateVariable(rootStore.restApiContext, variable); - if (variable.projectId) { - const project = projectStore.availableProjects?.find((p) => p.id === variable.projectId); - if (project) { - data.project = { ...project, name: project?.name ?? '' }; - } - } + setProjectScope(data, variable.projectId); allVariables.value = allVariables.value.map((v) => (v.id === data.id ? data : v)); return data; diff --git a/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.test.ts b/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.test.ts index 006a3460fc9..0389f0ba2ee 100644 --- a/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.test.ts +++ b/packages/frontend/editor-ui/src/features/settings/environments.ee/environments.test.ts @@ -2,6 +2,7 @@ import { afterAll, beforeAll } from 'vitest'; import { setActivePinia, createPinia } from 'pinia'; import { setupServer } from '@/__tests__/server'; import { useEnvironmentsStore } from './environments.store'; +import * as environmentsApi from './environments.api'; import type { EnvironmentVariable } from './environments.types'; import type { Project } from '@/features/collaboration/projects/projects.types'; import { useProjectsStore } from '@/features/collaboration/projects/projects.store'; @@ -98,6 +99,33 @@ describe('environments.store', () => { expect(environmentsStore.variables).toHaveLength(recordsCount + 1); expect(environmentsStore.variables[0]).toMatchObject(variable); }); + + it('keeps the selected project scope on a new variable before refresh (LIGO-868)', async () => { + const project = { id: 'personal-project', name: 'Personal Project' }; + const variable = { id: 'new-variable', key: 'NEW_VAR', value: 'value' }; + const projectStore = useProjectsStore(); + projectStore.personalProject = { + ...project, + type: 'personal', + scopes: ['projectVariable:create'], + } as Project; + const environmentsStore = useEnvironmentsStore(); + + vi.spyOn(environmentsApi, 'createVariable').mockResolvedValueOnce(variable); + vi.spyOn(environmentsApi, 'getVariables').mockResolvedValueOnce([{ ...variable, project }]); + + await environmentsStore.createVariable({ + key: variable.key, + value: variable.value, + projectId: project.id, + }); + + // The table uses this project to label the new row instead of Global. + expect.soft(environmentsStore.variables[0].project).toEqual(project); + + await environmentsStore.fetchAllVariables(); + expect(environmentsStore.variables[0].project).toEqual(project); + }); }); describe('updateVariable()', () => {