Skip to content

Commit 836121b

Browse files
committed
Add submitview data model and PR draft override plumbing
Introduce the internal/tui/submitview package that will back the new interactive `gh stack submit` TUI, and wire its per-PR override contract into the submit command without changing current behavior. - submitview: BranchState model (NEW/OPEN/DRAFT/QUEUED/MERGED/CLOSED) with selectability/editability rules, SubmitNode UI state with edit detection, PRDraft override type, state derivation, title/description prefill, and state-badge/panel/tab styles. - submit: refactor ensurePR/createPR to accept an optional per-branch override map (title/body/draft/include); deselected NEW branches are pushed but get no PR. The override map is nil on the --auto / non-interactive path, so the agent-compat contract is unchanged. Fully unit tested.
1 parent 6f6cefa commit 836121b

8 files changed

Lines changed: 1030 additions & 28 deletions

File tree

‎cmd/submit.go‎

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/github/gh-stack/internal/modify"
1515
"github.com/github/gh-stack/internal/pr"
1616
"github.com/github/gh-stack/internal/stack"
17+
"github.com/github/gh-stack/internal/tui/submitview"
1718
"github.com/spf13/cobra"
1819
)
1920

@@ -181,6 +182,12 @@ func runSubmit(cfg *config.Config, opts *submitOptions) error {
181182
// Push each branch and create/update its PR in stack order (bottom to top).
182183
// Sequential pushing ensures each branch's base is up-to-date on the
183184
// remote before the next branch is pushed, preventing race conditions.
185+
//
186+
// drafts carries per-PR overrides from the interactive editor. It is nil on
187+
// the --auto / non-interactive path, in which case ensurePR/createPR fall
188+
// back to auto-generated titles and bodies (today's behavior).
189+
var drafts map[string]*submitview.PRDraft
190+
184191
cfg.Printf("Pushing to %s...", remote)
185192
for i, b := range s.Branches {
186193
if s.Branches[i].IsMerged() || s.Branches[i].IsQueued() {
@@ -195,7 +202,7 @@ func runSubmit(cfg *config.Config, opts *submitOptions) error {
195202

196203
// Find or create PR, and fix base if needed
197204
baseBranch := s.ActiveBaseBranch(b.Branch)
198-
if err := ensurePR(cfg, client, s, i, baseBranch, opts, templateContent); err != nil {
205+
if err := ensurePR(cfg, client, s, i, baseBranch, opts, templateContent, drafts); err != nil {
199206
if errors.Is(err, errInterrupt) {
200207
printInterrupt(cfg)
201208
return ErrSilent
@@ -225,7 +232,11 @@ func runSubmit(cfg *config.Config, opts *submitOptions) error {
225232
// ensurePR finds or creates a PR for the branch at index i, and updates
226233
// its base branch if needed. This is the single place where PR state is
227234
// reconciled during submit.
228-
func ensurePR(cfg *config.Config, client github.ClientOps, s *stack.Stack, i int, baseBranch string, opts *submitOptions, templateContent string) error {
235+
//
236+
// drafts holds optional per-branch overrides from the interactive editor. When
237+
// a NEW branch has been deselected in the editor, it is pushed for stack
238+
// consistency but no PR is created for it.
239+
func ensurePR(cfg *config.Config, client github.ClientOps, s *stack.Stack, i int, baseBranch string, opts *submitOptions, templateContent string, drafts map[string]*submitview.PRDraft) error {
229240
b := s.Branches[i]
230241

231242
pr, err := client.FindPRForBranch(b.Branch)
@@ -235,7 +246,12 @@ func ensurePR(cfg *config.Config, client github.ClientOps, s *stack.Stack, i int
235246
}
236247

237248
if pr == nil {
238-
return createPR(cfg, client, s, i, baseBranch, opts, templateContent)
249+
// A NEW branch the user deselected in the editor: pushed for stack
250+
// consistency, but intentionally left without a PR.
251+
if d := drafts[b.Branch]; d != nil && !d.Include {
252+
return nil
253+
}
254+
return createPR(cfg, client, s, i, baseBranch, opts, templateContent, drafts)
239255
}
240256

241257
// PR exists — record it and fix base if needed.
@@ -292,30 +308,52 @@ func ensurePR(cfg *config.Config, client github.ClientOps, s *stack.Stack, i int
292308
}
293309

294310
// createPR creates a new PR for the branch at index i.
295-
func createPR(cfg *config.Config, client github.ClientOps, s *stack.Stack, i int, baseBranch string, opts *submitOptions, templateContent string) error {
311+
//
312+
// When the interactive editor has supplied a draft override for this branch
313+
// (drafts[branch] != nil), its title, body, and draft state are used verbatim
314+
// — the attribution footer is appended via generatePRBody. Otherwise the
315+
// auto-generated title/body path (with an optional line prompt in interactive
316+
// mode) is used, preserving today's --auto / non-interactive behavior.
317+
func createPR(cfg *config.Config, client github.ClientOps, s *stack.Stack, i int, baseBranch string, opts *submitOptions, templateContent string, drafts map[string]*submitview.PRDraft) error {
296318
b := s.Branches[i]
297319

298-
title, commitBody := defaultPRTitleBody(baseBranch, b.Branch)
299-
originalTitle := title
300-
if !opts.auto && cfg.IsInteractive() {
301-
input, err := inputWithPrefill(cfg, fmt.Sprintf("Title for PR (branch %s):", b.Branch), title)
302-
if err != nil {
303-
if isInterruptError(err) {
304-
return errInterrupt
320+
var title, body string
321+
isDraft := !opts.open
322+
323+
if d := drafts[b.Branch]; d != nil {
324+
// Interactive editor override. The user already edited the description
325+
// in the TUI (prefilled from the repo template when one exists), so
326+
// d.Body is the final body. Pass no template so generatePRBody keeps the
327+
// user's text and only appends the attribution footer, rather than
328+
// discarding their edits in favor of the raw template.
329+
title = d.Title
330+
body = generatePRBody(d.Body, "")
331+
isDraft = d.Draft
332+
} else {
333+
// Auto / non-interactive default path.
334+
var commitBody string
335+
title, commitBody = defaultPRTitleBody(baseBranch, b.Branch)
336+
originalTitle := title
337+
if !opts.auto && cfg.IsInteractive() {
338+
input, err := inputWithPrefill(cfg, fmt.Sprintf("Title for PR (branch %s):", b.Branch), title)
339+
if err != nil {
340+
if isInterruptError(err) {
341+
return errInterrupt
342+
}
343+
// Non-interrupt error: keep the auto-generated title.
344+
} else if input != "" {
345+
title = input
305346
}
306-
// Non-interrupt error: keep the auto-generated title.
307-
} else if input != "" {
308-
title = input
309347
}
310-
}
311348

312-
prBody := commitBody
313-
if title != originalTitle && commitBody != "" {
314-
prBody = originalTitle + "\n\n" + commitBody
349+
prBody := commitBody
350+
if title != originalTitle && commitBody != "" {
351+
prBody = originalTitle + "\n\n" + commitBody
352+
}
353+
body = generatePRBody(prBody, templateContent)
315354
}
316-
body := generatePRBody(prBody, templateContent)
317355

318-
newPR, createErr := client.CreatePR(baseBranch, b.Branch, title, body, !opts.open)
356+
newPR, createErr := client.CreatePR(baseBranch, b.Branch, title, body, isDraft)
319357
if createErr != nil {
320358
cfg.Warningf("failed to create PR for %s: %v", b.Branch, createErr)
321359
return nil

‎cmd/submit_test.go‎

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/github/gh-stack/internal/github"
1616
"github.com/github/gh-stack/internal/modify"
1717
"github.com/github/gh-stack/internal/stack"
18+
"github.com/github/gh-stack/internal/tui/submitview"
1819
"github.com/stretchr/testify/assert"
1920
"github.com/stretchr/testify/require"
2021
)
@@ -187,7 +188,7 @@ func TestSubmit_DefaultDraft(t *testing.T) {
187188

188189
cfg, _, _ := config.NewTestConfig()
189190
cfg.GitHubClientOverride = &github.MockClient{
190-
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
191+
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
191192
FindPRForBranchFn: func(string) (*github.PullRequest, error) { return nil, nil },
192193
CreatePRFn: func(base, head, title, body string, draft bool) (*github.PullRequest, error) {
193194
createdDraft = draft
@@ -229,7 +230,7 @@ func TestSubmit_OpenFlag(t *testing.T) {
229230

230231
cfg, _, _ := config.NewTestConfig()
231232
cfg.GitHubClientOverride = &github.MockClient{
232-
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
233+
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
233234
FindPRForBranchFn: func(string) (*github.PullRequest, error) { return nil, nil },
234235
CreatePRFn: func(base, head, title, body string, draft bool) (*github.PullRequest, error) {
235236
createdDraft = draft
@@ -1649,7 +1650,7 @@ func TestSubmit_UsesPRTemplate(t *testing.T) {
16491650

16501651
cfg, _, _ := config.NewTestConfig()
16511652
cfg.GitHubClientOverride = &github.MockClient{
1652-
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
1653+
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
16531654
FindPRForBranchFn: func(string) (*github.PullRequest, error) { return nil, nil },
16541655
CreatePRFn: func(base, head, title, body string, draft bool) (*github.PullRequest, error) {
16551656
capturedBody = body
@@ -1696,7 +1697,7 @@ func TestSubmit_NoTemplate_UsesFooter(t *testing.T) {
16961697

16971698
cfg, _, _ := config.NewTestConfig()
16981699
cfg.GitHubClientOverride = &github.MockClient{
1699-
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
1700+
ListStacksFn: func() ([]github.RemoteStack, error) { return nil, nil },
17001701
FindPRForBranchFn: func(string) (*github.PullRequest, error) { return nil, nil },
17011702
CreatePRFn: func(base, head, title, body string, draft bool) (*github.PullRequest, error) {
17021703
capturedBody = body
@@ -1835,8 +1836,8 @@ func TestSubmit_DisablesAutoMergeOnExistingPR(t *testing.T) {
18351836
case "b2":
18361837
return &github.PullRequest{
18371838
Number: 20, ID: "PR_20",
1838-
URL: "https://github.com/owner/repo/pull/20",
1839-
BaseRefName: "b1", HeadRefName: "b2",
1839+
URL: "https://github.com/owner/repo/pull/20",
1840+
BaseRefName: "b1", HeadRefName: "b2",
18401841
AutoMergeRequest: &github.AutoMergeRequest{EnabledAt: "2024-01-01T00:00:00Z"},
18411842
}, nil
18421843
}
@@ -1890,8 +1891,8 @@ func TestSubmit_DisableAutoMergeFailure_ContinuesWithWarning(t *testing.T) {
18901891
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
18911892
return &github.PullRequest{
18921893
Number: 10, ID: "PR_10",
1893-
URL: "https://github.com/owner/repo/pull/10",
1894-
BaseRefName: "main", HeadRefName: "b1",
1894+
URL: "https://github.com/owner/repo/pull/10",
1895+
BaseRefName: "main", HeadRefName: "b1",
18951896
AutoMergeRequest: &github.AutoMergeRequest{EnabledAt: "2024-01-01T00:00:00Z"},
18961897
}, nil
18971898
},
@@ -1963,3 +1964,89 @@ func TestSubmit_NoAutoMerge_SkipsDisable(t *testing.T) {
19631964

19641965
assert.NoError(t, err)
19651966
}
1967+
1968+
// --- Per-PR draft override plumbing (interactive editor contract) ---
1969+
1970+
func TestCreatePR_UsesDraftOverride(t *testing.T) {
1971+
s := &stack.Stack{
1972+
Trunk: stack.BranchRef{Branch: "main"},
1973+
Branches: []stack.BranchRef{{Branch: "b1"}},
1974+
}
1975+
1976+
var gotTitle, gotBody string
1977+
var gotDraft bool
1978+
cfg, _, _ := config.NewTestConfig()
1979+
client := &github.MockClient{
1980+
CreatePRFn: func(base, head, title, body string, draft bool) (*github.PullRequest, error) {
1981+
gotTitle, gotBody, gotDraft = title, body, draft
1982+
return &github.PullRequest{Number: 7, ID: "PR_7", URL: "https://github.com/o/r/pull/7"}, nil
1983+
},
1984+
}
1985+
1986+
drafts := map[string]*submitview.PRDraft{
1987+
"b1": {Branch: "b1", Include: true, Title: "Custom title", Body: "Custom body", Draft: true},
1988+
}
1989+
1990+
// --open would normally force ready; the override's Draft must win.
1991+
err := createPR(cfg, client, s, 0, "main", &submitOptions{open: true}, "", drafts)
1992+
require.NoError(t, err)
1993+
1994+
assert.Equal(t, "Custom title", gotTitle)
1995+
assert.Contains(t, gotBody, "Custom body")
1996+
assert.Contains(t, gotBody, "GitHub Stacks CLI", "footer is appended at submit time")
1997+
assert.True(t, gotDraft, "draft override should be honored over --open")
1998+
require.NotNil(t, s.Branches[0].PullRequest)
1999+
assert.Equal(t, 7, s.Branches[0].PullRequest.Number)
2000+
}
2001+
2002+
func TestCreatePR_DraftOverride_KeepsUserBodyOverTemplate(t *testing.T) {
2003+
s := &stack.Stack{
2004+
Trunk: stack.BranchRef{Branch: "main"},
2005+
Branches: []stack.BranchRef{{Branch: "b1"}},
2006+
}
2007+
2008+
var gotBody string
2009+
cfg, _, _ := config.NewTestConfig()
2010+
client := &github.MockClient{
2011+
CreatePRFn: func(base, head, title, body string, draft bool) (*github.PullRequest, error) {
2012+
gotBody = body
2013+
return &github.PullRequest{Number: 1, ID: "PR_1"}, nil
2014+
},
2015+
}
2016+
// The user edited the description in the TUI; the repo also has a template.
2017+
// The user's edits must win — the template was only the prefill.
2018+
drafts := map[string]*submitview.PRDraft{
2019+
"b1": {Branch: "b1", Include: true, Title: "T", Body: "My edited description"},
2020+
}
2021+
2022+
err := createPR(cfg, client, s, 0, "main", &submitOptions{}, "## Raw repo template", drafts)
2023+
require.NoError(t, err)
2024+
2025+
assert.Contains(t, gotBody, "My edited description", "the user's edited body is used")
2026+
assert.NotContains(t, gotBody, "Raw repo template", "the raw template does not override the user's edits")
2027+
assert.Contains(t, gotBody, "GitHub Stacks CLI", "the attribution footer is appended")
2028+
}
2029+
2030+
func TestEnsurePR_DeselectedNewBranchSkipsCreate(t *testing.T) {
2031+
s := &stack.Stack{
2032+
Trunk: stack.BranchRef{Branch: "main"},
2033+
Branches: []stack.BranchRef{{Branch: "b1"}},
2034+
}
2035+
2036+
cfg, _, _ := config.NewTestConfig()
2037+
client := &github.MockClient{
2038+
FindPRForBranchFn: func(string) (*github.PullRequest, error) { return nil, nil },
2039+
CreatePRFn: func(string, string, string, string, bool) (*github.PullRequest, error) {
2040+
t.Fatal("CreatePR must not be called for a deselected NEW branch")
2041+
return nil, nil
2042+
},
2043+
}
2044+
2045+
drafts := map[string]*submitview.PRDraft{
2046+
"b1": {Branch: "b1", Include: false},
2047+
}
2048+
2049+
err := ensurePR(cfg, client, s, 0, "main", &submitOptions{}, "", drafts)
2050+
require.NoError(t, err)
2051+
assert.Nil(t, s.Branches[0].PullRequest, "no PR should be recorded for a deselected branch")
2052+
}

0 commit comments

Comments
 (0)