Class: Aireview::CandidateChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/aireview/candidate_checker.rb

Overview

A mechanical check that a candidate points at the code, against the diff the model actually saw: file, line, quote. The anchoring is checked, not the bug itself: a quote that is not found is a reason for Critique to look closer, not proof of a fabrication. A file that is not among the MR changes is a different matter: there is nothing to check such a candidate against, it is dropped.

Defined Under Namespace

Classes: Section

Constant Summary collapse

HUNK_HEADER =
/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/
FILE_HEADER =
%r{^diff --git a/(.+?) b/(.+)$}
NOTE_QUOTE_NOT_FOUND =
'quoted_code not found in the diff shown to the model'
NOTE_LINE_RESET =
'line was outside the shown hunks and has been reset to null'
NOTE_NOT_VERIFIED =
'file shown partially or without a diff, location not verified'

Instance Method Summary collapse

Constructor Details

#initialize(changes:, diff_text:, coverage:, logger: Logger.new($stderr)) ⇒ CandidateChecker

Returns a new instance of CandidateChecker.



31
32
33
34
35
36
# File 'lib/aireview/candidate_checker.rb', line 31

def initialize(changes:, diff_text:, coverage:, logger: Logger.new($stderr))
  @mr_paths = changes.flat_map { |change| change.values_at('old_path', 'new_path') }.compact.to_set
  @coverage = coverage
  @logger = logger
  @sections = parse_sections(diff_text.to_s)
end

Instance Method Details

#check(candidates) ⇒ Object

Returns the candidates with marks: a note for Critique, quote_missing for the report, line reset to null when it was not confirmed. Candidates with a file outside the MR are dropped.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aireview/candidate_checker.rb', line 41

def check(candidates)
  candidates.filter_map do |candidate|
    id = value(candidate, 'id')
    path = resolve_path(value(candidate, 'file'))
    unless path
      @logger.warn("Candidate #{id} dropped: file #{value(candidate, 'file').inspect} is not in the merge request")
      next
    end

    annotate(candidate.dup, id: id, section: @sections[path])
  end
end