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

event_options.rb « helpers « cli « internal_events « scripts - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f53127798aa06912c24bbeb1dddc352f1a77ef02 (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
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
# frozen_string_literal: true

# Helpers related to listing existing event definitions
module InternalEventsCli
  module Helpers
    module EventOptions
      def get_event_options(events)
        options = events.filter_map do |(path, event)|
          next if duplicate_events?(event.action, events.values)

          description = format_help(" - #{trim_description(event.description)}")

          {
            name: "#{format_event_name(event)}#{description}",
            value: path
          }
        end

        options.sort_by do |option|
          category = events.dig(option[:value], 'category')
          event_sort_param(category, option[:name])
        end
      end

      def events_by_filepath(event_paths = [])
        event_paths = load_event_paths if event_paths.none?

        get_existing_events_for_paths(event_paths)
      end

      private

      def trim_description(description)
        return description if description.to_s.length < 50

        "#{description[0, 50]}..."
      end

      def format_event_name(event)
        case event.category
        when 'InternalEventTracking', 'default'
          event.action
        else
          "#{event.category}:#{event.action}"
        end
      end

      def event_sort_param(category, name)
        case category
        when 'InternalEventTracking'
          "0#{name}"
        when 'default'
          "1#{name}"
        else
          "2#{category}#{name}"
        end
      end

      def get_existing_events_for_paths(event_paths)
        event_paths.each_with_object({}) do |filepath, events|
          details = YAML.safe_load(File.read(filepath))
          fields = InternalEventsCli::NEW_EVENT_FIELDS.map(&:to_s)

          events[filepath] = Event.new(**details.slice(*fields))
        end
      end

      def duplicate_events?(action, events)
        events.count { |event| action == event.action } > 1
      end

      def load_event_paths
        [
          Dir["config/events/*.yml"],
          Dir["ee/config/events/*.yml"]
        ].flatten
      end
    end
  end
end