Lists all comments on an issue or PR via GET /api/v1/repos/{owner}/{repo}/issues/{index}/comments.
Read-only, allowlist-gated. Extended IssueComment struct with user/timestamps populated by the list endpoint.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
152 lines
5.3 KiB
Go
152 lines
5.3 KiB
Go
package gitea_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCreateIssue(t *testing.T) {
|
|
var captured []byte
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "/api/v1/repos/o/r/issues", r.URL.Path)
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
var err error
|
|
captured, err = io.ReadAll(r.Body)
|
|
require.NoError(t, err)
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"number":42,"title":"x","body":"y","html_url":"http://example.com/issues/42","state":"open"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
iss, err := c.CreateIssue(context.Background(), "o", "r", gitea.CreateIssueArgs{
|
|
Title: "x",
|
|
Body: "y",
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
var payload map[string]any
|
|
require.NoError(t, json.Unmarshal(captured, &payload))
|
|
assert.Equal(t, "x", payload["title"])
|
|
assert.Equal(t, "y", payload["body"])
|
|
|
|
assert.Equal(t, 42, iss.Number)
|
|
assert.Equal(t, "x", iss.Title)
|
|
assert.Equal(t, "y", iss.Body)
|
|
assert.Equal(t, "http://example.com/issues/42", iss.HTMLURL)
|
|
assert.Equal(t, "open", iss.State)
|
|
}
|
|
|
|
func TestGetIssue(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, http.MethodGet, r.Method)
|
|
assert.Equal(t, "/api/v1/repos/o/r/issues/42", r.URL.Path)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"number":42,"title":"fix auth","body":"details","state":"open","html_url":"http://example.com/issues/42","created_at":"2026-05-01T00:00:00Z","updated_at":"2026-05-02T00:00:00Z","comments":3}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
iss, err := c.GetIssue(context.Background(), "o", "r", 42)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 42, iss.Number)
|
|
assert.Equal(t, "fix auth", iss.Title)
|
|
assert.Equal(t, "open", iss.State)
|
|
assert.Equal(t, 3, iss.Comments)
|
|
}
|
|
|
|
func TestGetIssue_NotFound(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"message":"issue not found"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
_, err := c.GetIssue(context.Background(), "o", "r", 999)
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, gitea.ErrNotFound)
|
|
}
|
|
|
|
func TestCreateIssueComment(t *testing.T) {
|
|
var captured []byte
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "/api/v1/repos/o/r/issues/42/comments", r.URL.Path)
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
var err error
|
|
captured, err = io.ReadAll(r.Body)
|
|
require.NoError(t, err)
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"id":7,"body":"hello","html_url":"http://example.com/issues/42#comment-7"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
comment, err := c.CreateIssueComment(context.Background(), "o", "r", 42, "hello")
|
|
require.NoError(t, err)
|
|
|
|
var payload map[string]any
|
|
require.NoError(t, json.Unmarshal(captured, &payload))
|
|
assert.Equal(t, "hello", payload["body"])
|
|
|
|
assert.Equal(t, int64(7), comment.ID)
|
|
assert.Equal(t, "hello", comment.Body)
|
|
assert.Equal(t, "http://example.com/issues/42#comment-7", comment.HTMLURL)
|
|
}
|
|
|
|
func TestListIssueComments(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, http.MethodGet, r.Method)
|
|
assert.Equal(t, "/api/v1/repos/o/r/issues/42/comments", r.URL.Path)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`[
|
|
{"id":1,"body":"first","html_url":"http://example.com/issues/42#comment-1","user":{"login":"alice"},"created_at":"2026-05-01T00:00:00Z","updated_at":"2026-05-01T00:00:00Z"},
|
|
{"id":2,"body":"second","html_url":"http://example.com/issues/42#comment-2","user":{"login":"bob"},"created_at":"2026-05-02T00:00:00Z","updated_at":"2026-05-02T00:00:00Z"}
|
|
]`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
comments, err := c.ListIssueComments(context.Background(), "o", "r", 42)
|
|
require.NoError(t, err)
|
|
require.Len(t, comments, 2)
|
|
assert.Equal(t, int64(1), comments[0].ID)
|
|
assert.Equal(t, "first", comments[0].Body)
|
|
assert.Equal(t, "alice", comments[0].User.Login)
|
|
assert.Equal(t, "bob", comments[1].User.Login)
|
|
}
|
|
|
|
func TestListIssueComments_Empty(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`[]`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
comments, err := c.ListIssueComments(context.Background(), "o", "r", 42)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, comments)
|
|
}
|
|
|
|
func TestListIssueComments_NotFound(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"message":"issue not found"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
_, err := c.ListIssueComments(context.Background(), "o", "r", 999)
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, gitea.ErrNotFound)
|
|
}
|