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

field.rb « integrations « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbda418755b086d8d8ae3a02690e5d81e1504b2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

module Integrations
  class Field
    SECRET_NAME = %r/token|key|password|passphrase|secret/.freeze

    ATTRIBUTES = %i[
      section type placeholder required choices value checkbox_label
      title help
      non_empty_password_help
      non_empty_password_title
      api_only
      exposes_secrets
    ].freeze

    attr_reader :name, :integration_class

    def initialize(name:, integration_class:, type: 'text', api_only: false, **attributes)
      @name = name.to_s.freeze
      @integration_class = integration_class

      attributes[:type] = SECRET_NAME.match?(@name) ? 'password' : type
      attributes[:api_only] = api_only
      @attributes = attributes.freeze
    end

    def [](key)
      return name if key == :name

      value = @attributes[key]
      return integration_class.class_exec(&value) if value.respond_to?(:call)

      value
    end

    def secret?
      @attributes[:type] == 'password'
    end

    ATTRIBUTES.each do |name|
      define_method(name) { self[name] }
    end
  end
end