utils/github: use paginate_graphql in sponsorships

`sponsorships` has its own implementation of GraphQL pagination. We can
simplify this by using `paginate_graphql` instead.
This commit is contained in:
Carlo Cabrera 2024-08-25 01:26:06 +08:00
parent 019fc519da
commit 4a7579c693
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
2 changed files with 31 additions and 38 deletions

View File

@ -434,41 +434,37 @@ module GitHub
) )
} }
def self.sponsorships(user) def self.sponsorships(user)
has_next_page = T.let(true, T::Boolean) query = <<~EOS
after = "" query($user: String, $after: String) { organization(login: $user) {
sponsorships = T.let([], T::Array[Hash]) sponsorshipsAsMaintainer(first: 100, after: $after) {
errors = T.let([], T::Array[Hash]) pageInfo {
while has_next_page hasNextPage
query = <<~EOS endCursor
{ organization(login: "#{user}") { }
sponsorshipsAsMaintainer(first: 100 #{after}) { nodes {
pageInfo { tier {
startCursor monthlyPriceInDollars
hasNextPage closestLesserValueTier {
endCursor
}
totalCount
nodes {
tier {
monthlyPriceInDollars 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 # Some organisations do not permit themselves to be queried through the
# API like this and raise an error so handle these errors later. # API like this and raise an error so handle these errors later.
# This has been reported to GitHub. # This has been reported to GitHub.
result = API.open_graphql(query, scopes: ["user"], raise_errors: false)
errors += result["errors"] if result["errors"].present? errors += result["errors"] if result["errors"].present?
current_sponsorships = result["data"]["organization"]["sponsorshipsAsMaintainer"] current_sponsorships = result["data"]["organization"]["sponsorshipsAsMaintainer"]
@ -478,12 +474,7 @@ module GitHub
sponsorships += nodes sponsorships += nodes
end end
if (page_info = current_sponsorships["pageInfo"].presence) && current_sponsorships.fetch("pageInfo")
page_info["hasNextPage"].presence
after = %Q(, after: "#{page_info["endCursor"]}")
else
has_next_page = false
end
end end
# Only raise errors if we didn't get any sponsorships. # Only raise errors if we didn't get any sponsorships.

View File

@ -348,13 +348,15 @@ module GitHub
sig { sig {
params( params(
query: String, query: String,
variables: T.nilable(T::Hash[Symbol, T.untyped]), variables: T.nilable(T::Hash[Symbol, T.untyped]),
_block: T.proc.params(data: T::Hash[String, T.untyped]).returns(T::Hash[String, 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 ).void
} }
def self.paginate_graphql(query, variables: nil, &_block) def self.paginate_graphql(query, variables: nil, scopes: [].freeze, raise_errors: true, &_block)
result = API.open_graphql(query, variables:) result = API.open_graphql(query, variables:, scopes:, raise_errors:)
has_next_page = T.let(true, T::Boolean) has_next_page = T.let(true, T::Boolean)
variables ||= {} variables ||= {}
@ -363,7 +365,7 @@ module GitHub
has_next_page = page_info["hasNextPage"] has_next_page = page_info["hasNextPage"]
if has_next_page if has_next_page
variables[:after] = page_info["endCursor"] variables[:after] = page_info["endCursor"]
result = API.open_graphql(query, variables:) result = API.open_graphql(query, variables:, scopes:, raise_errors:)
end end
end end
end end