-
Notifications
You must be signed in to change notification settings - Fork 401
/
commenting_policy.rb
64 lines (51 loc) · 1.53 KB
/
commenting_policy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
class CommentingPolicy
COMMENT_LINE_DELIMITER = "<br>"
pattr_initialize :comments
def comment_on?(violation)
unreported_violation_messages(violation).any?
end
def outdated_comments(found_violations)
comments.select do |comment|
comment.user.type == "Bot" &&
comment.user.login.start_with?("hound") &&
outdated_comment?(comment, found_violations)
end
end
private
def outdated_comment?(comment, violations)
violations.none? do |violation|
matches_location?(violation, comment) &&
matches_messages?(violation, comment)
end
end
def unreported_violation_messages(violation)
violation.messages - existing_comment_messages(violation)
end
def existing_comment_messages(violation)
existing_comments_on_line(violation).flat_map do |comment|
comment_messages(comment)
end
end
def comment_messages(comment)
comment.body.split(COMMENT_LINE_DELIMITER)
end
def existing_comments_on_line(violation)
comments.select do |comment|
matches_location?(violation, comment)
end
end
def matches_location?(violation, comment)
comment.path == violation.filename && on_same_line?(violation, comment)
end
def on_same_line?(violation, comment)
if comment.position
comment.position == violation.patch_position
else
comment.original_position == violation.patch_position
end
end
def matches_messages?(violation, comment)
(comment_messages(comment) & violation.messages).any?
end
end