Skip to content
Merged

dev #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,14 @@ var createCmd = &cobra.Command{

path := filepath.Join(openDir(root), filename)

if err := issue.Write(path, iss); err != nil {
return err
var writeErr error
if openEditor {
writeErr = issue.WriteTemplate(path, iss)
} else {
writeErr = issue.Write(path, iss)
}
if writeErr != nil {
return writeErr
}

if openEditor {
Expand Down
8 changes: 6 additions & 2 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ func injectStdin(t *testing.T, content string) {
if _, err := w.WriteString(content); err != nil {
t.Fatal(err)
}
w.Close()
if err := w.Close(); err != nil {
t.Fatal(err)
}
old := os.Stdin
os.Stdin = r
t.Cleanup(func() {
os.Stdin = old
r.Close()
if err := r.Close(); err != nil {
t.Error(err)
}
})
}

Expand Down
47 changes: 47 additions & 0 deletions internal/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ func Write(path string, iss *Issue) error {
return os.WriteFile(path, buf.Bytes(), 0644)
}

// WriteTemplate writes an issue file with all user-editable fields expanded
// (even when empty) and a comment marking where the body begins. Used when
// opening a new issue in the editor so the user sees the full schema.
func WriteTemplate(path string, iss *Issue) error {
type tpl struct {
Title string `yaml:"title"`
State string `yaml:"state"`
Labels []string `yaml:"labels"`
Assignees []string `yaml:"assignees"`
Milestone string `yaml:"milestone"`
}
labels := iss.Labels
if labels == nil {
labels = []string{}
}
assignees := iss.Assignees
if assignees == nil {
assignees = []string{}
}
t := tpl{
Title: iss.Title,
State: iss.State,
Labels: labels,
Assignees: assignees,
Milestone: iss.Milestone,
}
var buf bytes.Buffer
buf.WriteString("---\n")
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
if err := enc.Encode(t); err != nil {
return err
}
_ = enc.Close()
buf.WriteString("# Body goes below this line\n")
buf.WriteString("---\n")
if iss.Body != "" {
buf.WriteString("\n")
body := iss.Body
if !strings.HasSuffix(body, "\n") {
body += "\n"
}
buf.WriteString(body)
}
return os.WriteFile(path, buf.Bytes(), 0644)
}

func Filename(iss *Issue) string {
return fmt.Sprintf("%d-%s.md", iss.Number, Slug(iss.Title))
}
58 changes: 58 additions & 0 deletions internal/issue/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package issue
import (
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -209,6 +210,63 @@ func TestWriteParse(t *testing.T) {
}
}

func TestWriteTemplate(t *testing.T) {
t.Run("empty issue shows all fields and separator", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "issue.md")
iss := &Issue{State: "open"}
if err := WriteTemplate(path, iss); err != nil {
t.Fatalf("WriteTemplate: %v", err)
}
raw, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
content := string(raw)
for _, want := range []string{"title:", "state:", "labels:", "assignees:", "milestone:", "# Body goes below this line"} {
if !strings.Contains(content, want) {
t.Errorf("template missing %q\ngot:\n%s", want, content)
}
}
})

t.Run("parses back with empty body", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "issue.md")
iss := &Issue{Title: "Draft", State: "open"}
if err := WriteTemplate(path, iss); err != nil {
t.Fatalf("WriteTemplate: %v", err)
}
got, err := Parse(path)
if err != nil {
t.Fatalf("Parse: %v", err)
}
if got.Title != "Draft" {
t.Errorf("Title = %q, want %q", got.Title, "Draft")
}
if got.Body != "" {
t.Errorf("Body = %q, want empty", got.Body)
}
})

t.Run("preserves populated fields", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "issue.md")
iss := &Issue{
Title: "My Issue",
State: "open",
Labels: []string{"bug", "urgent"},
Assignees: []string{"alice"},
Milestone: "v1.0",
}
if err := WriteTemplate(path, iss); err != nil {
t.Fatalf("WriteTemplate: %v", err)
}
got, err := Parse(path)
if err != nil {
t.Fatalf("Parse: %v", err)
}
checkIssue(t, got, iss)
})
}

func TestFilename(t *testing.T) {
tests := []struct {
name string
Expand Down