RocketChat/Rocket.Chat

View on GitHub
apps/meteor/app/integrations/server/lib/triggerHandler.js

Summary

Maintainability
F
1 wk
Test Coverage

File triggerHandler.js has 580 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import { Integrations, Users, Rooms, Messages } from '@rocket.chat/models';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
import { wrapExceptions } from '@rocket.chat/tools';
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';
Severity: Major
Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 1 day to fix

    Function executeTriggerUrl has 229 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        async executeTriggerUrl(url, trigger, { event, message, room, owner, user }, theHistoryId, tries = 0) {
            if (!this.isTriggerEnabled(trigger)) {
                outgoingLogger.warn(`The trigger "${trigger.name}" is no longer enabled, stopping execution of it at try: ${tries}`);
                return;
            }
    Severity: Major
    Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 1 day to fix

      Function getTriggersToExecute has a Cognitive Complexity of 53 (exceeds 5 allowed). Consider refactoring.
      Open

          getTriggersToExecute(room, message) {
              const triggersToExecute = new Set();
              if (room) {
                  switch (room.t) {
                      case 'd':
      Severity: Minor
      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 1 day to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function executeTriggerUrl has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
      Open

          async executeTriggerUrl(url, trigger, { event, message, room, owner, user }, theHistoryId, tries = 0) {
              if (!this.isTriggerEnabled(trigger)) {
                  outgoingLogger.warn(`The trigger "${trigger.name}" is no longer enabled, stopping execution of it at try: ${tries}`);
                  return;
              }
      Severity: Minor
      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 3 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function mapEventArgsToData has 76 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          mapEventArgsToData(data, { event, message, room, owner, user }) {
              switch (event) {
                  case 'sendMessage':
                      data.channel_id = room._id;
                      data.channel_name = room.name;
      Severity: Major
      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 3 hrs to fix

        Function getTriggersToExecute has 61 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            getTriggersToExecute(room, message) {
                const triggersToExecute = new Set();
                if (room) {
                    switch (room.t) {
                        case 'd':
        Severity: Major
        Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 2 hrs to fix

          Function mapEventArgsToData has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
          Open

              mapEventArgsToData(data, { event, message, room, owner, user }) {
                  switch (event) {
                      case 'sendMessage':
                          data.channel_id = room._id;
                          data.channel_name = room.name;
          Severity: Minor
          Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 2 hrs to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function eventNameArgumentsToObject has 52 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              eventNameArgumentsToObject(...args) {
                  const argObject = {
                      event: args[0],
                  };
          
          
          Severity: Major
          Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 2 hrs to fix

            Function eventNameArgumentsToObject has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

                eventNameArgumentsToObject(...args) {
                    const argObject = {
                        event: args[0],
                    };
            
            
            Severity: Minor
            Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function sendMessage has 38 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                async sendMessage({ trigger, nameOrId = '', room, message, data }) {
                    let user;
                    // Try to find the user who we are impersonating
                    if (trigger.impersonateUser) {
                        user = await Users.findOneByUsernameIgnoringCase(data.user_name);
            Severity: Minor
            Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 1 hr to fix

              Function sendMessage has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
              Open

                  async sendMessage({ trigger, nameOrId = '', room, message, data }) {
                      let user;
                      // Try to find the user who we are impersonating
                      if (trigger.impersonateUser) {
                          user = await Users.findOneByUsernameIgnoringCase(data.user_name);
              Severity: Minor
              Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 45 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Function executeTriggers has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
              Open

                  async executeTriggers(...args) {
                      outgoingLogger.debug({ msg: 'Execute Trigger:', arg: args[0] });
              
                      const argObject = this.eventNameArgumentsToObject(...args);
                      const { event, message, room } = argObject;
              Severity: Minor
              Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 45 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Consider simplifying this complex logical expression.
              Open

                      if (nameOrId || trigger.targetRoom || message.channel) {
                          tmpRoom =
                              (await getRoomByNameOrIdWithOptionToJoin({
                                  user,
                                  nameOrId: nameOrId || message.channel || trigger.targetRoom,
              Severity: Major
              Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 40 mins to fix

                Function addIntegration has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    addIntegration(record) {
                        outgoingLogger.debug(`Adding the integration ${record.name} of the event ${record.event}!`);
                        let channels;
                        if (record.event && !outgoingEvents[record.event].use.channel) {
                            outgoingLogger.debug('The integration doesnt rely on channels.');
                Severity: Minor
                Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 35 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Avoid too many return statements within this function.
                Open

                                                    return;
                Severity: Major
                Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                              return;
                  Severity: Major
                  Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                                        return;
                    Severity: Major
                    Found in apps/meteor/app/integrations/server/lib/triggerHandler.js - About 30 mins to fix

                      Similar blocks of code found in 2 locations. Consider refactoring.
                      Open

                                      default:
                                          if (this.triggers.all_private_groups) {
                                              for (const trigger of Object.values(this.triggers.all_private_groups)) {
                                                  triggersToExecute.add(trigger);
                                              }
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 1 other location - About 1 day to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 295..313

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 235.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 2 locations. Consider refactoring.
                      Open

                                      case 'c':
                                          if (this.triggers.all_public_channels) {
                                              for (const trigger of Object.values(this.triggers.all_public_channels)) {
                                                  triggersToExecute.add(trigger);
                                              }
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 1 other location - About 1 day to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 315..333

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 235.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 2 locations. Consider refactoring.
                      Open

                                          room.usernames
                                              .filter((username) => username !== message?.u?.username && this.triggers[`@${username}`])
                                              .forEach((username) => {
                                                  for (const trigger of Object.values(this.triggers[`@${username}`])) {
                                                      triggersToExecute.add(trigger);
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 1 other location - About 1 hr to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 279..285

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 57.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 2 locations. Consider refactoring.
                      Open

                                          room.uids
                                              .filter((uid) => this.triggers[`@${uid}`])
                                              .forEach((uid) => {
                                                  for (const trigger of Object.values(this.triggers[`@${uid}`])) {
                                                      triggersToExecute.add(trigger);
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 1 other location - About 1 hr to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 287..293

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 57.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 4 locations. Consider refactoring.
                      Open

                                  case 'roomArchived':
                                      if (args.length >= 3) {
                                          argObject.room = args[1];
                                          argObject.user = args[2];
                                      }
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 3 other locations - About 45 mins to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 129..134
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 149..154
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 156..161

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 50.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 4 locations. Consider refactoring.
                      Open

                                  case 'sendMessage':
                                      if (args.length >= 3) {
                                          argObject.message = args[1];
                                          argObject.room = args[2];
                                      }
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 3 other locations - About 45 mins to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 143..148
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 149..154
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 156..161

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 50.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 4 locations. Consider refactoring.
                      Open

                                  case 'roomCreated':
                                      if (args.length >= 3) {
                                          argObject.owner = args[1];
                                          argObject.room = args[2];
                                      }
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 3 other locations - About 45 mins to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 129..134
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 143..148
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 156..161

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 50.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 4 locations. Consider refactoring.
                      Open

                                  case 'roomLeft':
                                      if (args.length >= 3) {
                                          argObject.user = args[1];
                                          argObject.room = args[2];
                                      }
                      Severity: Major
                      Found in apps/meteor/app/integrations/server/lib/triggerHandler.js and 3 other locations - About 45 mins to fix
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 129..134
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 143..148
                      apps/meteor/app/integrations/server/lib/triggerHandler.js on lines 149..154

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 50.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      There are no issues that match your filters.

                      Category
                      Status