forked from houndci/hound
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit the size of GitHub review body comment
GitHub limits the review body (main comment) to be 65,536 characters or less. Otherwise, an error gets thrown. This change will truncate our errors to fit within that limit, while still trying to show the error.
- Loading branch information
Greg Lazarev
committed
May 29, 2017
1 parent
1d6b639
commit 3fa5782
Showing
4 changed files
with
130 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
class ReviewBody | ||
MAX_BODY_LENGTH = 65536 | ||
SUMMARY_LENGTH = 80 | ||
LINE_DELIMITER = "<br>" | ||
HEADER = "Some files could not be reviewed due to errors:" | ||
DETAILS_FORMAT = "<details><summary>%s</summary><pre>%s</pre></details>" | ||
|
||
def initialize(errors) | ||
@errors = errors | ||
end | ||
|
||
def to_s | ||
if errors.any? | ||
error_details = errors.map { |error| build_error_details(error) } | ||
[HEADER].concat(error_details).join | ||
else | ||
"" | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :errors | ||
|
||
def build_error_details(error) | ||
summary = error_summary(error) | ||
details = error[0...(allowed_error_length - summary.length)]. | ||
lines. | ||
map(&:rstrip). | ||
join(LINE_DELIMITER) | ||
|
||
sprintf(DETAILS_FORMAT, summary, details) | ||
end | ||
|
||
def error_summary(error) | ||
error.lines.first.strip.truncate(SUMMARY_LENGTH) | ||
end | ||
|
||
def allowed_error_length | ||
(MAX_BODY_LENGTH - formatting_characters_size) / errors.size | ||
end | ||
|
||
def formatting_characters_size | ||
HEADER.length + all_detail_formats_length + all_lines_delimiters_length | ||
end | ||
|
||
def all_detail_formats_length | ||
DETAILS_FORMAT.size * errors.size | ||
end | ||
|
||
def all_lines_delimiters_length | ||
(errors.flat_map(&:lines).size - errors.size) * LINE_DELIMITER.length | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require "app/models/review_body" | ||
|
||
RSpec.describe ReviewBody do | ||
describe "#to_s" do | ||
context "when no errors are passed" do | ||
it "returns empty string" do | ||
review_body = ReviewBody.new([]) | ||
|
||
result = review_body.to_s | ||
|
||
expect(result).to eq "" | ||
end | ||
end | ||
|
||
context "with errors" do | ||
it "returns formatted errors" do | ||
review_body = ReviewBody.new(["invalid config", "foo\n bar"]) | ||
|
||
result = review_body.to_s | ||
|
||
expect(result).to eq( | ||
"Some files could not be reviewed due to errors:" \ | ||
"<details><summary>invalid config</summary>" \ | ||
"<pre>invalid config</pre></details>" \ | ||
"<details><summary>foo</summary>" \ | ||
"<pre>foo<br> bar</pre></details>", | ||
) | ||
end | ||
|
||
context "when errors are too long" do | ||
it "truncates the errors" do | ||
stub_const("ReviewBody::MAX_BODY_LENGTH", 200) | ||
review_body = ReviewBody.new( | ||
[ | ||
"invalid config", | ||
"rule is unknown\n MyRule", | ||
], | ||
) | ||
|
||
result = review_body.to_s | ||
|
||
expect(result).to eq( | ||
"Some files could not be reviewed due to errors:" \ | ||
"<details><summary>invalid config</summary>" \ | ||
"<pre>invalid</pre></details>" \ | ||
"<details><summary>rule is unknown</summary>" \ | ||
"<pre>rule i</pre></details>", | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |