Skip to content

Commit

Permalink
Formatter for GitHub pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmozuras committed Sep 10, 2014
1 parent 013c4c4 commit d10596b
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New features

* [#27](https://github.com/mmozuras/pronto/issues/27): '--exit-code' option for 'pronto run'. Pronto exits with non-zero code if there were any warnings/errors.
* [#16](https://github.com/mmozuras/pronto/issues/16): New formatter: GithubPullRequestFormatter. Writes review comments on GitHub pull requests.

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/pronto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'pronto/formatter/text_formatter'
require 'pronto/formatter/json_formatter'
require 'pronto/formatter/github_formatter'
require 'pronto/formatter/github_pull_request_formatter'
require 'pronto/formatter/checkstyle_formatter'
require 'pronto/formatter/formatter'

Expand Down
1 change: 1 addition & 0 deletions lib/pronto/formatter/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.names

FORMATTERS = {
'github' => GithubFormatter,
'github_pr' => GithubPullRequestFormatter,
'json' => JsonFormatter,
'checkstyle' => CheckstyleFormatter,
'text' => TextFormatter
Expand Down
2 changes: 1 addition & 1 deletion lib/pronto/formatter/github_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ def format(messages, repo)
commit_messages = messages.map do |message|
github_slug = repo.github_slug
sha = message.commit_sha
position = message.line.commit_line.position if message.line
body = message.msg
path = message.path
position = message.line.commit_line.position if message.line

comment = Github::Comment.new(github_slug, sha, body, path, position)
create_comment(github_slug, sha, comment)
Expand Down
41 changes: 41 additions & 0 deletions lib/pronto/formatter/github_pull_request_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Pronto
module Formatter
class GithubPullRequestFormatter
def format(messages, repo)
commit_messages = messages.map do |message|
github_slug = repo.github_slug
body = message.msg
path = message.path

commits = repo.commits_until(message.commit_sha)

line = nil
sha = commits.find do |commit|
patches = repo.show_commit(commit)
line = patches.find_line(message.full_path, message.line.new_lineno)
line
end

position = line.position - 1

comment = Github::Comment.new(github_slug, sha, body, path, position)
create_comment(github_slug, sha, comment)
end

"#{commit_messages.compact.count} Pronto messages posted to GitHub"
end

private

def create_comment(repo, sha, comment)
comments = client.pull_comments(repo, sha)
existing = comments.any? { |c| comment == c }
client.create_pull_comment(repo, sha, comment) unless existing
end

def client
@client ||= Github.new
end
end
end
end
9 changes: 9 additions & 0 deletions lib/pronto/git/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def show_commit(sha)
Patches.new(self, sha, diff.patches)
end

def commits_until(sha)
result = []
@repo.walk('HEAD', Rugged::SORT_TOPO).take_while do |commit|
result << commit.oid
!commit.oid.start_with?(sha)
end
result
end

def path
Pathname.new(@repo.path).parent
end
Expand Down
17 changes: 17 additions & 0 deletions lib/pronto/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ def initialize
@comment_cache = {}
end

def pull_comments(repo, sha)
@comment_cache["#{repo}/#{pull_id}/#{sha}"] ||= begin
client.pull_comments(repo, pull_id).map do |comment|
Comment.new(repo, sha, comment.body, comment.path, comment.body)
end
end
end

def commit_comments(repo, sha)
@comment_cache["#{repo}/#{sha}"] ||= begin
client.commit_comments(repo, sha).map do |comment|
Expand All @@ -17,12 +25,21 @@ def create_commit_comment(repo, sha, comment)
nil, comment.position)
end

def create_pull_comment(repo, sha, comment)
client.create_pull_comment(repo, pull_id, comment.body, sha, comment.path,
comment.position)
end

private

def client
@client ||= Octokit::Client.new(access_token: access_token)
end

def pull_id
ENV['PULL_REQUEST_ID'].to_i
end

def access_token
ENV['GITHUB_ACCESS_TOKEN']
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pronto/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def initialize(path, line, level, msg, commit_sha = nil)
@commit_sha ||= line.commit_sha if line
end

def full_path
repo.path.join(path) if repo
end

def repo
line.patch.repo if line
end
Expand Down
7 changes: 6 additions & 1 deletion spec/pronto/formatter/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module Formatter
it { should be_an_instance_of GithubFormatter }
end

context 'github_pr' do
let(:name) { 'github_pr' }
it { should be_an_instance_of GithubPullRequestFormatter }
end

context 'json' do
let(:name) { 'json' }
it { should be_an_instance_of JsonFormatter }
Expand Down Expand Up @@ -38,7 +43,7 @@ module Formatter

describe '.names' do
subject { Formatter.names }
it { should =~ %w(github json checkstyle text) }
it { should =~ %w(github github_pr json checkstyle text) }
end
end
end
2 changes: 1 addition & 1 deletion spec/pronto/git/patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Git
let(:rugged_patch) do
double(delta: double(new_file: { path: 'test.md' }))
end
let(:repo) { double(path: '/house/of/cards/orig.md') }
let(:repo) { double(path: Pathname.new('/house/of/cards')) }
its(:to_s) { should == '/house/of/cards/test.md' }
end
end
Expand Down

0 comments on commit d10596b

Please sign in to comment.