Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/f2018071301 add option for warnings per pr review #304

Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4fab083
Add option for warnings per pr review
dimitrovv Jul 12, 2018
486862c
Use the warnings per pr review option to create consecutive pr reviews
dimitrovv Jul 12, 2018
685f61e
Add .idea to gitignore
dimitrovv Jul 12, 2018
48aa23e
Fix specs
dimitrovv Jul 12, 2018
8bb2cc4
Fix previously failing specs
dimitrovv Jul 12, 2018
bec326d
Remove extra empty line
dimitrovv Jul 12, 2018
1d039da
Extend config specs
dimitrovv Jul 12, 2018
60c683f
Fix and extend github specs
dimitrovv Jul 13, 2018
3f76cb4
Bump the version to 0.9.6
dimitrovv Jul 13, 2018
dfe7448
Change github client to work with a copy of comments
dimitrovv Jul 13, 2018
cca89f4
Extend github formatter specs
dimitrovv Jul 13, 2018
c546295
Restore version to 0.9.5
dimitrovv Jul 13, 2018
2d36014
Refactor github client spec
dimitrovv Jul 13, 2018
8a6e25e
Add info about the new config option in README.md
dimitrovv Jul 13, 2018
bf284c3
Use old hash syntax in specs to satisfy ruby 2.1.0
dimitrovv Jul 13, 2018
a43ece1
Remove ruby 2.0.0 from travis builds
dimitrovv Jul 13, 2018
1609117
Merge branch 'master' into feature/f2018071301-add_option_for_warning…
dimitrovv Jul 15, 2018
c561421
Restore .gitignore
dimitrovv Jul 15, 2018
90aacc8
Add default value of 30 to WARNING_PER_REVIEW
dimitrovv Jul 15, 2018
9c4573b
Restore specs' default codding standards
dimitrovv Jul 15, 2018
f2913c7
Update README.md
dimitrovv Jul 15, 2018
7df597e
Merge branch 'master' into feature/f2018071301-add_option_for_warning…
dimitrovv Jul 18, 2018
28a8b04
Merge branch 'master' into feature/f2018071301-add_option_for_warning…
dimitrovv May 15, 2019
a622d31
feature/f2018071301-add_option_for_warning_per_pr_review
dimitrovv May 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ If you want review to appear on pull request diff, instead of comments:
$ PRONTO_GITHUB_ACCESS_TOKEN=token pronto run -f github_pr_review -c origin/master
```

All the comments will now not be published into a single PR review, but separated into a number of PRs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dimitrovv my only feedback is the wording here which is a little confusing to me. We don't mean to say it will create new PRs but will only leave N number of feedback per Pronto run, right?

If I create a PR with 100 errors, Pronto will report the first 30.
If I fix them, and push a new commit to my PR, Pronto will report the next 30.

Is that understanding correct? If so, I think we could rephrase this as such:

In order to avoid rate limiting by providers, Pronto publishes a limited number of comments per execution. By default, this is limited to the first 30 reported issues. You can change the number of reported issues either through the .pronto.yml file or the PRONTO_WARNINGS_PER_REVIEW environment variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dimitrovv my only feedback is the wording here which is a little confusing to me.

Yes, I didn't define it the right way, will fix


If I create a PR with 100 errors, Pronto will report the first 30.
If I fix them, and push a new commit to my PR, Pronto will report the next 30.

Actually, it will post all N comments, but separated into X number of PR reviews (X = N / PRONTO_WARNINGS_PER_REVIEW). So all the comments should be published to Github only with a single pronto run

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @dimitrovv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doomspork, I've just tried to formulate the explanations around the new setting a bit better. Could you take a look when possible?

This can be managed via additional environment variable or in the config file (see below):

Note: In case no environment variable or config setting is specified in `.pronto.yml`,
a default value of `30` will be used.

```sh
$ PRONTO_WARNINGS_PER_REVIEW=30 PRONTO_GITHUB_ACCESS_TOKEN=token pronto run -f github_pr_review -c origin/master
```

Use `GithubStatusFormatter` to submit [commit status](https://github.com/blog/1227-commit-status-api):

```sh
Expand Down Expand Up @@ -210,6 +220,7 @@ bitbucket:
password: pass
web_endpoint: https://bitbucket.org/
max_warnings: 150
warnings_per_review: 30
verbose: false
```

Expand Down
4 changes: 4 additions & 0 deletions lib/pronto/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def bitbucket_hostname
URI.parse(bitbucket_web_endpoint).host
end

def warnings_per_review
ENV['PRONTO_WARNINGS_PER_REVIEW'] && Integer(ENV['PRONTO_WARNINGS_PER_REVIEW']) || @config_hash['warnings_per_review']
end

def max_warnings
ENV['PRONTO_MAX_WARNINGS'] && Integer(ENV['PRONTO_MAX_WARNINGS']) || @config_hash['max_warnings']
end
Expand Down
2 changes: 2 additions & 0 deletions lib/pronto/config_file.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Pronto
class ConfigFile
DEFAULT_MESSAGE_FORMAT = '%{msg}'.freeze
DEFAULT_WARNINGS_PER_REVIEW = 30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dimitrovv why is the default value 30? What's the GH rate limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmozuras we are using Github Enterprise and with disabled rate limit, but if I try to post about 40 comments per review GHE returns 502. I don't this is smth with the rate limit here, have not tried with Public Github. I suspect the server is not able to serve this request somehow.

Thus I can disable the default value here, so the comments will be separated into different reviews only if a default value is specified via config or env var. What do you think?


EMPTY = {
'all' => {
Expand Down Expand Up @@ -32,6 +33,7 @@ class ConfigFile
'runners' => [],
'formatters' => [],
'max_warnings' => nil,
'warnings_per_review' => DEFAULT_WARNINGS_PER_REVIEW,
'verbose' => false,
'format' => DEFAULT_MESSAGE_FORMAT
}.freeze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def pretty_name
end

def submit_comments(client, comments)
client.create_pull_request_review(comments)
client.publish_pull_request_comments(comments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dimitrovv why limit this feature to GitHub? Just trying to understand :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I thought about this, but I guess a review could only be submitted when using Github pronto client or I am wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

rescue Octokit::UnprocessableEntity, HTTParty::Error => e
$stderr.puts "Failed to post: #{e.message}"
end
Expand Down
36 changes: 25 additions & 11 deletions lib/pronto/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,12 @@ def create_pull_comment(comment)
end
end

def create_pull_request_review(comments)
return if comments.empty?

options = {
event: 'COMMENT',
accept: 'application/vnd.github.v3.diff+json', # https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
comments: comments.map do |c|
{ path: c.path, position: c.position, body: c.body }
end
}
client.create_pull_request_review(slug, pull_id, options)
def publish_pull_request_comments(comments)
comments_left = comments.clone
while comments_left.any?
comments_to_publish = comments_left.slice!(0, warnings_per_review)
create_pull_request_review(comments_to_publish)
end
end

def create_commit_status(status)
Expand All @@ -68,6 +63,21 @@ def create_commit_status(status)

private

def create_pull_request_review(comments)
options = {
event: 'COMMENT',
accept: 'application/vnd.github.v3.diff+json', # https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
comments: comments.map do |comment|
{
path: comment.path,
position: comment.position,
body: comment.body
}
end
}
client.create_pull_request_review(slug, pull_id, options)
end

def slug
return @config.github_slug if @config.github_slug
@slug ||= begin
Expand Down Expand Up @@ -103,5 +113,9 @@ def pull
@github_pull.pull_by_commit(@repo.head_commit_sha)
end
end

def warnings_per_review
@warnings_per_review ||= @config.warnings_per_review
end
end
end
49 changes: 29 additions & 20 deletions spec/pronto/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,41 @@ module Pronto
end
end

describe '#max_warnings' do
subject { config.max_warnings }

context 'from env variable' do
context 'with a valid value' do
before { stub_const('ENV', 'PRONTO_MAX_WARNINGS' => '20') }
it { should == 20 }
end
{
max_warnings: {
default_value: nil
},
warnings_per_review: {
default_value: ConfigFile::DEFAULT_WARNINGS_PER_REVIEW
}
}.each do |setting_name, specifics|
describe "##{setting_name}" do
subject { config.public_send(setting_name) }

context 'from env variable' do
context 'with a valid value' do
before { stub_const('ENV', "PRONTO_#{setting_name.upcase}" => '20') }
it { should == 20 }
end

context 'with an invalid value' do
before { stub_const('ENV', 'PRONTO_MAX_WARNINGS' => 'twenty') }
context 'with an invalid value' do
before { stub_const('ENV', "PRONTO_#{setting_name.upcase}" => 'twenty') }

specify do
-> { subject }.should raise_error(ArgumentError)
specify do
-> { subject }.should raise_error(ArgumentError)
end
end
end
end

context 'from config hash' do
let(:config_hash) { { 'max_warnings' => 40 } }
it { should == 40 }
end
context 'from config hash' do
let(:config_hash) { { setting_name.to_s => 40 } }
it { should == 40 }
end

context 'default' do
let(:config_hash) { ConfigFile::EMPTY }
it { should == nil }
context 'default' do
let(:config_hash) { ConfigFile::EMPTY }
it { should == specifics[:default_value] }
end
end
end

Expand Down
84 changes: 44 additions & 40 deletions spec/pronto/formatter/github_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,54 @@ module Formatter

describe '#format' do
subject { formatter.format(messages, repo, nil) }
let(:messages) { [message, message] }
let(:repo) { Git::Repository.new('spec/fixtures/test.git') }
let(:message) { Message.new('path/to', line, :warning, 'crucial') }
let(:line) { double(new_lineno: 1, commit_sha: '123', position: nil) }
before { line.stub(:commit_line).and_return(line) }

specify do
Octokit::Client.any_instance
.should_receive(:commit_comments)
.once
.and_return([])

Octokit::Client.any_instance
.should_receive(:create_commit_comment)
.once

subject
end
end

describe '#format without duplicates' do
subject { formatter.format(messages, repo, nil) }
let(:messages) { [message1, message2] }
let(:repo) { Git::Repository.new('spec/fixtures/test.git') }
let(:message1) { Message.new('path/to1', line1, :warning, 'crucial') }
let(:message2) { Message.new('path/to2', line2, :warning, 'crucial') }
let(:line1) { double(new_lineno: 1, commit_sha: '123', position: nil) }
let(:line2) { double(new_lineno: 2, commit_sha: '123', position: nil) }
before do
line1.stub(:commit_line).and_return(line1)
line2.stub(:commit_line).and_return(line2)
let(:published_comments_msg) { "%{count} Pronto messages posted to #{formatter.pretty_name}" }

context 'with duplicates' do
let(:messages) { [message, message] }
let(:message) { Message.new('path/to', line, :warning, 'crucial') }
let(:line) { double(new_lineno: 1, commit_sha: '123', position: nil) }
before { line.stub(:commit_line).and_return(line) }

specify do
Octokit::Client.any_instance
.should_receive(:commit_comments)
.once
.and_return([])

Octokit::Client.any_instance
.should_receive(:create_commit_comment)
.once

subject.should eq format(published_comments_msg, count: 1)
end
end

specify do
Octokit::Client.any_instance
.should_receive(:commit_comments)
.once
.and_return([])

Octokit::Client.any_instance
.should_receive(:create_commit_comment)
.twice

subject
context 'without duplicates' do
let(:messages) { [message1, message2] }
let(:message1) { Message.new('path/to1', line1, :warning, 'crucial') }
let(:message2) { Message.new('path/to2', line2, :warning, 'crucial') }
let(:line1) { double(new_lineno: 1, commit_sha: '123', position: nil) }
let(:line2) { double(new_lineno: 2, commit_sha: '123', position: nil) }

before do
line1.stub(:commit_line).and_return(line1)
line2.stub(:commit_line).and_return(line2)
end

specify do
Octokit::Client.any_instance
.should_receive(:commit_comments)
.once
.and_return([])

Octokit::Client.any_instance
.should_receive(:create_commit_comment)
.twice

subject.should eq format(published_comments_msg, count: 2)
end
end
end
end
Expand Down
Loading