summaryrefslogtreecommitdiff
path: root/lib/generators/gitlab/snowplow_event_definition_generator.rb
blob: 8baeb6ba8c7b19e3095a6af2e5b4e1fcdff685dd (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
# frozen_string_literal: true

require 'rails/generators'

module Gitlab
  class SnowplowEventDefinitionGenerator < Rails::Generators::Base
    CE_DIR = 'config/events'
    EE_DIR = 'ee/config/events'

    source_root File.expand_path('../../../generator_templates/snowplow_event_definition', __dir__)

    desc 'Generates an event definition yml file'

    class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if event is for ee'
    class_option :category, type: :string, optional: false, desc: 'Category of the event'
    class_option :action, type: :string, optional: false, desc: 'Action of the event'

    def create_event_file
      raise "Event definition already exists at #{file_path}" if definition_exists?

      template "event_definition.yml", file_path, force: false
    end

    def distributions
      (ee? ? ['- ee'] : ['- ce', '- ee']).join("\n")
    end

    def event_category
      options[:category]
    end

    def event_action
      options[:action]
    end

    def milestone
      Gitlab::VERSION.match('(\d+\.\d+)').captures.first
    end

    def ee?
      options[:ee]
    end

    private

    def definition_exists?
      File.exist?(ce_file_path) || File.exist?(ee_file_path)
    end

    def file_path
      ee? ? ee_file_path : ce_file_path
    end

    def ce_file_path
      File.join(CE_DIR, file_name)
    end

    def ee_file_path
      File.join(EE_DIR, file_name)
    end

    def file_name
      name = remove_special_chars("#{Time.current.to_i}_#{event_category}_#{event_action}")
      "#{name[0..95]}.yml" # max 100 chars, see https://gitlab.com/gitlab-com/gl-infra/delivery/-/issues/2030#note_679501200
    end

    def remove_special_chars(input)
      input.gsub("::", "__").gsub(/[^A-Za-z0-9_]/, '')
    end
  end
end