Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Manage version control settings #6079

Merged
merged 6 commits into from
Apr 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
validate preferences with class-validator
  • Loading branch information
flipswitchingmonkey committed Apr 24, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 2694afc663232f74ed24f0c9083f3d4067e70388
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { IsBoolean, IsEmail, IsHexColor, IsOptional, IsString, IsUrl } from 'class-validator';

export class VersionControlPreferences {
constructor(preferences: Partial<VersionControlPreferences> | undefined = undefined) {
if (preferences) Object.assign(this, preferences);
}

@IsBoolean()
connected: boolean;

@IsString()
@IsUrl()
repositoryUrl: string;

@IsString()
authorName: string;

@IsString()
@IsEmail()
authorEmail: string;

@@ -21,7 +23,6 @@ export class VersionControlPreferences {
@IsBoolean()
branchReadOnly: boolean;

@IsString()
@IsHexColor()
branchColor: string;

Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ export class VersionControlController {
privateKey: undefined,
publicKey: undefined,
};
await this.versionControlService.validateVersionControlPreferences(sanitizedPreferences);
return this.versionControlService.setPreferences(sanitizedPreferences);
}

Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ import { VersionControlPreferences } from './types/versionControlPreferences';
import { VERSION_CONTROL_PREFERENCES_DB_KEY } from './constants';
import * as Db from '@/Db';
import { jsonParse, LoggerProxy } from 'n8n-workflow';
import type { ValidationError } from 'class-validator';
import { validate } from 'class-validator';

@Service()
export class VersionControlService {
@@ -44,6 +46,24 @@ export class VersionControlService {
return keyPair;
}

async validateVersionControlPreferences(
preferences: Partial<VersionControlPreferences>,
): Promise<ValidationError[]> {
const preferencesObject = new VersionControlPreferences(preferences);
const validationResult = await validate(preferencesObject, {
forbidUnknownValues: false,
skipMissingProperties: true,
stopAtFirstError: false,
validationError: { target: false },
});
if (validationResult.length > 0) {
throw new Error(`Invalid version control preferences: ${JSON.stringify(validationResult)}`);
}
// TODO: if repositoryUrl is changed, check if it is valid
// TODO: if branchName is changed, check if it is valid
return validationResult;
}

async setPreferences(
preferences: Partial<VersionControlPreferences>,
saveToDb = true,