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

0.3.0 #34

Merged
merged 30 commits into from
Sep 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3014746
Remove 'grit' from gemspec and add 'rugged'
mmozuras Jul 13, 2014
b5df669
Move Rugged::Patch out of Diff namespace
mmozuras Jul 13, 2014
d04a0ef
Remove patches that are not needed with rugged '0.2.1'
mmozuras Jul 13, 2014
65ce672
Passing parents.first is no longer necessary for diff
mmozuras Jul 13, 2014
e013c9b
Add 'pry' as development dependency
mmozuras Jul 13, 2014
e329818
Use Rugged::Blame to find the commit line
mmozuras Jul 13, 2014
5a1d93d
Bump octokit from 2.7.0 to 3.2.0
mmozuras Jul 27, 2014
27895e6
Upgrade rspec to 3.0.0
mmozuras Jul 27, 2014
16e68af
To find commit location, use orig_ instead of final_start_line_number
mmozuras Jul 27, 2014
6b09caa
Use additional tracking options for more accurate blaming
mmozuras Jul 27, 2014
90f5bb7
Get rid of Rugged monkey patches, move them to Pronto::Git namespace
mmozuras Jul 29, 2014
064f42f
Extract Git::Patches, Git::Repository and Git::Remote
mmozuras Jul 30, 2014
27ed09e
Move #blame to Patch from Line
mmozuras Jul 30, 2014
6a7c001
Some specs for Git::Line
mmozuras Aug 3, 2014
ed3688b
Use Forwardable in Git::Line
mmozuras Aug 3, 2014
565345e
Some specs for Git::Patch
mmozuras Aug 3, 2014
befc3e7
Use Forwardable in Git::Patch
mmozuras Aug 3, 2014
62c3ea9
Replace usages of OpenStruct in specs to double()
mmozuras Aug 3, 2014
b5f80b6
More pessimistic dependencies of rugged and thor
mmozuras Aug 3, 2014
fbf8d76
Extract Pronto::Github to be used by multiple formatters
mmozuras Aug 3, 2014
8002773
More specs for Git::Patch
mmozuras Aug 3, 2014
0eabaa4
Extract comment related stuff to Pronto::Github
mmozuras Aug 3, 2014
e498de7
Add CHANGELOG.md
mmozuras Aug 10, 2014
d8064e9
Performance improvement - cache comments retrieved from GitHub
mmozuras Aug 10, 2014
d4cb558
Add '--exit-code' option for 'pronto run'
mmozuras Aug 10, 2014
b2575d7
Extract Git::Patches#find_line from Git::Line
mmozuras Aug 19, 2014
013c4c4
Return a path instead of string from Repository#path
mmozuras Aug 19, 2014
d10596b
Formatter for GitHub pull requests
mmozuras Aug 19, 2014
b74d51c
Fix: pass position when creating Comment from GitHub responses
mmozuras Aug 19, 2014
122c62a
Bump version to 0.3.0
mmozuras Aug 19, 2014
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
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

## 0.3.0

### 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

