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

refactor(core): Update tag endpoints to use DTOs and injectable config #12380

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

RicardoE105
Copy link
Contributor

@RicardoE105 RicardoE105 commented Dec 26, 2024

Summary

Title self explanatory

I have no clue why we have the N8N_WORKFLOW_TAGS_DISABLED env variable at all. Not sure in which scenario we want to disable tags 🤔. Removing this env variable will simplify things as we need to constantly check this value. @netroy any idea why?

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team ui Enhancement in /editor-ui or /design-system labels Dec 26, 2024
@RicardoE105 RicardoE105 requested a review from netroy December 26, 2024 22:09
if (this.config.getEnv('workflowTagsDisabled'))
throw new BadRequestError('Workflow tags are disabled');
workflowsEnabledMiddleware(_req: Request, res: Response, next: NextFunction) {
if (this.config.getEnv('workflowTagsDisabled')) {
Copy link
Contributor Author

@RicardoE105 RicardoE105 Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing an exception from a middleware returns 500 with an empty response instead of the BadRequest. Probably a bug, due to the error not handled correctly, so for now use the response object to return.

@RicardoE105 RicardoE105 changed the title refactor(core): Update tag endpoints to use DTOs refactor(core): Update tag endpoints to use DTOs and injectable config Dec 26, 2024
@RicardoE105 RicardoE105 requested review from netroy and removed request for netroy December 26, 2024 22:39
*/

@Env('N8N_WORKFLOW_TAGS_DISABLED')
workflowTagsDisabled: boolean = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we call the property disabled so we can do tags.disabled. Or move this to the workflow configuration instead? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. let's do that.

Copy link
Member

@netroy netroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of minor comments, but looks good so far. 🙇🏽

@@ -1,54 +1,72 @@
import { UpdateTagRequestDto, CreateTagRequestDto } from '@n8n/api-types';
import { RetrieveTagQueryDto } from '@n8n/api-types/src/dto/tag/retrieve-tag-query.dto';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no /src/ in released code. This will break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh wondering why the import added the source 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

*/

@Env('N8N_WORKFLOW_TAGS_DISABLED')
workflowTagsDisabled: boolean = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. let's do that.

import { z } from 'zod';
import { Z } from 'zod-class';

export class BaseTagRequestDto extends Z.class({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we really need to create two separate DTOs here. Can we instead have a CreateOrDeleteTagRequestDto or UpsertTagRequestDto ?

if (this.config.getEnv('workflowTagsDisabled'))
throw new BadRequestError('Workflow tags are disabled');
workflowsEnabledMiddleware(_req: Request, res: Response, next: NextFunction) {
if (this.globalConfig.tags.workflowTagsDisabled) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been wondering about this code. The logic to return 400 comes from a long time ago, and I don't know if we ever even discussed this.
Perhaps a better solution here would be delete this middleware, and load this controller conditionally, like we do for MFAController.

next();
}

@Get('/')
@GlobalScope('tag:list')
async getAll(req: TagsRequest.GetAll) {
return await this.tagService.getAll({ withUsageCount: req.query.withUsageCount === 'true' });
async getAll(_req: AuthenticatedRequest, _res: Response, @Query query: RetrieveTagQueryDto) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once we have migrated all controllers, we can remove these implicit parameters 🤞🏽

@@ -132,7 +131,7 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {

const relations: string[] = [];

const areTagsEnabled = !config.getEnv('workflowTagsDisabled');
const areTagsEnabled = !Container.get(GlobalConfig).tags.workflowTagsDisabled;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const areTagsEnabled = !Container.get(GlobalConfig).tags.workflowTagsDisabled;
const areTagsEnabled = !this.GlobalConfig.tags.workflowTagsDisabled;

@@ -10,9 +10,8 @@ import {
type FindManyOptions,
type FindOptionsRelations,
} from '@n8n/typeorm';
import { Service } from 'typedi';
import Container, { Service } from 'typedi';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import Container, { Service } from 'typedi';
import { Service } from 'typedi';

@@ -202,15 +203,17 @@ export class WorkflowService {
]),
);

if (tagIds && !config.getEnv('workflowTagsDisabled')) {
const tagsDisabled = Container.get(GlobalConfig).tags.workflowTagsDisabled;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add GlobalConfig to the constructor, and replace this with

Suggested change
const tagsDisabled = Container.get(GlobalConfig).tags.workflowTagsDisabled;
const tagsDisabled = this.globalConfig.tags.workflowTagsDisabled;

@@ -240,6 +240,8 @@ test('should not report outdated instance when up to date', async () => {

test('should report security settings', async () => {
Container.get(GlobalConfig).diagnostics.enabled = true;
Container.get(GlobalConfig).publicApi.disabled = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like an unrelated change. what's the context here?

const StringBooleanEnum = z.enum(['true', 'false']).optional();

export class RetrieveTagQueryDto extends Z.class({
withUsageCount: StringBooleanEnum,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should update this to use booleanFromString after #12220 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team ui Enhancement in /editor-ui or /design-system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants