mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
feat(comment): comment on issues and pull requests the gh way
tea issue comment 12 --body "LGTM" failed with "unknown flag: --body"; the comment had to be created with issue comment create. issue comment and pr comment now take the issue or pull request and -b/-F/-e directly, plus --edit-last and --delete-last for your last comment. The subcommands stay, create is also add, and a misspelled subcommand suggests the right one. Comments are written in the configured editor or a multi-line "Comment (markdown):" field, which edits start from the current text, without a confirmation afterwards. The new comment's URL goes to stdout with a ✓ line; deletes ask "Delete comment 81 on alice/x#1?". comment list shows each comment under a "#79 alice • about 2 hours ago" heading on a terminal and as key/value blocks when piped, and comment JSON gains issue_number. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
7 files changed
+514
-143
No files matched your search
@@ -7,15 +7,22 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCommentConfirmationNamesTheTarget(t *testing.T) {
|
||||
var created []string
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "GET /api/v1/repos/alice/project/issues/3":
|
||||
_, _ = w.Write([]byte(`{"number":3,"title":"Crash"}`))
|
||||
case "POST /api/v1/repos/alice/project/issues/3/comments":
|
||||
_, _ = w.Write([]byte(`{"id":12,"body":"Prompted"}`))
|
||||
created = append(created, payloadField(t, r, "body"))
|
||||
_, _ = w.Write([]byte(`{"id":12,"body":"Prompted","html_url":"https://example.org/alice/project/issues/3#issuecomment-12"}`))
|
||||
case "GET /api/v1/repos/alice/project/issues/comments/12":
|
||||
_, _ = w.Write([]byte(`{"id":12,"body":"Prompted","issue_url":"https://example.org/alice/project/issues/3"}`))
|
||||
case "DELETE /api/v1/repos/alice/project/issues/comments/12":
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
@@ -24,13 +31,22 @@ func TestCommentConfirmationNamesTheTarget(t *testing.T) {
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, _, _ := serverDeps(t, server.URL)
|
||||
deps, out, diagnostics := serverDeps(t, server.URL)
|
||||
deps.Getenv = serverEnv(server.URL, map[string]string{"NO_COLOR": "1"})
|
||||
prompt := &scriptedPrompter{accept: true, input: "Prompted"}
|
||||
deps.Prompter = prompt
|
||||
deps.Streams.InTTY, deps.Streams.ErrTTY = true, true
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"issue", "comment", "create", "3", "-R", "alice/project"}))
|
||||
deps.Streams.InTTY, deps.Streams.ErrTTY, deps.Streams.OutTTY = true, true, true
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"issue", "comment", "3", "-R", "alice/project"}))
|
||||
require.Equal(t, []string{"Prompted"}, created)
|
||||
require.Equal(t, []string{commentTitle}, prompt.asked)
|
||||
require.Equal(t, "https://example.org/alice/project/issues/3#issuecomment-12\n", out.String())
|
||||
require.Equal(t, "✓ Commented on alice/project#3 (Crash)\n", diagnostics.String())
|
||||
diagnostics.Reset()
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"issue", "comment", "delete", "12", "-R", "alice/project"}))
|
||||
require.Equal(t, []string{"Comment on alice/project#3", "Delete comment 12 on alice/project"}, prompt.confirmations)
|
||||
require.Equal(t, []string{"Delete comment 12 on alice/project#3?"}, prompt.confirmations)
|
||||
require.Equal(t, "✓ Deleted comment 12 on alice/project#3\n", diagnostics.String())
|
||||
err := Execute(context.Background(), deps, []string{"issue", "comment", "delete", "12", "-R", "alice/project", "--no-input"})
|
||||
require.EqualError(t, err, "confirmation required to delete comment 12 on alice/project#3; rerun with --yes")
|
||||
}
|
||||
|
||||
func TestPullCommentsAndAttachmentsRequirePullRequests(t *testing.T) {
|
||||
@@ -85,3 +101,68 @@ func TestPullCommentsAndAttachmentsRequirePullRequests(t *testing.T) {
|
||||
"POST /api/v1/repos/alice/project/issues/1/comments",
|
||||
}, changed)
|
||||
}
|
||||
|
||||
func TestCommentGroupCommentsLikeGh(t *testing.T) {
|
||||
var changed []string
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "GET /api/v1/repos/alice/project/issues/1":
|
||||
_, _ = w.Write([]byte(`{"number":1,"title":"Crash"}`))
|
||||
case "GET /api/v1/repos/alice/project/issues/13":
|
||||
_, _ = w.Write([]byte(`{"number":13,"title":"Fix","pull_request":{}}`))
|
||||
case "GET /api/v1/repos/alice/project/issues/1/comments":
|
||||
_, _ = w.Write([]byte(`[{"id":79,"body":"First **one**","user":{"login":"alice"},"created_at":"2026-10-11T18:00:00Z","updated_at":"2026-10-11T18:00:00Z","issue_url":"https://example.org/alice/project/issues/1"},` +
|
||||
`{"id":80,"body":"Second\nline","user":{"login":"bob"},"created_at":"2026-10-11T19:00:00Z","updated_at":"2026-10-11T19:30:00Z","issue_url":"https://example.org/alice/project/issues/1"},` +
|
||||
`{"id":81,"body":"Third","user":{"login":"bob"},"created_at":"2026-10-11T19:10:00Z","updated_at":"2026-10-11T19:10:00Z","issue_url":"https://example.org/alice/project/issues/1"}]`))
|
||||
default:
|
||||
changed = append(changed, r.Method+" "+r.URL.Path)
|
||||
_, _ = w.Write([]byte(`{"id":90,"body":"x","html_url":"https://example.org/alice/project/issues/1#issuecomment-90","issue_url":"https://example.org/alice/project/issues/1"}`))
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, out, diagnostics := serverDeps(t, server.URL)
|
||||
deps.Now = func() time.Time { return time.Date(2026, 10, 11, 20, 0, 0, 0, time.UTC) }
|
||||
run := func(args ...string) error {
|
||||
out.Reset()
|
||||
diagnostics.Reset()
|
||||
return Execute(context.Background(), deps, append(args, "-R", "alice/project", "--no-input"))
|
||||
}
|
||||
require.NoError(t, run("issue", "comment", "1", "-b", "LGTM"))
|
||||
require.Equal(t, "https://example.org/alice/project/issues/1#issuecomment-90\n", out.String())
|
||||
require.NoError(t, run("issue", "comment", "add", "#1", "--body", "LGTM"))
|
||||
require.NoError(t, run("pr", "comment", "13", "-b", "Ship it"))
|
||||
require.NoError(t, run("issue", "comment", "1", "--edit-last", "-b", "Edited"))
|
||||
require.NoError(t, run("issue", "comment", "1", "--delete-last", "--yes"))
|
||||
require.NoError(t, run("issue", "comment", "1", "-b", "x", "--json", "issue_number"))
|
||||
require.JSONEq(t, `{"issue_number":1}`, out.String())
|
||||
require.Equal(t, []string{
|
||||
"POST /api/v1/repos/alice/project/issues/1/comments",
|
||||
"POST /api/v1/repos/alice/project/issues/1/comments",
|
||||
"POST /api/v1/repos/alice/project/issues/13/comments",
|
||||
"PATCH /api/v1/repos/alice/project/issues/comments/79",
|
||||
"DELETE /api/v1/repos/alice/project/issues/comments/79",
|
||||
"POST /api/v1/repos/alice/project/issues/1/comments",
|
||||
}, changed)
|
||||
for _, test := range []struct {
|
||||
args []string
|
||||
want string
|
||||
}{
|
||||
{[]string{"issue", "comment", "craete", "1"}, `unknown command "craete" for "tea issue comment"; did you mean create?`},
|
||||
{[]string{"issue", "comment", "-b", "x"}, "missing issue number"},
|
||||
{[]string{"issue", "comment", "1", "--edit-last", "--delete-last"}, "--edit-last and --delete-last cannot be combined"},
|
||||
{[]string{"issue", "comment", "1"}, "missing required input: supply --body, --body-file or --editor"},
|
||||
{[]string{"pr", "comment", "1", "-b", "x"}, "#1 in alice/project is an issue, not a pull request; use tea issue comment 1"},
|
||||
} {
|
||||
require.ErrorContains(t, run(test.args...), test.want, test.args)
|
||||
}
|
||||
require.NoError(t, run("issue", "comment", "list", "1"))
|
||||
require.Equal(t, "id:\t79\nauthor:\talice\ncreated_at:\t2026-10-11T18:00:00Z\nurl:\t\n--\nFirst **one**\n--\n"+
|
||||
"id:\t80\nauthor:\tbob\ncreated_at:\t2026-10-11T19:00:00Z\nurl:\t\n--\nSecond\nline\n--\n"+
|
||||
"id:\t81\nauthor:\tbob\ncreated_at:\t2026-10-11T19:10:00Z\nurl:\t\n--\nThird\n--\n", out.String())
|
||||
deps.Streams.OutTTY = true
|
||||
deps.Getenv = serverEnv(server.URL, map[string]string{"NO_COLOR": "1", "TEA_MARKDOWN": "raw"})
|
||||
require.NoError(t, run("issue", "comment", "list", "1"))
|
||||
require.Equal(t, "#79 alice • about 2 hours ago\n First **one**\n\n"+
|
||||
"#80 bob • about 1 hour ago (edited)\n Second\n line\n\n"+
|
||||
"#81 bob • about 50 minutes ago\n Third\n", out.String())
|
||||
}
|
||||
Reference in new issue
Block a user