beads/internal/storage/issueops/get_issue.go
2026-03-13 14:25:01 -07:00

38 lines
1.2 KiB
Go

package issueops
import (
"context"
"database/sql"
"fmt"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
)
// GetIssueInTx retrieves a single issue by ID within an existing transaction,
// including its labels. Automatically routes to the wisps/wisp_labels tables
// if the ID is an active wisp. Returns storage.ErrNotFound (wrapped) if the
// issue does not exist in either table.
func GetIssueInTx(ctx context.Context, tx *sql.Tx, id string) (*types.Issue, error) {
isWisp := IsActiveWispInTx(ctx, tx, id)
issueTable, labelTable, _, _ := WispTableRouting(isWisp)
//nolint:gosec // G201: issueTable is from WispTableRouting ("issues" or "wisps")
row := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT %s FROM %s WHERE id = ?`, IssueSelectColumns, issueTable), id)
issue, err := ScanIssueFrom(row)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("%w: issue %s", storage.ErrNotFound, id)
}
if err != nil {
return nil, fmt.Errorf("get issue: %w", err)
}
// Fetch labels in the same transaction to avoid MaxOpenConns=1 deadlock.
labels, err := GetLabelsInTx(ctx, tx, labelTable, id)
if err != nil {
return nil, fmt.Errorf("get issue labels: %w", err)
}
issue.Labels = labels
return issue, nil
}