Class: Shiba::Reviewer

Inherits:
Object
  • Object
show all
Defined in:
lib/shiba/reviewer.rb

Overview

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

Constant Summary collapse

MESSAGE_FILTER_THRESHOLD =
0.005

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_url, problems, options) ⇒ Reviewer

Returns a new instance of Reviewer.



17
18
19
20
21
22
23
24
25
26
# File 'lib/shiba/reviewer.rb', line 17

def initialize(repo_url, problems, options)
  @repo_url = repo_url
  @problems = problems
  @options = options
  @commit_id = options.fetch("branch") do
    if options["submit"]
      raise Shiba::Error.new("Must specify a branch")
    end
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/shiba/reviewer.rb', line 15

def options
  @options
end

#problemsObject (readonly)

Returns the value of attribute problems.



15
16
17
# File 'lib/shiba/reviewer.rb', line 15

def problems
  @problems
end

#repo_urlObject (readonly)

Returns the value of attribute repo_url.



15
16
17
# File 'lib/shiba/reviewer.rb', line 15

def repo_url
  @repo_url
end

Instance Method Details

#commentsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shiba/reviewer.rb', line 28

def comments
  return @comments if @comments

  @comments = problems.map do |path, explain|
    file, line_number = path.split(":")
    if path.empty? || line_number.nil?
      raise Shiba::Error.new("Bad path received: #{line_number}")
    end

    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,
      path: file,
      line: line_number,
      position: position }
  end
end

#repo_hostObject



81
82
83
# File 'lib/shiba/reviewer.rb', line 81

def repo_host
  @repo_host ||= api.host_and_path.first
end

#repo_pathObject



85
86
87
# File 'lib/shiba/reviewer.rb', line 85

def repo_path
  @repo_path ||= api.host_and_path.last
end

#submitObject

FIXME: Only submit 10 comments for now. The rest just vanish. Submits commits, checking to makre sure the line doesn’t already have a review.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/shiba/reviewer.rb', line 55

def submit
  report("Connecting to #{api.uri}")

  api.connect do
    previous_reviews = api.previous_comments.map { |c| c['body'] }

    comments[0,10].each do |c|
      if previous_reviews.any? { |r| r == c[:body] }
        report("skipped duplicate comment")
        next
      end

      # :line isn't part of the github api
      comment = c.dup.tap { |dc| dc.delete(:line) }
      if options[:verbose]
        comment[:body] += " (verbose mode ts=#{Time.now.to_i})"
      end

      res = api.comment_on_pull_request(comment)
      report("API success #{res.inspect}")
    end
  end

  report("HTTP request finished")
end