diff --git a/lib/pronto/formatter/github_formatter.rb b/lib/pronto/formatter/github_formatter.rb index 1ab21075..2c81b961 100644 --- a/lib/pronto/formatter/github_formatter.rb +++ b/lib/pronto/formatter/github_formatter.rb @@ -3,13 +3,14 @@ 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" @@ -17,18 +18,10 @@ def format(messages, repo) 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 diff --git a/lib/pronto/git/repository.rb b/lib/pronto/git/repository.rb index 35c5196a..e3852438 100644 --- a/lib/pronto/git/repository.rb +++ b/lib/pronto/git/repository.rb @@ -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) @@ -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 diff --git a/lib/pronto/github.rb b/lib/pronto/github.rb index f7185827..2d2e06a0 100644 --- a/lib/pronto/github.rb +++ b/lib/pronto/github.rb @@ -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 @@ -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