-
Notifications
You must be signed in to change notification settings - Fork 156
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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), | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add |
||||||||
"--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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im testing
Seems like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
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), | ||||||||
|
There was a problem hiding this comment.
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