Skip to content

Commit

Permalink
Update type annotations, minor refactors, better error messages (#844)
Browse files Browse the repository at this point in the history
* refactor: add type annotations and use vim.json instead of vimscript version

* fix: better error messages to avoid regressions like #841
  • Loading branch information
TheLeoP authored Feb 4, 2025
1 parent ef589fc commit 08359aa
Show file tree
Hide file tree
Showing 27 changed files with 159 additions and 119 deletions.
18 changes: 9 additions & 9 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ function M.delete_comment()
cb = function(output)
-- TODO: deleting the last review thread comment, it deletes the whole thread and review
-- In issue buffers, we should hide the thread snippet
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)

-- remove comment lines from the buffer
if comment.reactionLine then
Expand Down Expand Up @@ -788,7 +788,7 @@ function M.resolve_thread()
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local thread = resp.data.resolveReviewThread.thread
if thread.isResolved then
update_review_thread_header(bufnr, thread, thread_id, thread_line)
Expand Down Expand Up @@ -818,7 +818,7 @@ function M.unresolve_thread()
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local thread = resp.data.unresolveReviewThread.thread
if not thread.isResolved then
update_review_thread_header(bufnr, thread, thread_id, thread_line)
Expand Down Expand Up @@ -876,7 +876,7 @@ function M.change_state(state)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)

local obj = get_obj(resp)
local new_state = obj.state
Expand Down Expand Up @@ -961,7 +961,7 @@ function M.save_issue(opts)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
require("octo").create_buffer("issue", resp.data.createIssue.issue, opts.repo, true)
vim.fn.execute "normal! Gk"
vim.fn.execute "startinsert"
Expand Down Expand Up @@ -1175,7 +1175,7 @@ function M.save_pr(opts)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local pr = resp.data.createPullRequest.pullRequest
utils.info(string.format("#%d - `%s` created successfully", pr.number, pr.title))
require("octo").create_buffer("pull", pr, opts.repo, true)
Expand Down Expand Up @@ -1401,7 +1401,7 @@ function M.reaction_action(reaction)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
if action == "add" then
reaction_groups = resp.data.addReaction.subject.reactionGroups
elseif action == "remove" then
Expand Down Expand Up @@ -1527,7 +1527,7 @@ function M.set_project_v2_card()
if add_stderr and not utils.is_blank(add_stderr) then
utils.error(add_stderr)
elseif add_output then
local resp = vim.fn.json_decode(add_output)
local resp = vim.json.decode(add_output)
local update_query = graphql(
"update_project_v2_item_mutation",
project_id,
Expand Down Expand Up @@ -1631,7 +1631,7 @@ function M.create_label(label)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local label = resp.data.createLabel.label
utils.info("Created label: " .. label.name)

Expand Down
3 changes: 3 additions & 0 deletions lua/octo/gh/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,9 @@ query($name: String!, $owner: String!, $n_milestones: Int!) {
}
]]

---@param query string
---@param ... string|table
---@return string
return function(query, ...)
local opts = { escape = true }
for _, v in ipairs { ... } do
Expand Down
8 changes: 4 additions & 4 deletions lua/octo/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function M.load(repo, kind, number, cb)
local obj = resp.data.repository[key]
cb(obj)
elseif kind == "repo" then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local obj = resp.data.repository
cb(obj)
end
Expand Down Expand Up @@ -184,7 +184,7 @@ function M.on_cursor_hold()
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local reactions = {}
local reactionGroups = resp.data.node.reactionGroups
for _, reactionGroup in ipairs(reactionGroups) do
Expand Down Expand Up @@ -220,7 +220,7 @@ function M.on_cursor_hold()
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local user = resp.data.user
local popup_bufnr = vim.api.nvim_create_buf(false, true)
local lines, max_length = writers.write_user_profile(popup_bufnr, user)
Expand Down Expand Up @@ -248,7 +248,7 @@ function M.on_cursor_hold()
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local issue = resp.data.repository.issueOrPullRequest
local popup_bufnr = vim.api.nvim_create_buf(false, true)
local max_length = 80
Expand Down
18 changes: 9 additions & 9 deletions lua/octo/model/octo-buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ function OctoBuffer:async_fetch_taggable_users()
args = { "api", string.format("repos/%s/contributors", self.repo) },
cb = function(response)
if not utils.is_blank(response) then
local resp = vim.fn.json_decode(response)
local resp = vim.json.decode(response)
for _, contributor in ipairs(resp) do
table.insert(users, contributor.login)
end
Expand All @@ -322,7 +322,7 @@ function OctoBuffer:async_fetch_issues()
args = { "api", string.format("repos/%s/issues", self.repo) },
cb = function(response)
local issues_metadata = {}
local resp = vim.fn.json_decode(response)
local resp = vim.json.decode(response)
for _, issue in ipairs(resp) do
table.insert(issues_metadata, { number = issue.number, title = issue.title })
end
Expand Down Expand Up @@ -399,7 +399,7 @@ function OctoBuffer:do_save_title_and_body()
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local obj
if self:isPullRequest() then
obj = resp.data.updatePullRequest.pullRequest
Expand Down Expand Up @@ -437,7 +437,7 @@ function OctoBuffer:do_add_issue_comment(comment_metadata)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local respBody = resp.data.addComment.commentEdge.node.body
local respId = resp.data.addComment.commentEdge.node.id
if utils.trim(comment_metadata.body) == utils.trim(respBody) then
Expand Down Expand Up @@ -472,7 +472,7 @@ function OctoBuffer:do_add_thread_comment(comment_metadata)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local resp_comment = resp.data.addPullRequestReviewComment.comment
local comment_end
if utils.trim(comment_metadata.body) == utils.trim(resp_comment.body) then
Expand Down Expand Up @@ -584,7 +584,7 @@ function OctoBuffer:do_add_new_thread(comment_metadata)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output).data.addPullRequestReviewThread
local resp = vim.json.decode(output).data.addPullRequestReviewThread
if not utils.is_blank(resp.thread) then
local new_comment = resp.thread.comments.nodes[1]
if utils.trim(comment_metadata.body) == utils.trim(new_comment.body) then
Expand Down Expand Up @@ -681,7 +681,7 @@ function OctoBuffer:do_add_new_thread(comment_metadata)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local r = vim.fn.json_decode(output)
local r = vim.json.decode(output)
local resp = r.data.addPullRequestReviewComment
if not utils.is_blank(resp.comment) then
if utils.trim(comment_metadata.body) == utils.trim(resp.comment.body) then
Expand Down Expand Up @@ -734,7 +734,7 @@ function OctoBuffer:do_add_pull_request_comment(comment_metadata)
if not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
if not utils.is_blank(resp) then
if utils.trim(comment_metadata.body) == utils.trim(resp.body) then
local comments = self.commentsMetadata
Expand Down Expand Up @@ -774,7 +774,7 @@ function OctoBuffer:do_update_comment(comment_metadata)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local resp_comment
if comment_metadata.kind == "IssueComment" then
resp_comment = resp.data.updateIssueComment.issueComment
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/model/pull-request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function PullRequest:get_changed_files(callback)
utils.error(stderr)
elseif output then
local FileEntry = require("octo.reviews.file-entry").FileEntry
local results = vim.fn.json_decode(output)
local results = vim.json.decode(output)
results = merge_pages(results)
local files = {}
for _, result in ipairs(results) do
Expand Down Expand Up @@ -135,7 +135,7 @@ function PullRequest:get_commit_changed_files(rev, callback)
utils.error(stderr)
elseif output then
local FileEntry = require("octo.reviews.file-entry").FileEntry
local results = vim.fn.json_decode(output)
local results = vim.json.decode(output)
results = merge_pages(results)
local files = {}
if results.files then
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/navigation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function M.go_to_issue()
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local kind = resp.data.repository.issueOrPullRequest.__typename
if kind == "Issue" then
utils.get_issue(number, repo)
Expand Down
4 changes: 3 additions & 1 deletion lua/octo/pickers/fzf-lua/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ local fzf = require "fzf-lua"

