Class: RuboCop::Formatter::GitLabFormatter

Inherits:
BaseFormatter
  • Object
show all
Includes:
PathUtil
Defined in:
lib/rubocop/formatter/gitlab_formatter.rb

Overview

A very simple RuboCop formatter for use with GitLab code quality artifact

Constant Summary collapse

SEVERITY_MAPPING =
{
  I: 'info',
  R: 'minor',
  C: 'minor',
  W: 'major',
  E: 'critical',
  F: 'blocker'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, options = {}) ⇒ GitLabFormatter

Returns a new instance of GitLabFormatter.



24
25
26
27
28
# File 'lib/rubocop/formatter/gitlab_formatter.rb', line 24

def initialize(output, options = {})
  super

  @output_array = []
end

Instance Attribute Details

#output_arrayObject (readonly)

Returns the value of attribute output_array.



22
23
24
# File 'lib/rubocop/formatter/gitlab_formatter.rb', line 22

def output_array
  @output_array
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object



30
31
32
33
34
# File 'lib/rubocop/formatter/gitlab_formatter.rb', line 30

def file_finished(file, offenses)
  offenses.each do |offense|
    output_array << hash_for_offense(file, offense)
  end
end

#finished(_inspected_files) ⇒ Object



36
37
38
# File 'lib/rubocop/formatter/gitlab_formatter.rb', line 36

def finished(_inspected_files)
  output.write output_array.to_json
end

#hash_for_location(file, location) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rubocop/formatter/gitlab_formatter.rb', line 49

def hash_for_location(file, location)
  {
    path: smart_path(file),
    lines: {
      begin: location.line
    }
  }
end

#hash_for_offense(file, offense) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rubocop/formatter/gitlab_formatter.rb', line 40

def hash_for_offense(file, offense)
  {
    description: offense.message,
    fingerprint: offense.hash,
    severity: SEVERITY_MAPPING[offense.severity.code.to_sym],
    location: hash_for_location(file, offense.location)
  }
end