Prompt for unnamed drafts directly before publication

This commit is contained in:
2026-10-10 01:46:08 +01:00
parent 68042aad5d
commit 6bc344dd29
5 changed files with 148 additions and 9 deletions
+4 -2
View File
@@ -85,8 +85,10 @@ Press `d` on the dashboard or use `:drafts` to browse the same retained drafts a
`pipelines drafts`. Use `n/p` to select, `j/k` to scroll source, `e`/Enter to reopen `pipelines drafts`. Use `n/p` to select, `j/k` to scroll source, `e`/Enter to reopen
the existing draft, `v` for server validation, and `s` for submission confirmation. the existing draft, `v` for server validation, and `s` for submission confirmation.
`r` refreshes the list. Submission preserves create-only/version guards and rejects `r` refreshes the list. Submission preserves create-only/version guards and rejects
drafts belonging to another server. An unnamed draft must be named through OpenCode drafts belonging to another server. For unnamed drafts, submission asks you to
or CLI `submit --name`. Draft selection/editor completion never publishes it. type a pipeline name directly in the TUI. Enter saves the name in retained metadata
and opens publication confirmation; it does not publish yet. Esc keeps the draft
without saving unfinished name input. Draft selection/editor completion never publishes it.
On editor exit, the connected TUI checks the authoritative server before asking On editor exit, the connected TUI checks the authoritative server before asking
to submit. If the draft source is already published (including a newer version to submit. If the draft source is already published (including a newer version
after the draft's base that was subsequently superseded), it skips confirmation after the draft's base that was subsequently superseded), it skips confirmation
+39
View File
@@ -116,6 +116,45 @@ func List() ([]Draft, error) {
return items, nil return items, nil
} }
// SetName updates only retained metadata, preserving source and publication scope.
func SetName(path, name string) error {
name = strings.TrimSpace(name)
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
return fmt.Errorf("pipeline name must be 1–200 bytes without line breaks or NUL")
}
d, err := Load(path)
if err != nil {
return err
}
metadata := d.Path + ".json"
info, err := os.Lstat(metadata)
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return fmt.Errorf("draft metadata must be a regular file, not a symlink")
}
d.Name = name
data, err := json.MarshalIndent(d, "", " ")
if err != nil {
return err
}
f, err := os.CreateTemp(filepath.Dir(metadata), ".draft-name-*")
if err != nil {
return err
}
defer os.Remove(f.Name())
_, writeErr := f.Write(data)
closeErr := f.Close()
if writeErr != nil {
return writeErr
}
if closeErr != nil {
return closeErr
}
return os.Rename(f.Name(), metadata)
}
// Delete only a selected, managed cache draft and its sidecar. Never recurse, // Delete only a selected, managed cache draft and its sidecar. Never recurse,
// follow a file symlink, or delete an arbitrary external submission source. // follow a file symlink, or delete an arbitrary external submission source.
func Delete(path string) error { func Delete(path string) error {
+59 -3
View File
@@ -27,7 +27,7 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) {
srv := httptest.NewServer(server.Handler(ctx, store)) srv := httptest.NewServer(server.Handler(ctx, store))
defer srv.Close() defer srv.Close()
url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect" url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect"
d, err := draft.Create("CLI-created draft", pipeline.Template([]string{"worker"}, true), url, true, 0) d, err := draft.Create("", pipeline.Template([]string{"worker"}, true), url, true, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -40,7 +40,7 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) {
m = updated.(Model) m = updated.(Model)
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}}) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}})
m = updated.(Model) m = updated.(Model)
if !m.draftOpen || len(m.draftItems) != 1 || m.draftItems[0].Path != d.Path || !strings.Contains(m.View(), "CLI-created draft") { if !m.draftOpen || len(m.draftItems) != 1 || m.draftItems[0].Path != d.Path || !strings.Contains(m.View(), "Unnamed draft") {
t.Fatal("retained draft not visible") t.Fatal("retained draft not visible")
} }
updated, validate := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'v'}}) updated, validate := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'v'}})
@@ -56,6 +56,22 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) {
if send != nil || !m.confirmOpen || len(items) != 0 { if send != nil || !m.confirmOpen || len(items) != 0 {
t.Fatal("draft selection published without confirmation") t.Fatal("draft selection published without confirmation")
} }
if !strings.Contains(m.View(), "Name this pipeline") {
t.Fatal("missing name prompt")
}
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("CLI-created draft")})
m = updated.(Model)
updated, send = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
items, _ = store.List(ctx)
d.Name = "CLI-created draft"
retained, err := draft.Load(d.Path)
if err != nil || retained.Name != d.Name || retained.Server != url || !retained.CreateOnly || retained.BaseVersion != 0 || send != nil || len(items) != 0 || !m.confirmOpen {
t.Fatal("naming lost metadata or published without confirmation", retained, err)
}
if source, err := draft.Read(d.Path); err != nil || source != pipeline.Template([]string{"worker"}, true) {
t.Fatal("naming changed source", err)
}
updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model) m = updated.(Model)
if send == nil { if send == nil {
@@ -113,6 +129,46 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) {
} }
} }
func TestDraftNamePromptCancelAndInvalidName(t *testing.T) {
t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir())
d, err := draft.Create("", "source", "", true, 0)
if err != nil {
t.Fatal(err)
}
m := New(context.Background(), "ws://this-server")
updated, _ := m.openDrafts()
m = updated.(Model)
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
m = updated.(Model)
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if !m.confirmOpen || m.pendingDraft.name != "" || m.submitNotice == "" {
t.Fatal("blank name accepted")
}
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(strings.Repeat("é", 101))})
m = updated.(Model)
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if cmd != nil || m.pendingDraft.name != "" {
t.Fatal("oversized byte name accepted")
}
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc})
m = updated.(Model)
retained, err := draft.Load(d.Path)
if err != nil || retained.Name != "" || m.confirmOpen || !m.draftOpen {
t.Fatal("cancel modified draft", retained, err)
}
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
m = updated.(Model)
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("new name")})
m = updated.(Model)
updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if cmd != nil || m.pendingDraft.name != "new name" {
t.Fatal("canceled input leaked into next prompt")
}
}
func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) { func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) {
t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir())
d, err := draft.Create("wrong", "source", "ws://another-server", true, 0) d, err := draft.Create("wrong", "source", "ws://another-server", true, 0)
@@ -131,7 +187,7 @@ func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) {
m.confirmOpen = true m.confirmOpen = true
updated, cmd = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) updated, cmd = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model) m = updated.(Model)
if cmd != nil || !strings.Contains(m.submitNotice, "needs a name") { if cmd != nil || !strings.Contains(m.submitNotice, "Name must") || !strings.Contains(m.confirmView(20), "Name this pipeline") {
t.Fatal("unnamed draft submitted") t.Fatal("unnamed draft submitted")
} }
} }
+1
View File
@@ -138,6 +138,7 @@ type Model struct {
mutationTitle string mutationTitle string
mutationCommand func() tea.Cmd mutationCommand func() tea.Cmd
pendingDraft *editedMsg pendingDraft *editedMsg
draftNameInput textinput.Model
submitID string submitID string
submitNotice string submitNotice string
stageIndex int stageIndex int
+45 -4
View File
@@ -277,6 +277,7 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if msg.String() == "esc" { if msg.String() == "esc" {
m.confirmOpen = false m.confirmOpen = false
m.keys.confirm = false m.keys.confirm = false
m.draftNameInput = newInput()
return m, nil return m, nil
} }
if m.submitID != "" { if m.submitID != "" {
@@ -287,6 +288,39 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.keys.confirm = false m.keys.confirm = false
return m, nil return m, nil
} }
if strings.TrimSpace(m.pendingDraft.name) == "" {
if m.draftNameInput.Prompt != "Name: " {
m.draftNameInput = newInput()
m.draftNameInput.Prompt = "Name: "
m.draftNameInput.Placeholder = "Pipeline name"
m.draftNameInput.Focus()
}
if msg.Type == tea.KeyEnter {
name := strings.TrimSpace(m.draftNameInput.Value())
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
m.submitNotice = "Name must be 1–200 bytes without line breaks or NUL."
return m, nil
}
if m.pendingDraft.path != "" {
if err := draft.SetName(m.pendingDraft.path, name); err != nil {
m.submitNotice = "Could not save draft name: " + err.Error()
return m, nil
}
}
m.pendingDraft.name = name
for i := range m.draftItems {
if m.draftItems[i].Path == m.pendingDraft.path {
m.draftItems[i].Name = name
}
}
m.draftNameInput = newInput()
m.submitNotice = ""
return m, nil
}
var cmd tea.Cmd
m.draftNameInput, cmd = m.draftNameInput.Update(msg)
return m, cmd
}
switch msg.String() { switch msg.String() {
case "e": case "e":
m.confirmOpen = false m.confirmOpen = false
@@ -295,10 +329,6 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
return editDraftMode(m.pendingDraft.name, m.pendingDraft.source, true, m.pendingDraft.createOnly, draft.Draft{Path: m.pendingDraft.path, Name: m.pendingDraft.name, Server: m.url, CreateOnly: m.pendingDraft.createOnly, BaseVersion: m.pendingDraft.baseVersion}) return editDraftMode(m.pendingDraft.name, m.pendingDraft.source, true, m.pendingDraft.createOnly, draft.Draft{Path: m.pendingDraft.path, Name: m.pendingDraft.name, Server: m.url, CreateOnly: m.pendingDraft.createOnly, BaseVersion: m.pendingDraft.baseVersion})
}) })
case "s", "enter": case "s", "enter":
if strings.TrimSpace(m.pendingDraft.name) == "" {
m.submitNotice = "Draft needs a name. Press e and ask OpenCode to name it, or submit with CLI --name."
return m, nil
}
if m.conn == nil { if m.conn == nil {
m.submitNotice = "Not connected; draft retained" m.submitNotice = "Not connected; draft retained"
return m, nil return m, nil
@@ -313,6 +343,17 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
} }
func (m Model) confirmView(height int) string { func (m Model) confirmView(height int) string {
if strings.TrimSpace(m.pendingDraft.name) == "" {
field := m.draftNameInput
if field.Prompt != "Name: " {
field = newInput()
field.Prompt = "Name: "
field.Placeholder = "Pipeline name"
field.Focus()
}
field.Width = max(1, min(60, m.width-12))
return popup(m.width, height, 76, []string{"Name this pipeline", "", field.View(), m.submitNotice, "Enter save name and review · Esc keep draft"})
}
lines := []string{"Submit edited pipeline", m.pendingDraft.name, "", "Creates a new immutable version.", "s submit · e edit again · Esc keep draft"} lines := []string{"Submit edited pipeline", m.pendingDraft.name, "", "Creates a new immutable version.", "s submit · e edit again · Esc keep draft"}
if m.pendingDraft.createOnly { if m.pendingDraft.createOnly {
lines[0] = "Create pipeline" lines[0] = "Create pipeline"