From 4a7579c693294bf5e12d8588cee980f32261b137 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 25 Aug 2024 01:26:06 +0800 Subject: [PATCH] utils/github: use `paginate_graphql` in `sponsorships` `sponsorships` has its own implementation of GraphQL pagination. We can simplify this by using `paginate_graphql` instead. --- Library/Homebrew/utils/github.rb | 55 ++++++++++++---------------- Library/Homebrew/utils/github/api.rb | 14 ++++--- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 667366fbcb..a78c65cba5 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -434,41 +434,37 @@ module GitHub ) } def self.sponsorships(user) - has_next_page = T.let(true, T::Boolean) - after = "" - sponsorships = T.let([], T::Array[Hash]) - errors = T.let([], T::Array[Hash]) - while has_next_page - query = <<~EOS - { organization(login: "#{user}") { - sponsorshipsAsMaintainer(first: 100 #{after}) { - pageInfo { - startCursor - hasNextPage - endCursor - } - totalCount - nodes { - tier { + query = <<~EOS + query($user: String, $after: String) { organization(login: $user) { + sponsorshipsAsMaintainer(first: 100, after: $after) { + pageInfo { + hasNextPage + endCursor + } + nodes { + tier { + monthlyPriceInDollars + closestLesserValueTier { monthlyPriceInDollars - closestLesserValueTier { - monthlyPriceInDollars - } - } - sponsorEntity { - __typename - ... on Organization { login name } - ... on User { login name } } } + sponsorEntity { + ... on Organization { login name } + ... on User { login name } + } } } } - EOS + } + EOS + + sponsorships = T.let([], T::Array[Hash]) + errors = T.let([], T::Array[Hash]) + + API.paginate_graphql(query, variables: { user: }, scopes: ["user"], raise_errors: false) do |result| # Some organisations do not permit themselves to be queried through the # API like this and raise an error so handle these errors later. # This has been reported to GitHub. - result = API.open_graphql(query, scopes: ["user"], raise_errors: false) errors += result["errors"] if result["errors"].present? current_sponsorships = result["data"]["organization"]["sponsorshipsAsMaintainer"] @@ -478,12 +474,7 @@ module GitHub sponsorships += nodes end - if (page_info = current_sponsorships["pageInfo"].presence) && - page_info["hasNextPage"].presence - after = %Q(, after: "#{page_info["endCursor"]}") - else - has_next_page = false - end + current_sponsorships.fetch("pageInfo") end # Only raise errors if we didn't get any sponsorships. diff --git a/Library/Homebrew/utils/github/api.rb b/Library/Homebrew/utils/github/api.rb index 1c06a9c82e..e24b78af61 100644 --- a/Library/Homebrew/utils/github/api.rb +++ b/Library/Homebrew/utils/github/api.rb @@ -348,13 +348,15 @@ module GitHub sig { params( - query: String, - variables: T.nilable(T::Hash[Symbol, T.untyped]), - _block: T.proc.params(data: T::Hash[String, T.untyped]).returns(T::Hash[String, T.untyped]), + query: String, + variables: T.nilable(T::Hash[Symbol, T.untyped]), + scopes: T::Array[String], + raise_errors: T::Boolean, + _block: T.proc.params(data: T::Hash[String, T.untyped]).returns(T::Hash[String, T.untyped]), ).void } - def self.paginate_graphql(query, variables: nil, &_block) - result = API.open_graphql(query, variables:) + def self.paginate_graphql(query, variables: nil, scopes: [].freeze, raise_errors: true, &_block) + result = API.open_graphql(query, variables:, scopes:, raise_errors:) has_next_page = T.let(true, T::Boolean) variables ||= {} @@ -363,7 +365,7 @@ module GitHub has_next_page = page_info["hasNextPage"] if has_next_page variables[:after] = page_info["endCursor"] - result = API.open_graphql(query, variables:) + result = API.open_graphql(query, variables:, scopes:, raise_errors:) end end end