Class: CodeScanning::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/code_scanning/rubocop/rule.rb

Instance Method Summary collapse

Constructor Details

#initialize(cop_name, severity = nil) ⇒ Rule

Returns a new instance of Rule.



7
8
9
10
11
# File 'lib/code_scanning/rubocop/rule.rb', line 7

def initialize(cop_name, severity = nil)
  @cop_name = cop_name
  @severity = severity.to_s
  @cop = RuboCop::Cop::Cop.registry.find_by_cop_name(cop_name)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



26
27
28
# File 'lib/code_scanning/rubocop/rule.rb', line 26

def ==(other)
  badge.match?(other.badge)
end

#badgeObject



31
32
33
# File 'lib/code_scanning/rubocop/rule.rb', line 31

def badge
  @cop.badge
end

#cop_configObject



80
81
82
83
84
# File 'lib/code_scanning/rubocop/rule.rb', line 80

def cop_config
  @config ||= RuboCop::ConfigStore.new.for(Pathname.new(Dir.pwd))
  @cop_config ||= @config.for_cop(@cop.department.to_s)
                         .merge(@config.for_cop(@cop))
end

#department_uriObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/code_scanning/rubocop/rule.rb', line 52

def department_uri
  case badge.department
  when :Performance
    "https://docs.rubocop.org/rubocop-performance/index.html"
  when :Packaging
    "https://docs.rubocop.org/rubocop-packaging/cops_packaging.html"
  when :Rails
    "https://docs.rubocop.org/rubocop-rails/cops_rails.html"
  when :Minitest
    "https://docs.rubocop.org/rubocop-minitest/cops_minitest.html"
  when :RSpec
    "https://docs.rubocop.org/rubocop-rspec/cops_rspec.html"
  when :"RSpec/Rails"
    "https://docs.rubocop.org/rubocop-rspec/cops_rspec_rails.html"
  when :"RSpec/Capybara"
    "https://docs.rubocop.org/rubocop-rspec/cops_rspec_capybara.html"
  when :"RSpec/FactoryBot"
    "https://docs.rubocop.org/rubocop-rspec/cops_rspec_factorybot.html"
  else
    STDERR.puts "WARNING: Unknown docs URI for department #{badge.department}"
    nil
  end
end

#help(format) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/code_scanning/rubocop/rule.rb', line 17

def help(format)
  case format
  when :text
    "More info: #{help_uri}"
  when :markdown
    "[More info](#{help_uri})"
  end
end

#help_uriObject



44
45
46
47
48
49
50
# File 'lib/code_scanning/rubocop/rule.rb', line 44

def help_uri
  return @cop.documentation_url if @cop.documentation_url
  return nil unless department_uri

  anchor = "#{badge.department}#{badge.cop_name}".downcase.tr("/", "")
  "#{department_uri}##{anchor}"
end

#idObject



13
14
15
# File 'lib/code_scanning/rubocop/rule.rb', line 13

def id
  @cop_name
end

#sarif_severityObject



35
36
37
38
39
40
41
42
# File 'lib/code_scanning/rubocop/rule.rb', line 35

def sarif_severity
  cop_severity = @cop.new.send(:find_severity, nil, @severity)
  return cop_severity if %w[warning error].include?(cop_severity)
  return "note" if %w[refactor convention].include?(cop_severity)
  return "error" if cop_severity == "fatal"

  "none"
end

#to_hObject



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
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/code_scanning/rubocop/rule.rb', line 86

def to_h
  properties = {
    "precision" => "very-high"
  }

  h = {
    "id" => @cop_name,
    "name" => @cop_name.tr("/", "").gsub("RSpec", "Rspec"),
    "defaultConfiguration" => {
      "level" => sarif_severity
    },
    "properties" => properties
  }

  desc = cop_config["Description"]
  unless desc.nil?
    h["shortDescription"] = { "text" => desc }
    h["fullDescription"] = { "text" => desc }
    properties["description"] = desc
  end

  if badge.qualified?
    kind = badge.department.to_s
    properties["tags"] = [kind.downcase]
  end

  if help_uri
    properties["queryURI"] = help_uri

    h.merge!(
      "helpUri" => help_uri,
      "help" => {
        "text" => help(:text),
        "markdown" => help(:markdown)
      }
    )
  end

  h
end

#to_json(opts = {}) ⇒ Object



76
77
78
# File 'lib/code_scanning/rubocop/rule.rb', line 76

def to_json(opts = {})
  to_h.to_json(opts)
end