Class: RailVerdict::Analyzers::RuboCop

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_verdict/analyzers/rubocop.rb

Defined Under Namespace

Classes: MalformedOutput, Probe

Constant Summary collapse

ANALYZER_ID =
"rubocop"
SUPPORTED_VERSIONS =
Gem::Requirement.new(">= 1.72", "< 2")
SEVERITY_MAP =
{
  "info" => "info",
  "refactor" => "low",
  "convention" => "low",
  "warning" => "medium",
  "error" => "high",
  "fatal" => "critical"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(command_resolver: nil) ⇒ RuboCop

Returns a new instance of RuboCop.



25
26
27
# File 'lib/rail_verdict/analyzers/rubocop.rb', line 25

def initialize(command_resolver: nil)
  @command_resolver = command_resolver || method(:default_command)
end

Instance Method Details

#probe(repository_root, runner: ProcessRunner, timeout_seconds: 15.0) ⇒ Object



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
# File 'lib/rail_verdict/analyzers/rubocop.rb', line 29

def probe(repository_root, runner: ProcessRunner, timeout_seconds: 15.0)
  command = @command_resolver.call(repository_root)
  invocation = invocation_for(command, ["--version"])
  result = runner.run(
    command.fetch(:executable),
    invocation.fetch("argv"),
    chdir: repository_root,
    timeout_seconds: timeout_seconds
  )

  return Probe.new(status: "unavailable", message: detail_for(result)) if result.status == :spawn_failed
  return Probe.new(status: "timed_out", message: detail_for(result)) if result.status == :timed_out
  return Probe.new(status: "signaled", message: detail_for(result)) if result.status == :signaled
  return Probe.new(status: "truncated", message: detail_for(result)) if truncated?(result)
  return Probe.new(status: "unavailable", message: detail_for(result)) unless result.exit_code == 0

  version = parse_version(result.stdout)
  return Probe.new(status: "unsupported", message: "RuboCop version could not be parsed") unless version
  return Probe.new(status: "unsupported", version: version, message: "unsupported RuboCop version #{version}") unless SUPPORTED_VERSIONS.satisfied_by?(Gem::Version.new(version))

  provenance = probe_provenance(repository_root)

  Probe.new(status: "succeeded", version: version, rubocop_rails_version: provenance[:rubocop_rails_version], rubocop_config_digest: provenance[:rubocop_config_digest])
rescue ArgumentError
  Probe.new(status: "unsupported", message: "RuboCop reported an invalid version")
rescue KeyError, ArgumentError => error
  Probe.new(status: "malformed", message: bounded_message(error.message))
end

#run(repository_root, runner: ProcessRunner, timeout_seconds: 30.0, probe_result: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
# File 'lib/rail_verdict/analyzers/rubocop.rb', line 58

def run(repository_root, runner: ProcessRunner, timeout_seconds: 30.0, probe_result: nil)
  command = @command_resolver.call(repository_root)
  probe_result ||= probe(repository_root, runner: runner, timeout_seconds: timeout_seconds)
  version_invocation = invocation_for(command, ["--version"])

  unless probe_result.status == "succeeded"
    return [failure_result(version_invocation, probe_result.status, probe_result.message, tool_version: probe_result.version), []]
  end

  invocation = invocation_for(command, ["--format", "json"])
  result = runner.run(
    command.fetch(:executable),
    invocation.fetch("argv"),
    chdir: repository_root,
    timeout_seconds: timeout_seconds
  )
  tool_version = probe_result.version

  if result.status == :spawn_failed
    return [failure_result(invocation, "unavailable", detail_for(result), tool_version: tool_version), []]
  end
  if truncated?(result)
    return [failure_result(invocation, "truncated", detail_for(result), tool_version: tool_version), []]
  end
  if result.status == :timed_out
    return [failure_result(invocation, "timed_out", detail_for(result), tool_version: tool_version), []]
  end
  if result.status == :signaled
    return [failure_result(invocation, "signaled", detail_for(result), tool_version: tool_version), []]
  end
  if result.exit_code && result.exit_code >= 2
    return [failure_result(invocation, "failed", execution_message(result), tool_version: tool_version), []]
  end

  begin
    parsed = JSON.parse(result.stdout)
  rescue JSON::ParserError => error
    return [failure_result(invocation, "parse_failed", bounded_message(error.message), tool_version: tool_version), []]
  end

  findings = normalize_findings(parsed)
  evidence_summary = build_evidence_summary(probe_result)
  analyzer_result = AnalyzerResult.new(
    analyzer: ANALYZER_ID,
    tool_version: tool_version,
    invocation: invocation,
    execution_status: "succeeded",
    finding_ids: findings.map(&:id),
    evidence_summary: evidence_summary.empty? ? nil : evidence_summary
  )
  [analyzer_result, findings]
rescue MalformedOutput => error
  [failure_result(invocation || { "executable" => "rubocop", "argv" => [] }, "malformed", bounded_message(error.message)), []]
end