Skip to content

Commit

Permalink
fix: handle nil pull.author.id
Browse files Browse the repository at this point in the history
fix: validate & document pull_requests.limit (default 100)

Update README.md

Co-authored-by: Jonas-Taha El Sesiy <github@elsesiy.com>
  • Loading branch information
legobeat and elsesiy committed Nov 4, 2024
1 parent c63216a commit d23e3f7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ require"octo".setup({
field = "CREATED_AT", -- either COMMENTS, CREATED_AT or UPDATED_AT (https://docs.github.com/en/graphql/reference/enums#issueorderfield)
direction = "DESC" -- either DESC or ASC (https://docs.github.com/en/graphql/reference/enums#orderdirection)
},
limit = 100 -- maximum number of pull requests in `Octo pr list` results
always_select_remote_on_create = false -- always give prompt to select base remote repo when creating PRs
},
file_panel = {
Expand Down
10 changes: 8 additions & 2 deletions lua/octo/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ local M = {}
---@class OctoConfigPR
---@field order_by OctoConfigOrderBy
---@field always_select_remote_on_create boolean
---@field limit number

---@class OctoConfigOrderBy
---@field field string
Expand Down Expand Up @@ -140,7 +141,7 @@ function M.get_default_values()
field = "CREATED_AT",
direction = "DESC",
},
limit = 50,
limit = 100,
always_select_remote_on_create = false,
},
file_panel = {
Expand Down Expand Up @@ -402,7 +403,12 @@ function M.validate_config()
validate_type(config.pull_requests.order_by.field, "pull_requests.order_by.field", "string")
validate_type(config.pull_requests.order_by.direction, "pull_requests.order_by.direction", "string")
end
validate_type(config.pull_requests.always_select_remote_on_create, "always_select_remote_on_create", "boolean")
validate_type(
config.pull_requests.always_select_remote_on_create,
"pull_requests.always_select_remote_on_create",
"boolean"
)
validate_type(config.pull_requests.limit, "pull_requests.limit", "number")
end

local function validate_mappings()
Expand Down
38 changes: 28 additions & 10 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,20 @@ function M.pull_requests(opts)

utils.info "Fetching pull requests (this may take a while) ..."
local args = {
"pr", "list",
"--limit", tostring(opts.limit),
"--json", "author,number,title,url,headRepository,headRepositoryOwner,headRefName,isDraft"
"pr",
"list",
"--limit",
tostring(opts.limit),
"--json",
"author,number,title,url,headRepository,headRepositoryOwner,headRefName,isDraft",
}

if opts.repo then
table.insert(args, "-R")
table.insert(args, opts.repo)
end
local keys = { "author", "assignee", "label", "state", "head", "base", "search" }

local keys = { "author", "assignee", "label", "state", "head", "base", "search" }
for _, key in ipairs(keys) do
if opts[key] then
table.insert(args, "--" .. key)
Expand All @@ -328,20 +332,29 @@ function M.pull_requests(opts)
local max_number = -1
local username_col_len = 0
local branch_name_col_len = 0
local authors = {}
local author_ids = {}
local author_count = 0
for _, pull in ipairs(pull_requests) do
-- 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
local author_id = "?"
local author_name = "?"
if pull.author ~= nil then
if pull.author.id ~= nil then
author_id = pull.author.id
end
if pull.author.login ~= nil then
author_name = pull.author.login
end
end
author_ids[author_id] = true
username_col_len = math.min(20, math.max(username_col_len, #tostring(author_name)))
max_number = math.max(max_number, #tostring(pull.number))
username_col_len = math.min(20, math.max(username_col_len, #tostring(pull.author.login)))
branch_name_col_len = math.min(20, math.max(branch_name_col_len, #tostring(pull.headRefName)))
end
for _ in pairs(authors) do
for _ in pairs(author_ids) do
author_count = author_count + 1
end

Expand All @@ -352,7 +365,12 @@ function M.pull_requests(opts)
.new(opts, {
finder = finders.new_table {
results = pull_requests,
entry_maker = entry_maker.gen_from_pull_request(max_number, username_col_len, branch_name_col_len, author_count),
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

0 comments on commit d23e3f7

Please sign in to comment.