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:
parent
019fc519da
commit
4a7579c693
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user