Files
gitea-tea/internal/app/comments_test.go
T
bircni c1d51c386d fix(pr): refuse pr comment and pr attachment on issues
Gitea serves pull request comments and attachments through issue
endpoints, so tea pr comment create 1 posted on issue #1. pr comment and
pr attachment commands now check that the number is a pull request, and
comment IDs that the comment is on one, and point at the issue command
otherwise: "#1 in alice/project is an issue, not a pull request; use tea
issue comment create 1". Issues gain a kind field (issue or pull) in JSON.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-12 00:14:30 +02:00

88 lines
3.9 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package app
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func TestCommentConfirmationNamesTheTarget(t *testing.T) {
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method + " " + r.URL.Path {
case "POST /api/v1/repos/alice/project/issues/3/comments":
_, _ = w.Write([]byte(`{"id":12,"body":"Prompted"}`))
case "DELETE /api/v1/repos/alice/project/issues/comments/12":
w.WriteHeader(http.StatusNoContent)
default:
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
deps, _, _ := serverDeps(t, server.URL)
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"}))
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)
}
func TestPullCommentsAndAttachmentsRequirePullRequests(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":"An issue"}`))
case "GET /api/v1/repos/alice/project/issues/13":
_, _ = w.Write([]byte(`{"number":13,"title":"A pull request","pull_request":{"merged":false}}`))
case "GET /api/v1/repos/alice/project/issues/comments/5":
_, _ = w.Write([]byte(`{"id":5,"body":"on an issue","issue_url":"http://x/alice/project/issues/1"}`))
case "GET /api/v1/repos/alice/project/issues/comments/6":
_, _ = w.Write([]byte(`{"id":6,"body":"on a pull request","pull_request_url":"http://x/alice/project/pulls/13"}`))
case "GET /api/v1/repos/alice/project/issues/13/comments", "GET /api/v1/repos/alice/project/issues/13/assets":
_, _ = w.Write([]byte(`[]`))
default:
changed = append(changed, r.Method+" "+r.URL.Path)
_, _ = w.Write([]byte(`{"id":7,"body":"x"}`))
}
}))
defer server.Close()
deps, out, _ := serverDeps(t, server.URL)
for _, test := range []struct {
args []string
want string
}{
{[]string{"pr", "comment", "create", "1", "--body", "x"}, "#1 in alice/project is an issue, not a pull request; use tea issue comment create 1"},
{[]string{"pr", "comment", "list", "1"}, "#1 in alice/project is an issue"},
{[]string{"pr", "comment", "edit", "5", "--body", "x"}, "comment 5 in alice/project is on an issue, not a pull request; use tea issue comment edit 5"},
{[]string{"pr", "comment", "delete", "5", "--yes"}, "comment 5 in alice/project is on an issue"},
{[]string{"pr", "attachment", "list", "1"}, "#1 in alice/project is an issue, not a pull request; use tea issue attachment list 1"},
{[]string{"pr", "attachment", "delete", "1", "2", "--yes"}, "#1 in alice/project is an issue"},
} {
err := Execute(context.Background(), deps, append(test.args, "-R", "alice/project"))
require.ErrorContains(t, err, test.want)
}
require.Empty(t, changed)
require.Empty(t, out.String())
for _, args := range [][]string{
{"pr", "comment", "create", "13", "--body", "x"},
{"pr", "comment", "edit", "6", "--body", "x"},
{"pr", "comment", "list", "13"},
{"pr", "attachment", "list", "13"},
{"issue", "comment", "create", "1", "--body", "x"},
} {
require.NoError(t, Execute(context.Background(), deps, append(args, "-R", "alice/project", "--json")))
}
require.Equal(t, []string{
"POST /api/v1/repos/alice/project/issues/13/comments",
"PATCH /api/v1/repos/alice/project/issues/comments/6",
"POST /api/v1/repos/alice/project/issues/1/comments",
}, changed)
}