mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-17 13:39:44 +02:00
chore: Variable scope shows as global until refresh (#39479)
Co-authored-by: n8n-cat-bot[bot] <n8n-cat-bot[bot]@users.noreply.github.com>
This commit is contained in:
parent
0347de1f22
commit
f9b2987716
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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()', () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user