From 19b6cd5a96125c3530b53f60d73a6896ea2eaaac Mon Sep 17 00:00:00 2001 From: Daniel Ma Date: Wed, 24 May 2017 13:51:28 -0700 Subject: [PATCH] change(PullRequest): only consider reviews Stop considering comments and labels when trying to understand if a pull request is in review and if it has been approved. Use reviews only --- lib/review_bot.rb | 1 + lib/review_bot/pull_request.rb | 69 +++++++++++---------------- lib/review_bot/pull_request_review.rb | 24 ++++++++++ 3 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 lib/review_bot/pull_request_review.rb diff --git a/lib/review_bot.rb b/lib/review_bot.rb index eb3bc26..09323db 100644 --- a/lib/review_bot.rb +++ b/lib/review_bot.rb @@ -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' diff --git a/lib/review_bot/pull_request.rb b/lib/review_bot/pull_request.rb index 0d4b649..d113685 100644 --- a/lib/review_bot/pull_request.rb +++ b/lib/review_bot/pull_request.rb @@ -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} ( " + [ @@ -39,7 +47,7 @@ def inspect private def approved? - labels.include?('+2') + approvals_count > 1 end def blocked? @@ -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 @@ -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 diff --git a/lib/review_bot/pull_request_review.rb b/lib/review_bot/pull_request_review.rb new file mode 100644 index 0000000..f1cfef8 --- /dev/null +++ b/lib/review_bot/pull_request_review.rb @@ -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