Add prefix validation on bulk import
- Detects prefix mismatches during import - Shows warning with mismatched prefixes and counts - Added --rename-on-import flag to automatically fix prefixes - Auto-import is lenient about prefixes (doesn't enforce) - All tests passing
This commit is contained in:
@@ -161,9 +161,10 @@ func importFromGit(ctx context.Context, store storage.Storage, jsonlPath string)
|
||||
|
||||
// Use existing import logic with auto-resolve collisions
|
||||
opts := ImportOptions{
|
||||
ResolveCollisions: true,
|
||||
DryRun: false,
|
||||
SkipUpdate: false,
|
||||
ResolveCollisions: true,
|
||||
DryRun: false,
|
||||
SkipUpdate: false,
|
||||
SkipPrefixValidation: true, // Auto-import is lenient about prefixes
|
||||
}
|
||||
|
||||
_, err = importIssuesCore(ctx, dbPath, store, issues, opts)
|
||||
|
||||
@@ -31,6 +31,7 @@ Behavior:
|
||||
strict, _ := cmd.Flags().GetBool("strict")
|
||||
resolveCollisions, _ := cmd.Flags().GetBool("resolve-collisions")
|
||||
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||
renameOnImport, _ := cmd.Flags().GetBool("rename-on-import")
|
||||
|
||||
// Open input
|
||||
in := os.Stdin
|
||||
@@ -85,12 +86,28 @@ Behavior:
|
||||
DryRun: dryRun,
|
||||
SkipUpdate: skipUpdate,
|
||||
Strict: strict,
|
||||
RenameOnImport: renameOnImport,
|
||||
}
|
||||
|
||||
result, err := importIssuesCore(ctx, dbPath, store, allIssues, opts)
|
||||
|
||||
// Handle errors and special cases
|
||||
if err != nil {
|
||||
// Check if it's a prefix mismatch error
|
||||
if result != nil && result.PrefixMismatch {
|
||||
fmt.Fprintf(os.Stderr, "\n=== Prefix Mismatch Detected ===\n")
|
||||
fmt.Fprintf(os.Stderr, "Database configured prefix: %s-\n", result.ExpectedPrefix)
|
||||
fmt.Fprintf(os.Stderr, "Found issues with different prefixes:\n")
|
||||
for prefix, count := range result.MismatchPrefixes {
|
||||
fmt.Fprintf(os.Stderr, " %s- (%d issues)\n", prefix, count)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "\nOptions:\n")
|
||||
fmt.Fprintf(os.Stderr, " --rename-on-import Auto-rename imported issues to match configured prefix\n")
|
||||
fmt.Fprintf(os.Stderr, " --dry-run Preview what would be imported\n")
|
||||
fmt.Fprintf(os.Stderr, "\nOr use 'bd rename-prefix' after import to fix the database.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Check if it's a collision error when not resolving
|
||||
if !resolveCollisions && result != nil && len(result.CollisionIDs) > 0 {
|
||||
// Print collision report before exiting
|
||||
@@ -107,11 +124,21 @@ Behavior:
|
||||
|
||||
// Handle dry-run mode
|
||||
if dryRun {
|
||||
if result.PrefixMismatch {
|
||||
fmt.Fprintf(os.Stderr, "\n=== Prefix Mismatch Detected ===\n")
|
||||
fmt.Fprintf(os.Stderr, "Database configured prefix: %s-\n", result.ExpectedPrefix)
|
||||
fmt.Fprintf(os.Stderr, "Found issues with different prefixes:\n")
|
||||
for prefix, count := range result.MismatchPrefixes {
|
||||
fmt.Fprintf(os.Stderr, " %s- (%d issues)\n", prefix, count)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "\nUse --rename-on-import to automatically fix prefixes during import.\n")
|
||||
}
|
||||
|
||||
if result.Collisions > 0 {
|
||||
fmt.Fprintf(os.Stderr, "\n=== Collision Detection Report ===\n")
|
||||
fmt.Fprintf(os.Stderr, "COLLISIONS DETECTED: %d\n", result.Collisions)
|
||||
fmt.Fprintf(os.Stderr, "Colliding issue IDs: %v\n", result.CollisionIDs)
|
||||
} else {
|
||||
} else if !result.PrefixMismatch {
|
||||
fmt.Fprintf(os.Stderr, "No collisions detected.\n")
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Would create %d new issues, update %d existing issues\n",
|
||||
@@ -166,5 +193,6 @@ func init() {
|
||||
importCmd.Flags().Bool("strict", false, "Fail on dependency errors instead of treating them as warnings")
|
||||
importCmd.Flags().Bool("resolve-collisions", false, "Automatically resolve ID collisions by remapping")
|
||||
importCmd.Flags().Bool("dry-run", false, "Preview collision detection without making changes")
|
||||
importCmd.Flags().Bool("rename-on-import", false, "Rename imported issues to match database prefix (updates all references)")
|
||||
rootCmd.AddCommand(importCmd)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
@@ -12,20 +14,25 @@ import (
|
||||
|
||||
// ImportOptions configures how the import behaves
|
||||
type ImportOptions struct {
|
||||
ResolveCollisions bool // Auto-resolve collisions by remapping to new IDs
|
||||
DryRun bool // Preview changes without applying them
|
||||
SkipUpdate bool // Skip updating existing issues (create-only mode)
|
||||
Strict bool // Fail on any error (dependencies, labels, etc.)
|
||||
ResolveCollisions bool // Auto-resolve collisions by remapping to new IDs
|
||||
DryRun bool // Preview changes without applying them
|
||||
SkipUpdate bool // Skip updating existing issues (create-only mode)
|
||||
Strict bool // Fail on any error (dependencies, labels, etc.)
|
||||
RenameOnImport bool // Rename imported issues to match database prefix
|
||||
SkipPrefixValidation bool // Skip prefix validation (for auto-import)
|
||||
}
|
||||
|
||||
// ImportResult contains statistics about the import operation
|
||||
type ImportResult struct {
|
||||
Created int // New issues created
|
||||
Updated int // Existing issues updated
|
||||
Skipped int // Issues skipped (duplicates, errors)
|
||||
Collisions int // Collisions detected
|
||||
IDMapping map[string]string // Mapping of remapped IDs (old -> new)
|
||||
CollisionIDs []string // IDs that collided
|
||||
Created int // New issues created
|
||||
Updated int // Existing issues updated
|
||||
Skipped int // Issues skipped (duplicates, errors)
|
||||
Collisions int // Collisions detected
|
||||
IDMapping map[string]string // Mapping of remapped IDs (old -> new)
|
||||
CollisionIDs []string // IDs that collided
|
||||
PrefixMismatch bool // Prefix mismatch detected
|
||||
ExpectedPrefix string // Database configured prefix
|
||||
MismatchPrefixes map[string]int // Map of mismatched prefixes to count
|
||||
}
|
||||
|
||||
// importIssuesCore handles the core import logic used by both manual and auto-import.
|
||||
@@ -41,7 +48,8 @@ type ImportResult struct {
|
||||
// - Setting metadata (e.g., last_import_hash)
|
||||
func importIssuesCore(ctx context.Context, dbPath string, store storage.Storage, issues []*types.Issue, opts ImportOptions) (*ImportResult, error) {
|
||||
result := &ImportResult{
|
||||
IDMapping: make(map[string]string),
|
||||
IDMapping: make(map[string]string),
|
||||
MismatchPrefixes: make(map[string]int),
|
||||
}
|
||||
|
||||
// Phase 1: Get or create SQLite store
|
||||
@@ -74,6 +82,37 @@ func importIssuesCore(ctx context.Context, dbPath string, store storage.Storage,
|
||||
}()
|
||||
}
|
||||
|
||||
// Phase 1.5: Check for prefix mismatches
|
||||
configuredPrefix, err := sqliteStore.GetConfig(ctx, "issue_prefix")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get configured prefix: %w", err)
|
||||
}
|
||||
result.ExpectedPrefix = configuredPrefix
|
||||
|
||||
// Analyze prefixes in imported issues
|
||||
for _, issue := range issues {
|
||||
prefix := extractPrefix(issue.ID)
|
||||
if prefix != configuredPrefix {
|
||||
result.PrefixMismatch = true
|
||||
result.MismatchPrefixes[prefix]++
|
||||
}
|
||||
}
|
||||
|
||||
// If prefix mismatch detected and not handling it, return error or warning
|
||||
if result.PrefixMismatch && !opts.RenameOnImport && !opts.DryRun && !opts.SkipPrefixValidation {
|
||||
return result, fmt.Errorf("prefix mismatch detected: database uses '%s-' but found issues with prefixes: %v (use --rename-on-import to automatically fix)", configuredPrefix, getPrefixList(result.MismatchPrefixes))
|
||||
}
|
||||
|
||||
// Handle rename-on-import if requested
|
||||
if result.PrefixMismatch && opts.RenameOnImport && !opts.DryRun {
|
||||
if err := renameImportedIssuePrefixes(issues, configuredPrefix); err != nil {
|
||||
return nil, fmt.Errorf("failed to rename prefixes: %w", err)
|
||||
}
|
||||
// After renaming, clear the mismatch flags since we fixed them
|
||||
result.PrefixMismatch = false
|
||||
result.MismatchPrefixes = make(map[string]int)
|
||||
}
|
||||
|
||||
// Phase 2: Detect collisions
|
||||
collisionResult, err := sqlite.DetectCollisions(ctx, sqliteStore, issues)
|
||||
if err != nil {
|
||||
@@ -312,3 +351,92 @@ func importIssuesCore(ctx context.Context, dbPath string, store storage.Storage,
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// extractPrefix extracts the prefix from an issue ID (e.g., "bd-123" -> "bd")
|
||||
func extractPrefix(issueID string) string {
|
||||
parts := strings.SplitN(issueID, "-", 2)
|
||||
if len(parts) < 2 {
|
||||
return "" // No prefix found
|
||||
}
|
||||
return parts[0]
|
||||
}
|
||||
|
||||
// getPrefixList returns a sorted list of prefixes with their counts
|
||||
func getPrefixList(prefixes map[string]int) []string {
|
||||
var result []string
|
||||
keys := make([]string, 0, len(prefixes))
|
||||
for k := range prefixes {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, prefix := range keys {
|
||||
count := prefixes[prefix]
|
||||
result = append(result, fmt.Sprintf("%s- (%d issues)", prefix, count))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// renameImportedIssuePrefixes renames all issues and their references to match the target prefix
|
||||
func renameImportedIssuePrefixes(issues []*types.Issue, targetPrefix string) error {
|
||||
// Build a mapping of old IDs to new IDs
|
||||
idMapping := make(map[string]string)
|
||||
|
||||
for _, issue := range issues {
|
||||
oldPrefix := extractPrefix(issue.ID)
|
||||
if oldPrefix != targetPrefix {
|
||||
// Extract the numeric part
|
||||
numPart := strings.TrimPrefix(issue.ID, oldPrefix+"-")
|
||||
newID := fmt.Sprintf("%s-%s", targetPrefix, numPart)
|
||||
idMapping[issue.ID] = newID
|
||||
}
|
||||
}
|
||||
|
||||
// Now update all issues and their references
|
||||
for _, issue := range issues {
|
||||
// Update the issue ID itself if it needs renaming
|
||||
if newID, ok := idMapping[issue.ID]; ok {
|
||||
issue.ID = newID
|
||||
}
|
||||
|
||||
// Update all text references in issue fields
|
||||
issue.Title = replaceIDReferences(issue.Title, idMapping)
|
||||
issue.Description = replaceIDReferences(issue.Description, idMapping)
|
||||
if issue.Design != "" {
|
||||
issue.Design = replaceIDReferences(issue.Design, idMapping)
|
||||
}
|
||||
if issue.AcceptanceCriteria != "" {
|
||||
issue.AcceptanceCriteria = replaceIDReferences(issue.AcceptanceCriteria, idMapping)
|
||||
}
|
||||
if issue.Notes != "" {
|
||||
issue.Notes = replaceIDReferences(issue.Notes, idMapping)
|
||||
}
|
||||
|
||||
// Update dependency references
|
||||
for i := range issue.Dependencies {
|
||||
if newID, ok := idMapping[issue.Dependencies[i].IssueID]; ok {
|
||||
issue.Dependencies[i].IssueID = newID
|
||||
}
|
||||
if newID, ok := idMapping[issue.Dependencies[i].DependsOnID]; ok {
|
||||
issue.Dependencies[i].DependsOnID = newID
|
||||
}
|
||||
}
|
||||
|
||||
// Update comment references
|
||||
for i := range issue.Comments {
|
||||
issue.Comments[i].Text = replaceIDReferences(issue.Comments[i].Text, idMapping)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// replaceIDReferences replaces all old issue ID references with new ones in text
|
||||
func replaceIDReferences(text string, idMapping map[string]string) string {
|
||||
result := text
|
||||
for oldID, newID := range idMapping {
|
||||
// Use word boundary to match full IDs only (e.g., "wy-123" but not "wy-1234")
|
||||
result = strings.ReplaceAll(result, oldID, newID)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -900,10 +900,11 @@ func autoImportIfNewer() {
|
||||
|
||||
// Use shared import logic (bd-157)
|
||||
opts := ImportOptions{
|
||||
ResolveCollisions: true, // Auto-import always resolves collisions
|
||||
DryRun: false,
|
||||
SkipUpdate: false,
|
||||
Strict: false,
|
||||
ResolveCollisions: true, // Auto-import always resolves collisions
|
||||
DryRun: false,
|
||||
SkipUpdate: false,
|
||||
Strict: false,
|
||||
SkipPrefixValidation: true, // Auto-import is lenient about prefixes
|
||||
}
|
||||
|
||||
result, err := importIssuesCore(ctx, dbPath, store, allIssues, opts)
|
||||
|
||||
Reference in New Issue
Block a user