File: extensions_gallery_metadata_generator.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (97 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true

module WebIde
  module Settings
    class ExtensionsGalleryMetadataGenerator
      include Messages

      # NOTE: These `disabled_reason` enumeration values are also referenced/consumed in
      #       the "gitlab-web-ide" and "gitlab-web-ide-vscode-fork" projects
      #       (https://gitlab.com/gitlab-org/gitlab-web-ide & https://gitlab.com/gitlab-org/gitlab-web-ide-vscode-fork),
      #       so we must ensure that any changes made here are also reflected in those projects.
      #       Please also see EE_DISABLED_REASONS in the relevant EE module.
      DISABLED_REASONS = %i[
        no_user
        no_flag
        instance_disabled
        opt_in_unset
        opt_in_disabled
      ].to_h { |reason| [reason, reason] }.freeze

      # @param [Hash] context
      # @return [Hash]
      def self.generate(context)
        return context unless context.fetch(:requested_setting_names).include?(:vscode_extensions_gallery_metadata)

        context => { options: Hash => options }
        options_with_defaults = { user: nil, vscode_extensions_marketplace_feature_flag_enabled: nil }.merge(options)
        options_with_defaults => {
          user: ::User | NilClass => user,
          vscode_extensions_marketplace_feature_flag_enabled: TrueClass | FalseClass | NilClass =>
            extensions_marketplace_feature_flag_enabled
        }

        extensions_gallery_metadata = build_metadata(
          user: user,
          flag_enabled: extensions_marketplace_feature_flag_enabled
        )

        context[:settings][:vscode_extensions_gallery_metadata] = extensions_gallery_metadata
        context
      end

      # @param [User, nil] user
      # @param [Boolean, nil] flag_enabled
      # @return [Hash]
      def self.build_metadata(user:, flag_enabled:)
        return metadata_disabled(:no_user) unless user
        return metadata_disabled(:no_flag) if flag_enabled.nil?
        return metadata_disabled(:instance_disabled) unless flag_enabled

        build_metadata_for_user(user)
      end

      def self.disabled_reasons
        DISABLED_REASONS
      end

      # note: This is overridden in EE
      #
      # @param [User] user
      # @return [Hash]
      def self.build_metadata_for_user(user)
        # noinspection RubyNilAnalysis -- RubyMine doesn't realize user can't be nil because of guard clause above
        opt_in_status = user.extensions_marketplace_opt_in_status.to_sym

        case opt_in_status
        when :enabled
          metadata_enabled
        when :unset
          metadata_disabled(:opt_in_unset)
        when :disabled
          metadata_disabled(:opt_in_disabled)
        else
          # This is an internal bug due to an enumeration mismatch/inconsistency with the model
          raise "Invalid user.extensions_marketplace_opt_in_status: '#{opt_in_status}'. " \
            "Supported statuses are: #{Enums::WebIde::ExtensionsMarketplaceOptInStatus.statuses.keys}."
        end
      end

      # @return [Hash]
      def self.metadata_enabled
        { enabled: true }
      end

      # @param [symbol] reason
      # @return [Hash]
      def self.metadata_disabled(reason)
        { enabled: false, disabled_reason: disabled_reasons.fetch(reason) }
      end

      private_class_method :build_metadata, :build_metadata_for_user, :disabled_reasons, :metadata_enabled,
        :metadata_disabled
    end
  end
end

WebIde::Settings::ExtensionsGalleryMetadataGenerator.prepend_mod