From dba3f44bc00de68113cc98db9afc6267f56ec04c Mon Sep 17 00:00:00 2001 From: Csaba Tuncsik Date: Tue, 6 Jun 2023 11:23:53 +0200 Subject: [PATCH] fix(editor): Adding branch color (#6380) * fix(editor): Adding branch color * fix(editor): backend refactor preferences update * fix(editor): frontend refactor preferences update --- .../types/versionControlPreferences.ts | 10 ++-- .../versionControl.controller.ee.ts | 46 +++++++++++++------ .../versionControl.service.ee.ts | 36 +-------------- .../versionControlPreferences.service.ee.ts | 9 ---- packages/editor-ui/src/api/versionControl.ts | 32 ++++--------- .../components/MainSidebarVersionControl.vue | 12 +++-- .../src/stores/versionControl.store.ts | 24 ++++------ .../src/views/SettingsVersionControl.vue | 18 ++++---- 8 files changed, 75 insertions(+), 112 deletions(-) diff --git a/packages/cli/src/environments/versionControl/types/versionControlPreferences.ts b/packages/cli/src/environments/versionControl/types/versionControlPreferences.ts index c17e334a7c..829cd0a176 100644 --- a/packages/cli/src/environments/versionControl/types/versionControlPreferences.ts +++ b/packages/cli/src/environments/versionControl/types/versionControlPreferences.ts @@ -44,12 +44,12 @@ export class VersionControlPreferences { ): VersionControlPreferences { return new VersionControlPreferences({ connected: preferences.connected ?? defaultPreferences.connected, - authorEmail: preferences.authorEmail ?? defaultPreferences.authorEmail, - authorName: preferences.authorName ?? defaultPreferences.authorName, - branchName: preferences.branchName ?? defaultPreferences.branchName, - branchColor: preferences.branchColor ?? defaultPreferences.branchColor, - branchReadOnly: preferences.branchReadOnly ?? defaultPreferences.branchReadOnly, repositoryUrl: preferences.repositoryUrl ?? defaultPreferences.repositoryUrl, + authorName: preferences.authorName ?? defaultPreferences.authorName, + authorEmail: preferences.authorEmail ?? defaultPreferences.authorEmail, + branchName: preferences.branchName ?? defaultPreferences.branchName, + branchReadOnly: preferences.branchReadOnly ?? defaultPreferences.branchReadOnly, + branchColor: preferences.branchColor ?? defaultPreferences.branchColor, }); } } diff --git a/packages/cli/src/environments/versionControl/versionControl.controller.ee.ts b/packages/cli/src/environments/versionControl/versionControl.controller.ee.ts index a85daa2ca7..65da1b773e 100644 --- a/packages/cli/src/environments/versionControl/versionControl.controller.ee.ts +++ b/packages/cli/src/environments/versionControl/versionControl.controller.ee.ts @@ -1,4 +1,4 @@ -import { Authorized, Get, Post, RestController } from '@/decorators'; +import { Authorized, Get, Post, Patch, RestController } from '@/decorators'; import { versionControlLicensedMiddleware, versionControlLicensedAndEnabledMiddleware, @@ -83,10 +83,38 @@ export class VersionControlController { } @Authorized(['global', 'owner']) - @Post('/set-read-only', { middlewares: [versionControlLicensedMiddleware] }) - async setReadOnly(req: VersionControlRequest.SetReadOnly) { + @Patch('/preferences', { middlewares: [versionControlLicensedMiddleware] }) + async updatePreferences(req: VersionControlRequest.UpdatePreferences) { try { - this.versionControlPreferencesService.setBranchReadOnly(req.body.branchReadOnly); + const sanitizedPreferences: Partial = { + ...req.body, + initRepo: false, + connected: undefined, + publicKey: undefined, + repositoryUrl: undefined, + authorName: undefined, + authorEmail: undefined, + }; + const currentPreferences = this.versionControlPreferencesService.getPreferences(); + await this.versionControlPreferencesService.validateVersionControlPreferences( + sanitizedPreferences, + ); + if ( + sanitizedPreferences.branchName && + sanitizedPreferences.branchName !== currentPreferences.branchName + ) { + await this.versionControlService.setBranch(sanitizedPreferences.branchName); + } + if (sanitizedPreferences.branchColor || sanitizedPreferences.branchReadOnly !== undefined) { + await this.versionControlPreferencesService.setPreferences( + { + branchColor: sanitizedPreferences.branchColor, + branchReadOnly: sanitizedPreferences.branchReadOnly, + }, + true, + ); + } + await this.versionControlService.init(); return this.versionControlPreferencesService.getPreferences(); } catch (error) { throw new BadRequestError((error as { message: string }).message); @@ -113,16 +141,6 @@ export class VersionControlController { } } - @Authorized(['global', 'owner']) - @Post('/set-branch', { middlewares: [versionControlLicensedMiddleware] }) - async setBranch(req: VersionControlRequest.SetBranch) { - try { - return await this.versionControlService.setBranch(req.body.branch); - } catch (error) { - throw new BadRequestError((error as { message: string }).message); - } - } - @Authorized(['global', 'owner']) @Post('/push-workfolder', { middlewares: [versionControlLicensedAndEnabledMiddleware] }) async pushWorkfolder( diff --git a/packages/cli/src/environments/versionControl/versionControl.service.ee.ts b/packages/cli/src/environments/versionControl/versionControl.service.ee.ts index 7a37cfe357..a2c2a30709 100644 --- a/packages/cli/src/environments/versionControl/versionControl.service.ee.ts +++ b/packages/cli/src/environments/versionControl/versionControl.service.ee.ts @@ -172,7 +172,7 @@ export class VersionControlService { async setBranch(branch: string): Promise<{ branches: string[]; currentBranch: string }> { await this.versionControlPreferencesService.setPreferences({ branchName: branch, - connected: true, + connected: branch?.length > 0, }); return this.gitService.setBranch(branch); } @@ -216,40 +216,6 @@ export class VersionControlService { }); } - // async pushWorkfolder( - // options: VersionControlPushWorkFolder, - // ): Promise { - // await this.gitService.fetch(); - // await this.export(); // refresh workfolder - // await this.stage(options); - // await this.gitService.commit(options.message ?? 'Updated Workfolder'); - // return this.gitService.push({ - // branch: this.versionControlPreferencesService.getBranchName(), - // force: options.force ?? false, - // }); - // } - - // TODO: Alternate implementation for pull - // async pullWorkfolder( - // options: VersionControllPullOptions, - // ): Promise { - // const diffResult = await this.getStatus(); - // const possibleConflicts = diffResult?.filter((file) => file.conflict); - // if (possibleConflicts?.length > 0 || options.force === true) { - // await this.unstage(); - // if (options.force === true) { - // return this.resetWorkfolder(options); - // } else { - // return diffResult; - // } - // } - // const pullResult = await this.gitService.pull(); - // if (options.importAfterPull) { - // return this.import(options); - // } - // return pullResult; - // } - async pullWorkfolder( options: VersionControllPullOptions, ): Promise { diff --git a/packages/cli/src/environments/versionControl/versionControlPreferences.service.ee.ts b/packages/cli/src/environments/versionControl/versionControlPreferences.service.ee.ts index 6233f53f97..c17ba8d7f6 100644 --- a/packages/cli/src/environments/versionControl/versionControlPreferences.service.ee.ts +++ b/packages/cli/src/environments/versionControl/versionControlPreferences.service.ee.ts @@ -114,19 +114,10 @@ export class VersionControlPreferencesService { return this.versionControlPreferences; } - setBranchReadOnly(branchReadOnly: boolean): void { - this._versionControlPreferences.branchReadOnly = branchReadOnly; - } - async validateVersionControlPreferences( preferences: Partial, allowMissingProperties = true, ): Promise { - if (this.isVersionControlConnected()) { - if (preferences.repositoryUrl !== this._versionControlPreferences.repositoryUrl) { - throw new Error('Cannot change repository while connected'); - } - } const preferencesObject = new VersionControlPreferences(preferences); const validationResult = await validate(preferencesObject, { forbidUnknownValues: false, diff --git a/packages/editor-ui/src/api/versionControl.ts b/packages/editor-ui/src/api/versionControl.ts index 299a3c3f82..a526ced47b 100644 --- a/packages/editor-ui/src/api/versionControl.ts +++ b/packages/editor-ui/src/api/versionControl.ts @@ -9,6 +9,14 @@ import type { IDataObject } from 'n8n-workflow'; const versionControlApiRoot = '/version-control'; +const createPreferencesRequestFn = + (method: 'POST' | 'PATCH') => + async ( + context: IRestApiContext, + preferences: Partial, + ): Promise => + makeRestApiRequest(context, method, `${versionControlApiRoot}/preferences`, preferences); + export const pushWorkfolder = async ( context: IRestApiContext, data: IDataObject, @@ -29,19 +37,8 @@ export const getBranches = async ( return makeRestApiRequest(context, 'GET', `${versionControlApiRoot}/get-branches`); }; -export const setBranch = async ( - context: IRestApiContext, - branch: string, -): Promise<{ branches: string[]; currentBranch: string }> => { - return makeRestApiRequest(context, 'POST', `${versionControlApiRoot}/set-branch`, { branch }); -}; - -export const setPreferences = async ( - context: IRestApiContext, - preferences: Partial, -): Promise => { - return makeRestApiRequest(context, 'POST', `${versionControlApiRoot}/preferences`, preferences); -}; +export const savePreferences = createPreferencesRequestFn('POST'); +export const updatePreferences = createPreferencesRequestFn('PATCH'); export const getPreferences = async ( context: IRestApiContext, @@ -68,15 +65,6 @@ export const disconnect = async ( }); }; -export const setBranchReadonly = async ( - context: IRestApiContext, - branchReadOnly: boolean, -): Promise => { - return makeRestApiRequest(context, 'POST', `${versionControlApiRoot}/set-read-only`, { - branchReadOnly, - }); -}; - export const generateKeyPair = async (context: IRestApiContext): Promise => { return makeRestApiRequest(context, 'POST', `${versionControlApiRoot}/generate-key-pair`); }; diff --git a/packages/editor-ui/src/components/MainSidebarVersionControl.vue b/packages/editor-ui/src/components/MainSidebarVersionControl.vue index 7566380edf..5d4c0ecd74 100644 --- a/packages/editor-ui/src/components/MainSidebarVersionControl.vue +++ b/packages/editor-ui/src/components/MainSidebarVersionControl.vue @@ -71,7 +71,10 @@ async function pullWorkfolder() {