OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#64347] extract custom field IDs from formula
Tobias Dillmannaf23dd7bdc9b
diff --git a/Gemfile b/Gemfileindex cac0a32a5f8..c91cc7a019a 100644--- a/Gemfile+++ b/Gemfile@@ -390,6 +390,9 @@ gem "googleauth", require: false# Required for contractsgem "disposable", "~> 0.6.2"+# Used for formula evaluation of calculated values+gem "dentaku", "~> 3.5"+platforms :mri, :mingw, :x64_mingw dogroup :postgres dogem "pg", "~> 1.5.0"diff --git a/Gemfile.lock b/Gemfile.lockindex aaca71eccfd..34b8bd2a6b2 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -464,6 +464,9 @@ GEMdeckar01-task_list (2.3.4)html-pipeline (~> 2.0)declarative (0.0.20)+ dentaku (3.5.4)+ bigdecimal+ concurrent-rubydescendants_tracker (0.0.4)thread_safe (~> 0.3, >= 0.3.1)diff-lcs (1.6.2)@@ -1371,6 +1374,7 @@ DEPENDENCIESdashboards!date_validator (~> 0.12.0)deckar01-task_list (~> 2.3.1)+ dentaku (~> 3.5)disposable (~> 0.6.2)doorkeeper (~> 5.8.0)dotenv-rails@@ -1639,6 +1643,7 @@ CHECKSUMSdate_validator (0.12.0) sha256=68c9834da240347b9c17441c553a183572508617ebfbe8c020020f3192ce3058deckar01-task_list (2.3.4) sha256=66abdc7e009ea759732bb53867e1ea42de550e2aa03ac30a015cbf42a04c1667declarative (0.0.20) sha256=8021dd6cb17ab2b61233c56903d3f5a259c5cf43c80ff332d447d395b17d9ff9+ dentaku (3.5.4) sha256=0f897acf360776c43c3b3629134224abbf6a90a59084707af6e194c5d69ad9c7descendants_tracker (0.0.4) sha256=e9c41dd4cfbb85829a9301ea7e7c48c2a03b26f09319db230e6479ccdc780897diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962disposable (0.6.3) sha256=7f2a3fb251bff6cd83f25b164043d4ec3531209b51b066ed476a9df9c2d384ccdiff --git a/app/models/custom_field.rb b/app/models/custom_field.rbindex e891af58154..06f76c891e9 100644--- a/app/models/custom_field.rb+++ b/app/models/custom_field.rb@@ -147,12 +147,19 @@ class CustomField < ApplicationRecord# Additionally, the formula may contain references to custom fields in the form of `cf_123` where 123 is the ID of# the custom field.# Once this basic validation passes, the formula will be parsed and validated by Dentaku, which builds an AST- # and ensures that the formula is really valid. A welcome side-effect of the basic validation done here is that+ # and ensures that the formula is really valid. A welcome side effect of the basic validation done here is that# it prevents built-in functions from being used in the formula, which we do not want to allow.common_chars = '[\+\-\/\*\(\)\s\d\.]'pattern = /\A#{common_chars}+(?:cf_\d+#{common_chars}*)*\z/errors.add(:formula, :invalid) unless formula_string.match?(pattern)++ # TODO: check for valid (i.e. visible & enabled) custom field references (see #cf_ids_used_in_formula)++ # Dentaku will return nil if the formula is invalid.+ # TODO: add support for referenced custom fields by injecting them as variables,+ # e.g. Dentaku(formula_string, cf_123: CustomField.find(123).value)+ errors.add(:formula, :invalid) unless Dentaku(formula_string)enddef has_regexp?@@ -331,8 +338,7 @@ class CustomField < ApplicationRecorddef formula=(value)if value.is_a?(String)- # TODO perform string parsing here- super({ formula: value })+ super({ formula: value, referenced_custom_fields: cf_ids_used_in_formula })elsesuperend@@ -361,6 +367,12 @@ class CustomField < ApplicationRecordprivate+ # Returns a list of custom field IDs used in the formula.+ # For a formula like `2 + cf_12 + cf_4` it returns `[12, 4]`.+ def cf_ids_used_in_formula+ (formula_string || "").scan(/cf_(\d+)/).flatten.map(&:to_i)+ end+def possible_versions(obj, options: {})project = deduce_project(obj)deduce_versions(project, options:)