Merge pull request #18152 from Homebrew/more-paginate-graphql

utils/github: use `paginate_graphql` in `sponsorships`
This commit is contained in:
Bo Anderson 2024-08-24 20:17:02 +01:00 committed by GitHub
commit 0408c184ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 38 deletions

View File

@ -434,20 +434,13 @@ 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}) {
query($user: String, $after: String) { organization(login: $user) {
sponsorshipsAsMaintainer(first: 100, after: $after) {
pageInfo {
startCursor
hasNextPage
endCursor
}
totalCount
nodes {
tier {
monthlyPriceInDollars
@ -456,7 +449,6 @@ module GitHub
}
}
sponsorEntity {
__typename
... on Organization { login name }
... on User { login name }
}
@ -465,10 +457,14 @@ module GitHub
}
}
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.

View File

@ -350,11 +350,13 @@ module GitHub
params(
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