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
[#64348] Use pattern input for formulas
Tobias Dillmann68ea8d302a54
diff --git a/app/forms/custom_fields/calculated_values/details_form.rb b/app/forms/custom_fields/calculated_values/details_form.rbindex 3bb8fd62f79..c62974f8f31 100644--- a/app/forms/custom_fields/calculated_values/details_form.rb+++ b/app/forms/custom_fields/calculated_values/details_form.rb@@ -53,9 +53,10 @@ module CustomFieldsendend- details_form.text_field(+ details_form.pattern_input(name: :formula,value: model.formula_string,+ suggestions: formula_suggestions,label: I18n.t(:label_formula),required: true,caption: I18n.t("custom_fields.instructions.formula")@@ -75,6 +76,22 @@ module CustomFieldsdetails_form.submit(name: :submit, label: I18n.t(:button_save), scheme: :primary)end++ private++ def formula_suggestions+ # TODO: move allowed operators to a constant of the model+ operators = %w[+ - * / ( )].map { |op| { key: op, label: op } }+ custom_fields = ProjectCustomField.where(field_format: %w[int float calculated_value]).map do |cf|+ { key: "cf_#{cf.id}", label: "#{cf.name} (#{helpers.label_for_custom_field_format(cf.field_format)})" }+ end++ # TODO: localize titles+ {+ operators: { title: "Operators", tokens: operators },+ custom_fields: { title: "Custom Fields", tokens: custom_fields }+ }+ endendendenddiff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex ffe933e6e45..ef607f226f4 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -51,13 +51,15 @@ module CustomField::CalculatedValue# WP-64348: 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 WP-64348: add support for referenced custom fields by injecting them as variables,- # e.g. calculator.evaluate(formula_string, cf_123: CustomField.find(123).value)- errors.add(:formula, :invalid) unless Dentaku::Calculator.new.evaluate(formula_string)+ calculation_result = perform_calculation+ unless calculation_result+ errors.add(:formula, :invalid)+ return+ end# TODO: consider differentiating between a formula that contains missing variables, invalid# syntax, or mathematical errors.+ true # return anything to appease RuboCop for nowenddef formula=(value)@@ -75,17 +77,30 @@ module CustomField::CalculatedValueprivate+ def perform_calculation+ # Dentaku will return nil if the formula is invalid.+ # Accept both cf_123 and {{cf_123}} in the formula string+ formula_for_eval = formula_string.gsub(/\{\{(cf_\d+)}}/, '\1')++ variables = cf_ids_used_in_formula(formula_for_eval).map do |cf_id|+ # TODO: insert real value here instead of hardcoded number+ ["cf_#{cf_id}", 1]+ end++ Dentaku::Calculator.new.evaluate(formula_for_eval, variables.to_h)+ end+def formula_contains_only_allowed_characters?# List of allowed characters in a formula. This only performs a very basic validation.# Allowed characters are:# + - / * ( ) whitespace digits and decimal points- # Additionally, the formula may contain references to custom fields in the form of `cf_123` where 123 is the ID of- # the custom field.+ # Additionally, the formula may contain references to custom fields in the form of `cf_123` or `{{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# it prevents built-in functions from being used in the formula, which we do not want to allow.allowed_chars = %w[+ - / * ( )] + [" "]- allowed_tokens = /\A(cf_\d+|\d+\.?\d*|\.\d+)\z/+ allowed_tokens = /\A(cf_\d+|\{\{cf_\d+}}|\d+\.?\d*|\.\d+)\z/formula_string.split(Regexp.union(allowed_chars)).reject(&:empty?).all? do |token|token.match?(allowed_tokens)@@ -93,9 +108,10 @@ module CustomField::CalculatedValueend# Returns a list of custom field IDs used in the formula.- # For a formula like `2 + cf_12 + cf_4` it returns `[12, 4]`.+ # For a formula like `2 + cf_12 + cf_4` or `2 + {{cf_12}} + {{cf_4}}` it returns `[12, 4]`.def cf_ids_used_in_formula(formula_str)- formula_str.scan(/\bcf_(\d+)\b/).flatten.map(&:to_i)+ # Match both cf_123 and {{cf_123}}+ formula_str.scan(/\b(?:cf_(\d+)|\{\{cf_(\d+)}})\b/).flatten.compact.map(&:to_i)endendend