Files
archie/modules/runs/plan.go
T

88 lines
2.6 KiB
Go

package runs
import (
json "encoding/json/v2"
"errors"
"fmt"
"strings"
)
// planDocument is a plan written as structured fields. The text every surface
// reads (the approval message and the next workflow's mission) and the
// dashboard's layout are both rendered from it, so what the approver approves
// is what the next workflow is handed.
type planDocument struct {
Title string `json:"title"`
Problem string `json:"problem"`
Solution string `json:"solution"`
Files []planFile `json:"files,omitempty"`
Acceptance []string `json:"acceptance,omitempty"`
NonGoals []string `json:"non_goals,omitempty"`
DiffEstimate string `json:"diff_estimate,omitempty"`
Risks []string `json:"risks,omitempty"`
}
// planFile is one file a plan adds, modifies or deletes.
type planFile struct {
Path string `json:"path"`
Change string `json:"change"`
Note string `json:"note,omitempty"`
}
// readPlanDocument decodes a rendered document setting. A value that is not an
// object, or an empty one (an absent reference renders empty), is no document.
func readPlanDocument(value any) (*planDocument, error) {
if fields, ok := value.(map[string]any); !ok || len(fields) == 0 {
return nil, nil //nolint:nilnil // no document is not an error
}
data, err := json.Marshal(value)
if err != nil {
return nil, err
}
var doc planDocument
if err := json.Unmarshal(data, &doc); err != nil {
return nil, fmt.Errorf("document: %w", err)
}
if strings.TrimSpace(doc.Problem) == "" || strings.TrimSpace(doc.Solution) == "" {
return nil, errors.New("document needs a problem and a solution")
}
return &doc, nil
}
// markdown is the plan as text.
func (d planDocument) markdown() string {
var b strings.Builder
if d.Title != "" {
fmt.Fprintf(&b, "# %s\n\n", d.Title)
}
fmt.Fprintf(&b, "## Problem\n\n%s\n\n## Proposed solution\n\n%s\n\n", strings.TrimSpace(d.Problem), strings.TrimSpace(d.Solution))
if len(d.Files) > 0 {
b.WriteString("## Files\n\n")
for _, f := range d.Files {
fmt.Fprintf(&b, "- `%s` (%s)", f.Path, f.Change)
if f.Note != "" {
fmt.Fprintf(&b, ": %s", f.Note)
}
b.WriteString("\n")
}
b.WriteString("\n")
}
list := func(heading string, items []string) {
if len(items) == 0 {
return
}
fmt.Fprintf(&b, "## %s\n\n", heading)
for _, item := range items {
fmt.Fprintf(&b, "- %s\n", item)
}
b.WriteString("\n")
}
list("Acceptance criteria", d.Acceptance)
list("Non-goals", d.NonGoals)
if d.DiffEstimate != "" {
fmt.Fprintf(&b, "## Estimated diff\n\n%s\n\n", d.DiffEstimate)
}
list("Risks", d.Risks)
return strings.TrimSpace(b.String())
}