class Linterbot::GitHubPullRequestCommenter

Attributes

github_client[RW]
pull_request_number[RW]
repository[RW]

Public Class Methods

new(repository, pull_request_number, github_client) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 11
def initialize(repository, pull_request_number, github_client)
  @repository = repository
  @pull_request_number = pull_request_number
  @github_client = github_client
end

Public Instance Methods

publish_comment(comment) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 17
def publish_comment(comment)
  message = "#{comment.severity.upcase} - #{comment.message}\n"
  if comment_exist?(message)
    puts "Comment was not published because it already exists: #{message}"
  else
    create_pull_request_comment(message, comment.sha, comment.file, comment.patch_line_number)
  end
end
publish_summary(summary) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 26
def publish_summary(summary)
  if same_as_last_summary?(summary)
    puts "Summary was not published because it's the same as the last result summary:\n #{summary}"
  else
    github_client.add_comment(repository, pull_request_number, summary)
  end
end

Private Instance Methods

bot_github_id() click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 79
def bot_github_id
  @bot_github_id ||= github_client.user[:id]
end
comment_exist?(message) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 48
def comment_exist?(message)
  existing_comments.find { |comment| same_comment?(comment, message) }
end
create_pull_request_comment(message, sha, file, patch_line_number) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 36
def create_pull_request_comment(message, sha, file, patch_line_number)
  args = [
    repository,
    pull_request_number,
    message,
    sha,
    file,
    patch_line_number
  ]
  github_client.create_pull_request_comment(*args)
end
existing_comments() click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 68
def existing_comments
  @existing_comments ||= fetch_existing_comments("pull_request")
end
existing_summaries() click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 52
def existing_summaries
  @existing_summaries ||= fetch_existing_comments("issue")
end
fetch_existing_comments(source) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 72
def fetch_existing_comments(source)
  github_client.send("#{source}_comments", repository, pull_request_number).map do |comment|
    user = OpenStruct.new(comment[:user].to_h)
    OpenStruct.new(comment.to_h.merge(user: user))
  end
end
latest_existing_comment() click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 56
def latest_existing_comment
  @latest_existing_comment ||= existing_summaries.sort { |a, b| b.created_at <=> a.created_at }.first
end
same_as_last_summary?(summary) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 60
def same_as_last_summary?(summary)
  latest_existing_comment && same_comment?(latest_existing_comment, summary)
end
same_comment?(comment, message) click to toggle source
# File lib/linterbot/github_pull_request_commenter.rb, line 64
def same_comment?(comment, message)
  comment.body == message && comment.user.id == bot_github_id
end