gitea.tea/cmd/pulls/review.go
Lunny Xiao a2561f2309 feat: allow non-interactive pull reviews with inline comments
Add --state, --description and --comments-file flags to 'tea pulls review' so reviews can be submitted from scripts and CI. The non-interactive path reuses ParseDiffComments to turn an annotated diff into code comments and submits them through CreatePullReview without opening an editor.

Fixes: https://gitea.com/gitea/tea/issues/1094
Signed-off-by: Lunny Xiao <xiaolunwen@gmail.com>
2026-08-21 09:52:48 -07:00

84 lines
2.3 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package pulls
import (
stdctx "context"
"fmt"
"os"
"gitea.dev/tea/cmd/flags"
"gitea.dev/tea/modules/context"
"gitea.dev/tea/modules/interact"
"gitea.dev/tea/modules/print"
"gitea.dev/tea/modules/utils"
"github.com/urfave/cli/v3"
)
var pullReviewFlags = append([]cli.Flag{
&cli.StringFlag{
Name: "state",
Usage: "Review state: comment, approve or request-changes (enables non-interactive mode)",
},
&cli.StringFlag{
Name: "description",
Aliases: []string{"d"},
Usage: "Concluding review comment (non-interactive). If omitted and stdin is piped, the comment is read from stdin",
},
&cli.StringFlag{
Name: "comments-file",
Usage: "Annotated diff file containing inline code comments (non-interactive)",
},
}, flags.AllDefaultFlags...)
// CmdPullsReview starts an interactive or non-interactive review session
var CmdPullsReview = cli.Command{
Name: "review",
Usage: "Review a pull request",
Description: "Review a pull request interactively, or non-interactively when --state is provided",
ArgsUsage: "<pull index>",
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
ctx, err := context.InitCommand(cmd)
if err != nil {
return err
}
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
return err
}
if !ctx.Args().Present() {
return fmt.Errorf("must specify at least one PR index")
}
if ctx.IsSet("state") {
return runNonInteractivePullReview(requestCtx, ctx, ctx.String("state"), ctx.String("description"), ctx.String("comments-file"))
}
if ctx.IsSet("description") || ctx.IsSet("comments-file") {
return fmt.Errorf("--state is required for non-interactive review")
}
// This command is intentionally interactive. Fail early in CI / non-TTY
// contexts rather than hanging on prompts.
if os.Getenv("CI") != "" || !print.IsInteractive() || interact.IsStdinPiped() {
return fmt.Errorf("pull review requires an interactive terminal; use --state for a non-interactive review")
}
for _, arg := range ctx.Args().Slice() {
idx, err := utils.ArgToIndex(arg)
if err != nil {
return err
}
if err := interact.ReviewPull(requestCtx, ctx, idx); err != nil && !interact.IsQuitting(err) {
return err
}
}
return nil
},
Flags: pullReviewFlags,
}