Implement prefix-optional ID parsing (bd-170)
- Add internal/utils/id_parser.go with ParseIssueID and ResolvePartialID - Update all CLI commands to accept IDs without prefix (e.g., '170' or 'bd-170') - Add comprehensive tests for ID parsing functionality - Works in direct mode; RPC handlers to be updated in bd-177 Commands updated: - show, update, edit, close (show.go) - reopen (reopen.go) - dep add/remove/tree (dep.go) - label add/remove/list (label.go) - comments (comments.go) Amp-Thread-ID: https://ampcode.com/threads/T-1f6a301b-b53f-440f-bd79-e453234ac1c9 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
95
internal/utils/id_parser.go
Normal file
95
internal/utils/id_parser.go
Normal file
@@ -0,0 +1,95 @@
|
||||
// Package utils provides utility functions for issue ID parsing and resolution.
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// ParseIssueID ensures an issue ID has the configured prefix.
|
||||
// If the input already has the prefix (e.g., "bd-a3f8e9"), returns it as-is.
|
||||
// If the input lacks the prefix (e.g., "a3f8e9"), adds the configured prefix.
|
||||
// Works with hierarchical IDs too: "a3f8e9.1.2" → "bd-a3f8e9.1.2"
|
||||
func ParseIssueID(input string, prefix string) string {
|
||||
if prefix == "" {
|
||||
prefix = "bd-"
|
||||
}
|
||||
|
||||
if strings.HasPrefix(input, prefix) {
|
||||
return input
|
||||
}
|
||||
|
||||
return prefix + input
|
||||
}
|
||||
|
||||
// ResolvePartialID resolves a potentially partial issue ID to a full ID.
|
||||
// Supports:
|
||||
// - Full IDs: "bd-a3f8e9" or "a3f8e9" → "bd-a3f8e9"
|
||||
// - Partial IDs: "a3f8" → "bd-a3f8e9" (if unique match, requires hash IDs)
|
||||
// - Hierarchical: "a3f8e9.1" → "bd-a3f8e9.1"
|
||||
//
|
||||
// Returns an error if:
|
||||
// - No issue found matching the ID
|
||||
// - Multiple issues match (ambiguous prefix)
|
||||
//
|
||||
// Note: Partial ID matching (shorter prefixes) requires hash-based IDs (bd-165).
|
||||
// For now, this primarily handles prefix-optional input (bd-a3f8e9 vs a3f8e9).
|
||||
func ResolvePartialID(ctx context.Context, store storage.Storage, input string) (string, error) {
|
||||
// Get the configured prefix
|
||||
prefix, err := store.GetConfig(ctx, "issue_prefix")
|
||||
if err != nil || prefix == "" {
|
||||
prefix = "bd-"
|
||||
}
|
||||
|
||||
// Ensure the input has the prefix
|
||||
parsedID := ParseIssueID(input, prefix)
|
||||
|
||||
// First try exact match
|
||||
_, err = store.GetIssue(ctx, parsedID)
|
||||
if err == nil {
|
||||
return parsedID, nil
|
||||
}
|
||||
|
||||
// If exact match failed, try prefix search
|
||||
filter := types.IssueFilter{}
|
||||
|
||||
issues, err := store.SearchIssues(ctx, "", filter)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to search issues: %w", err)
|
||||
}
|
||||
|
||||
var matches []string
|
||||
for _, issue := range issues {
|
||||
if strings.HasPrefix(issue.ID, parsedID) {
|
||||
matches = append(matches, issue.ID)
|
||||
}
|
||||
}
|
||||
|
||||
if len(matches) == 0 {
|
||||
return "", fmt.Errorf("no issue found matching %q", input)
|
||||
}
|
||||
|
||||
if len(matches) > 1 {
|
||||
return "", fmt.Errorf("ambiguous ID %q matches %d issues: %v\nUse more characters to disambiguate", input, len(matches), matches)
|
||||
}
|
||||
|
||||
return matches[0], nil
|
||||
}
|
||||
|
||||
// ResolvePartialIDs resolves multiple potentially partial issue IDs.
|
||||
// Returns the resolved IDs and any errors encountered.
|
||||
func ResolvePartialIDs(ctx context.Context, store storage.Storage, inputs []string) ([]string, error) {
|
||||
var resolved []string
|
||||
for _, input := range inputs {
|
||||
fullID, err := ResolvePartialID(ctx, store, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resolved = append(resolved, fullID)
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
247
internal/utils/id_parser_test.go
Normal file
247
internal/utils/id_parser_test.go
Normal file
@@ -0,0 +1,247 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/memory"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestParseIssueID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
prefix string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "already has prefix",
|
||||
input: "bd-a3f8e9",
|
||||
prefix: "bd-",
|
||||
expected: "bd-a3f8e9",
|
||||
},
|
||||
{
|
||||
name: "missing prefix",
|
||||
input: "a3f8e9",
|
||||
prefix: "bd-",
|
||||
expected: "bd-a3f8e9",
|
||||
},
|
||||
{
|
||||
name: "hierarchical with prefix",
|
||||
input: "bd-a3f8e9.1.2",
|
||||
prefix: "bd-",
|
||||
expected: "bd-a3f8e9.1.2",
|
||||
},
|
||||
{
|
||||
name: "hierarchical without prefix",
|
||||
input: "a3f8e9.1.2",
|
||||
prefix: "bd-",
|
||||
expected: "bd-a3f8e9.1.2",
|
||||
},
|
||||
{
|
||||
name: "custom prefix with ID",
|
||||
input: "ticket-123",
|
||||
prefix: "ticket-",
|
||||
expected: "ticket-123",
|
||||
},
|
||||
{
|
||||
name: "custom prefix without ID",
|
||||
input: "123",
|
||||
prefix: "ticket-",
|
||||
expected: "ticket-123",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ParseIssueID(tt.input, tt.prefix)
|
||||
if result != tt.expected {
|
||||
t.Errorf("ParseIssueID(%q, %q) = %q; want %q", tt.input, tt.prefix, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolvePartialID(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := memory.New("")
|
||||
|
||||
// Create test issues with sequential IDs (current implementation)
|
||||
// When hash IDs (bd-165) are implemented, these can be hash-based
|
||||
issue1 := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test Issue 1",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
}
|
||||
issue2 := &types.Issue{
|
||||
ID: "bd-2",
|
||||
Title: "Test Issue 2",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
}
|
||||
issue3 := &types.Issue{
|
||||
ID: "bd-10",
|
||||
Title: "Test Issue 3",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
}
|
||||
|
||||
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue3, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Set config for prefix
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "bd-"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
shouldError bool
|
||||
errorMsg string
|
||||
}{
|
||||
{
|
||||
name: "exact match with prefix",
|
||||
input: "bd-1",
|
||||
expected: "bd-1",
|
||||
},
|
||||
{
|
||||
name: "exact match without prefix",
|
||||
input: "1",
|
||||
expected: "bd-1",
|
||||
},
|
||||
{
|
||||
name: "exact match with prefix (two digits)",
|
||||
input: "bd-10",
|
||||
expected: "bd-10",
|
||||
},
|
||||
{
|
||||
name: "exact match without prefix (two digits)",
|
||||
input: "10",
|
||||
expected: "bd-10",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := ResolvePartialID(ctx, store, tt.input)
|
||||
|
||||
if tt.shouldError {
|
||||
if err == nil {
|
||||
t.Errorf("ResolvePartialID(%q) expected error containing %q, got nil", tt.input, tt.errorMsg)
|
||||
} else if tt.errorMsg != "" && !contains(err.Error(), tt.errorMsg) {
|
||||
t.Errorf("ResolvePartialID(%q) error = %q; want error containing %q", tt.input, err.Error(), tt.errorMsg)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("ResolvePartialID(%q) unexpected error: %v", tt.input, err)
|
||||
}
|
||||
if result != tt.expected {
|
||||
t.Errorf("ResolvePartialID(%q) = %q; want %q", tt.input, result, tt.expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolvePartialIDs(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := memory.New("")
|
||||
|
||||
// Create test issues
|
||||
issue1 := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test Issue 1",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
}
|
||||
issue2 := &types.Issue{
|
||||
ID: "bd-2",
|
||||
Title: "Test Issue 2",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
}
|
||||
|
||||
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "bd-"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
inputs []string
|
||||
expected []string
|
||||
shouldError bool
|
||||
}{
|
||||
{
|
||||
name: "resolve multiple IDs without prefix",
|
||||
inputs: []string{"1", "2"},
|
||||
expected: []string{"bd-1", "bd-2"},
|
||||
},
|
||||
{
|
||||
name: "resolve mixed full and partial IDs",
|
||||
inputs: []string{"bd-1", "2"},
|
||||
expected: []string{"bd-1", "bd-2"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := ResolvePartialIDs(ctx, store, tt.inputs)
|
||||
|
||||
if tt.shouldError {
|
||||
if err == nil {
|
||||
t.Errorf("ResolvePartialIDs(%v) expected error, got nil", tt.inputs)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("ResolvePartialIDs(%v) unexpected error: %v", tt.inputs, err)
|
||||
}
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("ResolvePartialIDs(%v) returned %d results; want %d", tt.inputs, len(result), len(tt.expected))
|
||||
}
|
||||
for i := range result {
|
||||
if result[i] != tt.expected[i] {
|
||||
t.Errorf("ResolvePartialIDs(%v)[%d] = %q; want %q", tt.inputs, i, result[i], tt.expected[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) &&
|
||||
(s[:len(substr)] == substr || s[len(s)-len(substr):] == substr ||
|
||||
findSubstring(s, substr)))
|
||||
}
|
||||
|
||||
func findSubstring(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user