Skip to content

Commit

Permalink
Merge pull request prontolabs#38 from mmozuras/detect_pr_automatically
Browse files Browse the repository at this point in the history
Detect Pull Request id automatically if not specified
  • Loading branch information
mmozuras committed Oct 15, 2014
2 parents 861f335 + b6d34a3 commit 1d13aaf
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### New features

* Try to detect pull request id automatically, if `PULL_REQUEST_ID` is not specified. Inspired by @willnet/prid.

## 0.3.3

### Bugs fixed
Expand Down
9 changes: 4 additions & 5 deletions lib/pronto/formatter/github_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ module Formatter
class GithubFormatter
def format(messages, repo)
commit_messages = messages.map do |message|
github_slug = repo.github_slug
sha = message.commit_sha
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)
create_comment(repo, sha, body, path, position)
end

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

private

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

def client
Expand Down
9 changes: 4 additions & 5 deletions lib/pronto/formatter/github_pull_request_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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

Expand All @@ -16,19 +15,19 @@ def format(messages, repo)
line
end

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

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

private

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

def client
Expand Down
6 changes: 4 additions & 2 deletions lib/pronto/git/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,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
@github_slug ||= begin
match = /.*github.com(:|\/)(?<slug>.*).git/.match(remote.url)
match[:slug] if match
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pronto/git/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def blame(patch, lineno)
track_copies_any_commit_copies: true)[0]
end

def branch
@repo.head.name.sub('refs/heads/', '') if @repo.head.branch?
end

private

def empty_patches(sha)
Expand Down
39 changes: 27 additions & 12 deletions lib/pronto/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ module Pronto
class Github
def initialize
@comment_cache = {}
@pull_id_cache = {}
end

def pull_comments(repo, sha)
@comment_cache["#{repo}/#{pull_id}/#{sha}"] ||= begin
client.pull_comments(repo, pull_id).map do |comment|
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)
end
end
end

def commit_comments(repo, sha)
@comment_cache["#{repo}/#{sha}"] ||= begin
client.commit_comments(repo, sha).map do |comment|
@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)
end
end
end

def create_commit_comment(repo, sha, comment)
client.create_commit_comment(repo, sha, comment.body, comment.path,
nil, comment.position)
def create_commit_comment(comment)
client.create_commit_comment(comment.repo.github_slug, comment.sha,
comment.body, comment.path, 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)
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)
end

private
Expand All @@ -36,8 +39,20 @@ def client
@client ||= Octokit::Client.new(access_token: access_token)
end

def pull_id
ENV['PULL_REQUEST_ID'].to_i
def pull_requests(repo)
client.pull_requests(repo.github_slug)
end

def pull_id(repo)
@pull_id_cache["#{repo.github_slug}"] ||= 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 }
pull[:number].to_i if pull
end
end
end

def access_token
Expand Down
2 changes: 1 addition & 1 deletion spec/pronto/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Pronto
subject { github.commit_comments(repo, sha) }

context 'three requests for same comments' do
let(:repo) { 'mmozuras/pronto' }
let(:repo) { double(github_slug: 'mmozuras/pronto') }
let(:sha) { '61e4bef' }

specify do
Expand Down

0 comments on commit 1d13aaf

Please sign in to comment.