@@ -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