Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the Octo pr list command #546

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/octo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ See |octo-command-examples| for examples.
<C-b> Open PR in browser.
<C-o> Checkout PR.
<C-y> Copy URL to system clipboard.
Available filter keys: see <https://docs.github.com/en/graphql/reference/input-objects#issuefilters>
Available filter keys: author,assignee,label,state,head,base,search,limit
search Live PR search.
edit [number] {repo} Edits issue/PR with the given number in the current or
specified repo.
Expand Down
1 change: 1 addition & 0 deletions lua/octo/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function M.get_default_values()
field = "CREATED_AT",
direction = "DESC",
},
limit = 50,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add to the docs and validate is a number

always_select_remote_on_create = false,
},
file_panel = {
Expand Down
1 change: 1 addition & 0 deletions lua/octo/gh/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,7 @@ query($endCursor: String) {
repository { nameWithOwner }
headRefName
isDraft
author { username: login }
}
pageInfo {
hasNextPage
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/prs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ return function(opts)
highlight = "OctoStateOpen"
end
local prefix = fzf.utils.ansi_from_hl(highlight, entry.value)
fzf_cb(prefix .. " " .. entry.obj.title)
fzf_cb(prefix .. " " .. entry.obj.title .. " (" .. entry.obj.headRefName .. ")")
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions lua/octo/pickers/telescope/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,58 @@ local utils = require "octo.utils"

local M = {}

function M.gen_from_pull_request(max_number, username_col_len, branch_name_col_len, author_count)
local make_display = function(entry)
if not entry then
return nil
end

local layout, columns
columns = {
{ entry.value, "TelescopeResultsNumber" },
{ entry.obj.author.username },
{ entry.obj.headRefName },
{ entry.obj.title },
}
layout = {
separator = " ",
items = {
{ width = max_number },
{ width = username_col_len },
{ width = branch_name_col_len },
{ remaining = true },
},
}

-- hide author column if there is just one author
if author_count == 1 then
table.remove(columns, 2)
table.remove(layout.items, 2)
end

local displayer = entry_display.create(layout)

return displayer(columns)
end

return function(obj)
if not obj or vim.tbl_isempty(obj) then
return nil
end
local filename = utils.get_pull_request_uri(obj.repository.nameWithOwner, obj.number)

return {
filename = filename,
kind = "pull_request",
value = obj.number,
ordinal = obj.number .. " " .. obj.title .. " " .. obj.author.username .. " " .. obj.headRefName,
display = make_display,
obj = obj,
repo = obj.repository.nameWithOwner,
}
end
end

function M.gen_from_issue(max_number, print_repo)
local make_display = function(entry)
if not entry then
Expand Down
63 changes: 42 additions & 21 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,50 +268,71 @@ end

function M.pull_requests(opts)
opts = opts or {}
if not opts.states then
opts.states = "OPEN"
local cfg = octo_config.values

if not opts.limit then
opts.limit = cfg.pull_requests.limit
end
local filter = get_filter(opts, "pull_request")
if utils.is_blank(opts.repo) then
opts.repo = utils.get_remote_name()

utils.info "Fetching pull requests (this may take a while) ..."
local args = {
"pr", "list",
"--limit", tostring(opts.limit),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add --paginate in case limit is bigger than 100?

"--json", "author,number,title,url,headRepository,headRepositoryOwner,headRefName,isDraft"
}
if opts.repo then
table.insert(args, "-R")
table.insert(args, opts.repo)
end
if not opts.repo then
utils.error "Cannot find repo"
return
local keys = { "author", "assignee", "label", "state", "head", "base", "search" }

for _, key in ipairs(keys) do
if opts[key] then
table.insert(args, "--" .. key)
table.insert(args, opts[key])
end
end

local owner, name = utils.split_repo(opts.repo)
local cfg = octo_config.values
local order_by = cfg.pull_requests.order_by
local query =
graphql("pull_requests_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })
utils.info "Fetching pull requests (this may take a while) ..."
gh.run {
args = { "api", "graphql", "--paginate", "--jq", ".", "-f", string.format("query=%s", query) },
args = args,

cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = utils.aggregate_pages(output, "data.repository.pullRequests.nodes")
local pull_requests = resp.data.repository.pullRequests.nodes
local pull_requests = vim.fn.json_decode(output)
if #pull_requests == 0 then
utils.error(string.format("There are no matching pull requests in %s.", opts.repo))
return
end
local max_number = -1
local username_col_len = 0
local branch_name_col_len = 0
local authors = {}
local author_count = 0
for _, pull in ipairs(pull_requests) do
if #tostring(pull.number) > max_number then
max_number = #tostring(pull.number)
end
-- modify result to be consistent with GraphQL output.
pull["__typename"] = "pull_request"
pull.author.username = pull.author.login
pull.repository = { nameWithOwner = pull.headRepositoryOwner.login .. "/" .. pull.headRepository.name }

authors[pull.author.id] = (authors[pull.author.id] or 0) + 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im testing Octo pr list for the neovim/neovim repo and getting the following error in this line:

Error executing vim.schedule lua callback: ...tester/octo.nvim/lua/octo/pickers/telescope/provider.lua:319: table index is nil
stack traceback:
        ...tester/octo.nvim/lua/octo/pickers/telescope/provider.lua:319: in function 'cb'
        .../src/github.com/pwntester/octo.nvim/lua/octo/gh/init.lua:161: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

Seems like id may be nil and needs to be handled

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
authors[pull.author.id] = (authors[pull.author.id] or 0) + 1
authorid = pull.author.id or 0
authors[pull.author.id] = (authors[pull.author.id] or 0) + 1

max_number = math.max(max_number, #tostring(pull.number))
username_col_len = math.min(20, math.max(username_col_len, #tostring(pull.author.username)))
branch_name_col_len = math.min(20, math.max(branch_name_col_len, #tostring(pull.headRefName)))
end
for _ in pairs(authors) do
author_count = author_count + 1
end

opts.preview_title = opts.preview_title or ""
opts.prompt_title = opts.prompt_title or ""
opts.results_title = opts.results_title or ""
pickers
.new(opts, {
finder = finders.new_table {
results = pull_requests,
entry_maker = entry_maker.gen_from_issue(max_number),
entry_maker = entry_maker.gen_from_pull_request(max_number, username_col_len, branch_name_col_len, author_count),
},
sorter = conf.generic_sorter(opts),
previewer = previewers.issue.new(opts),
Expand Down
Loading