mirror of
https://gitea.com/gitea/tea.git
synced 2026-07-16 11:07:39 +02:00
12947f068a
Closes #1042. ### What `tea issue create`, `tea issue edit` and `tea pr create` all take the body via `-d` / `--description`, but `tea comments add` and `tea comments edit` only accepted it positionally — passing `-d` errored with `flag provided but not defined: -d`. This adds `-d` / `--description` to both `comments` subcommands so every body-bearing command shares one flag. ### Behaviour / precedence Unchanged for existing usage. Body resolution is now: 1. positional argument (kept first for back-compat), 2. `-d` / `--description`, 3. piped stdin, 4. `$EDITOR` (interactive only). ### Testing - `go build`, `go vet ./...`, `go test -short ./cmd/... ./modules/...` all pass. - Manually verified `tea comments add --help` / `edit --help` list the flag, and that `-d` resolves the body (reaches the API, no parse error, no editor prompt, no stdin block). --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/1043 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Brien Coffield <coffbr01@gmail.com> Co-committed-by: Brien Coffield <coffbr01@gmail.com>
97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package comments
|
|
|
|
import (
|
|
stdctx "context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
gitea "gitea.dev/sdk"
|
|
|
|
"gitea.dev/tea/cmd/flags"
|
|
"gitea.dev/tea/modules/config"
|
|
"gitea.dev/tea/modules/context"
|
|
"gitea.dev/tea/modules/interact"
|
|
"gitea.dev/tea/modules/print"
|
|
"gitea.dev/tea/modules/theme"
|
|
"gitea.dev/tea/modules/utils"
|
|
|
|
"charm.land/huh/v2"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// CmdCommentsAdd adds a comment to an issue or pull request.
|
|
var CmdCommentsAdd = cli.Command{
|
|
Name: "add",
|
|
Aliases: []string{"a"},
|
|
Usage: "Add a comment to an issue or pull request",
|
|
Description: "Add a comment to an issue or pull request.",
|
|
ArgsUsage: "<issue / pr index> [<comment body>]",
|
|
Action: RunCommentsAdd,
|
|
Flags: append([]cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "description",
|
|
Aliases: []string{"d"},
|
|
Usage: "comment body (alternative to the positional argument)",
|
|
},
|
|
}, flags.AllDefaultFlags...),
|
|
}
|
|
|
|
// RunCommentsAdd creates a new comment.
|
|
func RunCommentsAdd(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().Len() == 0 {
|
|
return fmt.Errorf("please specify issue / pr index")
|
|
}
|
|
|
|
idx, err := utils.ArgToIndex(ctx.Args().First())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
stdinPiped := interact.IsStdinPiped()
|
|
body, err := resolveBody(strings.Join(ctx.Args().Tail(), " "), ctx.String("description"), stdinPiped, ctx.Reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(body) == 0 && !stdinPiped {
|
|
if err := huh.NewForm(
|
|
huh.NewGroup(
|
|
huh.NewText().
|
|
Title("Comment(markdown):").
|
|
ExternalEditor(config.GetPreferences().Editor).
|
|
EditorExtension("md").
|
|
Value(&body),
|
|
),
|
|
).WithTheme(theme.GetTheme()).
|
|
Run(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if len(body) == 0 {
|
|
return errors.New("no comment content provided")
|
|
}
|
|
|
|
client := ctx.Login.Client()
|
|
comment, _, err := client.Issues.CreateIssueComment(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.CreateIssueCommentOption{
|
|
Body: body,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
print.Comment(comment)
|
|
return nil
|
|
}
|