Problem: Forge refs required an issue/PR number (`gh:user/repo#42`). Users wanting to link a repo without a specific issue had no option. Solution: Accept `gh:user/repo` shorthand and `https://github.com/user/repo` URLs as `type='repo'` refs with `number=nil`. These conceal and render virtual text like numbered refs but skip all API calls (no validate, no fetch, no close). `format_label` strips `#%n` for bare refs. Omnifunc offers both `owner/repo#` and `owner/repo` completions. Closes #135
This commit is contained in:
parent
e76cbfe223
commit
287a4e32e3
3 changed files with 261 additions and 88 deletions
|
|
@ -5,8 +5,8 @@ local log = require('pending.log')
|
|||
---@field forge string
|
||||
---@field owner string
|
||||
---@field repo string
|
||||
---@field type 'issue'|'pull_request'|'merge_request'
|
||||
---@field number integer
|
||||
---@field type 'issue'|'pull_request'|'merge_request'|'repo'
|
||||
---@field number? integer
|
||||
---@field url string
|
||||
|
||||
---@class pending.ForgeCache
|
||||
|
|
@ -27,7 +27,7 @@ local log = require('pending.log')
|
|||
---@field auth_status_args string[]
|
||||
---@field default_icon string
|
||||
---@field default_issue_format string
|
||||
---@field _auth 'unknown'|'ok'|'failed'
|
||||
---@field _auth? 'unknown'|'ok'|'failed'
|
||||
---@field parse_url fun(self: pending.ForgeBackend, url: string): pending.ForgeRef?
|
||||
---@field api_args fun(self: pending.ForgeBackend, ref: pending.ForgeRef): string[]
|
||||
---@field parse_state fun(self: pending.ForgeBackend, decoded: table): 'open'|'closed'|'merged'
|
||||
|
|
@ -157,18 +157,35 @@ function M._parse_shorthand(token)
|
|||
return nil
|
||||
end
|
||||
local owner, repo, number = rest:match('^([%w%.%-_]+)/([%w%.%-_]+)#(%d+)$')
|
||||
if owner then
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local url = 'https://'
|
||||
.. backend.default_host
|
||||
.. '/'
|
||||
.. owner
|
||||
.. '/'
|
||||
.. repo
|
||||
.. '/issues/'
|
||||
.. num
|
||||
return {
|
||||
forge = backend.name,
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = 'issue',
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
owner, repo = rest:match('^([%w%.%-_]+)/([%w%.%-_]+)$')
|
||||
if not owner then
|
||||
return nil
|
||||
end
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local url = 'https://' .. backend.default_host .. '/' .. owner .. '/' .. repo .. '/issues/' .. num
|
||||
return {
|
||||
forge = backend.name,
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = 'issue',
|
||||
number = num,
|
||||
url = url,
|
||||
type = 'repo',
|
||||
url = 'https://' .. backend.default_host .. '/' .. owner .. '/' .. repo,
|
||||
}
|
||||
end
|
||||
|
||||
|
|
@ -261,7 +278,7 @@ end
|
|||
---@return string[]
|
||||
function M._api_args(ref)
|
||||
local backend = _by_name[ref.forge]
|
||||
if not backend then
|
||||
if not backend or not ref.number then
|
||||
return {}
|
||||
end
|
||||
return backend:api_args(ref)
|
||||
|
|
@ -278,12 +295,15 @@ function M.format_label(ref, cache)
|
|||
local default_icon = backend and backend.default_icon or ''
|
||||
local default_fmt = backend and backend.default_issue_format or '%i %o/%r#%n'
|
||||
local fmt = forge_cfg.issue_format or default_fmt
|
||||
if ref.type == 'repo' then
|
||||
fmt = fmt:gsub('#?%%n', ''):gsub('%s+$', '')
|
||||
end
|
||||
local icon = forge_cfg.icon or default_icon
|
||||
local text = fmt
|
||||
:gsub('%%i', icon)
|
||||
:gsub('%%o', ref.owner)
|
||||
:gsub('%%r', ref.repo)
|
||||
:gsub('%%n', tostring(ref.number))
|
||||
:gsub('%%n', ref.number and tostring(ref.number) or '')
|
||||
local hl = 'PendingForge'
|
||||
if cache then
|
||||
if cache.state == 'closed' or cache.state == 'merged' then
|
||||
|
|
@ -296,6 +316,10 @@ end
|
|||
---@param ref pending.ForgeRef
|
||||
---@param callback fun(cache: pending.ForgeCache?, err: pending.ForgeFetchError?)
|
||||
function M.fetch_metadata(ref, callback)
|
||||
if ref.type == 'repo' then
|
||||
callback(nil)
|
||||
return
|
||||
end
|
||||
local args = M._api_args(ref)
|
||||
vim.system(args, { text = true }, function(result)
|
||||
if result.code ~= 0 or not result.stdout or result.stdout == '' then
|
||||
|
|
@ -351,7 +375,12 @@ function M.refresh(s)
|
|||
local tasks = s:tasks()
|
||||
local by_forge = {} ---@type table<string, pending.Task[]>
|
||||
for _, task in ipairs(tasks) do
|
||||
if task.status ~= 'deleted' and task._extra and task._extra._forge_ref then
|
||||
if
|
||||
task.status ~= 'deleted'
|
||||
and task._extra
|
||||
and task._extra._forge_ref
|
||||
and task._extra._forge_ref.type ~= 'repo'
|
||||
then
|
||||
local fname = task._extra._forge_ref.forge
|
||||
if not by_forge[fname] then
|
||||
by_forge[fname] = {}
|
||||
|
|
@ -419,11 +448,15 @@ end
|
|||
function M.validate_refs(refs)
|
||||
local by_forge = {} ---@type table<string, pending.ForgeRef[]>
|
||||
for _, ref in ipairs(refs) do
|
||||
if ref.type == 'repo' then
|
||||
goto skip_ref
|
||||
end
|
||||
local fname = ref.forge
|
||||
if not by_forge[fname] then
|
||||
by_forge[fname] = {}
|
||||
end
|
||||
table.insert(by_forge[fname], ref)
|
||||
::skip_ref::
|
||||
end
|
||||
for fname, forge_refs in pairs(by_forge) do
|
||||
if not M.is_configured(fname) or not _by_name[fname] then
|
||||
|
|
@ -461,25 +494,29 @@ function M.gitea_forge(opts)
|
|||
_ensure_instances()
|
||||
local host, owner, repo, kind, number =
|
||||
url:match('^https?://([^/]+)/([%w%.%-_]+)/([%w%.%-_]+)/(%a+)/(%d+)$')
|
||||
if not host then
|
||||
return nil
|
||||
if host and (kind == 'issues' or kind == 'pulls') and _by_host[host] == self then
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'pulls' and 'pull_request' or 'issue'
|
||||
return {
|
||||
forge = self.name,
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
if kind ~= 'issues' and kind ~= 'pulls' then
|
||||
return nil
|
||||
host, owner, repo = url:match('^https?://([^/]+)/([%w%.%-_]+)/([%w%.%-_]+)/?$')
|
||||
if host and _by_host[host] == self then
|
||||
return {
|
||||
forge = self.name,
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = 'repo',
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
if _by_host[host] ~= self then
|
||||
return nil
|
||||
end
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'pulls' and 'pull_request' or 'issue'
|
||||
return {
|
||||
forge = self.name,
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
return nil
|
||||
end,
|
||||
api_args = function(self, ref)
|
||||
local endpoint = ref.type == 'pull_request' and 'pulls' or 'issues'
|
||||
|
|
@ -511,25 +548,29 @@ M.register({
|
|||
_ensure_instances()
|
||||
local host, owner, repo, kind, number =
|
||||
url:match('^https?://([^/]+)/([%w%.%-_]+)/([%w%.%-_]+)/(%a+)/(%d+)$')
|
||||
if not host then
|
||||
return nil
|
||||
if host and (kind == 'issues' or kind == 'pull') and _by_host[host] == self then
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'pull' and 'pull_request' or 'issue'
|
||||
return {
|
||||
forge = 'github',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
if kind ~= 'issues' and kind ~= 'pull' then
|
||||
return nil
|
||||
host, owner, repo = url:match('^https?://([^/]+)/([%w%.%-_]+)/([%w%.%-_]+)/?$')
|
||||
if host and _by_host[host] == self then
|
||||
return {
|
||||
forge = 'github',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = 'repo',
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
if _by_host[host] ~= self then
|
||||
return nil
|
||||
end
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'pull' and 'pull_request' or 'issue'
|
||||
return {
|
||||
forge = 'github',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
return nil
|
||||
end,
|
||||
api_args = function(_, ref)
|
||||
return {
|
||||
|
|
@ -560,29 +601,38 @@ M.register({
|
|||
parse_url = function(self, url)
|
||||
_ensure_instances()
|
||||
local host, path, kind, number = url:match('^https?://([^/]+)/(.+)/%-/([%w_]+)/(%d+)$')
|
||||
if not host then
|
||||
return nil
|
||||
if host and (kind == 'issues' or kind == 'merge_requests') and _by_host[host] == self then
|
||||
local owner, repo = path:match('^(.+)/([^/]+)$')
|
||||
if owner then
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'merge_requests' and 'merge_request' or 'issue'
|
||||
return {
|
||||
forge = 'gitlab',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
end
|
||||
if kind ~= 'issues' and kind ~= 'merge_requests' then
|
||||
return nil
|
||||
host, path = url:match('^https?://([^/]+)/(.+)$')
|
||||
if host and _by_host[host] == self then
|
||||
local trimmed = path:gsub('/$', '')
|
||||
if not trimmed:find('/%-/') then
|
||||
local owner, repo = trimmed:match('^(.+)/([^/]+)$')
|
||||
if owner then
|
||||
return {
|
||||
forge = 'gitlab',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = 'repo',
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
if _by_host[host] ~= self then
|
||||
return nil
|
||||
end
|
||||
local owner, repo = path:match('^(.+)/([^/]+)$')
|
||||
if not owner then
|
||||
return nil
|
||||
end
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'merge_requests' and 'merge_request' or 'issue'
|
||||
return {
|
||||
forge = 'gitlab',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
return nil
|
||||
end,
|
||||
api_args = function(_, ref)
|
||||
local encoded = (ref.owner .. '/' .. ref.repo):gsub('/', '%%2F')
|
||||
|
|
@ -616,25 +666,29 @@ M.register({
|
|||
_ensure_instances()
|
||||
local host, owner, repo, kind, number =
|
||||
url:match('^https?://([^/]+)/([%w%.%-_]+)/([%w%.%-_]+)/(%a+)/(%d+)$')
|
||||
if not host then
|
||||
return nil
|
||||
if host and (kind == 'issues' or kind == 'pulls') and _by_host[host] == self then
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'pulls' and 'pull_request' or 'issue'
|
||||
return {
|
||||
forge = 'codeberg',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
if kind ~= 'issues' and kind ~= 'pulls' then
|
||||
return nil
|
||||
host, owner, repo = url:match('^https?://([^/]+)/([%w%.%-_]+)/([%w%.%-_]+)/?$')
|
||||
if host and _by_host[host] == self then
|
||||
return {
|
||||
forge = 'codeberg',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = 'repo',
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
if _by_host[host] ~= self then
|
||||
return nil
|
||||
end
|
||||
local num = tonumber(number) --[[@as integer]]
|
||||
local ref_type = kind == 'pulls' and 'pull_request' or 'issue'
|
||||
return {
|
||||
forge = 'codeberg',
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
type = ref_type,
|
||||
number = num,
|
||||
url = url,
|
||||
}
|
||||
return nil
|
||||
end,
|
||||
api_args = function(_, ref)
|
||||
local endpoint = ref.type == 'pull_request' and 'pulls' or 'issues'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue