feat(comments): add list/edit/delete subcommands to tea comment (#1015)

## Why

Today `tea comment` can only *add* a comment. Editing or deleting requires falling back to `tea api`. This came up while I was iterating on PRs in this same repo earlier today and had to correct a couple of comments by hand. Every comparable forge CLI (gh, glab, etc.) exposes these operations as first-class commands.

## What

Restructures `tea comment` from a single-action command into a parent with four subcommands. The parent's default action remains the existing "add" behavior, so the historical shorthand keeps working.

| Command | Purpose |
|---|---|
| `tea comment add <idx> [<body>]` | Add a comment (explicit subcommand) |
| `tea comment list <idx>` | Tabular listing including comment IDs |
| `tea comment edit <id> [<body>]` | Replace the body of one comment |
| `tea comment delete <id> [<id>...]` | Delete one or more comments |
| `tea comment <idx> [<body>]` | Unchanged — still routes to `add` |

The `list` command exists specifically so users can discover the IDs that `edit` and `delete` accept.

## Backward compatibility

The whole point of routing the parent's default `Action` through `add` is to preserve every existing invocation. `tea comment 1 "body"` still does what it did before. No flag or arg names change.

## Input forms (for add and edit)

Same pattern as the original `tea comment`:

1. Positional body (`tea comment edit <id> "new body"`) — wins if present.
2. Piped stdin if no positional body is given.
3. External `$EDITOR` (pre-populated with the current body, on `edit`) if neither.

This matches the stdin-handling fix in #1011 — positional body wins over a non-TTY stdin so the command doesn't hang in CI/subshells.

## Verification

All four subcommands were exercised live against `https://gitea.com/dinsmoor/tea-testing` issue #1. The test artifacts and a summary log are visible on that issue right now. Specifically:

- The annotated summary comment lists every operation tested and the comment IDs each one acted on.
- Comments 1197162 (legacy add), 1197163 (subcommand add, later edited), 1197164 (stdin add) are still there to be inspected.
- Comment 1197166 was created and then deleted; its absence from `tea comment list` output is evidence that delete works.

## New files

- `cmd/comments/add.go` — extracted from the old `cmd/comment.go`
- `cmd/comments/list.go`
- `cmd/comments/edit.go`
- `cmd/comments/delete.go`
- `modules/print/comment.go` — adds `CommentsList` helper for the tabular output

`cmd/comment.go` is rewritten as a thin parent that wires these together.

## Open questions for the reviewer

- **Naming**: should the top-level command be `comments` (plural) or stay `comment` (singular)? I kept it singular with `comments` as an alias to match the existing user-visible name.
- **Delete confirmation**: I did not add a confirmation prompt — `delete` just deletes. Some projects gate this behind `--yes` / interactive `[y/N]`. I'd rather follow whatever convention the maintainers prefer.
- **Output format on list**: currently uses the existing `print.tableWithHeader` helper, matching `tea organizations list` etc. Other tea listings support `--output json` / `--output csv` via the shared `--output` flag, which works here automatically through the same helper.

---

This patch was authored interactively with an AI assistant, driven and reviewed by a human (Tyler / @dinsmoor) every step.

*pull request created by Tyler's lovingly wrangled demon machine <3*

Reviewed-on: https://gitea.com/gitea/tea/pulls/1015
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: dinsmoor <204368+dinsmoor@noreply.gitea.com>
Co-committed-by: dinsmoor <204368+dinsmoor@noreply.gitea.com>
This commit is contained in:
dinsmoor
2026-05-31 22:20:48 +00:00
committed by Lunny Xiao
parent 5fa24b9a65
commit 09fc09c2f7
9 changed files with 558 additions and 92 deletions
+97
View File
@@ -0,0 +1,97 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package comments
import (
stdctx "context"
"errors"
"fmt"
"io"
"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: 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
}
body := strings.Join(ctx.Args().Tail(), " ")
// Only consume stdin if no positional body was given. interact.IsStdinPiped()
// is true for any non-TTY stdin (CI, subshells, agent harnesses) — not just
// piped data — so reading unconditionally would block forever in those
// contexts when the body is supplied via args.
if len(body) == 0 && interact.IsStdinPiped() {
if bodyStdin, err := io.ReadAll(ctx.Reader); err != nil {
return err
} else if len(bodyStdin) != 0 {
body = string(bodyStdin)
}
} else if len(body) == 0 {
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
}
+55
View File
@@ -0,0 +1,55 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package comments
import (
stdctx "context"
"fmt"
"gitea.dev/tea/cmd/flags"
"gitea.dev/tea/modules/context"
"gitea.dev/tea/modules/utils"
"github.com/urfave/cli/v3"
)
// CmdCommentsDelete deletes one or more comments by ID.
var CmdCommentsDelete = cli.Command{
Name: "delete",
Aliases: []string{"rm"},
Usage: "Delete one or more comments by ID",
Description: "Delete one or more comments by their comment ID. Use 'tea comments list <issue>' to find IDs.",
ArgsUsage: "<comment id> [<comment id>...]",
Action: RunCommentsDelete,
Flags: flags.AllDefaultFlags,
}
// RunCommentsDelete removes one or more comments.
func RunCommentsDelete(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 at least one comment id")
}
ids, err := utils.ArgsToIndices(ctx.Args().Slice())
if err != nil {
return fmt.Errorf("invalid comment id: %s", err)
}
client := ctx.Login.Client()
for _, id := range ids {
if _, err := client.Issues.DeleteIssueComment(requestCtx, ctx.Owner, ctx.Repo, id); err != nil {
return fmt.Errorf("could not delete comment %d: %s", id, err)
}
fmt.Printf("Deleted comment %d\n", id)
}
return nil
}
+102
View File
@@ -0,0 +1,102 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package comments
import (
stdctx "context"
"errors"
"fmt"
"io"
"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"
)
// CmdCommentsEdit edits an existing comment.
var CmdCommentsEdit = cli.Command{
Name: "edit",
Aliases: []string{"e"},
Usage: "Edit the body of an existing comment",
Description: `Edit the body of an existing comment by its comment ID. Use 'tea comments list <issue>' to find IDs.
The new body can be supplied as a positional argument, piped on stdin, or (if neither is given and stdin is a terminal) entered in your $EDITOR.`,
ArgsUsage: "<comment id> [<new body>]",
Action: RunCommentsEdit,
Flags: flags.AllDefaultFlags,
}
// RunCommentsEdit updates the body of an existing comment.
func RunCommentsEdit(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 comment id")
}
id, err := utils.ArgToIndex(ctx.Args().First())
if err != nil {
return fmt.Errorf("invalid comment id %q: %s", ctx.Args().First(), err)
}
body := strings.Join(ctx.Args().Tail(), " ")
if len(body) == 0 && interact.IsStdinPiped() {
if bodyStdin, err := io.ReadAll(ctx.Reader); err != nil {
return err
} else if len(bodyStdin) != 0 {
body = string(bodyStdin)
}
} else if len(body) == 0 {
// Fetch current body to pre-populate the editor.
client := ctx.Login.Client()
current, _, fetchErr := client.Issues.GetIssueComment(requestCtx, ctx.Owner, ctx.Repo, id)
if fetchErr != nil {
return fmt.Errorf("could not fetch comment %d: %s", id, fetchErr)
}
body = current.Body
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.EditIssueComment(requestCtx, ctx.Owner, ctx.Repo, id, gitea.EditIssueCommentOption{
Body: body,
})
if err != nil {
return err
}
print.Comment(comment)
return nil
}
+62
View File
@@ -0,0 +1,62 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package comments
import (
stdctx "context"
"fmt"
gitea "gitea.dev/sdk"
"gitea.dev/tea/cmd/flags"
"gitea.dev/tea/modules/context"
"gitea.dev/tea/modules/print"
"gitea.dev/tea/modules/utils"
"github.com/urfave/cli/v3"
)
// CmdCommentsList lists comments on an issue or pull request.
var CmdCommentsList = cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "List comments on an issue or pull request",
Description: "List comments on an issue or pull request. Comment IDs returned here are the IDs accepted by 'tea comments edit' and 'tea comments delete'.",
ArgsUsage: "<issue / pr index>",
Action: RunCommentsList,
Flags: append([]cli.Flag{
&flags.PaginationPageFlag,
&flags.PaginationLimitFlag,
}, flags.AllDefaultFlags...),
}
// RunCommentsList lists comments on the given issue/PR.
func RunCommentsList(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
}
client := ctx.Login.Client()
comments, _, err := client.Issues.ListIssueComments(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.ListIssueCommentOptions{
ListOptions: flags.GetListOptions(cmd),
})
if err != nil {
return err
}
return print.CommentsList(comments, ctx.Output)
}