mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
refactor(core): Use DI in source-control. add more tests (#12554)
This commit is contained in:
committed by
GitHub
parent
b2cbed9865
commit
25a79ccf40
@@ -1,5 +1,5 @@
|
||||
import type { SourceControlledFile } from '@n8n/api-types';
|
||||
import { Container, Service } from '@n8n/di';
|
||||
import { Service } from '@n8n/di';
|
||||
import { rmSync } from 'fs';
|
||||
import { Credentials, InstanceSettings, Logger } from 'n8n-core';
|
||||
import { ApplicationError, type ICredentialDataDecryptedObject } from 'n8n-workflow';
|
||||
@@ -44,6 +44,10 @@ export class SourceControlExportService {
|
||||
private readonly logger: Logger,
|
||||
private readonly variablesService: VariablesService,
|
||||
private readonly tagRepository: TagRepository,
|
||||
private readonly sharedCredentialsRepository: SharedCredentialsRepository,
|
||||
private readonly sharedWorkflowRepository: SharedWorkflowRepository,
|
||||
private readonly workflowRepository: WorkflowRepository,
|
||||
private readonly workflowTagMappingRepository: WorkflowTagMappingRepository,
|
||||
instanceSettings: InstanceSettings,
|
||||
) {
|
||||
this.gitFolder = path.join(instanceSettings.n8nFolder, SOURCE_CONTROL_GIT_FOLDER);
|
||||
@@ -106,17 +110,16 @@ export class SourceControlExportService {
|
||||
try {
|
||||
sourceControlFoldersExistCheck([this.workflowExportFolder]);
|
||||
const workflowIds = candidates.map((e) => e.id);
|
||||
const sharedWorkflows =
|
||||
await Container.get(SharedWorkflowRepository).findByWorkflowIds(workflowIds);
|
||||
const workflows = await Container.get(WorkflowRepository).findByIds(workflowIds);
|
||||
const sharedWorkflows = await this.sharedWorkflowRepository.findByWorkflowIds(workflowIds);
|
||||
const workflows = await this.workflowRepository.findByIds(workflowIds);
|
||||
|
||||
// determine owner of each workflow to be exported
|
||||
const owners: Record<string, ResourceOwner> = {};
|
||||
sharedWorkflows.forEach((e) => {
|
||||
const project = e.project;
|
||||
sharedWorkflows.forEach((sharedWorkflow) => {
|
||||
const project = sharedWorkflow.project;
|
||||
|
||||
if (!project) {
|
||||
throw new ApplicationError(`Workflow ${e.workflow.display()} has no owner`);
|
||||
throw new ApplicationError(`Workflow ${sharedWorkflow.workflow.display()} has no owner`);
|
||||
}
|
||||
|
||||
if (project.type === 'personal') {
|
||||
@@ -124,14 +127,16 @@ export class SourceControlExportService {
|
||||
(pr) => pr.role === 'project:personalOwner',
|
||||
);
|
||||
if (!ownerRelation) {
|
||||
throw new ApplicationError(`Workflow ${e.workflow.display()} has no owner`);
|
||||
throw new ApplicationError(
|
||||
`Workflow ${sharedWorkflow.workflow.display()} has no owner`,
|
||||
);
|
||||
}
|
||||
owners[e.workflowId] = {
|
||||
owners[sharedWorkflow.workflowId] = {
|
||||
type: 'personal',
|
||||
personalEmail: ownerRelation.user.email,
|
||||
};
|
||||
} else if (project.type === 'team') {
|
||||
owners[e.workflowId] = {
|
||||
owners[sharedWorkflow.workflowId] = {
|
||||
type: 'team',
|
||||
teamId: project.id,
|
||||
teamName: project.name,
|
||||
@@ -156,6 +161,7 @@ export class SourceControlExportService {
|
||||
})),
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof ApplicationError) throw error;
|
||||
throw new ApplicationError('Failed to export workflows to work folder', { cause: error });
|
||||
}
|
||||
}
|
||||
@@ -204,7 +210,7 @@ export class SourceControlExportService {
|
||||
files: [],
|
||||
};
|
||||
}
|
||||
const mappings = await Container.get(WorkflowTagMappingRepository).find();
|
||||
const mappings = await this.workflowTagMappingRepository.find();
|
||||
const fileName = path.join(this.gitFolder, SOURCE_CONTROL_TAGS_EXPORT_FILE);
|
||||
await fsWriteFile(
|
||||
fileName,
|
||||
@@ -260,9 +266,10 @@ export class SourceControlExportService {
|
||||
try {
|
||||
sourceControlFoldersExistCheck([this.credentialExportFolder]);
|
||||
const credentialIds = candidates.map((e) => e.id);
|
||||
const credentialsToBeExported = await Container.get(
|
||||
SharedCredentialsRepository,
|
||||
).findByCredentialIds(credentialIds, 'credential:owner');
|
||||
const credentialsToBeExported = await this.sharedCredentialsRepository.findByCredentialIds(
|
||||
credentialIds,
|
||||
'credential:owner',
|
||||
);
|
||||
let missingIds: string[] = [];
|
||||
if (credentialsToBeExported.length !== credentialIds.length) {
|
||||
const foundCredentialIds = credentialsToBeExported.map((e) => e.credentialsId);
|
||||
|
||||
Reference in New Issue
Block a user