Add --clear-duplicate-external-refs flag to bd import
Fixes GH-234 by providing automatic resolution for duplicate external_ref values instead of forcing manual JSONL editing. Changes: - Add ClearDuplicateExternalRefs option to importer.Options - Modify validateNoDuplicateExternalRefs to clear duplicates when enabled - Keep first occurrence, clear rest when flag is set - Enhanced error message to suggest the flag - Add comprehensive tests for the new behavior Usage: bd import -i issues.jsonl --clear-duplicate-external-refs Amp-Thread-ID: https://ampcode.com/threads/T-932dcf45-76f2-4994-9b5c-a6eb20a86036 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -905,7 +905,7 @@ func TestValidateNoDuplicateExternalRefs(t *testing.T) {
|
||||
{ID: "bd-1", Title: "Issue 1"},
|
||||
{ID: "bd-2", Title: "Issue 2"},
|
||||
}
|
||||
err := validateNoDuplicateExternalRefs(issues)
|
||||
err := validateNoDuplicateExternalRefs(issues, false, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
@@ -918,7 +918,7 @@ func TestValidateNoDuplicateExternalRefs(t *testing.T) {
|
||||
{ID: "bd-1", Title: "Issue 1", ExternalRef: &ref1},
|
||||
{ID: "bd-2", Title: "Issue 2", ExternalRef: &ref2},
|
||||
}
|
||||
err := validateNoDuplicateExternalRefs(issues)
|
||||
err := validateNoDuplicateExternalRefs(issues, false, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
@@ -931,7 +931,7 @@ func TestValidateNoDuplicateExternalRefs(t *testing.T) {
|
||||
{ID: "bd-1", Title: "Issue 1", ExternalRef: &ref1},
|
||||
{ID: "bd-2", Title: "Issue 2", ExternalRef: &ref2},
|
||||
}
|
||||
err := validateNoDuplicateExternalRefs(issues)
|
||||
err := validateNoDuplicateExternalRefs(issues, false, nil)
|
||||
if err == nil {
|
||||
t.Error("Expected error for duplicate external_ref, got nil")
|
||||
}
|
||||
@@ -940,6 +940,30 @@ func TestValidateNoDuplicateExternalRefs(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("duplicate external_ref values with clear flag", func(t *testing.T) {
|
||||
ref1 := "JIRA-1"
|
||||
ref2 := "JIRA-1"
|
||||
issues := []*types.Issue{
|
||||
{ID: "bd-1", Title: "Issue 1", ExternalRef: &ref1},
|
||||
{ID: "bd-2", Title: "Issue 2", ExternalRef: &ref2},
|
||||
}
|
||||
result := &Result{}
|
||||
err := validateNoDuplicateExternalRefs(issues, true, result)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error with clear flag, got: %v", err)
|
||||
}
|
||||
// First issue should keep external_ref, second should be cleared
|
||||
if issues[0].ExternalRef == nil || *issues[0].ExternalRef != "JIRA-1" {
|
||||
t.Error("Expected first issue to keep external_ref JIRA-1")
|
||||
}
|
||||
if issues[1].ExternalRef != nil {
|
||||
t.Error("Expected second issue to have cleared external_ref")
|
||||
}
|
||||
if result.Skipped != 1 {
|
||||
t.Errorf("Expected 1 skipped (cleared), got %d", result.Skipped)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("multiple duplicates", func(t *testing.T) {
|
||||
jira1 := "JIRA-1"
|
||||
jira2 := "JIRA-2"
|
||||
@@ -949,7 +973,7 @@ func TestValidateNoDuplicateExternalRefs(t *testing.T) {
|
||||
{ID: "bd-3", Title: "Issue 3", ExternalRef: &jira2},
|
||||
{ID: "bd-4", Title: "Issue 4", ExternalRef: &jira2},
|
||||
}
|
||||
err := validateNoDuplicateExternalRefs(issues)
|
||||
err := validateNoDuplicateExternalRefs(issues, false, nil)
|
||||
if err == nil {
|
||||
t.Error("Expected error for duplicate external_ref, got nil")
|
||||
}
|
||||
@@ -968,7 +992,7 @@ func TestValidateNoDuplicateExternalRefs(t *testing.T) {
|
||||
{ID: "bd-2", Title: "Issue 2", ExternalRef: &empty},
|
||||
{ID: "bd-3", Title: "Issue 3", ExternalRef: &ref1},
|
||||
}
|
||||
err := validateNoDuplicateExternalRefs(issues)
|
||||
err := validateNoDuplicateExternalRefs(issues, false, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error for empty refs, got: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user