mirror of
https://github.com/n8n-io/n8n.git
synced 2026-06-03 18:27:09 +02:00
* initial telemetry setup and adjusted pull return * quicksave before merge * feat: add conflicting workflow list to pull modal * feat: update source control pull modal * fix: fix linting issue * feat: add Enter keydown event for submitting source control push modal (no-changelog) feat: add Enter keydown event for submitting source control push modal * quicksave * user workflow table for export * improve telemetry data * pull api telemetry * fix lint * Copy tweaks. * remove authorName and authorEmail and pick from user * rename owners.json to workflow_owners.json * ignore credential conflicts on pull * feat: several push/pull flow changes and design update * pull and push return same data format * fix: add One last step toast for successful pull * feat: add up to date pull toast * fix: add proper Learn more link for push and pull modals * do not await tracking being sent * fix import * fix await * add more sourcecontrolfile status * Minor copy tweak for "More info". * Minor copy tweak for "More info". * ignore variable_stub conflicts on pull * ignore whitespace differences * do not show remote workflows that are not yet created * fix telemetry * fix toast when pulling deleted wf * lint fix * refactor and make some imports dynamic * fix variable edit validation * fix telemetry response * improve telemetry * fix unintenional delete commit * fix status unknown issue * fix up to date toast * do not export active state and reapply versionid * use update instead of upsert * fix: show all workflows when clicking push to git * feat: update Up to date pull translation * fix: update read only env checks * do not update versionid of only active flag changes * feat: prevent access to new workflow and templates import when read only env * feat: send only active state and version if workflow state is not dirty * fix: Detect when only active state has changed and prevent generation a new version ID * feat: improve readonly env messages * make getPreferences public * fix telemetry issue * fix: add partial workflow update based on dirty state when changing active state * update unit tests * fix: remove unsaved changes check in readOnlyEnv * fix: disable push to git button when read onyl env * fix: update readonly toast duration * fix: fix pinning and title input in protected mode * initial commit (NOT working) * working push * cleanup and implement pull * fix getstatus * update import to new method * var and tag diffs are no conflicts * only show pull conflict for workflows * refactor and ignore faulty credentials * add sanitycheck for missing git folder * prefer fetch over pull and limit depth to 1 * back to pull... * fix setting branch on initial connect * fix test * remove clean workfolder * refactor: Remove some unnecessary code * Fixed links to docs. * fix getstatus query params * lint fix * dialog to show local and remote name on conflict * only show remote name on conflict * fix credential expression export * fix: Broken test * dont show toast on pull with empty var/tags and refactor * apply frontend changes from old branch * fix tag with same name import * fix buttons shown for non instance owners * prepare local storage key for removal * refactor: Change wording on pushing and pulling * refactor: Change menu item * test: Fix broken test * Update packages/cli/src/environments/sourceControl/types/sourceControlPushWorkFolder.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> --------- Co-authored-by: Alex Grozav <alex@grozav.com> Co-authored-by: Giulio Andreini <g.andreini@gmail.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
192 lines
5.9 KiB
TypeScript
192 lines
5.9 KiB
TypeScript
import { Service } from 'typedi';
|
|
import { SourceControlPreferences } from './types/sourceControlPreferences';
|
|
import type { ValidationError } from 'class-validator';
|
|
import { validate } from 'class-validator';
|
|
import { readFileSync as fsReadFileSync, existsSync as fsExistsSync } from 'fs';
|
|
import { writeFile as fsWriteFile, rm as fsRm } from 'fs/promises';
|
|
import {
|
|
generateSshKeyPair,
|
|
isSourceControlLicensed,
|
|
sourceControlFoldersExistCheck,
|
|
} from './sourceControlHelper.ee';
|
|
import { UserSettings } from 'n8n-core';
|
|
import { LoggerProxy, jsonParse } from 'n8n-workflow';
|
|
import * as Db from '@/Db';
|
|
import {
|
|
SOURCE_CONTROL_SSH_FOLDER,
|
|
SOURCE_CONTROL_GIT_FOLDER,
|
|
SOURCE_CONTROL_SSH_KEY_NAME,
|
|
SOURCE_CONTROL_PREFERENCES_DB_KEY,
|
|
} from './constants';
|
|
import path from 'path';
|
|
|
|
@Service()
|
|
export class SourceControlPreferencesService {
|
|
private _sourceControlPreferences: SourceControlPreferences = new SourceControlPreferences();
|
|
|
|
private sshKeyName: string;
|
|
|
|
private sshFolder: string;
|
|
|
|
private gitFolder: string;
|
|
|
|
constructor() {
|
|
const userFolder = UserSettings.getUserN8nFolderPath();
|
|
this.sshFolder = path.join(userFolder, SOURCE_CONTROL_SSH_FOLDER);
|
|
this.gitFolder = path.join(userFolder, SOURCE_CONTROL_GIT_FOLDER);
|
|
this.sshKeyName = path.join(this.sshFolder, SOURCE_CONTROL_SSH_KEY_NAME);
|
|
}
|
|
|
|
public get sourceControlPreferences(): SourceControlPreferences {
|
|
return {
|
|
...this._sourceControlPreferences,
|
|
connected: this._sourceControlPreferences.connected ?? false,
|
|
publicKey: this.getPublicKey(),
|
|
};
|
|
}
|
|
|
|
// merge the new preferences with the existing preferences when setting
|
|
public set sourceControlPreferences(preferences: Partial<SourceControlPreferences>) {
|
|
this._sourceControlPreferences = SourceControlPreferences.merge(
|
|
preferences,
|
|
this._sourceControlPreferences,
|
|
);
|
|
}
|
|
|
|
public isSourceControlSetup() {
|
|
return (
|
|
this.isSourceControlLicensedAndEnabled() &&
|
|
this.getPreferences().repositoryUrl &&
|
|
this.getPreferences().branchName
|
|
);
|
|
}
|
|
|
|
getPublicKey(): string {
|
|
try {
|
|
return fsReadFileSync(this.sshKeyName + '.pub', { encoding: 'utf8' });
|
|
} catch (error) {
|
|
LoggerProxy.error(`Failed to read public key: ${(error as Error).message}`);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
hasKeyPairFiles(): boolean {
|
|
return fsExistsSync(this.sshKeyName) && fsExistsSync(this.sshKeyName + '.pub');
|
|
}
|
|
|
|
async deleteKeyPairFiles(): Promise<void> {
|
|
try {
|
|
await fsRm(this.sshFolder, { recursive: true });
|
|
} catch (error) {
|
|
LoggerProxy.error(`Failed to delete ssh folder: ${(error as Error).message}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Will generate an ed25519 key pair and save it to the database and the file system
|
|
* Note: this will overwrite any existing key pair
|
|
*/
|
|
async generateAndSaveKeyPair(): Promise<SourceControlPreferences> {
|
|
sourceControlFoldersExistCheck([this.gitFolder, this.sshFolder]);
|
|
const keyPair = await generateSshKeyPair('ed25519');
|
|
if (keyPair.publicKey && keyPair.privateKey) {
|
|
try {
|
|
await fsWriteFile(this.sshKeyName + '.pub', keyPair.publicKey, {
|
|
encoding: 'utf8',
|
|
mode: 0o666,
|
|
});
|
|
await fsWriteFile(this.sshKeyName, keyPair.privateKey, { encoding: 'utf8', mode: 0o600 });
|
|
} catch (error) {
|
|
throw Error(`Failed to save key pair: ${(error as Error).message}`);
|
|
}
|
|
}
|
|
return this.getPreferences();
|
|
}
|
|
|
|
isBranchReadOnly(): boolean {
|
|
return this._sourceControlPreferences.branchReadOnly;
|
|
}
|
|
|
|
isSourceControlConnected(): boolean {
|
|
return this.sourceControlPreferences.connected;
|
|
}
|
|
|
|
isSourceControlLicensedAndEnabled(): boolean {
|
|
return this.isSourceControlConnected() && isSourceControlLicensed();
|
|
}
|
|
|
|
getBranchName(): string {
|
|
return this.sourceControlPreferences.branchName;
|
|
}
|
|
|
|
getPreferences(): SourceControlPreferences {
|
|
return this.sourceControlPreferences;
|
|
}
|
|
|
|
async validateSourceControlPreferences(
|
|
preferences: Partial<SourceControlPreferences>,
|
|
allowMissingProperties = true,
|
|
): Promise<ValidationError[]> {
|
|
const preferencesObject = new SourceControlPreferences(preferences);
|
|
const validationResult = await validate(preferencesObject, {
|
|
forbidUnknownValues: false,
|
|
skipMissingProperties: allowMissingProperties,
|
|
stopAtFirstError: false,
|
|
validationError: { target: false },
|
|
});
|
|
if (validationResult.length > 0) {
|
|
throw new Error(`Invalid source control preferences: ${JSON.stringify(validationResult)}`);
|
|
}
|
|
return validationResult;
|
|
}
|
|
|
|
async setPreferences(
|
|
preferences: Partial<SourceControlPreferences>,
|
|
saveToDb = true,
|
|
): Promise<SourceControlPreferences> {
|
|
sourceControlFoldersExistCheck([this.gitFolder, this.sshFolder]);
|
|
if (!this.hasKeyPairFiles()) {
|
|
LoggerProxy.debug('No key pair files found, generating new pair');
|
|
await this.generateAndSaveKeyPair();
|
|
}
|
|
this.sourceControlPreferences = preferences;
|
|
if (saveToDb) {
|
|
const settingsValue = JSON.stringify(this._sourceControlPreferences);
|
|
try {
|
|
await Db.collections.Settings.save({
|
|
key: SOURCE_CONTROL_PREFERENCES_DB_KEY,
|
|
value: settingsValue,
|
|
loadOnStartup: true,
|
|
});
|
|
} catch (error) {
|
|
throw new Error(`Failed to save source control preferences: ${(error as Error).message}`);
|
|
}
|
|
}
|
|
return this.sourceControlPreferences;
|
|
}
|
|
|
|
async loadFromDbAndApplySourceControlPreferences(): Promise<
|
|
SourceControlPreferences | undefined
|
|
> {
|
|
const loadedPreferences = await Db.collections.Settings.findOne({
|
|
where: { key: SOURCE_CONTROL_PREFERENCES_DB_KEY },
|
|
});
|
|
if (loadedPreferences) {
|
|
try {
|
|
const preferences = jsonParse<SourceControlPreferences>(loadedPreferences.value);
|
|
if (preferences) {
|
|
// set local preferences but don't write back to db
|
|
await this.setPreferences(preferences, false);
|
|
return preferences;
|
|
}
|
|
} catch (error) {
|
|
LoggerProxy.warn(
|
|
`Could not parse Source Control settings from database: ${(error as Error).message}`,
|
|
);
|
|
}
|
|
}
|
|
await this.setPreferences(new SourceControlPreferences(), true);
|
|
return this.sourceControlPreferences;
|
|
}
|
|
}
|