Skip to content

Commit 1710098

Browse files
committed
stack existing PRs from submit TUI
1 parent 7797931 commit 1710098

9 files changed

Lines changed: 231 additions & 18 deletions

File tree

‎cmd/submit.go‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ and draft each PR's title, description, and draft state, then submit them all at
4040
once with Ctrl+S. Pass --auto (or run in a non-interactive terminal) to skip the
4141
editor and use auto-generated titles.
4242
43+
If your branches already have open PRs but no stack on GitHub yet (for example,
44+
after deleting the stack) and you deselect every new PR, press Ctrl+B (or click
45+
the "STACK N PRs" button) to link the existing open PRs into a stack.
46+
4347
This command performs several steps:
4448
1. Pushes all branches to the remote
4549
2. Creates new PRs for the included branches
@@ -202,7 +206,8 @@ func runSubmit(cfg *config.Config, opts *submitOptions) error {
202206
// auto-generated titles and bodies (today's behavior).
203207
var drafts map[string]*submitview.PRDraft
204208
if cfg.IsInteractive() && !opts.auto {
205-
collected, cancelled, tuiErr := collectPRDrafts(cfg, client, s, currentBranch, prDetails, templateContent)
209+
canCreateStack := stacksAvailable && s.ID == ""
210+
collected, cancelled, tuiErr := collectPRDrafts(cfg, client, s, currentBranch, prDetails, templateContent, canCreateStack)
206211
if tuiErr != nil {
207212
cfg.Errorf("failed to run the submit editor: %s", tuiErr)
208213
return ErrSilent
@@ -263,7 +268,7 @@ func runSubmit(cfg *config.Config, opts *submitOptions) error {
263268
// returns the per-branch overrides, whether the user cancelled, and any error.
264269
// When the stack contains no branches without a PR, it skips the TUI and
265270
// returns nil drafts so the normal push/relink path runs.
266-
func collectPRDrafts(cfg *config.Config, client github.ClientOps, s *stack.Stack, currentBranch string, prDetails map[string]*github.PRDetails, templateContent string) (map[string]*submitview.PRDraft, bool, error) {
271+
func collectPRDrafts(cfg *config.Config, client github.ClientOps, s *stack.Stack, currentBranch string, prDetails map[string]*github.PRDetails, templateContent string, canCreateStack bool) (map[string]*submitview.PRDraft, bool, error) {
267272
// Fill in the real title/description for existing PRs that were synced
268273
// without them (e.g. merged branches) so the read-only cards show API data.
269274
enrichPRContent(client, prDetails)
@@ -290,10 +295,11 @@ func collectPRDrafts(cfg *config.Config, client github.ClientOps, s *stack.Stack
290295
}
291296

292297
model := submitview.New(submitview.Options{
293-
Nodes: nodes,
294-
Trunk: s.Trunk,
295-
RepoLabel: repoLabel,
296-
Version: Version,
298+
Nodes: nodes,
299+
Trunk: s.Trunk,
300+
RepoLabel: repoLabel,
301+
Version: Version,
302+
CanCreateStack: canCreateStack,
297303
})
298304

299305
// Use cell-motion mouse mode (clicks, drag, and wheel) rather than all-motion.

‎cmd/submit_tui_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestCollectPRDrafts_SkipsWhenNoNewBranches(t *testing.T) {
3636
"b2": {Number: 2, State: "OPEN"},
3737
}
3838

39-
drafts, cancelled, err := collectPRDrafts(cfg, nil, s, "b1", prDetails, "")
39+
drafts, cancelled, err := collectPRDrafts(cfg, nil, s, "b1", prDetails, "", false)
4040
require.NoError(t, err)
4141
assert.False(t, cancelled)
4242
assert.Nil(t, drafts, "no NEW branches means the TUI is skipped and drafts are nil")

‎internal/tui/submitview/data.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ func CountSelected(nodes []SubmitNode) int {
123123
return n
124124
}
125125

126+
// CountOpenPRs returns the number of branches that already have an open pull
127+
// request (open or draft). These are the PRs that can be linked into a stack via
128+
// the "STACK N PRs" action. Queued, merged, and closed PRs are excluded because
129+
// they cannot form a new stack.
130+
func CountOpenPRs(nodes []SubmitNode) int {
131+
n := 0
132+
for _, node := range nodes {
133+
if node.State == StateOpen || node.State == StateDraft {
134+
n++
135+
}
136+
}
137+
return n
138+
}
139+
126140
// HasClosed reports whether any branch in the list has a closed PR, which blocks
127141
// the stack and triggers the closed-branch callout.
128142
func HasClosed(nodes []SubmitNode) bool {

‎internal/tui/submitview/help.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var helpSections = []helpSection{
5353
heading: "Existing PRs",
5454
entries: []helpEntry{
5555
{"^o", "open the focused branch's PR on the web"},
56+
{"^b", "link the existing open PRs into a stack (when there's no stack on GitHub)"},
5657
},
5758
},
5859
{

‎internal/tui/submitview/model.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ type Options struct {
4545
RepoLabel string
4646
// Version is the CLI version string.
4747
Version string
48+
// CanCreateStack reports that the local stack has no remote stack object yet
49+
// but one could be created (stacked PRs are available on the repo). When
50+
// true, and once the user has deselected all new PRs, the TUI offers a
51+
// "STACK N PRs" action to link the existing open PRs into a stack.
52+
CanCreateStack bool
4853
}
4954

5055
// Model is the Bubble Tea model backing the interactive `gh stack submit` TUI.
@@ -54,6 +59,10 @@ type Model struct {
5459
repoLabel string
5560
version string
5661

62+
// canCreateStack mirrors Options.CanCreateStack: the local stack has no
63+
// remote stack object yet, but one could be created.
64+
canCreateStack bool
65+
5766
cursor int // index into nodes (the focused branch)
5867

5968
width, height int
@@ -139,6 +148,8 @@ func New(opts Options) Model {
139148
version: opts.Version,
140149
cursor: cursor,
141150

151+
canCreateStack: opts.CanCreateStack,
152+
142153
titleArea: tia,
143154
descArea: ta,
144155
focusedField: fieldTitle,
@@ -263,6 +274,15 @@ func (m Model) anyEdited() bool {
263274
return false
264275
}
265276

277+
// canStackExistingPRs reports whether the "STACK N PRs" action should be offered:
278+
// the local stack has no remote stack object yet, the user has deselected every
279+
// new PR, and there are at least two existing open PRs to link into a stack.
280+
func (m Model) canStackExistingPRs() bool {
281+
return m.canCreateStack &&
282+
CountSelected(m.nodes) == 0 &&
283+
CountOpenPRs(m.nodes) >= 2
284+
}
285+
266286
// quit marks the session cancelled and exits. If the user has unsaved edits, it
267287
// first raises a discard-edits confirmation instead of quitting immediately.
268288
func (m Model) quit() (tea.Model, tea.Cmd) {

‎internal/tui/submitview/mouse.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ func (m Model) leftCheckboxHit(x, y int) bool {
137137
return x >= leftW-5 && x < leftW-2
138138
}
139139

140+
// leftStackButtonHit reports whether (x,y) lands on the bottom-left "STACK N PRs"
141+
// button, when it is shown. The button occupies the last reserved row of the
142+
// left panel (one blank separator row sits above it).
143+
func (m Model) leftStackButtonHit(x, y int) bool {
144+
if !m.canStackExistingPRs() {
145+
return false
146+
}
147+
leftW, _ := m.panelWidths()
148+
if x < 0 || x >= leftW {
149+
return false
150+
}
151+
if y-m.panelTopRow() != m.leftVisibleHeight()+1 {
152+
return false
153+
}
154+
return x < lipgloss.Width(m.renderStackButton(leftW-2))
155+
}
156+
140157
// handleClick routes a left click to a branch row (left map) or an editor
141158
// element (right panel).
142159
func (m Model) handleClick(x, y int) (tea.Model, tea.Cmd) {
@@ -145,6 +162,12 @@ func (m Model) handleClick(x, y int) (tea.Model, tea.Cmd) {
145162

146163
leftW, rightW := m.panelWidths()
147164

165+
// Bottom-left STACK button: link the existing open PRs into a stack.
166+
if m.leftStackButtonHit(x, y) {
167+
m.saveEditor()
168+
return m.requestSubmit()
169+
}
170+
148171
// Left panel: focus a branch, toggling include when its checkbox is clicked.
149172
if idx := m.branchRowAt(x, y); idx != -1 {
150173
onCheckbox := m.leftCheckboxHit(x, y) && m.nodes[idx].State == StateNew

‎internal/tui/submitview/render.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,19 @@ func (m Model) buildHeaderConfig() shared.HeaderConfig {
8181
}
8282

8383
// headerShortcuts returns the six primary single-screen keyboard shortcuts shown
84-
// in the header (the help overlay lists the full set).
84+
// in the header (the help overlay lists the full set). When the STACK action is
85+
// offered (no stack yet and every new PR deselected), the submit hint is swapped
86+
// for the "^b stack PRs" action, which is the relevant control in that state.
8587
func (m Model) headerShortcuts() []shared.ShortcutEntry {
88+
primary := shared.ShortcutEntry{Key: "^s", Desc: "submit PRs"}
89+
if m.canStackExistingPRs() {
90+
primary = shared.ShortcutEntry{Key: "^b", Desc: "stack PRs"}
91+
}
8692
return []shared.ShortcutEntry{
8793
{Key: "↑↓", Desc: "select branch"},
8894
{Key: "tab", Desc: "cycle field"},
8995
{Key: "^x", Desc: "skip/include"},
90-
{Key: "^s", Desc: "submit PRs"},
96+
primary,
9197
{Key: "^h", Desc: "help"},
9298
{Key: "esc", Desc: "quit"},
9399
}

‎internal/tui/submitview/screen.go‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import (
1313

1414
// updateScreen handles all key input on the single submit screen.
1515
func (m Model) updateScreen(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
16+
// Ctrl+B links the existing open PRs into a stack. It is only intercepted
17+
// while the STACK action is offered; otherwise it falls through so the
18+
// editor's textarea keeps Ctrl+B (move cursor back) while editing.
19+
if msg.Type == tea.KeyCtrlB && m.canStackExistingPRs() {
20+
m.saveEditor()
21+
return m.requestSubmit()
22+
}
23+
1624
// Global keys, handled regardless of focus.
1725
switch msg.Type {
1826
case tea.KeyCtrlC:
@@ -688,7 +696,8 @@ func (m Model) renderLeftPanel(width, height int) string {
688696
fullW = 6
689697
}
690698
rows := m.buildLeftRows(fullW)
691-
visH := height - 2
699+
buttonRows := m.leftButtonRows()
700+
visH := height - 2 - buttonRows
692701
if visH < 1 {
693702
visH = 1
694703
}
@@ -698,14 +707,36 @@ func (m Model) renderLeftPanel(width, height int) string {
698707
end = len(rows)
699708
}
700709

701-
var b strings.Builder
702-
for i, r := range rows[scroll:end] {
703-
if i > 0 {
704-
b.WriteString("\n")
705-
}
706-
b.WriteString(r.text)
710+
lines := make([]string, 0, visH+buttonRows)
711+
for _, r := range rows[scroll:end] {
712+
lines = append(lines, r.text)
713+
}
714+
// Pad the scroll area to its full height so the button sits at the bottom.
715+
for len(lines) < visH {
716+
lines = append(lines, "")
707717
}
708-
return leftPanelBox(b.String(), width, height)
718+
if buttonRows > 0 {
719+
lines = append(lines, "", m.renderStackButton(fullW))
720+
}
721+
return leftPanelBox(strings.Join(lines, "\n"), width, height)
722+
}
723+
724+
// leftButtonRows returns the number of inner rows reserved at the bottom of the
725+
// left panel for the "STACK N PRs" button (a blank separator plus the button),
726+
// or 0 when the button is not shown.
727+
func (m Model) leftButtonRows() int {
728+
if m.canStackExistingPRs() {
729+
return 2
730+
}
731+
return 0
732+
}
733+
734+
// renderStackButton renders the bottom-left "(^b) STACK N PRs" action, styled
735+
// like the right-panel SUBMIT button. The keyboard hint sits to the left.
736+
func (m Model) renderStackButton(fullW int) string {
737+
n := CountOpenPRs(m.nodes)
738+
label := hintStyle.Render("(^b) ") + submitButtonStyle.Render(fmt.Sprintf("STACK %d PRs", n))
739+
return pad(1, false) + label
709740
}
710741

711742
// leftPanelBox frames the left panel with the shared rounded border but no inner
@@ -892,7 +923,7 @@ func pad(n int, focused bool) string {
892923

893924
// leftVisibleHeight is the number of timeline rows the left panel can show.
894925
func (m Model) leftVisibleHeight() int {
895-
h := m.contentHeight() - 2 // panel border
926+
h := m.contentHeight() - 2 - m.leftButtonRows() // panel border + reserved button rows
896927
if h < 1 {
897928
h = 1
898929
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package submitview
2+
3+
import (
4+
"testing"
5+
6+
tea "github.com/charmbracelet/bubbletea"
7+
"github.com/github/gh-stack/internal/stack"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// deselectedNew builds a NEW branch node that the user has deselected.
12+
func deselectedNew(branch string) SubmitNode {
13+
n := newNode(branch, StateNew)
14+
n.Included = false
15+
return n
16+
}
17+
18+
// stackModel builds a sized model with the given CanCreateStack flag.
19+
func stackModel(t *testing.T, nodes []SubmitNode, canCreateStack bool) Model {
20+
t.Helper()
21+
m := New(Options{
22+
Nodes: nodes,
23+
Trunk: stack.BranchRef{Branch: "main"},
24+
RepoLabel: "myorg/myrepo",
25+
Version: "1.0.0",
26+
CanCreateStack: canCreateStack,
27+
})
28+
m.openURL = func(string) {}
29+
updated, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 40})
30+
return updated.(Model)
31+
}
32+
33+
func TestCountOpenPRs(t *testing.T) {
34+
nodes := []SubmitNode{
35+
newNode("a", StateNew),
36+
newNode("b", StateOpen),
37+
newNode("c", StateDraft),
38+
newNode("d", StateQueued),
39+
newNode("e", StateMerged),
40+
newNode("f", StateClosed),
41+
}
42+
assert.Equal(t, 2, CountOpenPRs(nodes), "only open and draft PRs count")
43+
}
44+
45+
func TestCanStackExistingPRs(t *testing.T) {
46+
twoOpen := []SubmitNode{newNode("a", StateOpen), newNode("b", StateDraft)}
47+
tests := []struct {
48+
name string
49+
nodes []SubmitNode
50+
canCreateStack bool
51+
want bool
52+
}{
53+
{"two open PRs, no stack, none selected", twoOpen, true, true},
54+
{"deselected NEW + two open PRs", []SubmitNode{deselectedNew("n"), newNode("a", StateOpen), newNode("b", StateDraft)}, true, true},
55+
{"has a remote stack already", twoOpen, false, false},
56+
{"a NEW branch is still selected", []SubmitNode{newNode("n", StateNew), newNode("a", StateOpen), newNode("b", StateDraft)}, true, false},
57+
{"only one open PR", []SubmitNode{newNode("a", StateOpen)}, true, false},
58+
{"open PRs but queued/merged do not count", []SubmitNode{newNode("a", StateOpen), newNode("b", StateQueued), newNode("c", StateMerged)}, true, false},
59+
}
60+
for _, tt := range tests {
61+
t.Run(tt.name, func(t *testing.T) {
62+
m := stackModel(t, tt.nodes, tt.canCreateStack)
63+
assert.Equal(t, tt.want, m.canStackExistingPRs())
64+
})
65+
}
66+
}
67+
68+
func TestStackButton_RendersWhenEligible(t *testing.T) {
69+
m := stackModel(t, []SubmitNode{deselectedNew("feat/new"), newNode("a", StateOpen), newNode("b", StateDraft)}, true)
70+
view := m.View()
71+
assert.Contains(t, view, "STACK 2 PRs")
72+
assert.Contains(t, view, "(^b)")
73+
}
74+
75+
func TestStackButton_HiddenWhenNotEligible(t *testing.T) {
76+
// Has a remote stack already.
77+
withStack := stackModel(t, []SubmitNode{newNode("a", StateOpen), newNode("b", StateDraft)}, false)
78+
assert.NotContains(t, withStack.View(), "STACK 2 PRs")
79+
assert.NotContains(t, withStack.View(), "(^b)")
80+
81+
// A NEW branch is still selected for creation.
82+
newSelected := stackModel(t, []SubmitNode{newNode("n", StateNew), newNode("a", StateOpen), newNode("b", StateDraft)}, true)
83+
assert.NotContains(t, newSelected.View(), "STACK 2 PRs")
84+
assert.NotContains(t, newSelected.View(), "(^b)")
85+
}
86+
87+
func TestStackButton_HeaderHintSwaps(t *testing.T) {
88+
eligible := stackModel(t, []SubmitNode{deselectedNew("feat/new"), newNode("a", StateOpen), newNode("b", StateDraft)}, true)
89+
view := eligible.View()
90+
assert.Contains(t, view, "stack PRs", "header shows the stack hint when eligible")
91+
assert.NotContains(t, view, "submit PRs", "the submit hint is swapped out in stack mode")
92+
}
93+
94+
func TestCtrlB_TriggersSubmitWhenEligible(t *testing.T) {
95+
m := stackModel(t, []SubmitNode{deselectedNew("feat/new"), newNode("a", StateOpen), newNode("b", StateDraft)}, true)
96+
m = sendKey(t, m, tea.KeyMsg{Type: tea.KeyCtrlB})
97+
assert.True(t, m.SubmitRequested(), "Ctrl+B requests submit when the STACK action is offered")
98+
}
99+
100+
func TestCtrlB_NoOpWhenNotEligible(t *testing.T) {
101+
// A NEW branch is included (editing context) — Ctrl+B must not submit.
102+
m := stackModel(t, []SubmitNode{newNode("n", StateNew), newNode("a", StateOpen)}, true)
103+
m = sendKey(t, m, tea.KeyMsg{Type: tea.KeyCtrlB})
104+
assert.False(t, m.SubmitRequested(), "Ctrl+B does not submit when the STACK action is not offered")
105+
}
106+
107+
func TestStackButton_MouseClickTriggersSubmit(t *testing.T) {
108+
m := stackModel(t, []SubmitNode{deselectedNew("feat/new"), newNode("a", StateOpen), newNode("b", StateDraft)}, true)
109+
y := m.panelTopRow() + m.leftVisibleHeight() + 1 // the button's reserved bottom row
110+
updated, _ := m.Update(tea.MouseMsg{Action: tea.MouseActionPress, Button: tea.MouseButtonLeft, X: 2, Y: y})
111+
assert.True(t, updated.(Model).SubmitRequested(), "clicking the STACK button requests submit")
112+
}

0 commit comments

Comments
 (0)