mirror of
https://gitea.com/gitea/tea.git
synced 2026-06-05 18:58:43 +02:00
d1b9b7735e
## The bug
`tea comment <idx> "body"` hangs forever in any non-TTY context — CI pipelines, subshells, agent harnesses, scripts — unless the user explicitly redirects stdin with `< /dev/null`.
### Reproduction (released `tea` 0.14.1)
```
# Plain positional body — hangs forever
$ tea comment --repo owner/repo 1 "body text"
# Heredoc body — hangs forever
$ tea comment --repo owner/repo 1 "$(cat <<'EOM'
body text
EOM
)"
# Workaround that works (but shouldn't be necessary)
$ tea comment --repo owner/repo 1 "body text" < /dev/null
```
Verified on `gitea.com` (server 1.26.0+dev) with `tea 0.14.1`. Both hanging cases hit a 30s timeout in testing; in normal shell use they hang indefinitely.
## Root cause
`cmd/comment.go:58-65`:
```go
body := strings.Join(ctx.Args().Tail(), " ")
if interact.IsStdinPiped() {
if bodyStdin, err := io.ReadAll(ctx.Reader); err != nil {
return err
} else if len(bodyStdin) != 0 {
body = strings.Join([]string{body, string(bodyStdin)}, "\n\n")
}
}
```
`interact.IsStdinPiped()` is implemented as `!term.IsTerminal(os.Stdin)` — true for *any* non-TTY stdin, not just for piped data. When tea enters this branch in a subshell where stdin is open but no producer ever writes to it (the typical case for shell scripts and automation), `io.ReadAll` blocks waiting for an EOF that never arrives.
## Fix
Only consume stdin when the user did **not** supply a positional body. If they passed a body via args, that's their content — ignore stdin entirely.
```go
if len(body) == 0 && interact.IsStdinPiped() {
// ... read stdin ...
body = string(bodyStdin)
}
```
## Behavior matrix
| Invocation | Before | After |
|---|---|---|
| `tea comment <idx> "body"` (TTY) | works | works |
| `tea comment <idx> "body"` (non-TTY, no redirect) | **hangs forever** | **works** |
| `tea comment <idx> "body" < /dev/null` | works | works |
| `echo body \| tea comment <idx>` | works | works |
| `tea comment <idx> "prefix"` + piped stdin | "prefix" + stdin concatenated | positional body wins, stdin ignored |
| `tea comment <idx>` (TTY, no body, no pipe) | opens editor | opens editor |
The one behavior change is the prefix-plus-stdin case (5th row). I couldn't find anyone relying on that pattern and it wasn't documented; defaulting to "positional body wins" matches the principle of least surprise.
## Verification
Confirmed each row against `dinsmoor/tea-testing` issue #1 on `gitea.com` with the patched binary. Previously-hanging invocations now post in <0.5s. Piped-stdin path unchanged.
---
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*
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/gitea/tea/pulls/1011
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Tyler <tyler@dinsmoor.us>
Co-committed-by: Tyler <tyler@dinsmoor.us>
101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
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"
|
|
)
|
|
|
|
// CmdAddComment is the main command to operate with notifications
|
|
var CmdAddComment = cli.Command{
|
|
Name: "comment",
|
|
Aliases: []string{"c"},
|
|
Category: catEntities,
|
|
Usage: "Add a comment to an issue / pr",
|
|
Description: "Add a comment to an issue / pr",
|
|
ArgsUsage: "<issue / pr index> [<comment body>]",
|
|
Action: runAddComment,
|
|
Flags: flags.AllDefaultFlags,
|
|
}
|
|
|
|
func runAddComment(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
|
|
}
|
|
|
|
args := ctx.Args()
|
|
if 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() {
|
|
// custom solution until https://github.com/AlecAivazis/survey/issues/328 is fixed
|
|
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
|
|
}
|