refactor(core): Make Logger a service (no-changelog) (#7494)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-10-25 16:35:22 +02:00
committed by GitHub
parent db4e61ba24
commit 05586a900d
131 changed files with 761 additions and 919 deletions

View File

@@ -1,3 +1,6 @@
import { Container, Service } from 'typedi';
import type { PullResult } from 'simple-git';
import express from 'express';
import { Authorized, Get, Post, Patch, RestController } from '@/decorators';
import {
sourceControlLicensedMiddleware,
@@ -10,10 +13,7 @@ import type { SourceControlPreferences } from './types/sourceControlPreferences'
import type { SourceControlledFile } from './types/sourceControlledFile';
import { SOURCE_CONTROL_API_ROOT, SOURCE_CONTROL_DEFAULT_BRANCH } from './constants';
import { BadRequestError } from '@/ResponseHelper';
import type { PullResult } from 'simple-git';
import express from 'express';
import type { ImportResult } from './types/importResult';
import Container, { Service } from 'typedi';
import { InternalHooks } from '../../InternalHooks';
import { getRepoType } from './sourceControlHelper.ee';
import { SourceControlGetStatus } from './types/sourceControlGetStatus';

View File

@@ -14,7 +14,6 @@ import {
SOURCE_CONTROL_DEFAULT_NAME,
SOURCE_CONTROL_README,
} from './constants';
import { LoggerProxy } from 'n8n-workflow';
import { SourceControlGitService } from './sourceControlGit.service.ee';
import type { PushResult } from 'simple-git';
import { SourceControlExportService } from './sourceControlExport.service.ee';
@@ -35,6 +34,7 @@ import type { SourceControlWorkflowVersionId } from './types/sourceControlWorkfl
import type { ExportableCredential } from './types/exportableCredential';
import { InternalHooks } from '@/InternalHooks';
import { TagRepository } from '@/databases/repositories';
import { Logger } from '@/Logger';
@Service()
export class SourceControlService {
@@ -45,6 +45,7 @@ export class SourceControlService {
private gitFolder: string;
constructor(
private readonly logger: Logger,
private gitService: SourceControlGitService,
private sourceControlPreferencesService: SourceControlPreferencesService,
private sourceControlExportService: SourceControlExportService,
@@ -123,14 +124,14 @@ export class SourceControlService {
if (!this.gitService.git) {
await this.initGitService();
}
LoggerProxy.debug('Initializing repository...');
this.logger.debug('Initializing repository...');
await this.gitService.initRepository(preferences, user);
let getBranchesResult;
try {
getBranchesResult = await this.getBranches();
} catch (error) {
if ((error as Error).message.includes('Warning: Permanently added')) {
LoggerProxy.debug('Added repository host to the list of known hosts. Retrying...');
this.logger.debug('Added repository host to the list of known hosts. Retrying...');
getBranchesResult = await this.getBranches();
} else {
throw error;
@@ -152,7 +153,7 @@ export class SourceControlService {
getBranchesResult = await this.getBranches();
await this.gitService.setBranch(preferences.branchName);
} catch (fileError) {
LoggerProxy.error(`Failed to create initial commit: ${(fileError as Error).message}`);
this.logger.error(`Failed to create initial commit: ${(fileError as Error).message}`);
}
}
}
@@ -193,7 +194,7 @@ export class SourceControlService {
await this.gitService.resetBranch();
await this.gitService.pull();
} catch (error) {
LoggerProxy.error(`Failed to reset workfolder: ${(error as Error).message}`);
this.logger.error(`Failed to reset workfolder: ${(error as Error).message}`);
throw new Error(
'Unable to fetch updates from git - your folder might be out of sync. Try reconnecting from the Source Control settings page.',
);

View File

@@ -8,7 +8,6 @@ import {
} from './constants';
import * as Db from '@/Db';
import type { ICredentialDataDecryptedObject } from 'n8n-workflow';
import { LoggerProxy } from 'n8n-workflow';
import { writeFile as fsWriteFile, rm as fsRm } from 'fs/promises';
import { rmSync } from 'fs';
import { Credentials, InstanceSettings } from 'n8n-core';
@@ -27,6 +26,7 @@ import { In } from 'typeorm';
import type { SourceControlledFile } from './types/sourceControlledFile';
import { VariablesService } from '../variables/variables.service';
import { TagRepository } from '@/databases/repositories';
import { Logger } from '@/Logger';
@Service()
export class SourceControlExportService {
@@ -37,6 +37,7 @@ export class SourceControlExportService {
private credentialExportFolder: string;
constructor(
private readonly logger: Logger,
private readonly variablesService: VariablesService,
private readonly tagRepository: TagRepository,
instanceSettings: InstanceSettings,
@@ -61,7 +62,7 @@ export class SourceControlExportService {
try {
await fsRm(this.gitFolder, { recursive: true });
} catch (error) {
LoggerProxy.error(`Failed to delete work folder: ${(error as Error).message}`);
this.logger.error(`Failed to delete work folder: ${(error as Error).message}`);
}
}
@@ -69,7 +70,7 @@ export class SourceControlExportService {
try {
filesToBeDeleted.forEach((e) => rmSync(e));
} catch (error) {
LoggerProxy.error(`Failed to delete workflows from work folder: ${(error as Error).message}`);
this.logger.error(`Failed to delete workflows from work folder: ${(error as Error).message}`);
}
return filesToBeDeleted;
}
@@ -91,7 +92,7 @@ export class SourceControlExportService {
versionId: e.versionId,
owner: owners[e.id],
};
LoggerProxy.debug(`Writing workflow ${e.id} to ${fileName}`);
this.logger.debug(`Writing workflow ${e.id} to ${fileName}`);
return fsWriteFile(fileName, JSON.stringify(sanitizedWorkflow, null, 2));
}),
);
@@ -224,7 +225,7 @@ export class SourceControlExportService {
continue;
}
} catch (error) {
LoggerProxy.error(`Failed to sanitize credential data: ${(error as Error).message}`);
this.logger.error(`Failed to sanitize credential data: ${(error as Error).message}`);
throw error;
}
}
@@ -262,7 +263,7 @@ export class SourceControlExportService {
data: sanitizedData,
nodesAccess: sharedCredential.credentials.nodesAccess,
};
LoggerProxy.debug(`Writing credential ${sharedCredential.credentials.id} to ${fileName}`);
this.logger.debug(`Writing credential ${sharedCredential.credentials.id} to ${fileName}`);
return fsWriteFile(fileName, JSON.stringify(sanitizedCredential, null, 2));
}),
);

View File

@@ -1,6 +1,5 @@
import { Service } from 'typedi';
import { execSync } from 'child_process';
import { LoggerProxy } from 'n8n-workflow';
import path from 'path';
import type {
CommitResult,
@@ -20,8 +19,9 @@ import {
SOURCE_CONTROL_ORIGIN,
} from './constants';
import { sourceControlFoldersExistCheck } from './sourceControlHelper.ee';
import type { User } from '../../databases/entities/User';
import type { User } from '@db/entities/User';
import { getInstanceOwner } from '../../UserManagement/UserManagementHelper';
import { Logger } from '@/Logger';
@Service()
export class SourceControlGitService {
@@ -29,17 +29,19 @@ export class SourceControlGitService {
private gitOptions: Partial<SimpleGitOptions> = {};
constructor(private readonly logger: Logger) {}
/**
* Run pre-checks before initialising git
* Checks for existence of required binaries (git and ssh)
*/
private preInitCheck(): boolean {
LoggerProxy.debug('GitService.preCheck');
this.logger.debug('GitService.preCheck');
try {
const gitResult = execSync('git --version', {
stdio: ['pipe', 'pipe', 'pipe'],
});
LoggerProxy.debug(`Git binary found: ${gitResult.toString()}`);
this.logger.debug(`Git binary found: ${gitResult.toString()}`);
} catch (error) {
throw new Error(`Git binary not found: ${(error as Error).message}`);
}
@@ -47,7 +49,7 @@ export class SourceControlGitService {
const sshResult = execSync('ssh -V', {
stdio: ['pipe', 'pipe', 'pipe'],
});
LoggerProxy.debug(`SSH binary found: ${sshResult.toString()}`);
this.logger.debug(`SSH binary found: ${sshResult.toString()}`);
} catch (error) {
throw new Error(`SSH binary not found: ${(error as Error).message}`);
}
@@ -66,13 +68,13 @@ export class SourceControlGitService {
sshKeyName,
sshFolder,
} = options;
LoggerProxy.debug('GitService.init');
this.logger.debug('GitService.init');
if (this.git !== null) {
return;
}
this.preInitCheck();
LoggerProxy.debug('Git pre-check passed');
this.logger.debug('Git pre-check passed');
sourceControlFoldersExistCheck([gitFolder, sshFolder]);
@@ -135,13 +137,13 @@ export class SourceControlGitService {
(e) => e.name === SOURCE_CONTROL_ORIGIN && e.refs.push === remote,
);
if (foundRemote) {
LoggerProxy.debug(`Git remote found: ${foundRemote.name}: ${foundRemote.refs.push}`);
this.logger.debug(`Git remote found: ${foundRemote.name}: ${foundRemote.refs.push}`);
return true;
}
} catch (error) {
throw new Error(`Git is not initialized ${(error as Error).message}`);
}
LoggerProxy.debug(`Git remote not found: ${remote}`);
this.logger.debug(`Git remote not found: ${remote}`);
return false;
}
@@ -159,14 +161,14 @@ export class SourceControlGitService {
try {
await this.git.init();
} catch (error) {
LoggerProxy.debug(`Git init: ${(error as Error).message}`);
this.logger.debug(`Git init: ${(error as Error).message}`);
}
}
try {
await this.git.addRemote(SOURCE_CONTROL_ORIGIN, sourceControlPreferences.repositoryUrl);
} catch (error) {
if ((error as Error).message.includes('remote origin already exists')) {
LoggerProxy.debug(`Git remote already exists: ${(error as Error).message}`);
this.logger.debug(`Git remote already exists: ${(error as Error).message}`);
} else {
throw error;
}
@@ -182,7 +184,7 @@ export class SourceControlGitService {
await this.git.raw(['branch', '-M', sourceControlPreferences.branchName]);
}
} catch (error) {
LoggerProxy.debug(`Git init: ${(error as Error).message}`);
this.logger.debug(`Git init: ${(error as Error).message}`);
}
}
}
@@ -305,7 +307,7 @@ export class SourceControlGitService {
try {
await this.git.rm(Array.from(deletedFiles));
} catch (error) {
LoggerProxy.debug(`Git rm: ${(error as Error).message}`);
this.logger.debug(`Git rm: ${(error as Error).message}`);
}
}
return this.git.add(Array.from(files));

View File

@@ -1,9 +1,8 @@
import Container from 'typedi';
import { Container } from 'typedi';
import { License } from '@/License';
import { generateKeyPairSync } from 'crypto';
import type { KeyPair } from './types/keyPair';
import { constants as fsConstants, mkdirSync, accessSync } from 'fs';
import { LoggerProxy } from 'n8n-workflow';
import {
SOURCE_CONTROL_GIT_KEY_COMMENT,
SOURCE_CONTROL_TAGS_EXPORT_FILE,
@@ -12,6 +11,7 @@ import {
import type { SourceControlledFile } from './types/sourceControlledFile';
import path from 'path';
import type { KeyPairType } from './types/keyPairType';
import { Logger } from '@/Logger';
export function stringContainsExpression(testString: string): boolean {
return /^=.*\{\{.*\}\}/.test(testString);
@@ -51,7 +51,7 @@ export function sourceControlFoldersExistCheck(
try {
mkdirSync(folder, { recursive: true });
} catch (error) {
LoggerProxy.error((error as Error).message);
Container.get(Logger).error((error as Error).message);
}
}
}

View File

@@ -1,4 +1,4 @@
import Container, { Service } from 'typedi';
import { Container, Service } from 'typedi';
import path from 'path';
import {
SOURCE_CONTROL_CREDENTIAL_EXPORT_FOLDER,
@@ -9,7 +9,7 @@ import {
} from './constants';
import * as Db from '@/Db';
import glob from 'fast-glob';
import { LoggerProxy, jsonParse } from 'n8n-workflow';
import { jsonParse } from 'n8n-workflow';
import { readFile as fsReadFile } from 'fs/promises';
import { Credentials, InstanceSettings } from 'n8n-core';
import type { IWorkflowToImport } from '@/Interfaces';
@@ -28,6 +28,7 @@ import { RoleService } from '@/services/role.service';
import { VariablesService } from '../variables/variables.service';
import { TagRepository } from '@/databases/repositories';
import { UM_FIX_INSTRUCTION } from '@/constants';
import { Logger } from '@/Logger';
@Service()
export class SourceControlImportService {
@@ -38,6 +39,7 @@ export class SourceControlImportService {
private credentialExportFolder: string;
constructor(
private readonly logger: Logger,
private readonly variablesService: VariablesService,
private readonly activeWorkflowRunner: ActiveWorkflowRunner,
private readonly tagRepository: TagRepository,
@@ -88,7 +90,7 @@ export class SourceControlImportService {
});
const remoteWorkflowFilesParsed = await Promise.all(
remoteWorkflowFiles.map(async (file) => {
LoggerProxy.debug(`Parsing workflow file ${file}`);
this.logger.debug(`Parsing workflow file ${file}`);
const remote = jsonParse<IWorkflowToImport>(await fsReadFile(file, { encoding: 'utf8' }));
if (!remote?.id) {
return undefined;
@@ -130,7 +132,7 @@ export class SourceControlImportService {
});
const remoteCredentialFilesParsed = await Promise.all(
remoteCredentialFiles.map(async (file) => {
LoggerProxy.debug(`Parsing credential file ${file}`);
this.logger.debug(`Parsing credential file ${file}`);
const remote = jsonParse<ExportableCredential>(
await fsReadFile(file, { encoding: 'utf8' }),
);
@@ -169,7 +171,7 @@ export class SourceControlImportService {
absolute: true,
});
if (variablesFile.length > 0) {
LoggerProxy.debug(`Importing variables from file ${variablesFile[0]}`);
this.logger.debug(`Importing variables from file ${variablesFile[0]}`);
return jsonParse<Variables[]>(await fsReadFile(variablesFile[0], { encoding: 'utf8' }), {
fallbackValue: [],
});
@@ -190,7 +192,7 @@ export class SourceControlImportService {
absolute: true,
});
if (tagsFile.length > 0) {
LoggerProxy.debug(`Importing tags from file ${tagsFile[0]}`);
this.logger.debug(`Importing tags from file ${tagsFile[0]}`);
const mappedTags = jsonParse<{ tags: TagEntity[]; mappings: WorkflowTagMapping[] }>(
await fsReadFile(tagsFile[0], { encoding: 'utf8' }),
{ fallbackValue: { tags: [], mappings: [] } },
@@ -232,7 +234,7 @@ export class SourceControlImportService {
const cachedOwnerIds = new Map<string, string>();
const importWorkflowsResult = await Promise.all(
candidates.map(async (candidate) => {
LoggerProxy.debug(`Parsing workflow file ${candidate.file}`);
this.logger.debug(`Parsing workflow file ${candidate.file}`);
const importedWorkflow = jsonParse<IWorkflowToImport & { owner: string }>(
await fsReadFile(candidate.file, { encoding: 'utf8' }),
);
@@ -241,7 +243,7 @@ export class SourceControlImportService {
}
const existingWorkflow = existingWorkflows.find((e) => e.id === importedWorkflow.id);
importedWorkflow.active = existingWorkflow?.active ?? false;
LoggerProxy.debug(`Updating workflow id ${importedWorkflow.id ?? 'new'}`);
this.logger.debug(`Updating workflow id ${importedWorkflow.id ?? 'new'}`);
const upsertResult = await Db.collections.Workflow.upsert({ ...importedWorkflow }, ['id']);
if (upsertResult?.identifiers?.length !== 1) {
throw new Error(`Failed to upsert workflow ${importedWorkflow.id ?? 'new'}`);
@@ -299,14 +301,14 @@ export class SourceControlImportService {
if (existingWorkflow?.active) {
try {
// remove active pre-import workflow
LoggerProxy.debug(`Deactivating workflow id ${existingWorkflow.id}`);
this.logger.debug(`Deactivating workflow id ${existingWorkflow.id}`);
await workflowRunner.remove(existingWorkflow.id);
// try activating the imported workflow
LoggerProxy.debug(`Reactivating workflow id ${existingWorkflow.id}`);
this.logger.debug(`Reactivating workflow id ${existingWorkflow.id}`);
await workflowRunner.add(existingWorkflow.id, 'activate');
// update the versionId of the workflow to match the imported workflow
} catch (error) {
LoggerProxy.error(`Failed to activate workflow ${existingWorkflow.id}`, error as Error);
this.logger.error(`Failed to activate workflow ${existingWorkflow.id}`, error as Error);
} finally {
await Db.collections.Workflow.update(
{ id: existingWorkflow.id },
@@ -347,7 +349,7 @@ export class SourceControlImportService {
let importCredentialsResult: Array<{ id: string; name: string; type: string }> = [];
importCredentialsResult = await Promise.all(
candidates.map(async (candidate) => {
LoggerProxy.debug(`Importing credentials file ${candidate.file}`);
this.logger.debug(`Importing credentials file ${candidate.file}`);
const credential = jsonParse<ExportableCredential>(
await fsReadFile(candidate.file, { encoding: 'utf8' }),
);
@@ -367,7 +369,7 @@ export class SourceControlImportService {
}
newCredentialObject.nodesAccess = nodesAccess || existingCredential?.nodesAccess || [];
LoggerProxy.debug(`Updating credential id ${newCredentialObject.id as string}`);
this.logger.debug(`Updating credential id ${newCredentialObject.id as string}`);
await Db.collections.Credentials.upsert(newCredentialObject, ['id']);
if (!sharedOwner) {
@@ -395,13 +397,13 @@ export class SourceControlImportService {
public async importTagsFromWorkFolder(candidate: SourceControlledFile) {
let mappedTags;
try {
LoggerProxy.debug(`Importing tags from file ${candidate.file}`);
this.logger.debug(`Importing tags from file ${candidate.file}`);
mappedTags = jsonParse<{ tags: TagEntity[]; mappings: WorkflowTagMapping[] }>(
await fsReadFile(candidate.file, { encoding: 'utf8' }),
{ fallbackValue: { tags: [], mappings: [] } },
);
} catch (error) {
LoggerProxy.error(`Failed to import tags from file ${candidate.file}`, error as Error);
this.logger.error(`Failed to import tags from file ${candidate.file}`, error as Error);
return;
}
@@ -462,13 +464,13 @@ export class SourceControlImportService {
const result: { imported: string[] } = { imported: [] };
let importedVariables;
try {
LoggerProxy.debug(`Importing variables from file ${candidate.file}`);
this.logger.debug(`Importing variables from file ${candidate.file}`);
importedVariables = jsonParse<Array<Partial<Variables>>>(
await fsReadFile(candidate.file, { encoding: 'utf8' }),
{ fallbackValue: [] },
);
} catch (error) {
LoggerProxy.error(`Failed to import tags from file ${candidate.file}`, error as Error);
this.logger.error(`Failed to import tags from file ${candidate.file}`, error as Error);
return;
}
const overriddenKeys = Object.keys(valueOverrides ?? {});
@@ -490,12 +492,12 @@ export class SourceControlImportService {
await Db.collections.Variables.upsert({ ...variable }, ['id']);
} catch (errorUpsert) {
if (isUniqueConstraintError(errorUpsert as Error)) {
LoggerProxy.debug(`Variable ${variable.key} already exists, updating instead`);
this.logger.debug(`Variable ${variable.key} already exists, updating instead`);
try {
await Db.collections.Variables.update({ key: variable.key }, { ...variable });
} catch (errorUpdate) {
LoggerProxy.debug(`Failed to update variable ${variable.key}, skipping`);
LoggerProxy.debug((errorUpdate as Error).message);
this.logger.debug(`Failed to update variable ${variable.key}, skipping`);
this.logger.debug((errorUpdate as Error).message);
}
}
} finally {

View File

@@ -10,7 +10,7 @@ import {
sourceControlFoldersExistCheck,
} from './sourceControlHelper.ee';
import { InstanceSettings } from 'n8n-core';
import { LoggerProxy, jsonParse } from 'n8n-workflow';
import { jsonParse } from 'n8n-workflow';
import * as Db from '@/Db';
import {
SOURCE_CONTROL_SSH_FOLDER,
@@ -21,6 +21,7 @@ import {
import path from 'path';
import type { KeyPairType } from './types/keyPairType';
import config from '@/config';
import { Logger } from '@/Logger';
@Service()
export class SourceControlPreferencesService {
@@ -32,7 +33,10 @@ export class SourceControlPreferencesService {
readonly gitFolder: string;
constructor(instanceSettings: InstanceSettings) {
constructor(
instanceSettings: InstanceSettings,
private readonly logger: Logger,
) {
this.sshFolder = path.join(instanceSettings.n8nFolder, SOURCE_CONTROL_SSH_FOLDER);
this.gitFolder = path.join(instanceSettings.n8nFolder, SOURCE_CONTROL_GIT_FOLDER);
this.sshKeyName = path.join(this.sshFolder, SOURCE_CONTROL_SSH_KEY_NAME);
@@ -66,7 +70,7 @@ export class SourceControlPreferencesService {
try {
return fsReadFileSync(this.sshKeyName + '.pub', { encoding: 'utf8' });
} catch (error) {
LoggerProxy.error(`Failed to read public key: ${(error as Error).message}`);
this.logger.error(`Failed to read public key: ${(error as Error).message}`);
}
return '';
}
@@ -79,7 +83,7 @@ export class SourceControlPreferencesService {
try {
await fsRm(this.sshFolder, { recursive: true });
} catch (error) {
LoggerProxy.error(`Failed to delete ssh folder: ${(error as Error).message}`);
this.logger.error(`Failed to delete ssh folder: ${(error as Error).message}`);
}
}
@@ -160,7 +164,7 @@ export class SourceControlPreferencesService {
const keyPairType =
preferences.keyGeneratorType ??
(config.get('sourceControl.defaultKeyPairType') as KeyPairType);
LoggerProxy.debug(`No key pair files found, generating new pair using type: ${keyPairType}`);
this.logger.debug(`No key pair files found, generating new pair using type: ${keyPairType}`);
await this.generateAndSaveKeyPair(keyPairType);
}
this.sourceControlPreferences = preferences;
@@ -194,7 +198,7 @@ export class SourceControlPreferencesService {
return preferences;
}
} catch (error) {
LoggerProxy.warn(
this.logger.warn(
`Could not parse Source Control settings from database: ${(error as Error).message}`,
);
}