MCPcopy Create free account
hub / github.com/cli/cli / createComment

Function createComment

pkg/cmd/pr/shared/commentable.go:212–268  ·  view source on GitHub ↗
(commentable Commentable, opts *CommentableOptions, uploader *attachments.Uploader)

Source from the content-addressed store, hash-verified

210}
211
212func createComment(commentable Commentable, opts *CommentableOptions, uploader *attachments.Uploader) error {
213 switch opts.InputType {
214 case InputTypeWeb:
215 openURL := commentable.Link() + "#issuecomment-new"
216 if opts.IO.IsStdoutTTY() && !opts.Quiet {
217 fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(openURL))
218 }
219 return opts.OpenInBrowser(openURL)
220 case InputTypeEditor:
221 var body string
222 var err error
223 if opts.Interactive {
224 body, err = opts.InteractiveEditSurvey("")
225 } else {
226 body, err = opts.EditSurvey("")
227 }
228 if err != nil {
229 return err
230 }
231 opts.Body = body
232 }
233
234 if opts.Interactive {
235 cont, err := opts.ConfirmSubmitSurvey()
236 if err != nil {
237 return err
238 }
239 if !cont {
240 return errors.New("Discarding...")
241 }
242 }
243
244 httpClient, err := opts.HttpClient()
245 if err != nil {
246 return err
247 }
248
249 body, write, uploadErr := bodyForWrite(opts, uploader)
250 if !write {
251 return fmt.Errorf("%w\nno comment was posted", uploadErr)
252 }
253
254 apiClient := api.NewClientFromHTTP(httpClient)
255 params := api.CommentCreateInput{Body: body, SubjectId: commentable.Identifier()}
256 url, err := api.CommentCreate(apiClient, opts.Host, params)
257 if err != nil {
258 // Upload first: an uploaded asset outlives the failed write, and the
259 // user needs to know it exists before hearing the comment did not save.
260 return errors.Join(uploadErr, err)
261 }
262
263 if !opts.Quiet {
264 fmt.Fprintln(opts.IO.Out, url)
265 }
266
267 return uploadErr
268}
269

Callers 1

CommentableRunFunction · 0.85

Calls 9

DisplayURLFunction · 0.92
NewClientFromHTTPFunction · 0.92
CommentCreateFunction · 0.92
bodyForWriteFunction · 0.85
IsStdoutTTYMethod · 0.80
JoinMethod · 0.80
LinkMethod · 0.65
ErrorfMethod · 0.65
IdentifierMethod · 0.65

Tested by

no test coverage detected