From a14feec8ab10c1998e62207410c66ad709a66a9e Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 14:16:01 +0000 Subject: [PATCH] feat(gitea): add current user helper Co-Authored-By: Virgil --- docs/architecture.md | 2 +- gitea/client.go | 11 +++++++++++ gitea/client_test.go | 19 +++++++++++++++++++ gitea/testhelper_test.go | 11 +++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/architecture.md b/docs/architecture.md index 083ad3a..0c2e1d7 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -119,7 +119,7 @@ The two packages are structurally parallel but intentionally not unified behind - PR merge, draft status, reviews, combined status, review dismissal - Repository migration (full import with issues/labels/PRs) -The Gitea client has a `CreateMirror` method for setting up pull mirrors from GitHub -- a capability specific to the public mirror workflow. +The Gitea client has a `GetCurrentUser` helper and a `CreateMirror` method for setting up pull mirrors from GitHub -- a capability specific to the public mirror workflow. **SDK limitation:** The Forgejo SDK v2 does not accept `context.Context` on API methods. All SDK calls are synchronous. Context propagation through the wrapper layer is nominal -- contexts are accepted at the boundary but cannot be forwarded. diff --git a/gitea/client.go b/gitea/client.go index 0662c1e..f985867 100644 --- a/gitea/client.go +++ b/gitea/client.go @@ -45,3 +45,14 @@ func (c *Client) URL() string { return c.url } // Token returns the Gitea API token. // Usage: Token(...) func (c *Client) Token() string { return c.token } + +// GetCurrentUser returns the authenticated user's information. +// Usage: GetCurrentUser(...) +func (c *Client) GetCurrentUser() (*gitea.User, error) { + user, _, err := c.api.GetMyUserInfo() + if err != nil { + return nil, log.E("gitea.GetCurrentUser", "failed to get current user", err) + } + + return user, nil +} diff --git a/gitea/client_test.go b/gitea/client_test.go index 3f9f8c3..fa61106 100644 --- a/gitea/client_test.go +++ b/gitea/client_test.go @@ -38,3 +38,22 @@ func TestClient_URL_Good(t *testing.T) { assert.Equal(t, srv.URL, client.URL()) } + +func TestClient_GetCurrentUser_Good(t *testing.T) { + client, srv := newTestClient(t) + defer srv.Close() + + user, err := client.GetCurrentUser() + require.NoError(t, err) + require.NotNil(t, user) + assert.Equal(t, "test-user", user.UserName) +} + +func TestClient_GetCurrentUser_Bad_ServerError_Good(t *testing.T) { + client, srv := newErrorServer(t) + defer srv.Close() + + _, err := client.GetCurrentUser() + assert.Error(t, err) + assert.Contains(t, err.Error(), "failed to get current user") +} diff --git a/gitea/testhelper_test.go b/gitea/testhelper_test.go index 3e7d36c..f023d80 100644 --- a/gitea/testhelper_test.go +++ b/gitea/testhelper_test.go @@ -27,6 +27,17 @@ func newGiteaMux() *http.ServeMux { jsonResponse(w, map[string]string{"version": "1.21.0"}) }) + // User info endpoint for GetCurrentUser / GetMyUserInfo. + mux.HandleFunc("/api/v1/user", func(w http.ResponseWriter, r *http.Request) { + jsonResponse(w, map[string]any{ + "id": 1, + "login": "test-user", + "full_name": "Test User", + "email": "test@example.com", + "login_name": "test-user", + }) + }) + // User repos listing. mux.HandleFunc("/api/v1/user/repos", func(w http.ResponseWriter, r *http.Request) { jsonResponse(w, []map[string]any{