Add prefix validation to prevent wrong-database issue creation

Closes bd-156 (originally bd-151, remapped during auto-import)

Validates that explicit --id prefix matches database prefix from config.
Prevents accidental creation of issues with wrong prefix (e.g., creating
'bd-118' in a database configured for 'vc-' prefix).

Features:
- Checks issue_prefix from config table when --id is provided
- Fails with helpful error message showing the mismatch
- Suggests correct prefix format
- Adds --force flag to override validation if intentional
- Only validates in direct mode (daemon needs RPC enhancement)

Example: bd create 'Test' --id vc-99  # Error: prefix mismatch, use bd-99
  bd create 'Test' --id vc-99 --force  # OK, forced override
Amp-Thread-ID: https://ampcode.com/threads/T-4e1ac6f1-7465-442a-a385-adaa98b539ad
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-17 22:20:18 -07:00
parent 6ab9cc9a91
commit 048522c41a
2 changed files with 30 additions and 3 deletions

View File

@@ -960,6 +960,7 @@ var createCmd = &cobra.Command{
explicitID, _ := cmd.Flags().GetString("id")
externalRef, _ := cmd.Flags().GetString("external-ref")
deps, _ := cmd.Flags().GetStringSlice("deps")
forceCreate, _ := cmd.Flags().GetBool("force")
// Validate explicit ID format if provided (prefix-number)
if explicitID != "" {
@@ -974,6 +975,30 @@ var createCmd = &cobra.Command{
fmt.Fprintf(os.Stderr, "Error: invalid ID format '%s' (numeric suffix required, e.g., 'bd-42')\n", explicitID)
os.Exit(1)
}
// Validate prefix matches database prefix (unless --force is used)
if !forceCreate {
requestedPrefix := parts[0]
ctx := context.Background()
// Get database prefix from config
var dbPrefix string
if daemonClient != nil {
// Using daemon - need to get config via RPC
// For now, skip validation in daemon mode (needs RPC enhancement)
} else {
// Direct mode - check config
dbPrefix, _ = store.GetConfig(ctx, "issue_prefix")
}
if dbPrefix != "" && dbPrefix != requestedPrefix {
fmt.Fprintf(os.Stderr, "Error: prefix mismatch detected\n")
fmt.Fprintf(os.Stderr, " This database uses prefix '%s-', but you specified '%s-'\n", dbPrefix, requestedPrefix)
fmt.Fprintf(os.Stderr, " Did you mean to create '%s-%s'?\n", dbPrefix, parts[1])
fmt.Fprintf(os.Stderr, " Use --force to create with mismatched prefix anyway\n")
os.Exit(1)
}
}
}
var externalRefPtr *string
@@ -1116,6 +1141,7 @@ func init() {
createCmd.Flags().String("id", "", "Explicit issue ID (e.g., 'bd-42' for partitioning)")
createCmd.Flags().String("external-ref", "", "External reference (e.g., 'gh-9', 'jira-ABC')")
createCmd.Flags().StringSlice("deps", []string{}, "Dependencies in format 'type:id' or 'id' (e.g., 'discovered-from:bd-20,blocks:bd-15' or 'bd-20')")
createCmd.Flags().Bool("force", false, "Force creation even if prefix doesn't match database prefix")
rootCmd.AddCommand(createCmd)
}