github: fetch approved reviews for a pull request

This commit is contained in:
Jonathan Chang 2020-06-27 22:01:51 +10:00
parent b4cd99c67c
commit 90309e5f42
2 changed files with 53 additions and 0 deletions

View File

@ -42,6 +42,13 @@ describe GitHub do
end
end
describe "::approved_reviews", :needs_network do
it "can get reviews for a pull request" do
reviews = subject.approved_reviews("Homebrew", "homebrew-core", 1, commit: "deadbeef")
expect(reviews).to eq([])
end
end
describe "::get_artifact_url", :needs_network do
it "fails to find a nonexistant workflow" do
expect {

View File

@ -394,6 +394,52 @@ module GitHub
open_api(uri) { |json| json.fetch("items", []) }
end
def approved_reviews(user, repo, pr, commit: nil)
url = "https://api.github.com/graphql"
data = {
query: <<~EOS,
{ repository(name: "#{repo}", owner: "#{user}") {
pullRequest(number: #{pr}) {
reviews(states: APPROVED, first: 100) {
nodes {
author {
... on User { email login name databaseId }
... on Organization { email login name databaseId }
}
authorAssociation
commit { oid }
}
}
}
}
}
EOS
}
result = open_api(url, data: data, request_method: "POST")
raise Error, result["errors"] if result["errors"].present?
reviews = result["data"]["repository"]["pullRequest"]["reviews"]["nodes"]
reviews.map do |r|
next if commit.present? && commit != r["commit"]["oid"]
next unless %w[MEMBER OWNER].include? r["authorAssociation"]
email = if r["author"]["email"].empty?
"#{r["author"]["databaseId"]}+#{r["author"]["login"]}@users.noreply.github.com"
else
r["author"]["email"]
end
name = r["author"]["name"] || r["author"]["login"]
{
"email" => email,
"name" => name,
"login" => r["author"]["login"],
}
end.compact
end
def dispatch_event(user, repo, event, **payload)
url = "#{API_URL}/repos/#{user}/#{repo}/dispatches"
open_api(url, data: { event_type: event, client_payload: payload },