feat(forge): support bare repo-level forge refs (#135) (#140)

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:
Barrett Ruth 2026-03-11 12:34:17 -04:00 committed by Barrett Ruth
parent 1064b7535a
commit 46b5d52b60
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
3 changed files with 261 additions and 88 deletions

View file

@ -44,12 +44,30 @@ describe('forge', function()
assert.is_nil(forge._parse_shorthand('xx:user/repo#1'))
end)
it('rejects missing number', function()
assert.is_nil(forge._parse_shorthand('gh:user/repo'))
it('parses bare gh: shorthand without number', function()
local ref = forge._parse_shorthand('gh:user/repo')
assert.is_not_nil(ref)
assert.equals('github', ref.forge)
assert.equals('user', ref.owner)
assert.equals('repo', ref.repo)
assert.equals('repo', ref.type)
assert.is_nil(ref.number)
assert.equals('https://github.com/user/repo', ref.url)
end)
it('parses bare gl: shorthand without number', function()
local ref = forge._parse_shorthand('gl:group/project')
assert.is_not_nil(ref)
assert.equals('gitlab', ref.forge)
assert.equals('group', ref.owner)
assert.equals('project', ref.repo)
assert.equals('repo', ref.type)
assert.is_nil(ref.number)
end)
it('rejects missing repo', function()
assert.is_nil(forge._parse_shorthand('gh:user#1'))
assert.is_nil(forge._parse_shorthand('gh:user'))
end)
end)
@ -73,6 +91,23 @@ describe('forge', function()
it('rejects non-github URL', function()
assert.is_nil(forge._parse_github_url('https://example.com/user/repo/issues/1'))
end)
it('parses bare repo URL', function()
local ref = forge._parse_github_url('https://github.com/user/repo')
assert.is_not_nil(ref)
assert.equals('github', ref.forge)
assert.equals('user', ref.owner)
assert.equals('repo', ref.repo)
assert.equals('repo', ref.type)
assert.is_nil(ref.number)
end)
it('parses bare repo URL with trailing slash', function()
local ref = forge._parse_github_url('https://github.com/user/repo/')
assert.is_not_nil(ref)
assert.equals('repo', ref.type)
assert.is_nil(ref.number)
end)
end)
describe('_parse_gitlab_url', function()
@ -98,6 +133,16 @@ describe('forge', function()
assert.equals('org/sub', ref.owner)
assert.equals('project', ref.repo)
end)
it('parses bare repo URL', function()
local ref = forge._parse_gitlab_url('https://gitlab.com/group/project')
assert.is_not_nil(ref)
assert.equals('gitlab', ref.forge)
assert.equals('group', ref.owner)
assert.equals('project', ref.repo)
assert.equals('repo', ref.type)
assert.is_nil(ref.number)
end)
end)
describe('_parse_codeberg_url', function()
@ -116,6 +161,16 @@ describe('forge', function()
assert.is_not_nil(ref)
assert.equals('pull_request', ref.type)
end)
it('parses bare repo URL', function()
local ref = forge._parse_codeberg_url('https://codeberg.org/user/repo')
assert.is_not_nil(ref)
assert.equals('codeberg', ref.forge)
assert.equals('user', ref.owner)
assert.equals('repo', ref.repo)
assert.equals('repo', ref.type)
assert.is_nil(ref.number)
end)
end)
describe('parse_ref', function()
@ -141,6 +196,14 @@ describe('forge', function()
assert.is_nil(forge.parse_ref('hello'))
assert.is_nil(forge.parse_ref('due:tomorrow'))
end)
it('dispatches bare shorthand', function()
local ref = forge.parse_ref('gh:user/repo')
assert.is_not_nil(ref)
assert.equals('github', ref.forge)
assert.equals('repo', ref.type)
assert.is_nil(ref.number)
end)
end)
describe('find_refs', function()
@ -184,6 +247,17 @@ describe('forge', function()
assert.equals(0, refs[1].start_byte)
assert.equals(8, refs[1].end_byte)
end)
it('finds bare shorthand ref', function()
local refs = forge.find_refs('Fix gh:user/repo')
assert.equals(1, #refs)
assert.equals('github', refs[1].ref.forge)
assert.equals('repo', refs[1].ref.type)
assert.is_nil(refs[1].ref.number)
assert.equals('gh:user/repo', refs[1].raw)
assert.equals(4, refs[1].start_byte)
assert.equals(16, refs[1].end_byte)
end)
end)
describe('_api_args', function()
@ -262,6 +336,30 @@ describe('forge', function()
assert.equals('PendingForgeClosed', hl)
end)
it('formats bare repo ref without #N', function()
local text = forge.format_label({
forge = 'github',
owner = 'user',
repo = 'repo',
type = 'repo',
url = '',
}, nil)
assert.truthy(text:find('user/repo'))
assert.is_nil(text:find('#'))
end)
it('still formats numbered ref with #N', function()
local text = forge.format_label({
forge = 'github',
owner = 'user',
repo = 'repo',
type = 'issue',
number = 42,
url = '',
}, nil)
assert.truthy(text:find('user/repo#42'))
end)
it('uses closed highlight for merged state', function()
local _, hl = forge.format_label({
forge = 'gitlab',
@ -542,4 +640,19 @@ describe('forge diff integration', function()
assert.equals(1, updated._extra._forge_ref.number)
os.remove(tmp)
end)
it('stores bare forge_ref in _extra on new task', function()
local tmp = os.tmpname()
local s = store.new(tmp)
s:load()
diff.apply({ '- [ ] Check out gh:user/repo' }, s)
local tasks = s:active_tasks()
assert.equals(1, #tasks)
assert.is_not_nil(tasks[1]._extra)
assert.is_not_nil(tasks[1]._extra._forge_ref)
assert.equals('github', tasks[1]._extra._forge_ref.forge)
assert.equals('repo', tasks[1]._extra._forge_ref.type)
assert.is_nil(tasks[1]._extra._forge_ref.number)
os.remove(tmp)
end)
end)