local M = {}

---@param issue_table table
---@return table|nil
function M.gen_from_issue(issue_table)
if not issue_table or vim.tbl_isempty(issue_table) then
return nil
end
local kind = issue_table.__typename == "Issue" and "issue" or "pull_request"
local filename
local filename ---@type string
if kind == "issue" then
filename = utils.get_issue_uri(issue_table.number, issue_table.repository.nameWithOwner)
else
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/assigned_labels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ return function(cb)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local labels = resp.data.repository[key].labels.nodes

for _, label in ipairs(labels) do
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/assignees.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ return function(cb)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local assignees = resp.data.repository[key].assignees.nodes

for _, user in ipairs(assignees) do
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/changed_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return function(opts)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local results = vim.fn.json_decode(output)
local results = vim.json.decode(output)

for _, result in ipairs(results) do
local entry = entry_maker.gen_from_git_changed_files(result)
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/commits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return function(opts)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local results = vim.fn.json_decode(output)
local results = vim.json.decode(output)

for _, result in ipairs(results) do
local entry = entry_maker.gen_from_git_commits(result)
Expand Down
4 changes: 4 additions & 0 deletions lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local octo_config = require "octo.config"
local utils = require "octo.utils"
local M = {}

---@param formatted_items table<string, table> entry.ordinal -> entry
---@return table<string, function>
M.common_buffer_actions = function(formatted_items)
return {
["default"] = function(selected)
Expand All @@ -20,6 +22,8 @@ M.common_buffer_actions = function(formatted_items)
}
end

---@param formatted_items table<string, table> entry.ordinal -> entry
---@return table<string, function>
M.common_open_actions = function(formatted_items)
local cfg = octo_config.values
return vim.tbl_extend("force", M.common_buffer_actions(formatted_items), {
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ return function(opts)

local query = graphql("issues_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })

local formatted_issues = {}
local formatted_issues = {} ---@type table<string, table> entry.ordinal -> entry

local get_contents = function(fzf_cb)
gh.run {
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/labels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ return function(cb)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)
local labels = resp.data.repository.labels.nodes

for _, label in ipairs(labels) do
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/project_columns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ return function(cb)
args = { "api", "graphql", "--paginate", "-f", string.format("query=%s", query) },
cb = function(output)
if output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)

local projects = {}
local user_projects = resp.data.user and resp.data.user.projects.nodes or {}
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/pickers/fzf-lua/pickers/project_columns_v2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ return function(cb)
return
end

local formatted_projects = {}
local formatted_projects = {} ---@type table<string, table> entry.ordinal -> entry
local common_fzf_opts = vim.tbl_deep_extend("force", picker_utils.dropdown_opts, {
fzf_opts = {
["--delimiter"] = "' '",
Expand All @@ -27,7 +27,7 @@ return function(cb)
args = { "api", "graphql", "--paginate", "-f", string.format("query=%s", query) },
cb = function(output)
if output then
local resp = vim.fn.json_decode(output)
local resp = vim.json.decode(output)

local unsorted_projects = {}
local user_projects = resp.data.user and resp.data.user.projects.nodes or {}
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 @@ -43,7 +43,7 @@ return function(opts)
local query =
graphql("pull_requests_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })

local formatted_pulls = {}
local formatted_pulls = {} ---@type table<string, table> entry.ordinal -> entry

local get_contents = function(fzf_cb)
gh.run {
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/repos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ return function(opts)
end
end

local formatted_repos = {}
local formatted_repos = {} ---@type table<string, table> entry.ordinal -> entry

local get_contents = function(fzf_cb)
local query = graphql("repos_query", opts.login)
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/review_commits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ return function(thread_cb)
utils.error(err)
fzf_cb()
elseif output then
local results = vim.fn.json_decode(output)
local results = vim.json.decode(output)

if #formatted_commits == 0 then
local full_pr = entry_maker.gen_from_git_commits(make_full_pr(current_review))
Expand Down
Loading

0 comments on commit 08359aa

Please sign in to comment.