From b6d34a3bf333138d7c003616dad7fa2ff31a2123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Moz=C5=ABras?= Date: Mon, 13 Oct 2014 15:12:17 +0300 Subject: [PATCH] Detect Pull Request id automatically if not specified --- CHANGELOG.md | 6 +++ lib/pronto/formatter/github_formatter.rb | 9 ++--- .../github_pull_request_formatter.rb | 9 ++--- lib/pronto/git/remote.rb | 6 ++- lib/pronto/git/repository.rb | 4 ++ lib/pronto/github.rb | 39 +++++++++++++------ spec/pronto/github_spec.rb | 2 +- 7 files changed, 50 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ff604f1..59ac7c3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/pronto/formatter/github_formatter.rb b/lib/pronto/formatter/github_formatter.rb index f7655c84..e0de75d4 100644 --- a/lib/pronto/formatter/github_formatter.rb +++ b/lib/pronto/formatter/github_formatter.rb @@ -3,14 +3,12 @@ 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" @@ -18,10 +16,11 @@ def format(messages, repo) 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 diff --git a/lib/pronto/formatter/github_pull_request_formatter.rb b/lib/pronto/formatter/github_pull_request_formatter.rb index 0c44bd80..a07f0fdb 100644 --- a/lib/pronto/formatter/github_pull_request_formatter.rb +++ b/lib/pronto/formatter/github_pull_request_formatter.rb @@ -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 @@ -16,8 +15,7 @@ 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" @@ -25,10 +23,11 @@ def format(messages, repo) 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 diff --git a/lib/pronto/git/remote.rb b/lib/pronto/git/remote.rb index d837e837..8797b9d7 100644 --- a/lib/pronto/git/remote.rb +++ b/lib/pronto/git/remote.rb @@ -2,8 +2,10 @@ module Pronto module Git class Remote < Struct.new(:remote) def github_slug - match = /.*github.com(:|\/)(?.*).git/.match(remote.url) - match[:slug] if match + @github_slug ||= begin + match = /.*github.com(:|\/)(?.*).git/.match(remote.url) + match[:slug] if match + end end end end diff --git a/lib/pronto/git/repository.rb b/lib/pronto/git/repository.rb index 22688704..f7e59cd5 100644 --- a/lib/pronto/git/repository.rb +++ b/lib/pronto/git/repository.rb @@ -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) diff --git a/lib/pronto/github.rb b/lib/pronto/github.rb index bff2d16c..75ccfd57 100644 --- a/lib/pronto/github.rb +++ b/lib/pronto/github.rb @@ -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 @@ -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 diff --git a/spec/pronto/github_spec.rb b/spec/pronto/github_spec.rb index 208cfc85..be2816e7 100644 --- a/spec/pronto/github_spec.rb +++ b/spec/pronto/github_spec.rb @@ -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