refactor(core): Move tag collection into repository (no-changelog) (#6860)

* refactor(core): Move tag collection into repository

* Fix tests

* Address feedback

* Fix missing spot
This commit is contained in:
Iván Ovejero
2023-08-08 14:08:56 +02:00
committed by GitHub
parent 8de28fe4d0
commit 11440bfd3c
15 changed files with 59 additions and 61 deletions

View File

@@ -1,6 +1,5 @@
import Container, { Service } from 'typedi';
import path from 'path';
import * as Db from '@/Db';
import {
getTagsPath,
getTrackingInformationFromPostPushResult,
@@ -39,6 +38,7 @@ import type { Variables } from '@db/entities/Variables';
import type { SourceControlWorkflowVersionId } from './types/sourceControlWorkflowVersionId';
import type { ExportableCredential } from './types/exportableCredential';
import { InternalHooks } from '@/InternalHooks';
import { TagRepository } from '@/databases/repositories';
@Service()
export class SourceControlService {
private sshKeyName: string;
@@ -52,6 +52,7 @@ export class SourceControlService {
private sourceControlPreferencesService: SourceControlPreferencesService,
private sourceControlExportService: SourceControlExportService,
private sourceControlImportService: SourceControlImportService,
private tagRepository: TagRepository,
) {
const userFolder = UserSettings.getUserN8nFolderPath();
this.sshFolder = path.join(userFolder, SOURCE_CONTROL_SSH_FOLDER);
@@ -682,7 +683,7 @@ export class SourceControlService {
options: SourceControlGetStatus,
sourceControlledFiles: SourceControlledFile[],
) {
const lastUpdatedTag = await Db.collections.Tag.find({
const lastUpdatedTag = await this.tagRepository.find({
order: { updatedAt: 'DESC' },
take: 1,
select: ['updatedAt'],

View File

@@ -26,6 +26,7 @@ import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import { In } from 'typeorm';
import type { SourceControlledFile } from './types/sourceControlledFile';
import { VariablesService } from '../variables/variables.service';
import { TagRepository } from '@/databases/repositories';
@Service()
export class SourceControlExportService {
@@ -35,7 +36,10 @@ export class SourceControlExportService {
private credentialExportFolder: string;
constructor(private readonly variablesService: VariablesService) {
constructor(
private readonly variablesService: VariablesService,
private readonly tagRepository: TagRepository,
) {
const userFolder = UserSettings.getUserN8nFolderPath();
this.gitFolder = path.join(userFolder, SOURCE_CONTROL_GIT_FOLDER);
this.workflowExportFolder = path.join(this.gitFolder, SOURCE_CONTROL_WORKFLOW_EXPORT_FOLDER);
@@ -167,7 +171,7 @@ export class SourceControlExportService {
async exportTagsToWorkFolder(): Promise<ExportResult> {
try {
sourceControlFoldersExistCheck([this.gitFolder]);
const tags = await Db.collections.Tag.find();
const tags = await this.tagRepository.find();
// do not export empty tags
if (tags.length === 0) {
return {

View File

@@ -27,6 +27,7 @@ import { getCredentialExportPath, getWorkflowExportPath } from './sourceControlH
import type { SourceControlledFile } from './types/sourceControlledFile';
import { RoleService } from '@/services/role.service';
import { VariablesService } from '../variables/variables.service';
import { TagRepository } from '@/databases/repositories';
@Service()
export class SourceControlImportService {
@@ -39,6 +40,7 @@ export class SourceControlImportService {
constructor(
private readonly variablesService: VariablesService,
private readonly activeWorkflowRunner: ActiveWorkflowRunner,
private readonly tagRepository: TagRepository,
) {
const userFolder = UserSettings.getUserN8nFolderPath();
this.gitFolder = path.join(userFolder, SOURCE_CONTROL_GIT_FOLDER);
@@ -265,7 +267,7 @@ export class SourceControlImportService {
tags: TagEntity[];
mappings: WorkflowTagMapping[];
}> {
const localTags = await Db.collections.Tag.find({
const localTags = await this.tagRepository.find({
select: ['id', 'name'],
});
const localMappings = await Db.collections.WorkflowTagMapping.find({
@@ -481,7 +483,7 @@ export class SourceControlImportService {
await Promise.all(
mappedTags.tags.map(async (tag) => {
const findByName = await Db.collections.Tag.findOne({
const findByName = await this.tagRepository.findOne({
where: { name: tag.name },
select: ['id'],
});
@@ -490,7 +492,7 @@ export class SourceControlImportService {
`A tag with the name <strong>${tag.name}</strong> already exists locally.<br />Please either rename the local tag, or the remote one with the id <strong>${tag.id}</strong> in the tags.json file.`,
);
}
await Db.collections.Tag.upsert(
await this.tagRepository.upsert(
{
...tag,
},