package workflow import ( "encoding/json" "fmt" "strings" ) // PlanDocument is a plan written as structured fields. The text every surface // reads (the approval message, the forge, 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"` } // planDocument 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 planDocument(value any) (*PlanDocument, error) { if fields, ok := value.(map[string]any); !ok || len(fields) == 0 { return nil, nil } 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, fmt.Errorf("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()) }