lib/shiba/reviewer.rb in shiba-0.5.0 vs lib/shiba/reviewer.rb in shiba-0.6.0
- old
+ new
@@ -1,28 +1,29 @@
require 'open3'
require 'shiba'
-require 'shiba/diff'
+require 'shiba/review/diff'
require 'shiba/review/api'
require 'shiba/review/comment_renderer'
module Shiba
# TODO:
# 1. Properly handle more than a handful of review failures
# 2. May make sense to edit the comment on a commit line when the code
# is semi-corrected but still a problem
class Reviewer
- TEMPLATE_FILE = File.join(Shiba.root, 'lib/shiba/output/tags.yaml')
MESSAGE_FILTER_THRESHOLD = 0.005
attr_reader :repo_url, :problems, :options
def initialize(repo_url, problems, options)
@repo_url = repo_url
@problems = problems
@options = options
@commit_id = options.fetch("branch") do
- raise Shiba::Error.new("Must specify a branch") if !options['diff']
+ if options["submit"]
+ raise Shiba::Error.new("Must specify a branch")
+ end
end
end
def comments
return @comments if @comments
@@ -31,11 +32,15 @@
file, line_number = path.split(":")
if path.empty? || line_number.nil?
raise Shiba::Error.new("Bad path received: #{line_number}")
end
- position = diff.find_position(file, line_number.to_i)
+ position = if path == "none:-1"
+ nil
+ else
+ diff_parser.find_position(file, line_number.to_i)
+ end
explain = keep_only_dangerous_messages(explain)
{ body: renderer.render(explain),
commit_id: @commit_id,
@@ -95,28 +100,24 @@
message['cost'] && message['cost'] > MESSAGE_FILTER_THRESHOLD
end
explain_b
end
- def diff
- return @diff if @diff
- output = options['diff'] ? file_diff : git_diff
- @diff = Shiba::Diff.new(output)
+ def diff_parser
+ @diff_parser ||= Review::Diff::Parser.new(diff.file)
end
- def git_diff
- cmd ="git diff origin/HEAD..#{@commit_id}"
- report("Finding PR position using: #{cmd}")
-
- output = StringIO.new(`#{cmd}`)
+ def diff
+ @diff ||= if options['diff']
+ report("Finding PR position using file: #{options['diff']}")
+ Review::Diff::FileDiff.new(options['diff'])
+ else
+ report("Finding PR position using git")
+ Review::Diff::GitDiff.new(options)
+ end
end
- def file_diff
- report("Finding PR position using file: #{options['diff']}")
- File.open(options['diff'], 'r')
- end
-
def api
@api ||= begin
api_options = {
"token" => options["token"],
"pull_request" => options["pull_request"]
@@ -128,10 +129,10 @@
def renderer
@renderer ||= Review::CommentRenderer.new(tags)
end
def tags
- @tags ||= YAML.load_file(TEMPLATE_FILE)
+ @tags ||= YAML.load_file(Shiba::TEMPLATE_FILE)
end
end
end