Module: Labkit::Logging::FieldValidator::Config

Defined in:
lib/labkit/logging/field_validator/config.rb

Constant Summary collapse

FILE_NAME =
'.labkit_logging_todo.yml'

Class Method Summary collapse

Class Method Details

.config_file_exists?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/labkit/logging/field_validator/config.rb', line 68

def config_file_exists?
  File.exist?(path)
end

.deprecated_fieldsObject



22
23
24
# File 'lib/labkit/logging/field_validator/config.rb', line 22

def deprecated_fields
  @deprecated_fields ||= Labkit::Fields::Deprecated.all.freeze
end

.detect_rootObject



76
77
78
79
80
81
# File 'lib/labkit/logging/field_validator/config.rb', line 76

def detect_root
  return Bundler.root.to_s if defined?(Bundler) && Bundler.respond_to?(:root)
  return Rails.root.to_s if defined?(Rails) && Rails.respond_to?(:root)

  Dir.pwd
end

.file_nameObject



18
19
20
# File 'lib/labkit/logging/field_validator/config.rb', line 18

def file_name
  FILE_NAME
end

.headerObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/labkit/logging/field_validator/config.rb', line 83

def header
  "    # LabKit Logging Field Standardization TODO\n    # AUTO-GENERATED FILE. DO NOT EDIT MANUALLY.\n    #\n    # This file tracks deprecated logging fields that need to be migrated to standard fields.\n    # Each offense represents a file using a deprecated field that should be replaced.\n    #\n    # === HOW TO FIX ===\n    #\n    # 1. Replace the deprecated field with the standard field constant\n    # 2. Remove the deprecated field entirely (adding the new field is not enough)\n    # 3. Run your tests - the offense will be automatically removed\n    #\n    # Example:\n    #   # Before\n    #   logger.info(user_id: 123)\n    #\n    #   # After\n    #   logger.info(Labkit::Fields::GL_USER_ID => 123)\n    #\n    # === ADDING OFFENSES (if fixing is not immediately possible) ===\n    #\n    # Run: LABKIT_LOGGING_TODO_UPDATE=true bundle exec rspec <spec_file>\n    #\n    # === REGENERATE ENTIRE TODO ===\n    #\n    # Delete this file and run: LABKIT_LOGGING_TODO_UPDATE=true bundle exec rspec\n\n  HEADER\nend\n"

.init_file!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/labkit/logging/field_validator/config.rb', line 45

def init_file!
  return if config_file_exists?

  content = "    # LabKit Logging Field Standardization TODO\n    # This file tracks deprecated logging fields that need migration.\n    #\n    # To collect offenses from CI:\n    #   bundle exec labkit-logging fetch <project> <pipeline_id>\n    #\n    # To collect offenses locally:\n    #   LABKIT_LOGGING_TODO_UPDATE=true <local development process>\n    #\n    # More info: https://gitlab.com/gitlab-org/ruby/gems/labkit-ruby/-/blob/master/doc/FIELD_STANDARDIZATION.md\n\n    skip_ci_failure: true  # Remove this flag once baseline is established\n\n    offenses: []\n  YAML\n\n  write_file(nil, header: content)\nend\n"

.loadObject



26
27
28
29
30
# File 'lib/labkit/logging/field_validator/config.rb', line 26

def load
  return {} unless config_file_exists?

  YAML.safe_load_file(path) || {}
end

.pathObject



14
15
16
# File 'lib/labkit/logging/field_validator/config.rb', line 14

def path
  @path ||= File.join(root, file_name).freeze
end

.rootObject



10
11
12
# File 'lib/labkit/logging/field_validator/config.rb', line 10

def root
  @root ||= detect_root.freeze
end

.skip_ci_failure?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/labkit/logging/field_validator/config.rb', line 72

def skip_ci_failure?
  load.fetch('skip_ci_failure', false) == true
end

.update!(new_offenses, removed_offenses = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/labkit/logging/field_validator/config.rb', line 32

def update!(new_offenses, removed_offenses = [])
  baseline_offenses = load.fetch('offenses', [])

  if removed_offenses.any?
    removed_keys = removed_offenses.to_set { |o| offense_key(o) }
    baseline_offenses = baseline_offenses.reject { |o| removed_keys.include?(offense_key(o)) }
  end

  updated = process_offenses(baseline_offenses + new_offenses)
  write_file({ 'offenses' => updated }, header: header)
  updated
end