beads/internal/validation/template.go
emma 63432acf64 fix: --validate checks --acceptance field, add validation.on-close config (PR #2654)
Three fixes for validation consistency:

1. bd create --validate now uses LintIssue() instead of ValidateTemplate(),
   bringing it to parity with bd lint. A non-empty --acceptance field now
   satisfies the "Acceptance Criteria" requirement without needing the
   heading in --description. (GH#2468 parity)

2. --dry-run no longer bypasses --validate. Validation now runs before the
   dry-run exit, so users get feedback on template issues during preview.

3. New validation.on-close config option (none|warn|error, default: none).
   When enabled, warns on terse close reasons (<20 chars) or default
   "Closed" reasons. Mirrors existing validation.on-create pattern.

Co-Authored-By: seanmartinsmith <114885497+seanmartinsmith@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:21:23 -07:00

125 lines
3.8 KiB
Go

package validation
import (
"fmt"
"strings"
"github.com/steveyegge/beads/internal/types"
)
// MissingSection describes a section that should be present but isn't.
type MissingSection struct {
Heading string // The expected heading, e.g., "## Steps to Reproduce"
Hint string // Guidance for what to include
}
// TemplateError is returned when template validation fails.
// It contains all missing sections for a single error report.
type TemplateError struct {
IssueType types.IssueType
Missing []MissingSection
}
func (e *TemplateError) Error() string {
if len(e.Missing) == 0 {
return ""
}
var b strings.Builder
fmt.Fprintf(&b, "missing required sections for %s:", e.IssueType)
for _, m := range e.Missing {
fmt.Fprintf(&b, "\n - %s (%s)", m.Heading, m.Hint)
}
return b.String()
}
// ValidateTemplate checks if the description contains all required sections
// for the given issue type. Returns nil if validation passes or if the
// issue type has no required sections.
//
// Section matching is case-insensitive and looks for the heading text
// anywhere in the description (doesn't require exact markdown format).
func ValidateTemplate(issueType types.IssueType, description string) error {
required := issueType.RequiredSections()
if len(required) == 0 {
return nil
}
descLower := strings.ToLower(description)
var missing []MissingSection
for _, section := range required {
// Extract the heading text without markdown prefix for flexible matching
// e.g., "## Steps to Reproduce" -> "steps to reproduce"
headingText := strings.TrimPrefix(section.Heading, "## ")
headingText = strings.TrimPrefix(headingText, "# ")
headingLower := strings.ToLower(headingText)
if !strings.Contains(descLower, headingLower) {
missing = append(missing, MissingSection{
Heading: section.Heading,
Hint: section.Hint,
})
}
}
if len(missing) > 0 {
return &TemplateError{
IssueType: issueType,
Missing: missing,
}
}
return nil
}
// LintIssue checks an existing issue for missing template sections.
// Unlike ValidateTemplate, this operates on a full Issue struct.
// It checks both Description and AcceptanceCriteria fields.
// A non-empty AcceptanceCriteria field satisfies the "Acceptance Criteria"
// (or "Success Criteria" for epics) requirement without needing a heading. (GH#2468)
// Returns nil if the issue passes validation or has no requirements.
func LintIssue(issue *types.Issue) error {
if issue == nil {
return nil
}
text := issue.Description
if issue.AcceptanceCriteria != "" {
text = text + "\n" + issue.AcceptanceCriteria
}
err := ValidateTemplate(issue.IssueType, text)
if err == nil || issue.AcceptanceCriteria == "" {
return err
}
// A non-empty AcceptanceCriteria field satisfies "Acceptance Criteria"
// or "Success Criteria" requirements even without the heading text.
templateErr, ok := err.(*TemplateError)
if !ok {
return err
}
var remaining []MissingSection
for _, m := range templateErr.Missing {
heading := strings.ToLower(strings.TrimPrefix(m.Heading, "## "))
if heading == "acceptance criteria" || heading == "success criteria" {
continue // satisfied by the dedicated field
}
remaining = append(remaining, m)
}
if len(remaining) == 0 {
return nil
}
templateErr.Missing = remaining
return templateErr
}
// ValidateCloseReason checks if a close reason meets minimum quality standards.
// Returns nil if the reason is acceptable. Used by validation.on-close config.
func ValidateCloseReason(reason string) error {
reason = strings.TrimSpace(reason)
if reason == "" || strings.EqualFold(reason, "closed") {
return fmt.Errorf("close reason is empty or default; provide a summary of what was done")
}
if len(reason) < 20 {
return fmt.Errorf("close reason is terse (%d chars); aim for 20+ characters describing what was done", len(reason))
}
return nil
}