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,20 +434,13 @@ module GitHub
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
def self.sponsorships(user)
|
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
|
query = <<~EOS
|
||||||
{ organization(login: "#{user}") {
|
query($user: String, $after: String) { organization(login: $user) {
|
||||||
sponsorshipsAsMaintainer(first: 100 #{after}) {
|
sponsorshipsAsMaintainer(first: 100, after: $after) {
|
||||||
pageInfo {
|
pageInfo {
|
||||||
startCursor
|
|
||||||
hasNextPage
|
hasNextPage
|
||||||
endCursor
|
endCursor
|
||||||
}
|
}
|
||||||
totalCount
|
|
||||||
nodes {
|
nodes {
|
||||||
tier {
|
tier {
|
||||||
monthlyPriceInDollars
|
monthlyPriceInDollars
|
||||||
@ -456,7 +449,6 @@ module GitHub
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sponsorEntity {
|
sponsorEntity {
|
||||||
__typename
|
|
||||||
... on Organization { login name }
|
... on Organization { login name }
|
||||||
... on User { login name }
|
... on User { login name }
|
||||||
}
|
}
|
||||||
@ -465,10 +457,14 @@ module GitHub
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
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.
|
||||||
|
|||||||
@ -350,11 +350,13 @@ module GitHub
|
|||||||
params(
|
params(
|
||||||
query: String,
|
query: String,
|
||||||
variables: T.nilable(T::Hash[Symbol, T.untyped]),
|
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]),
|
_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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user