* [#29](https://github.com/mmozuras/pronto/issues/29): Be compatible and depend on rugged '0.21.0'.
* Performance improvement: use Rugged::Blame instead of one provided by Grit.
* Performance improvement: cache comments retrieved from GitHub.

### Bugs fixed
36 changes: 20 additions & 16 deletions lib/pronto.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
require 'rugged'
require 'pronto/rugged/diff'
require 'pronto/rugged/diff/delta'
require 'pronto/rugged/diff/patch'
require 'pronto/rugged/diff/line'
require 'pronto/rugged/tree'
require 'pronto/rugged/remote'
require 'pronto/rugged/repository'
require 'pronto/rugged/commit'
require 'octokit'
require 'forwardable'

require 'pronto/git/repository'
require 'pronto/git/patches'
require 'pronto/git/patch'
require 'pronto/git/line'
require 'pronto/git/remote'

require 'pronto/plugin'
require 'pronto/message'
require 'pronto/runner'
require 'pronto/github'

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'

module Pronto
def self.run(commit = 'master', repo_path = '.', formatter = nil)
repo = Rugged::Repository.new(repo_path)
commit ||= 'master'
merge_base = repo.merge_base(commit, repo.head.target)
patches = repo.diff(merge_base, repo.head.target)

result = run_all_runners(patches, merge_base)
repo = Git::Repository.new(repo_path)
patches = repo.diff(commit)

result = run_all_runners(patches)

formatter ||= default_formatter
formatter.format(result, repo)
puts formatter.format(result, repo)

result
end

def self.gem_names
Expand All @@ -37,7 +41,7 @@ def self.gem_names
true
elsif gem.name != 'pronto'
runner_path = File.join(gem.full_gem_path, "lib/pronto/#{gem.name}.rb")
File.exists?(runner_path)
File.exist?(runner_path)
end
end

Expand All @@ -48,9 +52,9 @@ def self.gem_names

private

def self.run_all_runners(patches, commit)
def self.run_all_runners(patches)
Runner.runners.map do |runner|
runner.new.run(patches, commit)
runner.new.run(patches, patches.commit)
end.flatten.compact
end

Expand Down
7 changes: 6 additions & 1 deletion lib/pronto/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def is_thor_reserved_word?(word, type)

desc 'run', 'Run Pronto'

method_option :'exit-code',
type: :boolean,
banner: 'Exits with non-zero code if there were any warnings/errors.'

method_option :commit,
type: :string,
default: 'master',
Expand All @@ -39,7 +43,8 @@ def run
end

formatter = ::Pronto::Formatter.get(options[:formatter])
puts ::Pronto.run(options[:commit], '.', formatter)
messages = ::Pronto.run(options[:commit], '.', formatter)
exit(messages.count) if options[:'exit-code']
rescue Rugged::RepositoryError
puts '"pronto" should be run from a git repository'
end
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
31 changes: 9 additions & 22 deletions lib/pronto/formatter/github_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
require 'octokit'

module Pronto
module Formatter
class GithubFormatter
def format(messages, repo)
commit_messages = messages.map do |message|
github_slug = repo.remotes.map(&:github_slug).compact.first
github_slug = repo.github_slug
sha = message.commit_sha
position = message.line.commit_line.position if message.line
path = message.path
body = message.msg
path = message.path
position = message.line.commit_line.position if message.line

create_comment(github_slug, sha, position, path, body)
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, position, path, body)
def create_comment(repo, sha, comment)
comments = client.commit_comments(repo, sha)

existing_comment = comments.find do |comment|
comment.position == position &&
comment.path == path &&
comment.body == body
end

unless existing_comment
client.create_commit_comment(repo, sha, body, path, nil, position)
end
end

def access_token
ENV['GITHUB_ACCESS_TOKEN']
existing = comments.any? { |c| comment == c }
client.create_commit_comment(repo, sha, comment) unless existing
end

def client
@client ||= Octokit::Client.new(access_token: access_token)
@client ||= Github.new
end
end
end
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
44 changes: 44 additions & 0 deletions lib/pronto/git/line.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Pronto
module Git
class Line < Struct.new(:line, :patch, :hunk)
extend Forwardable

def_delegators :line, :addition?, :deletion?, :content, :new_lineno,
:old_lineno, :line_origin

def position
hunk_index = patch.hunks.find_index { |h| h.header == hunk.header }
line_index = patch.lines.find_index(line)

line_index + hunk_index + 1
end

def commit_sha
blame[:final_commit_id] if blame
end

def commit_line
@commit_line ||= begin
patches = patch.repo.show_commit(commit_sha)

result = patches.find_line(patch.new_file_full_path,
blame[:orig_start_line_number])
result || self # no commit_line means that it was just added
end
end

def ==(other)
content == other.content &&
line_origin == other.line_origin &&
old_lineno == other.old_lineno &&
new_lineno == other.new_lineno
end

private

def blame
@blame ||= patch.blame(new_lineno)
end
end
end
end
41 changes: 41 additions & 0 deletions lib/pronto/git/patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Pronto
module Git
class Patch < Struct.new(:patch, :repo)
extend Forwardable

def_delegators :patch, :delta, :hunks, :stat

def additions
stat[0]
end

def deletions
stat[1]
end

def blame(lineno)
repo.blame(self, lineno)
end

def lines
@lines ||= begin
hunks.flat_map do |hunk|
hunk.lines.map { |line| Line.new(line, self, hunk) }
end
end
end

def added_lines
lines.select(&:addition?)
end

def deleted_lines
lines.select(&:deletion?)
end

def new_file_full_path
repo.path.join(delta.new_file[:path])
end
end
end
end
24 changes: 24 additions & 0 deletions lib/pronto/git/patches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Pronto
module Git
class Patches
include Enumerable

attr_reader :commit, :repo

def initialize(repo, commit, patches)
@commit = commit
@patches = patches.map { |patch| Git::Patch.new(patch, repo) }
end

def each(&block)
@patches.each(&block)
end

def find_line(path, line)
patch = find { |p| p.new_file_full_path == path }
lines = patch ? patch.lines : []
lines.find { |l| l.new_lineno == line }
end
end
end
end
10 changes: 10 additions & 0 deletions lib/pronto/git/remote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Pronto
module Git
class Remote < Struct.new(:remote)
def github_slug
match = /.*github.com(:|\/)(?<slug>.*).git/.match(remote.url)
match[:slug] if match
end
end
end
end
Loading