feat(gitea): add current user helper
Some checks failed
Security Scan / security (push) Failing after 17s
Test / test (push) Successful in 1m38s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 14:16:01 +00:00
parent 8a269fa107
commit a14feec8ab
4 changed files with 42 additions and 1 deletions

View file

@ -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.

View file

@ -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
}

View file

@ -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")
}

View file

@ -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{