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
# File 'lib/labkit/logging/field_validator/config.rb', line 83

def header
  <<~HEADER
    # LabKit Logging Field Standardization TODO
    # AUTO-GENERATED FILE. DO NOT EDIT MANUALLY.
    #
    # This file tracks deprecated logging fields that need to be migrated to standard fields.
    # Each offense represents a file using a deprecated field that should be replaced.
    #
    # How to fix: https://gitlab.com/gitlab-org/ruby/gems/labkit-ruby/-/blob/master/doc/FIELD_STANDARDIZATION.md#how-to-fix-an-offense
    #
    # Adding offenses (if fixing is not immediately possible):
    #   LABKIT_LOGGING_TODO_UPDATE=true bundle exec rspec <spec_file>
    #
    # Regenerate entire TODO:
    #   Delete this file and run: LABKIT_LOGGING_TODO_UPDATE=true bundle exec rspec

  HEADER
end

.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 = <<~YAML
    # LabKit Logging Field Standardization TODO
    # This file tracks deprecated logging fields that need migration.
    #
    # To collect offenses from CI:
    #   bundle exec labkit-logging fetch <project> <pipeline_id>
    #
    # To collect offenses locally:
    #   LABKIT_LOGGING_TODO_UPDATE=true <local development process>
    #
    # More info: https://gitlab.com/gitlab-org/ruby/gems/labkit-ruby/-/blob/master/doc/FIELD_STANDARDIZATION.md

    skip_ci_failure: true  # Remove this flag once baseline is established

    offenses: []
  YAML

  write_file(nil, header: content)
end

.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