issue_get: GET /repos/{owner}/{repo}/issues/{number} — full issue with labels, assignees, comment count
release_create: POST /repos/{owner}/{repo}/releases — create release and tag in one call
repo_delete: DELETE /repos/{owner}/{repo} — confirm=<repo name> required, blocks accidents
104 lines
3.4 KiB
Go
104 lines
3.4 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)
|
|
}
|