-
Notifications
You must be signed in to change notification settings - Fork 401
/
submit_review.rb
70 lines (57 loc) · 1.59 KB
/
submit_review.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
65
66
67
68
69
70
# frozen_string_literal: true
class SubmitReview
static_facade :call
pattr_initialize :build
def call
if new_violations.any? || build.review_errors.any?
comments = new_violations.map { |violation| build_comment(violation) }
send_review(comments)
end
if build.repo.installation_id
remove_resolved_violations
end
end
private
def send_review(comments)
github.create_pull_request_review(
build.repo_name,
build.pull_request_number,
comments,
ReviewBody.new(build.review_errors).to_s,
)
end
def remove_resolved_violations
commenting_policy.outdated_comments(build.violations).each do |comment|
github.delete_pull_request_comment(build.repo_name, comment)
end
end
def new_violations
@_new_violations ||= build.violations.
select { |violation| commenting_policy.comment_on?(violation) }.
take(Hound::MAX_COMMENTS)
end
def build_comment(violation)
{
path: violation.filename,
position: violation.patch_position,
body: violation.messages.join(CommentingPolicy::COMMENT_LINE_DELIMITER),
}
end
def commenting_policy
@_commenting_policy ||= CommentingPolicy.new(existing_comments)
end
def existing_comments
github.pull_request_comments(build.repo_name, build.pull_request_number)
end
def github
@_github ||= GitHubApi.new(github_token)
end
def github_token
if build.repo.installation_id
app = GitHubApi.new(AppToken.new.generate)
app.create_installation_token(build.repo.installation_id)
else
Hound::GITHUB_TOKEN
end
end
end