Add label management and import collision detection

- Implement 'bd label' command with add/remove/list subcommands
- Add import --dry-run and --resolve-collisions for safe merges
- Support label filtering in 'bd list' and 'bd create'
- Update AGENTS.md with collision handling workflow
- Extends bd-224 (data consistency) and bd-84 (import reliability)
This commit is contained in:
Steve Yegge
2025-10-15 17:19:16 -07:00
parent 6bfaad8731
commit 3c8e4d78e5
7 changed files with 341 additions and 1 deletions

View File

@@ -92,6 +92,16 @@ Output to stdout by default, or use -o flag for file output.`,
issue.Dependencies = allDeps[issue.ID]
}
// Populate labels for all issues
for _, issue := range issues {
labels, err := store.GetLabels(ctx, issue.ID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting labels for %s: %v\n", issue.ID, err)
os.Exit(1)
}
issue.Labels = labels
}
// Open output
out := os.Stdout
var tempFile *os.File

View File

@@ -316,6 +316,54 @@ Behavior:
}
}
// Phase 7: Process labels
// Sync labels for all imported issues
var labelsAdded, labelsRemoved int
for _, issue := range allIssues {
if issue.Labels == nil {
continue
}
// Get current labels for the issue
currentLabels, err := store.GetLabels(ctx, issue.ID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting labels for %s: %v\n", issue.ID, err)
os.Exit(1)
}
// Convert slices to maps for easier comparison
currentLabelMap := make(map[string]bool)
for _, label := range currentLabels {
currentLabelMap[label] = true
}
importedLabelMap := make(map[string]bool)
for _, label := range issue.Labels {
importedLabelMap[label] = true
}
// Add missing labels
for _, label := range issue.Labels {
if !currentLabelMap[label] {
if err := store.AddLabel(ctx, issue.ID, label, "import"); err != nil {
fmt.Fprintf(os.Stderr, "Error adding label %s to %s: %v\n", label, issue.ID, err)
os.Exit(1)
}
labelsAdded++
}
}
// Remove labels not in imported data
for _, label := range currentLabels {
if !importedLabelMap[label] {
if err := store.RemoveLabel(ctx, issue.ID, label, "import"); err != nil {
fmt.Fprintf(os.Stderr, "Error removing label %s from %s: %v\n", label, issue.ID, err)
os.Exit(1)
}
labelsRemoved++
}
}
}
// Schedule auto-flush after import completes
markDirtyAndScheduleFlush()
@@ -333,6 +381,16 @@ Behavior:
if len(idMapping) > 0 {
fmt.Fprintf(os.Stderr, ", %d issues remapped", len(idMapping))
}
if labelsAdded > 0 || labelsRemoved > 0 {
fmt.Fprintf(os.Stderr, ", %d labels synced", labelsAdded+labelsRemoved)
if labelsAdded > 0 && labelsRemoved > 0 {
fmt.Fprintf(os.Stderr, " (%d added, %d removed)", labelsAdded, labelsRemoved)
} else if labelsAdded > 0 {
fmt.Fprintf(os.Stderr, " (%d added)", labelsAdded)
} else {
fmt.Fprintf(os.Stderr, " (%d removed)", labelsRemoved)
}
}
fmt.Fprintf(os.Stderr, "\n")
},
}

204
cmd/bd/label.go Normal file
View File

@@ -0,0 +1,204 @@
// Package main implements the bd CLI label management commands.
package main
import (
"context"
"fmt"
"os"
"sort"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/types"
)
var labelCmd = &cobra.Command{
Use: "label",
Short: "Manage issue labels",
}
var labelAddCmd = &cobra.Command{
Use: "add [issue-id] [label]",
Short: "Add a label to an issue",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
issueID := args[0]
label := args[1]
ctx := context.Background()
if err := store.AddLabel(ctx, issueID, label, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Schedule auto-flush
markDirtyAndScheduleFlush()
if jsonOutput {
outputJSON(map[string]interface{}{
"status": "added",
"issue_id": issueID,
"label": label,
})
return
}
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("%s Added label '%s' to %s\n", green("✓"), label, issueID)
},
}
var labelRemoveCmd = &cobra.Command{
Use: "remove [issue-id] [label]",
Short: "Remove a label from an issue",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
issueID := args[0]
label := args[1]
ctx := context.Background()
if err := store.RemoveLabel(ctx, issueID, label, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Schedule auto-flush
markDirtyAndScheduleFlush()
if jsonOutput {
outputJSON(map[string]interface{}{
"status": "removed",
"issue_id": issueID,
"label": label,
})
return
}
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("%s Removed label '%s' from %s\n", green("✓"), label, issueID)
},
}
var labelListCmd = &cobra.Command{
Use: "list [issue-id]",
Short: "List labels for an issue",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
issueID := args[0]
ctx := context.Background()
labels, err := store.GetLabels(ctx, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if jsonOutput {
// Always output array, even if empty
if labels == nil {
labels = []string{}
}
outputJSON(labels)
return
}
if len(labels) == 0 {
fmt.Printf("\n%s has no labels\n", issueID)
return
}
cyan := color.New(color.FgCyan).SprintFunc()
fmt.Printf("\n%s Labels for %s:\n", cyan("🏷"), issueID)
for _, label := range labels {
fmt.Printf(" - %s\n", label)
}
fmt.Println()
},
}
var labelListAllCmd = &cobra.Command{
Use: "list-all",
Short: "List all unique labels in the database",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
// Get all issues to collect labels
issues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Collect unique labels with counts
labelCounts := make(map[string]int)
for _, issue := range issues {
labels, err := store.GetLabels(ctx, issue.ID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting labels for %s: %v\n", issue.ID, err)
os.Exit(1)
}
for _, label := range labels {
labelCounts[label]++
}
}
if len(labelCounts) == 0 {
if jsonOutput {
outputJSON([]string{})
} else {
fmt.Println("\nNo labels found in database")
}
return
}
// Sort labels alphabetically
labels := make([]string, 0, len(labelCounts))
for label := range labelCounts {
labels = append(labels, label)
}
sort.Strings(labels)
if jsonOutput {
// Output as array of {label, count} objects
type labelInfo struct {
Label string `json:"label"`
Count int `json:"count"`
}
result := make([]labelInfo, 0, len(labels))
for _, label := range labels {
result = append(result, labelInfo{
Label: label,
Count: labelCounts[label],
})
}
outputJSON(result)
return
}
cyan := color.New(color.FgCyan).SprintFunc()
fmt.Printf("\n%s All labels (%d unique):\n", cyan("🏷"), len(labels))
// Find longest label for alignment
maxLen := 0
for _, label := range labels {
if len(label) > maxLen {
maxLen = len(label)
}
}
for _, label := range labels {
padding := strings.Repeat(" ", maxLen-len(label))
fmt.Printf(" %s%s (%d issues)\n", label, padding, labelCounts[label])
}
fmt.Println()
},
}
func init() {
labelCmd.AddCommand(labelAddCmd)
labelCmd.AddCommand(labelRemoveCmd)
labelCmd.AddCommand(labelListCmd)
labelCmd.AddCommand(labelListAllCmd)
rootCmd.AddCommand(labelCmd)
}

View File

@@ -393,6 +393,48 @@ func autoImportIfNewer() {
}
}
// Import labels (skip colliding issues to maintain consistency)
for _, issue := range allIssues {
// Skip if this issue was filtered out due to collision
if collidingIDs[issue.ID] {
continue
}
if issue.Labels == nil {
continue
}
// Get existing labels
existingLabels, err := store.GetLabels(ctx, issue.ID)
if err != nil {
continue
}
// Convert to maps for comparison
existingLabelMap := make(map[string]bool)
for _, label := range existingLabels {
existingLabelMap[label] = true
}
importedLabelMap := make(map[string]bool)
for _, label := range issue.Labels {
importedLabelMap[label] = true
}
// Add missing labels
for _, label := range issue.Labels {
if !existingLabelMap[label] {
_ = store.AddLabel(ctx, issue.ID, label, "auto-import")
}
}
// Remove labels not in imported data
for _, label := range existingLabels {
if !importedLabelMap[label] {
_ = store.RemoveLabel(ctx, issue.ID, label, "auto-import")
}
}
}
// Store new hash after successful import
_ = store.SetMetadata(ctx, "last_import_hash", currentHash)
}
@@ -599,6 +641,14 @@ func flushToJSONL() {
}
issue.Dependencies = deps
// Get labels for this issue
labels, err := store.GetLabels(ctx, issueID)
if err != nil {
recordFailure(fmt.Errorf("failed to get labels for %s: %w", issueID, err))
return
}
issue.Labels = labels
// Update map
issueMap[issueID] = issue
}
@@ -1039,6 +1089,7 @@ var listCmd = &cobra.Command{
assignee, _ := cmd.Flags().GetString("assignee")
issueType, _ := cmd.Flags().GetString("type")
limit, _ := cmd.Flags().GetInt("limit")
labels, _ := cmd.Flags().GetStringSlice("label")
filter := types.IssueFilter{
Limit: limit,
@@ -1059,6 +1110,9 @@ var listCmd = &cobra.Command{
t := types.IssueType(issueType)
filter.IssueType = &t
}
if len(labels) > 0 {
filter.Labels = labels
}
ctx := context.Background()
issues, err := store.SearchIssues(ctx, "", filter)
@@ -1089,6 +1143,7 @@ func init() {
listCmd.Flags().IntP("priority", "p", 0, "Filter by priority")
listCmd.Flags().StringP("assignee", "a", "", "Filter by assignee")
listCmd.Flags().StringP("type", "t", "", "Filter by type")
listCmd.Flags().StringSliceP("label", "l", []string{}, "Filter by labels (comma-separated)")
listCmd.Flags().IntP("limit", "n", 0, "Limit results")
rootCmd.AddCommand(listCmd)
}