2020-12-21 16:07:35 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package interact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"code.gitea.io/tea/modules/context"
|
|
|
|
"code.gitea.io/tea/modules/print"
|
2021-10-14 14:36:08 +00:00
|
|
|
|
2020-12-21 16:07:35 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2023-04-30 03:43:26 +00:00
|
|
|
"golang.org/x/term"
|
2020-12-21 16:07:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ShowCommentsMaybeInteractive fetches & prints comments, depending on the --comments flag.
|
|
|
|
// If that flag is unset, and output is not piped, prompts the user first.
|
|
|
|
func ShowCommentsMaybeInteractive(ctx *context.TeaContext, idx int64, totalComments int) error {
|
|
|
|
if ctx.Bool("comments") {
|
|
|
|
opts := gitea.ListIssueCommentOptions{ListOptions: ctx.GetListOptions()}
|
|
|
|
c := ctx.Login.Client()
|
|
|
|
comments, _, err := c.ListIssueComments(ctx.Owner, ctx.Repo, idx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
print.Comments(comments)
|
2022-09-13 18:35:15 +00:00
|
|
|
} else if print.IsInteractive() && !ctx.IsSet("comments") {
|
2020-12-21 16:07:35 +00:00
|
|
|
// if we're interactive, but --comments hasn't been explicitly set to false
|
|
|
|
if err := ShowCommentsPaginated(ctx, idx, totalComments); err != nil {
|
|
|
|
fmt.Printf("error while loading comments: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowCommentsPaginated prompts if issue/pr comments should be shown and continues to do so.
|
|
|
|
func ShowCommentsPaginated(ctx *context.TeaContext, idx int64, totalComments int) error {
|
|
|
|
c := ctx.Login.Client()
|
|
|
|
opts := gitea.ListIssueCommentOptions{ListOptions: ctx.GetListOptions()}
|
|
|
|
prompt := "show comments?"
|
|
|
|
commentsLoaded := 0
|
|
|
|
|
|
|
|
// paginated fetch
|
|
|
|
// NOTE: as of gitea 1.13, pagination is not provided by this endpoint, but handles
|
|
|
|
// this function gracefully anyways.
|
|
|
|
for {
|
|
|
|
loadComments := false
|
|
|
|
confirm := survey.Confirm{Message: prompt, Default: true}
|
|
|
|
if err := survey.AskOne(&confirm, &loadComments); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !loadComments {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if comments, _, err := c.ListIssueComments(ctx.Owner, ctx.Repo, idx, opts); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(comments) != 0 {
|
|
|
|
print.Comments(comments)
|
|
|
|
commentsLoaded += len(comments)
|
|
|
|
}
|
|
|
|
if commentsLoaded >= totalComments {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
opts.ListOptions.Page++
|
|
|
|
prompt = "load more?"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 17:47:36 +00:00
|
|
|
// IsStdinPiped checks if stdin is piped
|
|
|
|
func IsStdinPiped() bool {
|
2023-04-30 03:43:26 +00:00
|
|
|
return !term.IsTerminal(int(os.Stdin.Fd()))
|
2021-02-28 17:47:36 +00:00
|
|
|
}
|