Skip to content

Commit

Permalink
Merge pull request #17 from danielma/dma/only-consider-reviews
Browse files Browse the repository at this point in the history
change(PullRequest): only consider reviews
  • Loading branch information
Daniel Ma authored May 24, 2017
2 parents 858a80d + 19b6cd5 commit ec2427b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
1 change: 1 addition & 0 deletions lib/review_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

require_relative 'review_bot/hour_of_day'
require_relative 'review_bot/pull_request'
require_relative 'review_bot/pull_request_review'
require_relative 'review_bot/extensions'
require_relative 'review_bot/reviewer'
require_relative 'review_bot/reminder'
Expand Down
69 changes: 29 additions & 40 deletions lib/review_bot/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ def needs_review?
end

def needs_first_review?
needs_review? && reviewers.count == 0
needs_review? && reviewers.count.zero?
end

def reviewers
[comments_from_other_humans, reviews_from_other_humans].flatten
.map { |i| i['user']['login'] }
.uniq
reviews_from_other_humans
.map { |r| r.user.login }
.uniq
end

def last_touched_at
if last_touch
Time.parse(last_touch['created_at'] || last_touch['submitted_at'])
Time.parse(last_touch.created_at)
else
Time.parse(created_at)
end
end

def repo_owner
base.repo.owner.login
end

def repo_name
base.repo.name
end

def inspect
"GithubPullRequest##{number} ( " +
[
Expand All @@ -39,7 +47,7 @@ def inspect
private

def approved?
labels.include?('+2')
approvals_count > 1
end

def blocked?
Expand All @@ -51,24 +59,16 @@ def review_in_progress?
when 0
false
when 1
!labels.include?('+1')
approvals_count != 1
else
!approved?
end
end

def last_touch
@last_touch ||= [comments_from_other_humans, reviews_from_other_humans].flatten
.sort_by { |comment_or_review| comment_or_review['created_at'] || comment_or_review['submitted_at'] }
.last
end

def repo_owner
base.repo.owner.login
end

def repo_name
base.repo.name
@last_touch ||= reviews_from_other_humans
.sort_by(&:created_at)
.last
end

def labels
Expand All @@ -79,35 +79,24 @@ def issue
@issue ||= GH.issues.get(repo_owner, repo_name, number)
end

def comments
@comments ||= GH.issues.comments.list(repo_owner, repo_name, number: number).body
end

def comments_from_humans
comments.reject { |c| c.user.login.include?('-bot') }
end

def comments_from_other_humans
comments_from_humans.select { |c| c.user.login != user.login }
end

def reviews
# github_api doesn't support this yet
@reviews ||= begin
conn = Faraday.new(
url: 'https://api.github.com',
headers: { Accept: 'application/vnd.github.black-cat-preview+json' }
)
JSON.parse(conn.get("/repos/#{repo_owner}/#{repo_name}/pulls/#{number}/reviews?access_token=#{ENV['GH_AUTH_TOKEN']}").body)
end
@reviews ||= PullRequestReview.for_pull_request(self)
end

def reviews_from_humans
reviews.reject { |r| r['user']['login'].include?('-bot') }
reviews.reject { |r| r.user.login.include?('-bot') }
end

def reviews_from_other_humans
reviews_from_humans.select { |r| r['user']['login'] != user.login }
reviews_from_humans.select { |r| r.user.login != user.login }
end

def approvals_count
reviews_from_other_humans
.select(&:approved?)
.map { |r| r.user.login }
.uniq
.count
end
end
end
24 changes: 24 additions & 0 deletions lib/review_bot/pull_request_review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module ReviewBot
class PullRequestReview < Hashie::Mash
def self.for_pull_request(pull_request)
# github_api doesn't support this yet
conn = Faraday.new(
url: 'https://api.github.com',
headers: { Accept: 'application/vnd.github.black-cat-preview+json' }
)
reviews_json = conn.get(
"/repos/#{pull_request.repo_owner}/#{pull_request.repo_name}/pulls/#{pull_request.number}/reviews?access_token=#{ENV['GH_AUTH_TOKEN']}"
).body

JSON.parse(reviews_json).map { |r| new r }
end

def approved?
state == 'APPROVED'
end

def created_at
submitted_at
end
end
end

0 comments on commit ec2427b

Please sign in to comment.