Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/serializers/integrations/field_entity.rb')
-rw-r--r--app/serializers/integrations/field_entity.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/app/serializers/integrations/field_entity.rb b/app/serializers/integrations/field_entity.rb
new file mode 100644
index 00000000000..697b53a737e
--- /dev/null
+++ b/app/serializers/integrations/field_entity.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+module Integrations
+ class FieldEntity < Grape::Entity
+ include RequestAwareEntity
+ include Gitlab::Utils::StrongMemoize
+
+ expose :section, :type, :name, :placeholder, :required, :choices, :checkbox_label
+
+ expose :title do |field|
+ non_empty_password?(field) ? field[:non_empty_password_title] : field[:title]
+ end
+
+ expose :help do |field|
+ non_empty_password?(field) ? field[:non_empty_password_help] : field[:help]
+ end
+
+ expose :value do |field|
+ value = value_for(field)
+
+ if non_empty_password?(field)
+ 'true'
+ elsif field[:type] == 'checkbox'
+ ActiveRecord::Type::Boolean.new.deserialize(value).to_s
+ else
+ value
+ end
+ end
+
+ private
+
+ def integration
+ request.integration
+ end
+
+ def value_for(field)
+ strong_memoize(:value_for) do
+ # field[:name] is not user input and so can assume is safe
+ integration.public_send(field[:name]) # rubocop:disable GitlabSecurity/PublicSend
+ end
+ end
+
+ def non_empty_password?(field)
+ strong_memoize(:non_empty_password) do
+ field[:type] == 'password' && value_for(field).present?
+ end
+ end
+ end
+end