Skip to content

Commit

Permalink
802 error on large number of discussions (#901)
Browse files Browse the repository at this point in the history
* handle pagination

* refactor to new API

* add tests

* use existing function
  • Loading branch information
wd60622 authored Feb 25, 2025
1 parent de0f441 commit dbf3a6a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
26 changes: 11 additions & 15 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1346,27 +1346,19 @@ function M.discussions(opts)

local replace = create_replace(opts.cb)

local cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
local create_discussion_picker = function(discussions)
if #discussions == 0 then
utils.error(string.format("There are no matching discussions in %s.", opts.repo))
return
end

local resp = utils.aggregate_pages(output, "data.repository.discussions.node")
local discussions = resp.data.repository.discussions.nodes

local max_number = -1
for _, discussion in ipairs(discussions) do
if #tostring(discussion.number) > max_number then
max_number = #tostring(discussion.number)
end
end

if #discussions == 0 then
utils.error(string.format("There are no matching discussions in %s.", opts.repo))
return
end

opts.preview_title = opts.preview_title or ""

pickers
Expand All @@ -1388,11 +1380,10 @@ function M.discussions(opts)

local owner, name = utils.split_repo(opts.repo)
local order_by = cfg.discussions.order_by
local query = graphql "discussions_query"
utils.info "Fetching discussions (this may take a while) ..."

gh.api.graphql {
query = query,
query = graphql "discussions_query",
fields = {
owner = owner,
name = name,
Expand All @@ -1401,9 +1392,14 @@ function M.discussions(opts)
direction = order_by.direction,
},
paginate = true,
jq = ".",
jq = ".data.repository.discussions.nodes",
opts = {
cb = cb,
cb = gh.create_callback {
success = function(output)
local discussions = utils.get_flatten_pages(output)
create_discussion_picker(discussions)
end,
},
},
}
end
Expand Down
28 changes: 12 additions & 16 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -766,30 +766,26 @@ function M.get_repo_templates(repo)
return templates
end

---Helper method to aggregate an API paginated response
---@param text string
---@return table[]
function M.get_pages(text)
function M.callback_per_page(text, cb)
local results = {}
local page_outputs = vim.split(text, "\n")
for _, page in ipairs(page_outputs) do
local page_output = vim.split(text, "\n")
for _, page in ipairs(page_output) do
local decoded_page = vim.json.decode(page)
table.insert(results, decoded_page)
cb(results, decoded_page)
end
return results
end

---Helper method to aggregate an API paginated response
---@param text string
---@return table[]
function M.get_pages(text)
return M.callback_per_page(text, table.insert)
end

--- Helper method to aggregate an API paginated response
function M.get_flatten_pages(text)
local results = {}
local page_outputs = vim.split(text, "\n")
for _, page in ipairs(page_outputs) do
local decoded_page = vim.json.decode(page)
for _, result in ipairs(decoded_page) do
table.insert(results, result)
end
end
return results
return M.callback_per_page(text, vim.list_extend)
end

--- Helper method to aggregate an API paginated response
Expand Down
45 changes: 45 additions & 0 deletions lua/tests/plenary/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,48 @@ describe("Utils parse_remote_url(): ", function()
eq(expected, this.parse_remote_url(url, ssh_aliases))
end)
end)
describe("get_pages", function()
it("handles empty single page", function()
local text = "[]"
local actual = this.get_pages(text)

eq(actual, { {} })
end)
it("handles multiple pages", function()
local text = vim.trim [[
[1,2,3]
[4,5,6]
[7,8,9]
]]

local actual = this.get_pages(text)
eq(actual, { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } })
end)
end)
describe("get_flatten_pages", function()
it("handles empty single page", function()
local text = "[]"
local actual = this.get_flatten_pages(text)

eq(actual, {})
end)
it("handles multiple list pages", function()
local text = vim.trim [[
[1,2,3]
[4,5,6]
[7,8,9]
]]
local actual = this.get_flatten_pages(text)

eq(actual, { 1, 2, 3, 4, 5, 6, 7, 8, 9 })
end)
it("handles multiple json pages", function()
local text = vim.trim [[
[{"a": 1},{"b": 2, "name": "foo"}]
[{"c": 3}]
[{"d": 4}]
]]
local actual = this.get_flatten_pages(text)
eq(actual, { { a = 1 }, { b = 2, name = "foo" }, { c = 3 }, { d = 4 } })
end)
end)

0 comments on commit dbf3a6a

Please sign in to comment.