Skip to content

Commit 2f64809

Browse files
feat(editor): Enable source environment push button for project admins (#15527)
1 parent 88caa21 commit 2f64809

File tree

4 files changed

+140
-8
lines changed

4 files changed

+140
-8
lines changed

packages/@n8n/permissions/src/roles/scopes/project-scopes.ee.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const REGULAR_PROJECT_ADMIN_SCOPES: Scope[] = [
3131
'folder:delete',
3232
'folder:list',
3333
'folder:move',
34+
'sourceControl:push',
3435
];
3536

3637
export const PERSONAL_PROJECT_OWNER_SCOPES: Scope[] = [

packages/frontend/@n8n/i18n/src/locales/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,8 @@
21462146
"settings.sourceControl.sync.prompt.error": "Please enter a commit message",
21472147
"settings.sourceControl.button.push": "Push",
21482148
"settings.sourceControl.button.pull": "Pull",
2149+
"settings.sourceControl.button.pull.forbidden": "Only the instance owner or instance admins can pull changes",
2150+
"settings.sourceControl.button.push.forbidden": "You can't push changes from a protected instance",
21492151
"settings.sourceControl.modals.push.title": "Commit and push changes",
21502152
"settings.sourceControl.modals.push.description": "The following will be committed: ",
21512153
"settings.sourceControl.modals.push.description.learnMore": "More info",

packages/frontend/editor-ui/src/components/MainSidebarSourceControl.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import { useSourceControlStore } from '@/stores/sourceControl.store';
1111
import { useUIStore } from '@/stores/ui.store';
1212
import { useRBACStore } from '@/stores/rbac.store';
1313
import { createComponentRenderer } from '@/__tests__/render';
14+
import { useProjectsStore } from '@/stores/projects.store';
1415

1516
let pinia: ReturnType<typeof createTestingPinia>;
1617
let sourceControlStore: ReturnType<typeof useSourceControlStore>;
1718
let uiStore: ReturnType<typeof useUIStore>;
1819
let rbacStore: ReturnType<typeof useRBACStore>;
20+
let projectStore: ReturnType<typeof useProjectsStore>;
1921

2022
const showMessage = vi.fn();
2123
const showError = vi.fn();
@@ -38,6 +40,7 @@ describe('MainSidebarSourceControl', () => {
3840
});
3941

4042
rbacStore = useRBACStore(pinia);
43+
projectStore = useProjectsStore(pinia);
4144
vi.spyOn(rbacStore, 'hasScope').mockReturnValue(true);
4245

4346
sourceControlStore = useSourceControlStore();
@@ -58,8 +61,72 @@ describe('MainSidebarSourceControl', () => {
5861
expect(getByTestId('main-sidebar-source-control')).toBeEmptyDOMElement();
5962
});
6063

64+
describe('when connected as project admin', () => {
65+
beforeEach(() => {
66+
vi.spyOn(rbacStore, 'hasScope').mockReturnValue(false);
67+
vi.spyOn(sourceControlStore, 'preferences', 'get').mockReturnValue({
68+
branchName: 'main',
69+
branches: [],
70+
repositoryUrl: '',
71+
branchReadOnly: false,
72+
branchColor: '#5296D6',
73+
connected: true,
74+
publicKey: '',
75+
});
76+
projectStore.myProjects = [
77+
{
78+
id: '1',
79+
name: 'Test Project',
80+
type: 'team',
81+
scopes: ['sourceControl:push'],
82+
icon: { type: 'emoji', value: '🚀' },
83+
createdAt: '2023-01-01T00:00:00Z',
84+
updatedAt: '2023-01-01T00:00:00Z',
85+
role: 'project:admin',
86+
},
87+
];
88+
});
89+
90+
it('should render the appropriate content', async () => {
91+
const { getByTestId, queryByTestId } = renderComponent({
92+
pinia,
93+
props: { isCollapsed: false },
94+
});
95+
expect(getByTestId('main-sidebar-source-control-connected')).toBeInTheDocument();
96+
expect(queryByTestId('main-sidebar-source-control-setup')).not.toBeInTheDocument();
97+
98+
const pushButton = queryByTestId('main-sidebar-source-control-push');
99+
expect(pushButton).toBeInTheDocument();
100+
expect(pushButton).not.toBeDisabled();
101+
102+
const pullButton = queryByTestId('main-sidebar-source-control-pull');
103+
expect(pullButton).toBeInTheDocument();
104+
expect(pullButton).toBeDisabled();
105+
});
106+
107+
it('should disable push button if branch is read-only', async () => {
108+
vi.spyOn(sourceControlStore, 'preferences', 'get').mockReturnValue({
109+
branchName: 'main',
110+
branches: [],
111+
repositoryUrl: '',
112+
branchReadOnly: true,
113+
branchColor: '#5296D6',
114+
connected: true,
115+
publicKey: '',
116+
});
117+
118+
const { getByTestId } = renderComponent({
119+
pinia,
120+
props: { isCollapsed: false },
121+
});
122+
const pushButton = getByTestId('main-sidebar-source-control-push');
123+
expect(pushButton).toBeDisabled();
124+
});
125+
});
126+
61127
describe('when connected', () => {
62128
beforeEach(() => {
129+
vi.spyOn(rbacStore, 'hasScope').mockReturnValue(true);
63130
vi.spyOn(sourceControlStore, 'preferences', 'get').mockReturnValue({
64131
branchName: 'main',
65132
branches: [],
@@ -78,6 +145,32 @@ describe('MainSidebarSourceControl', () => {
78145
});
79146
expect(getByTestId('main-sidebar-source-control-connected')).toBeInTheDocument();
80147
expect(queryByTestId('main-sidebar-source-control-setup')).not.toBeInTheDocument();
148+
149+
const pushButton = queryByTestId('main-sidebar-source-control-push');
150+
expect(pushButton).toBeInTheDocument();
151+
expect(pushButton).not.toBeDisabled();
152+
153+
const pullButton = queryByTestId('main-sidebar-source-control-pull');
154+
expect(pullButton).toBeInTheDocument();
155+
expect(pullButton).not.toBeDisabled();
156+
});
157+
158+
it('should disable push button if branch is read-only', async () => {
159+
vi.spyOn(sourceControlStore, 'preferences', 'get').mockReturnValue({
160+
branchName: 'main',
161+
branches: [],
162+
repositoryUrl: '',
163+
branchReadOnly: true,
164+
branchColor: '#5296D6',
165+
connected: true,
166+
publicKey: '',
167+
});
168+
const { getByTestId } = renderComponent({
169+
pinia,
170+
props: { isCollapsed: false },
171+
});
172+
const pushButton = getByTestId('main-sidebar-source-control-push');
173+
expect(pushButton).toBeDisabled();
81174
});
82175

83176
it('should show toast error if pull response http status code is not 409', async () => {

packages/frontend/editor-ui/src/components/MainSidebarSourceControl.vue

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { computed, ref } from 'vue';
33
import { createEventBus } from '@n8n/utils/event-bus';
44
import { useI18n } from '@n8n/i18n';
55
import { hasPermission } from '@/utils/rbac/permissions';
6+
import { getResourcePermissions } from '@/permissions';
67
import { useToast } from '@/composables/useToast';
78
import { useLoadingService } from '@/composables/useLoadingService';
89
import { useUIStore } from '@/stores/ui.store';
910
import { useSourceControlStore } from '@/stores/sourceControl.store';
1011
import { SOURCE_CONTROL_PULL_MODAL_KEY, SOURCE_CONTROL_PUSH_MODAL_KEY } from '@/constants';
1112
import { sourceControlEventBus } from '@/event-bus/source-control';
1213
import { notifyUserAboutPullWorkFolderOutcome } from '@/utils/sourceControlUtils';
14+
import { useProjectsStore } from '@/stores/projects.store';
1315
1416
defineProps<{
1517
isCollapsed: boolean;
@@ -22,6 +24,7 @@ const responseStatuses = {
2224
const loadingService = useLoadingService();
2325
const uiStore = useUIStore();
2426
const sourceControlStore = useSourceControlStore();
27+
const projectStore = useProjectsStore();
2528
const toast = useToast();
2629
const i18n = useI18n();
2730
@@ -31,10 +34,26 @@ const tooltipOpenDelay = ref(300);
3134
const currentBranch = computed(() => {
3235
return sourceControlStore.preferences.branchName;
3336
});
37+
38+
// Check if the user has permission to push for at least one project
39+
const hasPushPermission = computed(() => {
40+
return (
41+
hasPermission(['rbac'], { rbac: { scope: 'sourceControl:push' } }) ||
42+
projectStore.myProjects.some(
43+
(project) =>
44+
project.type === 'team' && getResourcePermissions(project?.scopes)?.sourceControl?.push,
45+
)
46+
);
47+
});
48+
49+
const hasPullPermission = computed(() => {
50+
return hasPermission(['rbac'], { rbac: { scope: 'sourceControl:pull' } });
51+
});
52+
3453
const sourceControlAvailable = computed(
3554
() =>
3655
sourceControlStore.isEnterpriseSourceControlEnabled &&
37-
hasPermission(['rbac'], { rbac: { scope: 'sourceControl:manage' } }),
56+
(hasPullPermission.value || hasPushPermission.value),
3857
);
3958
4059
async function pushWorkfolder() {
@@ -113,17 +132,27 @@ async function pullWorkfolder() {
113132
{{ currentBranch }}
114133
</span>
115134
<div :class="{ 'pt-xs': !isCollapsed }">
116-
<n8n-tooltip :disabled="!isCollapsed" :show-after="tooltipOpenDelay" placement="right">
135+
<n8n-tooltip
136+
:disabled="!isCollapsed && hasPullPermission"
137+
:show-after="tooltipOpenDelay"
138+
:placement="isCollapsed ? 'right' : 'top'"
139+
>
117140
<template #content>
118141
<div>
119-
{{ i18n.baseText('settings.sourceControl.button.pull') }}
142+
{{
143+
!hasPullPermission
144+
? i18n.baseText('settings.sourceControl.button.pull.forbidden')
145+
: i18n.baseText('settings.sourceControl.button.pull')
146+
}}
120147
</div>
121148
</template>
122149
<n8n-button
123150
:class="{
124151
'mr-2xs': !isCollapsed,
125-
'mb-2xs': isCollapsed && !sourceControlStore.preferences.branchReadOnly,
152+
'mb-2xs': isCollapsed,
126153
}"
154+
:disabled="!hasPullPermission"
155+
data-test-id="main-sidebar-source-control-pull"
127156
icon="arrow-down"
128157
type="tertiary"
129158
size="mini"
@@ -133,19 +162,26 @@ async function pullWorkfolder() {
133162
/>
134163
</n8n-tooltip>
135164
<n8n-tooltip
136-
v-if="!sourceControlStore.preferences.branchReadOnly"
137-
:disabled="!isCollapsed"
165+
:disabled="
166+
!isCollapsed && !sourceControlStore.preferences.branchReadOnly && hasPushPermission
167+
"
138168
:show-after="tooltipOpenDelay"
139-
placement="right"
169+
:placement="isCollapsed ? 'right' : 'top'"
140170
>
141171
<template #content>
142172
<div>
143-
{{ i18n.baseText('settings.sourceControl.button.push') }}
173+
{{
174+
sourceControlStore.preferences.branchReadOnly || !hasPushPermission
175+
? i18n.baseText('settings.sourceControl.button.push.forbidden')
176+
: i18n.baseText('settings.sourceControl.button.push')
177+
}}
144178
</div>
145179
</template>
146180
<n8n-button
147181
:square="isCollapsed"
148182
:label="isCollapsed ? '' : i18n.baseText('settings.sourceControl.button.push')"
183+
:disabled="sourceControlStore.preferences.branchReadOnly || !hasPushPermission"
184+
data-test-id="main-sidebar-source-control-push"
149185
icon="arrow-up"
150186
type="tertiary"
151187
size="mini"

0 commit comments

Comments
 (0)