Refactor issueDataChanged to reduce cyclomatic complexity (bd-55)
- Extract field comparison logic into fieldComparator struct - Reduce complexity from 39 to 11 - Improve code organization and testability - All tests passing Amp-Thread-ID: https://ampcode.com/threads/T-4d8cdee6-84e8-4348-ad30-9e59fb3e30cf Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -10,11 +10,18 @@ import (
|
|||||||
"github.com/steveyegge/beads/internal/types"
|
"github.com/steveyegge/beads/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// issueDataChanged checks if any fields in the updates map differ from the existing issue
|
// fieldComparator handles comparison logic for a specific field type
|
||||||
// Returns true if any field changed, false if all fields match
|
type fieldComparator struct {
|
||||||
func issueDataChanged(existing *types.Issue, updates map[string]interface{}) bool {
|
|
||||||
// Helper to safely extract string from interface (handles string and *string)
|
// Helper to safely extract string from interface (handles string and *string)
|
||||||
strFrom := func(v interface{}) (string, bool) {
|
strFrom func(v interface{}) (string, bool)
|
||||||
|
// Helper to safely extract int from interface
|
||||||
|
intFrom func(v interface{}) (int64, bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFieldComparator() *fieldComparator {
|
||||||
|
fc := &fieldComparator{}
|
||||||
|
|
||||||
|
fc.strFrom = func(v interface{}) (string, bool) {
|
||||||
switch t := v.(type) {
|
switch t := v.(type) {
|
||||||
case string:
|
case string:
|
||||||
return t, true
|
return t, true
|
||||||
@@ -29,30 +36,8 @@ func issueDataChanged(existing *types.Issue, updates map[string]interface{}) boo
|
|||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to compare string field (treats empty and nil as equal)
|
fc.intFrom = func(v interface{}) (int64, bool) {
|
||||||
equalStr := func(existingVal string, newVal interface{}) bool {
|
|
||||||
s, ok := strFrom(newVal)
|
|
||||||
if !ok {
|
|
||||||
return false // Type mismatch means changed
|
|
||||||
}
|
|
||||||
return existingVal == s
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to compare *string field (treats empty and nil as equal)
|
|
||||||
equalPtrStr := func(existing *string, newVal interface{}) bool {
|
|
||||||
s, ok := strFrom(newVal)
|
|
||||||
if !ok {
|
|
||||||
return false // Type mismatch means changed
|
|
||||||
}
|
|
||||||
if existing == nil {
|
|
||||||
return s == ""
|
|
||||||
}
|
|
||||||
return *existing == s
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to safely extract int from interface
|
|
||||||
intFrom := func(v interface{}) (int64, bool) {
|
|
||||||
switch t := v.(type) {
|
switch t := v.(type) {
|
||||||
case int:
|
case int:
|
||||||
return int64(t), true
|
return int64(t), true
|
||||||
@@ -70,82 +55,106 @@ func issueDataChanged(existing *types.Issue, updates map[string]interface{}) boo
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fc
|
||||||
|
}
|
||||||
|
|
||||||
// Helper to compare Status field
|
// equalStr compares string field (treats empty and nil as equal)
|
||||||
equalStatus := func(existing types.Status, newVal interface{}) bool {
|
func (fc *fieldComparator) equalStr(existingVal string, newVal interface{}) bool {
|
||||||
switch t := newVal.(type) {
|
s, ok := fc.strFrom(newVal)
|
||||||
case types.Status:
|
if !ok {
|
||||||
return existing == t
|
return false // Type mismatch means changed
|
||||||
case string:
|
|
||||||
return string(existing) == t
|
|
||||||
default:
|
|
||||||
return false // Unknown type means changed
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return existingVal == s
|
||||||
|
}
|
||||||
|
|
||||||
// Helper to compare IssueType field
|
// equalPtrStr compares *string field (treats empty and nil as equal)
|
||||||
equalIssueType := func(existing types.IssueType, newVal interface{}) bool {
|
func (fc *fieldComparator) equalPtrStr(existing *string, newVal interface{}) bool {
|
||||||
switch t := newVal.(type) {
|
s, ok := fc.strFrom(newVal)
|
||||||
case types.IssueType:
|
if !ok {
|
||||||
return existing == t
|
return false // Type mismatch means changed
|
||||||
case string:
|
|
||||||
return string(existing) == t
|
|
||||||
default:
|
|
||||||
return false // Unknown type means changed
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if existing == nil {
|
||||||
|
return s == ""
|
||||||
|
}
|
||||||
|
return *existing == s
|
||||||
|
}
|
||||||
|
|
||||||
|
// equalStatus compares Status field
|
||||||
|
func (fc *fieldComparator) equalStatus(existing types.Status, newVal interface{}) bool {
|
||||||
|
switch t := newVal.(type) {
|
||||||
|
case types.Status:
|
||||||
|
return existing == t
|
||||||
|
case string:
|
||||||
|
return string(existing) == t
|
||||||
|
default:
|
||||||
|
return false // Unknown type means changed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// equalIssueType compares IssueType field
|
||||||
|
func (fc *fieldComparator) equalIssueType(existing types.IssueType, newVal interface{}) bool {
|
||||||
|
switch t := newVal.(type) {
|
||||||
|
case types.IssueType:
|
||||||
|
return existing == t
|
||||||
|
case string:
|
||||||
|
return string(existing) == t
|
||||||
|
default:
|
||||||
|
return false // Unknown type means changed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// equalPriority compares priority field
|
||||||
|
func (fc *fieldComparator) equalPriority(existing int, newVal interface{}) bool {
|
||||||
|
p, ok := fc.intFrom(newVal)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return existing == int(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkFieldChanged checks if a specific field has changed
|
||||||
|
func (fc *fieldComparator) checkFieldChanged(key string, existing *types.Issue, newVal interface{}) bool {
|
||||||
|
switch key {
|
||||||
|
case "title":
|
||||||
|
return !fc.equalStr(existing.Title, newVal)
|
||||||
|
case "description":
|
||||||
|
return !fc.equalStr(existing.Description, newVal)
|
||||||
|
case "status":
|
||||||
|
return !fc.equalStatus(existing.Status, newVal)
|
||||||
|
case "priority":
|
||||||
|
return !fc.equalPriority(existing.Priority, newVal)
|
||||||
|
case "issue_type":
|
||||||
|
return !fc.equalIssueType(existing.IssueType, newVal)
|
||||||
|
case "design":
|
||||||
|
return !fc.equalStr(existing.Design, newVal)
|
||||||
|
case "acceptance_criteria":
|
||||||
|
return !fc.equalStr(existing.AcceptanceCriteria, newVal)
|
||||||
|
case "notes":
|
||||||
|
return !fc.equalStr(existing.Notes, newVal)
|
||||||
|
case "assignee":
|
||||||
|
return !fc.equalStr(existing.Assignee, newVal)
|
||||||
|
case "external_ref":
|
||||||
|
return !fc.equalPtrStr(existing.ExternalRef, newVal)
|
||||||
|
default:
|
||||||
|
// Unknown field - treat as changed to be conservative
|
||||||
|
// This prevents skipping updates when new fields are added
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// issueDataChanged checks if any fields in the updates map differ from the existing issue
|
||||||
|
// Returns true if any field changed, false if all fields match
|
||||||
|
func issueDataChanged(existing *types.Issue, updates map[string]interface{}) bool {
|
||||||
|
fc := newFieldComparator()
|
||||||
|
|
||||||
// Check each field in updates map
|
// Check each field in updates map
|
||||||
for key, newVal := range updates {
|
for key, newVal := range updates {
|
||||||
switch key {
|
if fc.checkFieldChanged(key, existing, newVal) {
|
||||||
case "title":
|
|
||||||
if !equalStr(existing.Title, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "description":
|
|
||||||
if !equalStr(existing.Description, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "status":
|
|
||||||
if !equalStatus(existing.Status, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "priority":
|
|
||||||
p, ok := intFrom(newVal)
|
|
||||||
if !ok || existing.Priority != int(p) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "issue_type":
|
|
||||||
if !equalIssueType(existing.IssueType, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "design":
|
|
||||||
if !equalStr(existing.Design, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "acceptance_criteria":
|
|
||||||
if !equalStr(existing.AcceptanceCriteria, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "notes":
|
|
||||||
if !equalStr(existing.Notes, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "assignee":
|
|
||||||
if !equalStr(existing.Assignee, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case "external_ref":
|
|
||||||
if !equalPtrStr(existing.ExternalRef, newVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// Unknown field - treat as changed to be conservative
|
|
||||||
// This prevents skipping updates when new fields are added
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false // No changes detected
|
return false // No changes detected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user