X Tutup
Skip to content

Commit 723e9e3

Browse files
committed
Address PR comments
1 parent 155507d commit 723e9e3

File tree

4 files changed

+52
-54
lines changed

4 files changed

+52
-54
lines changed

api/queries_comments.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ func CommentsForPullRequest(client *Client, repo ghrepo.Interface, pr *PullReque
101101
return &Comments{Nodes: comments, TotalCount: len(comments)}, nil
102102
}
103103

104-
func CommentCreate(client *Client, repoHost string, params map[string]string) (string, error) {
104+
type CommentCreateInput struct {
105+
Body string
106+
SubjectId string
107+
}
108+
109+
func CommentCreate(client *Client, repoHost string, params CommentCreateInput) (string, error) {
105110
var mutation struct {
106111
AddComment struct {
107112
CommentEdge struct {
@@ -114,8 +119,8 @@ func CommentCreate(client *Client, repoHost string, params map[string]string) (s
114119

115120
variables := map[string]interface{}{
116121
"input": githubv4.AddCommentInput{
117-
Body: githubv4.String(params["body"]),
118-
SubjectID: graphql.ID(params["subjectId"]),
122+
Body: githubv4.String(params.Body),
123+
SubjectID: graphql.ID(params.SubjectId),
119124
},
120125
}
121126

pkg/cmd/issue/comment/comment.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ type CommentOptions struct {
2323
IO *iostreams.IOStreams
2424
BaseRepo func() (ghrepo.Interface, error)
2525
EditSurvey func() (string, error)
26-
InputTypeSurvey func() (int, error)
26+
InputTypeSurvey func() (inputType, error)
2727
ConfirmSubmitSurvey func() (bool, error)
2828
OpenInBrowser func(string) error
2929

3030
SelectorArg string
3131
Interactive bool
32-
InputType int
32+
InputType inputType
3333
Body string
3434
}
3535

36+
type inputType int
37+
3638
const (
37-
editor = iota
38-
web
39-
inline
39+
inputTypeEditor inputType = iota
40+
inputTypeInline
41+
inputTypeWeb
4042
)
4143

4244
func NewCmdComment(f *cmdutil.Factory, runF func(*CommentOptions) error) *cobra.Command {
@@ -66,15 +68,15 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*CommentOptions) error) *cobra.
6668

6769
inputFlags := 0
6870
if cmd.Flags().Changed("body") {
69-
opts.InputType = inline
71+
opts.InputType = inputTypeInline
7072
inputFlags++
7173
}
7274
if webMode {
73-
opts.InputType = web
75+
opts.InputType = inputTypeWeb
7476
inputFlags++
7577
}
7678
if editorMode {
77-
opts.InputType = editor
79+
opts.InputType = inputTypeEditor
7880
inputFlags++
7981
}
8082

@@ -84,7 +86,7 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*CommentOptions) error) *cobra.
8486
}
8587
opts.Interactive = true
8688
} else if inputFlags == 1 {
87-
if !opts.IO.CanPrompt() && opts.InputType == editor {
89+
if !opts.IO.CanPrompt() && opts.InputType == inputTypeEditor {
8890
return &cmdutil.FlagError{Err: errors.New("--body or --web required when not running interactively")}
8991
}
9092
} else if inputFlags > 1 {
@@ -126,24 +128,18 @@ func commentRun(opts *CommentOptions) error {
126128
}
127129

128130
switch opts.InputType {
129-
case web:
131+
case inputTypeWeb:
130132
openURL := issue.URL + "#issuecomment-new"
131133
if opts.IO.IsStdoutTTY() {
132134
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
133135
}
134136
return opts.OpenInBrowser(openURL)
135-
case editor:
137+
case inputTypeEditor:
136138
body, err := opts.EditSurvey()
137139
if err != nil {
138140
return err
139141
}
140142
opts.Body = body
141-
if opts.Interactive {
142-
cs := opts.IO.ColorScheme()
143-
fmt.Fprint(opts.IO.Out, cs.GreenBold("?"))
144-
fmt.Fprint(opts.IO.Out, cs.Bold(" Body "))
145-
fmt.Fprint(opts.IO.Out, cs.Cyan("<Received>\n"))
146-
}
147143
}
148144

149145
if opts.Interactive {
@@ -156,10 +152,7 @@ func commentRun(opts *CommentOptions) error {
156152
}
157153
}
158154

159-
params := map[string]string{
160-
"subjectId": issue.ID,
161-
"body": opts.Body,
162-
}
155+
params := api.CommentCreateInput{Body: opts.Body, SubjectId: issue.ID}
163156
url, err := api.CommentCreate(apiClient, baseRepo.RepoHost(), params)
164157
if err != nil {
165158
return err
@@ -168,14 +161,22 @@ func commentRun(opts *CommentOptions) error {
168161
return nil
169162
}
170163

171-
var inputTypeSurvey = func() (int, error) {
172-
var inputType int
164+
var inputTypeSurvey = func() (inputType, error) {
165+
var result int
173166
inputTypeQuestion := &survey.Select{
174167
Message: "Where do you want to draft your comment?",
175168
Options: []string{"Editor", "Web"},
176169
}
177-
err := survey.AskOne(inputTypeQuestion, &inputType)
178-
return inputType, err
170+
err := survey.AskOne(inputTypeQuestion, &result)
171+
if err != nil {
172+
return 0, err
173+
}
174+
175+
if result == 0 {
176+
return inputTypeEditor, nil
177+
} else {
178+
return inputTypeWeb, nil
179+
}
179180
}
180181

181182
var confirmSubmitSurvey = func() (bool, error) {

pkg/cmd/issue/comment/comment_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestNewCmdComment(t *testing.T) {
5454
output: CommentOptions{
5555
SelectorArg: "1",
5656
Interactive: false,
57-
InputType: inline,
57+
InputType: inputTypeInline,
5858
Body: "test",
5959
},
6060
wantsErr: false,
@@ -65,7 +65,7 @@ func TestNewCmdComment(t *testing.T) {
6565
output: CommentOptions{
6666
SelectorArg: "1",
6767
Interactive: false,
68-
InputType: editor,
68+
InputType: inputTypeEditor,
6969
Body: "",
7070
},
7171
wantsErr: false,
@@ -76,7 +76,7 @@ func TestNewCmdComment(t *testing.T) {
7676
output: CommentOptions{
7777
SelectorArg: "1",
7878
Interactive: false,
79-
InputType: web,
79+
InputType: inputTypeWeb,
8080
Body: "",
8181
},
8282
wantsErr: false,
@@ -164,7 +164,7 @@ func Test_commentRun(t *testing.T) {
164164
InputType: 0,
165165
Body: "",
166166

167-
InputTypeSurvey: func() (int, error) { return web, nil },
167+
InputTypeSurvey: func() (inputType, error) { return inputTypeWeb, nil },
168168
OpenInBrowser: func(string) error { return nil },
169169
},
170170
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
@@ -181,21 +181,21 @@ func Test_commentRun(t *testing.T) {
181181
Body: "",
182182

183183
EditSurvey: func() (string, error) { return "comment body", nil },
184-
InputTypeSurvey: func() (int, error) { return editor, nil },
184+
InputTypeSurvey: func() (inputType, error) { return inputTypeEditor, nil },
185185
ConfirmSubmitSurvey: func() (bool, error) { return true, nil },
186186
},
187187
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
188188
mockIssueFromNumber(t, reg)
189189
mockCommentCreate(t, reg)
190190
},
191-
stdout: "? Body <Received>\nhttps://github.com/OWNER/REPO/issues/123#issuecomment-456\n",
191+
stdout: "https://github.com/OWNER/REPO/issues/123#issuecomment-456\n",
192192
},
193193
{
194194
name: "non-interactive web",
195195
input: &CommentOptions{
196196
SelectorArg: "123",
197197
Interactive: false,
198-
InputType: web,
198+
InputType: inputTypeWeb,
199199
Body: "",
200200

201201
OpenInBrowser: func(string) error { return nil },
@@ -210,7 +210,7 @@ func Test_commentRun(t *testing.T) {
210210
input: &CommentOptions{
211211
SelectorArg: "123",
212212
Interactive: false,
213-
InputType: editor,
213+
InputType: inputTypeEditor,
214214
Body: "",
215215

216216
EditSurvey: func() (string, error) { return "comment body", nil },
@@ -226,7 +226,7 @@ func Test_commentRun(t *testing.T) {
226226
input: &CommentOptions{
227227
SelectorArg: "123",
228228
Interactive: false,
229-
InputType: inline,
229+
InputType: inputTypeInline,
230230
Body: "comment body",
231231
},
232232
httpStubs: func(t *testing.T, reg *httpmock.Registry) {

pkg/iostreams/color.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ import (
99
)
1010

1111
var (
12-
magenta = ansi.ColorFunc("magenta")
13-
cyan = ansi.ColorFunc("cyan")
14-
red = ansi.ColorFunc("red")
15-
yellow = ansi.ColorFunc("yellow")
16-
blue = ansi.ColorFunc("blue")
17-
green = ansi.ColorFunc("green")
18-
gray = ansi.ColorFunc("black+h")
19-
bold = ansi.ColorFunc("default+b")
20-
cyanBold = ansi.ColorFunc("cyan+b")
21-
greenBold = ansi.ColorFunc("green+b")
12+
magenta = ansi.ColorFunc("magenta")
13+
cyan = ansi.ColorFunc("cyan")
14+
red = ansi.ColorFunc("red")
15+
yellow = ansi.ColorFunc("yellow")
16+
blue = ansi.ColorFunc("blue")
17+
green = ansi.ColorFunc("green")
18+
gray = ansi.ColorFunc("black+h")
19+
bold = ansi.ColorFunc("default+b")
20+
cyanBold = ansi.ColorFunc("cyan+b")
2221

2322
gray256 = func(t string) string {
2423
return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[m", 38, 242, t)
@@ -85,13 +84,6 @@ func (c *ColorScheme) Green(t string) string {
8584
return green(t)
8685
}
8786

88-
func (c *ColorScheme) GreenBold(t string) string {
89-
if !c.enabled {
90-
return t
91-
}
92-
return greenBold(t)
93-
}
94-
9587
func (c *ColorScheme) Gray(t string) string {
9688
if !c.enabled {
9789
return t

0 commit comments

Comments
 (0)
X Tutup