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:
Steve Yegge
2025-10-30 15:41:46 -07:00
parent 9876d825a6
commit 2eb4c883ab
7 changed files with 503 additions and 33 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/rpc"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
var commentsCmd = &cobra.Command{
@@ -63,6 +64,13 @@ Examples:
os.Exit(1)
}
ctx := context.Background()
fullID, err := utils.ResolvePartialID(ctx, store, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", issueID, err)
os.Exit(1)
}
issueID = fullID
result, err := store.GetIssueComments(ctx, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting comments: %v\n", err)
@@ -176,7 +184,14 @@ Examples:
os.Exit(1)
}
ctx := context.Background()
var err error
fullID, err := utils.ResolvePartialID(ctx, store, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", issueID, err)
os.Exit(1)
}
issueID = fullID
comment, err = store.AddIssueComment(ctx, issueID, author, commentText)
if err != nil {
fmt.Fprintf(os.Stderr, "Error adding comment: %v\n", err)

View File

@@ -11,6 +11,7 @@ import (
"github.com/steveyegge/beads/internal/rpc"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
var depCmd = &cobra.Command{
@@ -51,13 +52,26 @@ var depAddCmd = &cobra.Command{
}
// Direct mode
ctx := context.Background()
fullFromID, err := utils.ResolvePartialID(ctx, store, args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving issue ID %s: %v\n", args[0], err)
os.Exit(1)
}
fullToID, err := utils.ResolvePartialID(ctx, store, args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving dependency ID %s: %v\n", args[1], err)
os.Exit(1)
}
dep := &types.Dependency{
IssueID: args[0],
DependsOnID: args[1],
IssueID: fullFromID,
DependsOnID: fullToID,
Type: types.DependencyType(depType),
}
ctx := context.Background()
if err := store.AddDependency(ctx, dep, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
@@ -94,8 +108,8 @@ var depAddCmd = &cobra.Command{
if jsonOutput {
outputJSON(map[string]interface{}{
"status": "added",
"issue_id": args[0],
"depends_on_id": args[1],
"issue_id": fullFromID,
"depends_on_id": fullToID,
"type": depType,
})
return
@@ -103,7 +117,7 @@ var depAddCmd = &cobra.Command{
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("%s Added dependency: %s depends on %s (%s)\n",
green("✓"), args[0], args[1], depType)
green("✓"), fullFromID, fullToID, depType)
},
}
@@ -138,7 +152,20 @@ var depRemoveCmd = &cobra.Command{
// Direct mode
ctx := context.Background()
if err := store.RemoveDependency(ctx, args[0], args[1], actor); err != nil {
fullFromID, err := utils.ResolvePartialID(ctx, store, args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving issue ID %s: %v\n", args[0], err)
os.Exit(1)
}
fullToID, err := utils.ResolvePartialID(ctx, store, args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving dependency ID %s: %v\n", args[1], err)
os.Exit(1)
}
if err := store.RemoveDependency(ctx, fullFromID, fullToID, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
@@ -149,15 +176,15 @@ var depRemoveCmd = &cobra.Command{
if jsonOutput {
outputJSON(map[string]interface{}{
"status": "removed",
"issue_id": args[0],
"depends_on_id": args[1],
"issue_id": fullFromID,
"depends_on_id": fullToID,
})
return
}
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("%s Removed dependency: %s no longer depends on %s\n",
green("✓"), args[0], args[1])
green("✓"), fullFromID, fullToID)
},
}
@@ -187,7 +214,14 @@ var depTreeCmd = &cobra.Command{
}
ctx := context.Background()
tree, err := store.GetDependencyTree(ctx, args[0], maxDepth, showAllPaths, reverse)
fullID, err := utils.ResolvePartialID(ctx, store, args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", args[0], err)
os.Exit(1)
}
tree, err := store.GetDependencyTree(ctx, fullID, maxDepth, showAllPaths, reverse)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
@@ -204,18 +238,18 @@ var depTreeCmd = &cobra.Command{
if len(tree) == 0 {
if reverse {
fmt.Printf("\n%s has no dependents\n", args[0])
fmt.Printf("\n%s has no dependents\n", fullID)
} else {
fmt.Printf("\n%s has no dependencies\n", args[0])
fmt.Printf("\n%s has no dependencies\n", fullID)
}
return
}
cyan := color.New(color.FgCyan).SprintFunc()
if reverse {
fmt.Printf("\n%s Dependent tree for %s:\n\n", cyan("🌲"), args[0])
fmt.Printf("\n%s Dependent tree for %s:\n\n", cyan("🌲"), fullID)
} else {
fmt.Printf("\n%s Dependency tree for %s:\n\n", cyan("🌲"), args[0])
fmt.Printf("\n%s Dependency tree for %s:\n\n", cyan("🌲"), fullID)
}
hasTruncation := false

View File

@@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/rpc"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
var labelCmd = &cobra.Command{
@@ -79,6 +80,22 @@ var labelAddCmd = &cobra.Command{
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
issueIDs, label := parseLabelArgs(args)
// Resolve partial IDs if in direct mode
if daemonClient == nil {
ctx := context.Background()
resolvedIDs := make([]string, 0, len(issueIDs))
for _, id := range issueIDs {
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
continue
}
resolvedIDs = append(resolvedIDs, fullID)
}
issueIDs = resolvedIDs
}
processBatchLabelOperation(issueIDs, label, "added",
func(issueID, lbl string) error {
_, err := daemonClient.AddLabel(&rpc.LabelAddArgs{ID: issueID, Label: lbl})
@@ -97,6 +114,22 @@ var labelRemoveCmd = &cobra.Command{
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
issueIDs, label := parseLabelArgs(args)
// Resolve partial IDs if in direct mode
if daemonClient == nil {
ctx := context.Background()
resolvedIDs := make([]string, 0, len(issueIDs))
for _, id := range issueIDs {
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
continue
}
resolvedIDs = append(resolvedIDs, fullID)
}
issueIDs = resolvedIDs
}
processBatchLabelOperation(issueIDs, label, "removed",
func(issueID, lbl string) error {
_, err := daemonClient.RemoveLabel(&rpc.LabelRemoveArgs{ID: issueID, Label: lbl})
@@ -117,6 +150,16 @@ var labelListCmd = &cobra.Command{
ctx := context.Background()
var labels []string
// Resolve partial ID if in direct mode
if daemonClient == nil {
fullID, err := utils.ResolvePartialID(ctx, store, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", issueID, err)
os.Exit(1)
}
issueID = fullID
}
// Use daemon if available
if daemonClient != nil {

View File

@@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/rpc"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
var reopenCmd = &cobra.Command{
@@ -73,24 +74,30 @@ This is more explicit than 'bd update --status open' and emits a Reopened event.
}
for _, id := range args {
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
continue
}
// UpdateIssue automatically clears closed_at when status changes from closed
updates := map[string]interface{}{
"status": string(types.StatusOpen),
}
if err := store.UpdateIssue(ctx, id, updates, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error reopening %s: %v\n", id, err)
if err := store.UpdateIssue(ctx, fullID, updates, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error reopening %s: %v\n", fullID, err)
continue
}
// Add reason as a comment if provided
if reason != "" {
if err := store.AddComment(ctx, id, actor, reason); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to add comment to %s: %v\n", id, err)
if err := store.AddComment(ctx, fullID, actor, reason); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to add comment to %s: %v\n", fullID, err)
}
}
if jsonOutput {
issue, _ := store.GetIssue(ctx, id)
issue, _ := store.GetIssue(ctx, fullID)
if issue != nil {
reopenedIssues = append(reopenedIssues, issue)
}
@@ -100,7 +107,7 @@ This is more explicit than 'bd update --status open' and emits a Reopened event.
if reason != "" {
reasonMsg = ": " + reason
}
fmt.Printf("%s Reopened %s%s\n", blue("↻"), id, reasonMsg)
fmt.Printf("%s Reopened %s%s\n", blue("↻"), fullID, reasonMsg)
}
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/rpc"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
var showCmd = &cobra.Command{
@@ -160,13 +161,19 @@ var showCmd = &cobra.Command{
ctx := context.Background()
allDetails := []interface{}{}
for idx, id := range args {
issue, err := store.GetIssue(ctx, id)
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching %s: %v\n", id, err)
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
continue
}
issue, err := store.GetIssue(ctx, fullID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching %s: %v\n", fullID, err)
continue
}
if issue == nil {
fmt.Fprintf(os.Stderr, "Issue %s not found\n", id)
fmt.Fprintf(os.Stderr, "Issue %s not found\n", fullID)
continue
}
@@ -412,19 +419,25 @@ var updateCmd = &cobra.Command{
ctx := context.Background()
updatedIssues := []*types.Issue{}
for _, id := range args {
if err := store.UpdateIssue(ctx, id, updates, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error updating %s: %v\n", id, err)
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
continue
}
if err := store.UpdateIssue(ctx, fullID, updates, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error updating %s: %v\n", fullID, err)
continue
}
if jsonOutput {
issue, _ := store.GetIssue(ctx, id)
issue, _ := store.GetIssue(ctx, fullID)
if issue != nil {
updatedIssues = append(updatedIssues, issue)
}
} else {
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("%s Updated issue: %s\n", green("✓"), id)
fmt.Printf("%s Updated issue: %s\n", green("✓"), fullID)
}
}
@@ -456,6 +469,16 @@ Examples:
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
ctx := context.Background()
// Resolve partial ID if in direct mode
if daemonClient == nil {
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
os.Exit(1)
}
id = fullID
}
// Determine which field to edit
fieldToEdit := "description"
@@ -670,18 +693,24 @@ var closeCmd = &cobra.Command{
ctx := context.Background()
closedIssues := []*types.Issue{}
for _, id := range args {
if err := store.CloseIssue(ctx, id, reason, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error closing %s: %v\n", id, err)
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
continue
}
if err := store.CloseIssue(ctx, fullID, reason, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error closing %s: %v\n", fullID, err)
continue
}
if jsonOutput {
issue, _ := store.GetIssue(ctx, id)
issue, _ := store.GetIssue(ctx, fullID)
if issue != nil {
closedIssues = append(closedIssues, issue)
}
} else {
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("%s Closed %s: %s\n", green("✓"), id, reason)
fmt.Printf("%s Closed %s: %s\n", green("✓"), fullID, reason)
}
}