class Ducalis::Commentators::Github

Constants

SIMILARITY_THRESHOLD
STATUS

Public Class Methods

new(repo, id) click to toggle source
# File lib/ducalis/commentators/github.rb, line 9
def initialize(repo, id)
  @repo = repo
  @id = id
end

Public Instance Methods

call(offenses) click to toggle source
# File lib/ducalis/commentators/github.rb, line 14
def call(offenses)
  comments = offenses.reject { |offense| already_commented?(offense) }
                     .map { |offense| present_offense(offense) }

  return if comments.empty?

  Utils.octokit
       .create_pull_request_review(@repo, @id,
                                   event: STATUS, comments: comments)
end

Private Instance Methods

already_commented?(offense) click to toggle source
# File lib/ducalis/commentators/github.rb, line 27
def already_commented?(offense)
  current_offence = present_offense(offense)
  commented_offenses.find do |commented_offense|
    [
      current_offence[:path] == commented_offense[:path],
      current_offence[:position] == commented_offense[:position],
      similar_messages?(current_offence[:body], commented_offense[:body])
    ].all?
  end
end
commented_offenses() click to toggle source
# File lib/ducalis/commentators/github.rb, line 55
def commented_offenses
  @commented_offenses ||= Utils.octokit.pull_request_comments(@repo, @id)
end
diff_for(offense) click to toggle source
# File lib/ducalis/commentators/github.rb, line 51
def diff_for(offense)
  GitAccess.instance.for(offense.location.source_buffer.name)
end
present_offense(offense) click to toggle source
# File lib/ducalis/commentators/github.rb, line 43
def present_offense(offense)
  {
    body: offense.message,
    path: diff_for(offense).path,
    position: diff_for(offense).patch_line(offense.line)
  }
end
similar_messages?(message, body) click to toggle source
# File lib/ducalis/commentators/github.rb, line 38
def similar_messages?(message, body)
  body.include?(message) ||
    Utils.similarity(message, body) > SIMILARITY_THRESHOLD
end