Files
beads/internal/storage/sqlite/epics.go
Steve Yegge b855c444d4 Enable errcheck linter and fix all production code warnings
- Enabled errcheck linter (previously disabled)
- Set tests: false in .golangci.yml to focus on production code
- Fixed 27 errcheck warnings using Go best practices:
  * Database resources: defer func() { _ = rows.Close() }()
  * Transaction rollbacks: defer func() { _ = tx.Rollback() }()
  * Best-effort closers: _ = store.Close(), _ = client.Close()
  * File writes: proper error checking on Close()
  * Interactive input: handle EOF gracefully
  * File ops: ignore ENOENT on os.Remove()
- All tests pass
- Closes bd-58

Amp-Thread-ID: https://ampcode.com/threads/T-57c9afd3-9adf-40c2-8be7-3e493d200361
Co-authored-by: Amp <amp@ampcode.com>
2025-10-25 18:44:38 -07:00

87 lines
2.2 KiB
Go

package sqlite
import (
"context"
"database/sql"
"github.com/steveyegge/beads/internal/types"
)
// GetEpicsEligibleForClosure returns all epics with their completion status
func (s *SQLiteStorage) GetEpicsEligibleForClosure(ctx context.Context) ([]*types.EpicStatus, error) {
query := `
WITH epic_children AS (
SELECT
d.depends_on_id AS epic_id,
i.id AS child_id,
i.status AS child_status
FROM dependencies d
JOIN issues i ON i.id = d.issue_id
WHERE d.type = 'parent-child'
),
epic_stats AS (
SELECT
epic_id,
COUNT(*) AS total_children,
SUM(CASE WHEN child_status = 'closed' THEN 1 ELSE 0 END) AS closed_children
FROM epic_children
GROUP BY epic_id
)
SELECT
i.id, i.title, i.description, i.design, i.acceptance_criteria, i.notes,
i.status, i.priority, i.issue_type, i.assignee, i.estimated_minutes,
i.created_at, i.updated_at, i.closed_at, i.external_ref,
COALESCE(es.total_children, 0) AS total_children,
COALESCE(es.closed_children, 0) AS closed_children
FROM issues i
LEFT JOIN epic_stats es ON es.epic_id = i.id
WHERE i.issue_type = 'epic'
AND i.status != 'closed'
ORDER BY i.priority ASC, i.created_at ASC
`
rows, err := s.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
var results []*types.EpicStatus
for rows.Next() {
var epic types.Issue
var totalChildren, closedChildren int
var assignee sql.NullString
err := rows.Scan(
&epic.ID, &epic.Title, &epic.Description, &epic.Design,
&epic.AcceptanceCriteria, &epic.Notes, &epic.Status,
&epic.Priority, &epic.IssueType, &assignee,
&epic.EstimatedMinutes, &epic.CreatedAt, &epic.UpdatedAt,
&epic.ClosedAt, &epic.ExternalRef,
&totalChildren, &closedChildren,
)
if err != nil {
return nil, err
}
// Convert sql.NullString to string
if assignee.Valid {
epic.Assignee = assignee.String
}
eligibleForClose := false
if totalChildren > 0 && closedChildren == totalChildren {
eligibleForClose = true
}
results = append(results, &types.EpicStatus{
Epic: &epic,
TotalChildren: totalChildren,
ClosedChildren: closedChildren,
EligibleForClose: eligibleForClose,
})
}
return results, rows.Err()
}