class Linterbot::CommentGenerator

Attributes

commit[RW]
commits_count_for_file[RW]
filename[RW]
pull_request_file_patch[RW]

Public Class Methods

new(filename, commit, pull_request_file_patch, commits_count_for_file) click to toggle source
# File lib/linterbot/comment_generator.rb, line 10
def initialize(filename, commit, pull_request_file_patch, commits_count_for_file)
  @filename = filename
  @commit = commit
  @pull_request_file_patch = Patch.new(pull_request_file_patch)
  @commits_count_for_file = commits_count_for_file
end

Public Instance Methods

file() click to toggle source
# File lib/linterbot/comment_generator.rb, line 29
def file
  @file ||= find_file
end
generate_comment_for_hint(hint) click to toggle source
# File lib/linterbot/comment_generator.rb, line 22
def generate_comment_for_hint(hint)
  patch_line_number = comment_position_for_hint(hint)
  if patch_line_number
    Comment.new(sha: commit.sha, patch_line_number: patch_line_number, hint: hint)
  end
end
generate_comments(hints) click to toggle source
# File lib/linterbot/comment_generator.rb, line 17
def generate_comments(hints)
  hints.map { |hint| generate_comment_for_hint(hint) }
    .select { |comment| comment != nil }
end

Private Instance Methods

comment_position_for_hint(hint) click to toggle source
# File lib/linterbot/comment_generator.rb, line 63
def comment_position_for_hint(hint)
  if new_file? && commits_count_for_file == 1
    hint.line
  elsif modified_file? && included_in_file_patch?(hint)
    pull_request_file_patch_line_number(hint)
  end
end
file_patch() click to toggle source
# File lib/linterbot/comment_generator.rb, line 48
def file_patch
  Patch.new(file.patch)
end
find_file() click to toggle source
# File lib/linterbot/comment_generator.rb, line 35
def find_file
  file_index = commit.files.find_index { |file| file.filename == filename }
  commit.files[file_index]
end
included_in_file_patch?(hint) click to toggle source
# File lib/linterbot/comment_generator.rb, line 52
def included_in_file_patch?(hint)
  file_patch.included_in_patch?(hint)
end
modified_file?() click to toggle source
# File lib/linterbot/comment_generator.rb, line 44
def modified_file?
  file.status == "modified"
end
new_file?() click to toggle source
# File lib/linterbot/comment_generator.rb, line 40
def new_file?
  file.status == "added"
end
pull_request_file_patch_line_number(hint) click to toggle source
# File lib/linterbot/comment_generator.rb, line 56
def pull_request_file_patch_line_number(hint)
  pull_request_file_patch
    .additions_ranges_for_hint(hint)
    .map { |diff_range, line_number|  line_number + (hint.line - diff_range.first) + 1 }
    .first
end