Skip to content

Commit

Permalink
Extract comment related stuff to Pronto::Github
Browse files Browse the repository at this point in the history
  • Loading branch information
mmozuras committed Sep 10, 2014
1 parent 8002773 commit 0eabaa4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
21 changes: 7 additions & 14 deletions lib/pronto/formatter/github_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@ 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

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
existing = comments.any? { |c| comment == c }
client.create_commit_comment(repo, sha, comment) unless existing
end

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

def remotes
@repo.remotes.map { |remote| Remote.new(remote) }
def github_slug
remotes.map(&:github_slug).compact.first
end

def diff(commit)
Expand Down Expand Up @@ -48,6 +48,10 @@ def merge_base(commit)
def head
@repo.head.target
end

def remotes
@remotes ||= @repo.remotes.map { |remote| Remote.new(remote) }
end
end
end
end
19 changes: 17 additions & 2 deletions lib/pronto/github.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
module Pronto
class Github
extend Forwardable
def commit_comments(repo, sha)
client.commit_comments(repo, sha).map do |comment|
Comment.new(repo, sha, comment.body, comment.path, comment.body)
end
end

def_delegators :client, :commit_comments, :create_commit_comment
def create_commit_comment(repo, sha, comment)
client.create_commit_comment(repo, sha, comment.body, comment.path,
nil, comment.position)
end

private

Expand All @@ -13,5 +20,13 @@ def client
def access_token
ENV['GITHUB_ACCESS_TOKEN']
end

class Comment < Struct.new(:repo, :sha, :body, :path, :position)
def ==(other)
position == other.position &&
path == other.path &&
body == other.body
end
end
end
end

0 comments on commit 0eabaa4

Please sign in to comment.