feat(bd-1pj6): Add custom status states via config

Users can now define custom status states for multi-step pipelines using:
  bd config set status.custom "awaiting_review,awaiting_testing,awaiting_docs"

Changes:
- Add Status.IsValidWithCustom() method for custom status validation
- Add Issue.ValidateWithCustomStatuses() method
- Add GetCustomStatuses() method to storage interface
- Update CreateIssue/UpdateIssue to support custom statuses
- Add comprehensive tests for custom status functionality
- Update config command help text with custom status documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-11-28 23:19:16 -08:00
parent 26f84b6a47
commit a5ec5c6977
12 changed files with 375 additions and 30 deletions

View File

@@ -10,7 +10,14 @@ import (
)
// validateBatchIssues validates all issues in a batch and sets timestamps if not provided
// Uses built-in statuses only for backward compatibility.
func validateBatchIssues(issues []*types.Issue) error {
return validateBatchIssuesWithCustomStatuses(issues, nil)
}
// validateBatchIssuesWithCustomStatuses validates all issues in a batch,
// allowing custom statuses in addition to built-in ones (bd-1pj6).
func validateBatchIssuesWithCustomStatuses(issues []*types.Issue, customStatuses []string) error {
now := time.Now()
for i, issue := range issues {
if issue == nil {
@@ -25,7 +32,7 @@ func validateBatchIssues(issues []*types.Issue) error {
issue.UpdatedAt = now
}
if err := issue.Validate(); err != nil {
if err := issue.ValidateWithCustomStatuses(customStatuses); err != nil {
return fmt.Errorf("validation failed for issue %d: %w", i, err)
}
}
@@ -146,8 +153,14 @@ func (s *SQLiteStorage) CreateIssuesWithFullOptions(ctx context.Context, issues
return nil
}
// Phase 1: Validate all issues first (fail-fast)
if err := validateBatchIssues(issues); err != nil {
// Fetch custom statuses for validation (bd-1pj6)
customStatuses, err := s.GetCustomStatuses(ctx)
if err != nil {
return fmt.Errorf("failed to get custom statuses: %w", err)
}
// Phase 1: Validate all issues first (fail-fast, with custom status support)
if err := validateBatchIssuesWithCustomStatuses(issues, customStatuses); err != nil {
return err
}

View File

@@ -3,6 +3,7 @@ package sqlite
import (
"context"
"database/sql"
"strings"
)
// SetConfig sets a configuration value
@@ -93,3 +94,37 @@ func (s *SQLiteStorage) GetMetadata(ctx context.Context, key string) (string, er
}
return value, wrapDBError("get metadata", err)
}
// CustomStatusConfigKey is the config key for custom status states
const CustomStatusConfigKey = "status.custom"
// GetCustomStatuses retrieves the list of custom status states from config.
// Custom statuses are stored as comma-separated values in the "status.custom" config key.
// Returns an empty slice if no custom statuses are configured.
func (s *SQLiteStorage) GetCustomStatuses(ctx context.Context) ([]string, error) {
value, err := s.GetConfig(ctx, CustomStatusConfigKey)
if err != nil {
return nil, err
}
if value == "" {
return nil, nil
}
return parseCustomStatuses(value), nil
}
// parseCustomStatuses splits a comma-separated string into a slice of trimmed status names.
// Empty entries are filtered out.
func parseCustomStatuses(value string) []string {
if value == "" {
return nil
}
parts := strings.Split(value, ",")
result := make([]string, 0, len(parts))
for _, p := range parts {
trimmed := strings.TrimSpace(p)
if trimmed != "" {
result = append(result, trimmed)
}
}
return result
}

View File

@@ -124,6 +124,12 @@ func (s *SQLiteStorage) importJSONLFile(ctx context.Context, jsonlPath, sourceRe
}
defer file.Close()
// Fetch custom statuses for validation (bd-1pj6)
customStatuses, err := s.GetCustomStatuses(ctx)
if err != nil {
return 0, fmt.Errorf("failed to get custom statuses: %w", err)
}
scanner := bufio.NewScanner(file)
// Increase buffer size for large issues
buf := make([]byte, 0, 64*1024)
@@ -161,8 +167,8 @@ func (s *SQLiteStorage) importJSONLFile(ctx context.Context, jsonlPath, sourceRe
issue.ContentHash = issue.ComputeContentHash()
}
// Insert or update issue
if err := s.upsertIssueInTx(ctx, tx, &issue); err != nil {
// Insert or update issue (with custom status support)
if err := s.upsertIssueInTx(ctx, tx, &issue, customStatuses); err != nil {
return 0, fmt.Errorf("failed to import issue %s at line %d: %w", issue.ID, lineNum, err)
}
@@ -182,9 +188,9 @@ func (s *SQLiteStorage) importJSONLFile(ctx context.Context, jsonlPath, sourceRe
// upsertIssueInTx inserts or updates an issue within a transaction.
// Uses INSERT OR REPLACE to handle both new and existing issues.
func (s *SQLiteStorage) upsertIssueInTx(ctx context.Context, tx *sql.Tx, issue *types.Issue) error {
// Validate issue
if err := issue.Validate(); err != nil {
func (s *SQLiteStorage) upsertIssueInTx(ctx context.Context, tx *sql.Tx, issue *types.Issue, customStatuses []string) error {
// Validate issue (with custom status support, bd-1pj6)
if err := issue.ValidateWithCustomStatuses(customStatuses); err != nil {
return fmt.Errorf("validation failed: %w", err)
}

View File

@@ -27,8 +27,14 @@ import (
// CreateIssue creates a new issue
func (s *SQLiteStorage) CreateIssue(ctx context.Context, issue *types.Issue, actor string) error {
// Validate issue before creating
if err := issue.Validate(); err != nil {
// Fetch custom statuses for validation (bd-1pj6)
customStatuses, err := s.GetCustomStatuses(ctx)
if err != nil {
return fmt.Errorf("failed to get custom statuses: %w", err)
}
// Validate issue before creating (with custom status support)
if err := issue.ValidateWithCustomStatuses(customStatuses); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
@@ -471,6 +477,12 @@ func (s *SQLiteStorage) UpdateIssue(ctx context.Context, id string, updates map[
return fmt.Errorf("issue %s not found", id)
}
// Fetch custom statuses for validation (bd-1pj6)
customStatuses, err := s.GetCustomStatuses(ctx)
if err != nil {
return wrapDBError("get custom statuses", err)
}
// Build update query with validated field names
setClauses := []string{"updated_at = ?"}
args := []interface{}{time.Now()}
@@ -481,8 +493,8 @@ func (s *SQLiteStorage) UpdateIssue(ctx context.Context, id string, updates map[
return fmt.Errorf("invalid field for update: %s", key)
}
// Validate field values
if err := validateFieldUpdate(key, value); err != nil {
// Validate field values (with custom status support)
if err := validateFieldUpdateWithCustomStatuses(key, value, customStatuses); err != nil {
return wrapDBError("validate field update", err)
}

View File

@@ -91,8 +91,14 @@ func (s *SQLiteStorage) RunInTransaction(ctx context.Context, fn func(tx storage
// CreateIssue creates a new issue within the transaction.
func (t *sqliteTxStorage) CreateIssue(ctx context.Context, issue *types.Issue, actor string) error {
// Validate issue before creating
if err := issue.Validate(); err != nil {
// Fetch custom statuses for validation (bd-1pj6)
customStatuses, err := t.GetCustomStatuses(ctx)
if err != nil {
return fmt.Errorf("failed to get custom statuses: %w", err)
}
// Validate issue before creating (with custom status support)
if err := issue.ValidateWithCustomStatuses(customStatuses); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
@@ -108,7 +114,7 @@ func (t *sqliteTxStorage) CreateIssue(ctx context.Context, issue *types.Issue, a
// Get prefix from config (needed for both ID generation and validation)
var prefix string
err := t.conn.QueryRowContext(ctx, `SELECT value FROM config WHERE key = ?`, "issue_prefix").Scan(&prefix)
err = t.conn.QueryRowContext(ctx, `SELECT value FROM config WHERE key = ?`, "issue_prefix").Scan(&prefix)
if err == sql.ErrNoRows || prefix == "" {
// CRITICAL: Reject operation if issue_prefix config is missing (bd-166)
return fmt.Errorf("database not initialized: issue_prefix config is missing (run 'bd init --prefix <prefix>' first)")
@@ -170,10 +176,16 @@ func (t *sqliteTxStorage) CreateIssues(ctx context.Context, issues []*types.Issu
return nil
}
// Validate and prepare all issues first
// Fetch custom statuses for validation (bd-1pj6)
customStatuses, err := t.GetCustomStatuses(ctx)
if err != nil {
return fmt.Errorf("failed to get custom statuses: %w", err)
}
// Validate and prepare all issues first (with custom status support)
now := time.Now()
for _, issue := range issues {
if err := issue.Validate(); err != nil {
if err := issue.ValidateWithCustomStatuses(customStatuses); err != nil {
return fmt.Errorf("validation failed for issue: %w", err)
}
issue.CreatedAt = now
@@ -185,7 +197,7 @@ func (t *sqliteTxStorage) CreateIssues(ctx context.Context, issues []*types.Issu
// Get prefix from config
var prefix string
err := t.conn.QueryRowContext(ctx, `SELECT value FROM config WHERE key = ?`, "issue_prefix").Scan(&prefix)
err = t.conn.QueryRowContext(ctx, `SELECT value FROM config WHERE key = ?`, "issue_prefix").Scan(&prefix)
if err == sql.ErrNoRows || prefix == "" {
return fmt.Errorf("database not initialized: issue_prefix config is missing")
} else if err != nil {
@@ -297,6 +309,12 @@ func (t *sqliteTxStorage) UpdateIssue(ctx context.Context, id string, updates ma
return fmt.Errorf("issue %s not found", id)
}
// Fetch custom statuses for validation (bd-1pj6)
customStatuses, err := t.GetCustomStatuses(ctx)
if err != nil {
return fmt.Errorf("failed to get custom statuses: %w", err)
}
// Build update query with validated field names
setClauses := []string{"updated_at = ?"}
args := []interface{}{time.Now()}
@@ -307,8 +325,8 @@ func (t *sqliteTxStorage) UpdateIssue(ctx context.Context, id string, updates ma
return fmt.Errorf("invalid field for update: %s", key)
}
// Validate field values
if err := validateFieldUpdate(key, value); err != nil {
// Validate field values (with custom status support)
if err := validateFieldUpdateWithCustomStatuses(key, value, customStatuses); err != nil {
return fmt.Errorf("failed to validate field update: %w", err)
}
@@ -791,6 +809,18 @@ func (t *sqliteTxStorage) GetConfig(ctx context.Context, key string) (string, er
return value, nil
}
// GetCustomStatuses retrieves the list of custom status states from config within the transaction.
func (t *sqliteTxStorage) GetCustomStatuses(ctx context.Context) ([]string, error) {
value, err := t.GetConfig(ctx, CustomStatusConfigKey)
if err != nil {
return nil, err
}
if value == "" {
return nil, nil
}
return parseCustomStatuses(value), nil
}
// SetMetadata sets a metadata value within the transaction.
func (t *sqliteTxStorage) SetMetadata(ctx context.Context, key, value string) error {
_, err := t.conn.ExecContext(ctx, `

View File

@@ -16,10 +16,15 @@ func validatePriority(value interface{}) error {
return nil
}
// validateStatus validates a status value
// validateStatus validates a status value (built-in statuses only)
func validateStatus(value interface{}) error {
return validateStatusWithCustom(value, nil)
}
// validateStatusWithCustom validates a status value, allowing custom statuses.
func validateStatusWithCustom(value interface{}, customStatuses []string) error {
if status, ok := value.(string); ok {
if !types.Status(status).IsValid() {
if !types.Status(status).IsValidWithCustom(customStatuses) {
return fmt.Errorf("invalid status: %s", status)
}
}
@@ -65,8 +70,18 @@ var fieldValidators = map[string]func(interface{}) error{
"estimated_minutes": validateEstimatedMinutes,
}
// validateFieldUpdate validates a field update value
// validateFieldUpdate validates a field update value (built-in statuses only)
func validateFieldUpdate(key string, value interface{}) error {
return validateFieldUpdateWithCustomStatuses(key, value, nil)
}
// validateFieldUpdateWithCustomStatuses validates a field update value,
// allowing custom statuses for status field validation.
func validateFieldUpdateWithCustomStatuses(key string, value interface{}, customStatuses []string) error {
// Special handling for status field to support custom statuses
if key == "status" {
return validateStatusWithCustom(value, customStatuses)
}
if validator, ok := fieldValidators[key]; ok {
return validator(value)
}

View File

@@ -149,3 +149,72 @@ func TestValidateFieldUpdate(t *testing.T) {
})
}
}
func TestValidateFieldUpdateWithCustomStatuses(t *testing.T) {
customStatuses := []string{"awaiting_review", "awaiting_testing"}
tests := []struct {
name string
key string
value interface{}
customStatuses []string
wantErr bool
}{
// Built-in statuses work with or without custom statuses
{"built-in status no custom", "status", string(types.StatusOpen), nil, false},
{"built-in status with custom", "status", string(types.StatusOpen), customStatuses, false},
{"built-in closed with custom", "status", string(types.StatusClosed), customStatuses, false},
// Custom statuses work when configured
{"custom status configured", "status", "awaiting_review", customStatuses, false},
{"custom status awaiting_testing", "status", "awaiting_testing", customStatuses, false},
// Custom statuses fail without config
{"custom status not configured", "status", "awaiting_review", nil, true},
{"custom status not in list", "status", "unknown_status", customStatuses, true},
// Non-status fields work as before
{"valid priority", "priority", 1, customStatuses, false},
{"invalid priority", "priority", 5, customStatuses, true},
{"unknown field", "unknown_field", "any value", customStatuses, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateFieldUpdateWithCustomStatuses(tt.key, tt.value, tt.customStatuses)
if (err != nil) != tt.wantErr {
t.Errorf("validateFieldUpdateWithCustomStatuses() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestParseCustomStatuses(t *testing.T) {
tests := []struct {
name string
value string
want []string
}{
{"empty string", "", nil},
{"single status", "awaiting_review", []string{"awaiting_review"}},
{"multiple statuses", "awaiting_review,awaiting_testing", []string{"awaiting_review", "awaiting_testing"}},
{"with spaces", "awaiting_review, awaiting_testing, awaiting_docs", []string{"awaiting_review", "awaiting_testing", "awaiting_docs"}},
{"empty entries filtered", "awaiting_review,,awaiting_testing", []string{"awaiting_review", "awaiting_testing"}},
{"whitespace only entries", "awaiting_review, , awaiting_testing", []string{"awaiting_review", "awaiting_testing"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseCustomStatuses(tt.value)
if len(got) != len(tt.want) {
t.Errorf("parseCustomStatuses() = %v, want %v", got, tt.want)
return
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("parseCustomStatuses()[%d] = %v, want %v", i, got[i], tt.want[i])
}
}
})
}
}