Skip to content

Commit

Permalink
Refactor Github to receive repo via constructor
Browse files Browse the repository at this point in the history
Simplifies the whole class and moves the slug parsing to a more natural
place.
  • Loading branch information
mmozuras committed Nov 15, 2014
1 parent 603ffef commit c2db6da
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 85 deletions.
1 change: 0 additions & 1 deletion lib/pronto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'pronto/git/patches'
require 'pronto/git/patch'
require 'pronto/git/line'
require 'pronto/git/remote'

require 'pronto/plugin'
require 'pronto/message'
Expand Down
13 changes: 5 additions & 8 deletions lib/pronto/formatter/github_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@ module Formatter
class GithubFormatter
def format(messages, repo)
messages = messages.uniq { |message| [message.msg, message.line.new_lineno] }
client = Github.new(repo)

commit_messages = messages.map do |message|
sha = message.commit_sha
body = message.msg
path = message.path
position = message.line.commit_line.position if message.line

create_comment(repo, sha, body, path, position)
create_comment(client, sha, body, path, position)
end

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

private

def create_comment(repo, sha, body, path, position)
comment = Github::Comment.new(repo, sha, body, path, position)
comments = client.commit_comments(repo, sha)
def create_comment(client, sha, body, path, position)
comment = Github::Comment.new(sha, body, path, position)
comments = client.commit_comments(sha)
existing = comments.any? { |c| comment == c }
client.create_commit_comment(comment) unless existing
end

def client
@client ||= Github.new
end
end
end
end
13 changes: 5 additions & 8 deletions lib/pronto/formatter/github_pull_request_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Formatter
class GithubPullRequestFormatter
def format(messages, repo)
messages = messages.uniq { |message| [message.msg, message.line.new_lineno] }
client = Github.new(repo)

commit_messages = messages.map do |message|
body = message.msg
Expand All @@ -17,24 +18,20 @@ def format(messages, repo)
line
end

create_comment(repo, sha, body, path, line.position)
create_comment(client, sha, body, path, line.position)
end

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

private

def create_comment(repo, sha, body, path, position)
comment = Github::Comment.new(repo, sha, body, path, position)
comments = client.pull_comments(repo, sha)
def create_comment(client, sha, body, path, position)
comment = Github::Comment.new(sha, body, path, position)
comments = client.pull_comments(sha)
existing = comments.any? { |c| comment == c }
client.create_pull_comment(comment) unless existing
end

def client
@client ||= Github.new
end
end
end
end
12 changes: 0 additions & 12 deletions lib/pronto/git/remote.rb

This file was deleted.

12 changes: 4 additions & 8 deletions lib/pronto/git/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ def initialize(path)
@repo = Rugged::Repository.new(path)
end

def github_slug
remotes.map(&:github_slug).compact.first
end

def diff(commit)
if commit == :index
patches = @repo.index.diff
Expand Down Expand Up @@ -59,6 +55,10 @@ def branch
@repo.head.name.sub('refs/heads/', '') if @repo.head.branch?
end

def remote_urls
@repo.remotes.map(&:url)
end

private

def empty_patches(sha)
Expand All @@ -72,10 +72,6 @@ def merge_base(commit)
def head
@repo.head.target
end

def remotes
@remotes ||= @repo.remotes.map { |remote| Remote.new(remote) }
end
end
end
end
52 changes: 30 additions & 22 deletions lib/pronto/github.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,63 @@
module Pronto
class Github
def initialize
def initialize(repo)
@repo = repo
@comment_cache = {}
@pull_id_cache = {}
end

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

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

def create_commit_comment(comment)
client.create_commit_comment(comment.repo.github_slug, comment.sha,
comment.body, comment.path, nil, comment.position)
client.create_commit_comment(slug, comment.sha, comment.body,
comment.path, nil, comment.position)
end

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

private

def slug
@slug ||= begin
@repo.remote_urls.map do |url|
match = /.*github.com(:|\/)(?<slug>.*).git/.match(url)
match[:slug] if match
end.compact.first
end
end

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

def pull_requests(repo)
client.pull_requests(repo.github_slug)
def pull_requests
@pull_requests ||= client.pull_requests(slug)
end

def pull_id(repo)
@pull_id_cache["#{repo.github_slug}"] ||= begin
def pull_id
@pull_id ||= begin
pull_id = ENV['PULL_REQUEST_ID']
if pull_id
pull_id.to_i
elsif repo.branch
pull = pull_requests(repo).find { |pr| pr[:head][:ref] == repo.branch }
elsif @repo.branch
pull = pull_requests.find { |pr| pr[:head][:ref] == @repo.branch }
pull[:number].to_i if pull
end
end
Expand All @@ -59,7 +67,7 @@ def access_token
ENV['GITHUB_ACCESS_TOKEN']
end

class Comment < Struct.new(:repo, :sha, :body, :path, :position)
class Comment < Struct.new(:sha, :body, :path, :position)
def ==(other)
position == other.position &&
path == other.path &&
Expand Down
23 changes: 0 additions & 23 deletions spec/pronto/git/remote_spec.rb

This file was deleted.

30 changes: 27 additions & 3 deletions spec/pronto/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

module Pronto
describe Github do
let(:github) { Github.new }
let(:github) { Github.new(repo) }

describe '#commit_comments' do
subject { github.commit_comments(repo, sha) }
subject { github.commit_comments(sha) }

context 'three requests for same comments' do
let(:repo) { double(github_slug: 'mmozuras/pronto') }
let(:repo) { double(remote_urls: ['[email protected]:mmozuras/pronto.git']) }
let(:sha) { '61e4bef' }

specify do
Octokit::Client.any_instance
.should_receive(:commit_comments)
.with('mmozuras/pronto', sha)
.once
.and_return([])

Expand All @@ -23,5 +24,28 @@ module Pronto
end
end
end

describe '#pull_comments' do
subject { github.pull_comments(sha) }

context 'three requests for same comments' do
let(:repo) { double(remote_urls: ['https://github.com/mmozuras/pronto.git']) }
let(:sha) { '61e4bef' }

specify do
Octokit::Client.any_instance
.should_receive(:pull_comments)
.with('mmozuras/pronto', 10)
.once
.and_return([])

ENV['PULL_REQUEST_ID'] = '10'

subject
subject
subject
end
end
end
end
end

0 comments on commit c2db6da

Please sign in to comment.