Skip to content

Commit 83fcece

Browse files
authored
Merge pull request cli#3658 from chemotaxis/fix-pr-issue-body
Handle default body text when creating issues and pull requests
2 parents b166376 + c2c691f commit 83fcece

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/cli/oauth v0.8.0
1212
github.com/cli/safeexec v1.0.0
1313
github.com/cpuguy83/go-md2man/v2 v2.0.0
14+
github.com/creack/pty v1.1.13
1415
github.com/gabriel-vasile/mimetype v1.1.2
1516
github.com/google/go-cmp v0.5.2
1617
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
6262
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
6363
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
6464
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
65+
github.com/creack/pty v1.1.13 h1:rTPnd/xocYRjutMfqide2zle1u96upp1gm6eUHKi7us=
66+
github.com/creack/pty v1.1.13/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
6567
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
6668
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
6769
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

pkg/surveyext/editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int
105105
}
106106
if r == '\r' || r == '\n' {
107107
if e.BlankAllowed {
108-
return "", nil
108+
return initialValue, nil
109109
} else {
110110
continue
111111
}

pkg/surveyext/editor_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package surveyext
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"strings"
9+
"sync"
10+
"testing"
11+
"time"
12+
13+
"github.com/AlecAivazis/survey/v2"
14+
"github.com/AlecAivazis/survey/v2/terminal"
15+
pseudotty "github.com/creack/pty"
16+
"github.com/stretchr/testify/assert"
17+
"github.com/stretchr/testify/require"
18+
)
19+
20+
func Test_GhEditor_Prompt(t *testing.T) {
21+
e := &GhEditor{
22+
BlankAllowed: true,
23+
EditorCommand: "false",
24+
Editor: &survey.Editor{
25+
Message: "Body",
26+
FileName: "*.md",
27+
Default: "initial value",
28+
HideDefault: true,
29+
AppendDefault: true,
30+
},
31+
}
32+
33+
pty, tty, err := pseudotty.Open()
34+
if errors.Is(err, pseudotty.ErrUnsupported) {
35+
return
36+
}
37+
require.NoError(t, err)
38+
defer pty.Close()
39+
defer tty.Close()
40+
41+
err = pseudotty.Setsize(tty, &pseudotty.Winsize{Cols: 72, Rows: 30})
42+
require.NoError(t, err)
43+
44+
out := teeWriter{File: tty}
45+
e.WithStdio(terminal.Stdio{
46+
In: tty,
47+
Out: &out,
48+
Err: tty,
49+
})
50+
51+
var res string
52+
errc := make(chan error)
53+
54+
go func() {
55+
r, err := e.Prompt(defaultPromptConfig())
56+
if r != nil {
57+
res = r.(string)
58+
}
59+
errc <- err
60+
}()
61+
62+
time.Sleep(5 * time.Millisecond)
63+
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch false, enter to skip] \x1b[0m\x1b[?25l", out.String())
64+
fmt.Fprint(pty, "\n") // send Enter key
65+
66+
err = <-errc
67+
assert.NoError(t, err)
68+
assert.Equal(t, "initial value", res)
69+
assert.Equal(t, "\x1b[?25h", out.String())
70+
}
71+
72+
// survey doesn't expose this
73+
func defaultPromptConfig() *survey.PromptConfig {
74+
return &survey.PromptConfig{
75+
PageSize: 7,
76+
HelpInput: "?",
77+
SuggestInput: "tab",
78+
Icons: survey.IconSet{
79+
Error: survey.Icon{
80+
Text: "X",
81+
Format: "red",
82+
},
83+
Help: survey.Icon{
84+
Text: "?",
85+
Format: "cyan",
86+
},
87+
Question: survey.Icon{
88+
Text: "?",
89+
Format: "green+hb",
90+
},
91+
MarkedOption: survey.Icon{
92+
Text: "[x]",
93+
Format: "green",
94+
},
95+
UnmarkedOption: survey.Icon{
96+
Text: "[ ]",
97+
Format: "default+hb",
98+
},
99+
SelectFocus: survey.Icon{
100+
Text: ">",
101+
Format: "cyan+b",
102+
},
103+
},
104+
Filter: func(filter string, value string, index int) (include bool) {
105+
filter = strings.ToLower(filter)
106+
return strings.Contains(strings.ToLower(value), filter)
107+
},
108+
KeepFilter: false,
109+
}
110+
}
111+
112+
// teeWriter is a writer that duplicates all writes to a file into a buffer
113+
type teeWriter struct {
114+
*os.File
115+
buf bytes.Buffer
116+
mu sync.Mutex
117+
}
118+
119+
func (f *teeWriter) Write(p []byte) (n int, err error) {
120+
f.mu.Lock()
121+
defer f.mu.Unlock()
122+
_, _ = f.buf.Write(p)
123+
return f.File.Write(p)
124+
}
125+
126+
func (f *teeWriter) String() string {
127+
f.mu.Lock()
128+
s := f.buf.String()
129+
f.buf.Reset()
130+
f.mu.Unlock()
131+
return s
132+
}

0 commit comments

Comments
 (0)