feat: change the pull request handling
This commit is contained in:
@@ -7,9 +7,9 @@
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
--------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
-- A simple completion function for the :Gitea command
|
-- A simple completion function for the :Gitea command
|
||||||
--------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
function M._gitea_cmd_complete(arg_lead, cmd_line, cursor_pos)
|
function M._gitea_cmd_complete(arg_lead, cmd_line, cursor_pos)
|
||||||
local tokens = vim.split(cmd_line, "%s+")
|
local tokens = vim.split(cmd_line, "%s+")
|
||||||
|
|
||||||
@@ -47,7 +47,6 @@ function M._gitea_cmd_complete(arg_lead, cmd_line, cursor_pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- If "pr" was selected, next subcommands can be list, create
|
-- If "pr" was selected, next subcommands can be list, create
|
||||||
-- (removed "open" and "merge" as subcommands)
|
|
||||||
if main_sub == "pr" then
|
if main_sub == "pr" then
|
||||||
if #tokens == 3 then
|
if #tokens == 3 then
|
||||||
local candidates = { "list", "create" }
|
local candidates = { "list", "create" }
|
||||||
@@ -106,7 +105,8 @@ function M.setup_commands(core)
|
|||||||
elseif main == "pr" then
|
elseif main == "pr" then
|
||||||
local sub = args[2] or ""
|
local sub = args[2] or ""
|
||||||
if sub == "list" then
|
if sub == "list" then
|
||||||
vim.cmd([[echo "TODO: :Gitea pr list"]])
|
-- :Gitea pr list
|
||||||
|
telescope_mod.list_pulls_in_telescope()
|
||||||
elseif sub == "create" then
|
elseif sub == "create" then
|
||||||
vim.cmd([[echo "TODO: :Gitea pr create"]])
|
vim.cmd([[echo "TODO: :Gitea pr create"]])
|
||||||
else
|
else
|
||||||
@@ -126,4 +126,4 @@ function M.setup_commands(core)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
@@ -133,7 +133,7 @@ local function list_issues(owner, repo, on_done)
|
|||||||
if not owner or not repo then
|
if not owner or not repo then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local ep = string.format("/api/v1/repos/%s/%s/issues", owner, repo)
|
local ep = string.format("/api/v1/repos/%s/%s/issues?type=issues", owner, repo)
|
||||||
core.request("GET", ep, nil, function(data, err)
|
core.request("GET", ep, nil, function(data, err)
|
||||||
if err then
|
if err then
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
--
|
--
|
||||||
-- Provides functions specifically for Gitea pull requests:
|
-- Provides functions specifically for Gitea pull requests:
|
||||||
-- list, create, merge, comment, etc.
|
-- list, create, merge, comment, etc.
|
||||||
-- We add a key binding to merge the PR in the PR buffer.
|
-- Modified so the "list_pulls" call only returns open PRs.
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
@@ -12,7 +12,7 @@ local Job = require('plenary.job')
|
|||||||
local lyaml = require("lyaml")
|
local lyaml = require("lyaml")
|
||||||
|
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
-- fallback for owner/repo
|
-- fallback_owner_repo
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
local function fallback_owner_repo(owner, repo)
|
local function fallback_owner_repo(owner, repo)
|
||||||
if (not owner or owner == "") or (not repo or repo == "") then
|
if (not owner or owner == "") or (not repo or repo == "") then
|
||||||
@@ -30,12 +30,13 @@ end
|
|||||||
|
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
-- M.list_pulls
|
-- M.list_pulls
|
||||||
|
-- Use "?state=open" so it only shows open PRs
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
function M.list_pulls(owner, repo, on_done)
|
function M.list_pulls(owner, repo, on_done)
|
||||||
owner, repo = fallback_owner_repo(owner, repo)
|
owner, repo = fallback_owner_repo(owner, repo)
|
||||||
if not owner or not repo then return end
|
if not owner or not repo then return end
|
||||||
|
|
||||||
local ep = string.format("/api/v1/repos/%s/%s/pulls", owner, repo)
|
local ep = string.format("/api/v1/repos/%s/%s/pulls?state=open", owner, repo)
|
||||||
core.request("GET", ep, nil, function(data, err)
|
core.request("GET", ep, nil, function(data, err)
|
||||||
if err then
|
if err then
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
@@ -46,7 +47,7 @@ function M.list_pulls(owner, repo, on_done)
|
|||||||
if on_done then
|
if on_done then
|
||||||
on_done(data)
|
on_done(data)
|
||||||
else
|
else
|
||||||
print(string.format("Got %d PRs for %s/%s", #data, owner, repo))
|
print(string.format("Got %d open PRs for %s/%s", #data, owner, repo))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -58,7 +59,7 @@ function M.merge_pull(owner, repo, pr_number)
|
|||||||
owner, repo = fallback_owner_repo(owner, repo)
|
owner, repo = fallback_owner_repo(owner, repo)
|
||||||
if not owner or not repo or not pr_number then
|
if not owner or not repo or not pr_number then
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
vim.cmd([[echoerr "Usage: :Gitea pr merge <owner> <repo> <pr_number>"]])
|
vim.cmd([[echoerr "No valid <owner>/<repo>/<pr_number> for merge_pull()"]])
|
||||||
end)
|
end)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -107,7 +108,7 @@ function M.create_pull(owner, repo, head, base, title, body, cb)
|
|||||||
end
|
end
|
||||||
|
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
-- Buffer approach for the PR (like issues)
|
-- Buffer approach for the PR
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
local function set_pr_buffer_options(buf)
|
local function set_pr_buffer_options(buf)
|
||||||
vim.api.nvim_buf_set_option(buf, "buftype", "acwrite") -- allow saving
|
vim.api.nvim_buf_set_option(buf, "buftype", "acwrite") -- allow saving
|
||||||
@@ -125,7 +126,6 @@ local function attach_pr_autocmds(buf)
|
|||||||
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
||||||
local doc_str = table.concat(lines, "\n")
|
local doc_str = table.concat(lines, "\n")
|
||||||
|
|
||||||
-- Parse as YAML
|
|
||||||
local ok, docs = pcall(lyaml.load, doc_str)
|
local ok, docs = pcall(lyaml.load, doc_str)
|
||||||
if not ok or type(docs) ~= "table" then
|
if not ok or type(docs) ~= "table" then
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
@@ -160,9 +160,6 @@ local function attach_pr_autocmds(buf)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
----------------------------------------------------------------------------
|
|
||||||
-- Keymap callback to merge the current PR
|
|
||||||
----------------------------------------------------------------------------
|
|
||||||
function M.merge_current_pr()
|
function M.merge_current_pr()
|
||||||
local buf = vim.api.nvim_get_current_buf()
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
local owner = vim.api.nvim_buf_get_var(buf, "gitea_pr_owner")
|
local owner = vim.api.nvim_buf_get_var(buf, "gitea_pr_owner")
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
-- lua/gitea/telescope.lua
|
-- lua/gitea/telescope.lua
|
||||||
--
|
--
|
||||||
-- Minimal telescope integration for listing issues
|
-- Minimal telescope integration for listing issues (and now PRs)
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
local pickers = require("telescope.pickers")
|
local pickers = require("telescope.pickers")
|
||||||
local finders = require("telescope.finders")
|
local finders = require("telescope.finders")
|
||||||
local conf = require("telescope.config").values
|
local conf = require("telescope.config").values
|
||||||
local actions = require("telescope.actions")
|
local actions = require("telescope.actions")
|
||||||
local action_state = require("telescope.actions.state")
|
local action_state = require("telescope.actions.state")
|
||||||
|
|
||||||
local issues_mod = require("gitea.issues")
|
local issues_mod = require("gitea.issues")
|
||||||
|
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
-- Open selected issue
|
||||||
|
----------------------------------------------------------------------------
|
||||||
local function open_selected_issue(prompt_bufnr)
|
local function open_selected_issue(prompt_bufnr)
|
||||||
local selection = action_state.get_selected_entry()
|
local selection = action_state.get_selected_entry()
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
@@ -23,6 +26,9 @@ local function open_selected_issue(prompt_bufnr)
|
|||||||
issues_mod.open_issue(nil, nil, number)
|
issues_mod.open_issue(nil, nil, number)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
-- list_issues_in_telescope
|
||||||
|
----------------------------------------------------------------------------
|
||||||
function M.list_issues_in_telescope()
|
function M.list_issues_in_telescope()
|
||||||
issues_mod.list_issues(nil, nil, function(data)
|
issues_mod.list_issues(nil, nil, function(data)
|
||||||
pickers.new({}, {
|
pickers.new({}, {
|
||||||
@@ -47,4 +53,46 @@ function M.list_issues_in_telescope()
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
-- Open selected PR
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
local function open_selected_pr(prompt_bufnr)
|
||||||
|
local selection = action_state.get_selected_entry()
|
||||||
|
actions.close(prompt_bufnr)
|
||||||
|
if not selection then return end
|
||||||
|
|
||||||
|
local pr_obj = selection.value
|
||||||
|
local number = pr_obj.number
|
||||||
|
local pulls_mod = require("gitea.pulls")
|
||||||
|
pulls_mod.open_pull_in_buffer(nil, nil, number)
|
||||||
|
end
|
||||||
|
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
-- list_pulls_in_telescope
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
function M.list_pulls_in_telescope()
|
||||||
|
local pulls_mod = require("gitea.pulls")
|
||||||
|
pulls_mod.list_pulls(nil, nil, function(data)
|
||||||
|
pickers.new({}, {
|
||||||
|
prompt_title = "Gitea Pull Requests",
|
||||||
|
finder = finders.new_table {
|
||||||
|
results = data,
|
||||||
|
entry_maker = function(pr)
|
||||||
|
return {
|
||||||
|
value = pr,
|
||||||
|
display = ("#%d: %s"):format(pr.number, pr.title or ""),
|
||||||
|
ordinal = ("%d %s"):format(pr.number, (pr.title or ""))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
},
|
||||||
|
sorter = conf.generic_sorter({}),
|
||||||
|
attach_mappings = function(prompt_bufnr, map)
|
||||||
|
map("i", "<CR>", open_selected_pr)
|
||||||
|
map("n", "<CR>", open_selected_pr)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
}):find()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
Reference in New Issue
Block a user