feat(gitea): add current user helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
8a269fa107
commit
a14feec8ab
4 changed files with 42 additions and 1 deletions
|
|
@ -119,7 +119,7 @@ The two packages are structurally parallel but intentionally not unified behind
|
||||||
- PR merge, draft status, reviews, combined status, review dismissal
|
- PR merge, draft status, reviews, combined status, review dismissal
|
||||||
- Repository migration (full import with issues/labels/PRs)
|
- 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.
|
**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.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,3 +45,14 @@ func (c *Client) URL() string { return c.url }
|
||||||
// Token returns the Gitea API token.
|
// Token returns the Gitea API token.
|
||||||
// Usage: Token(...)
|
// Usage: Token(...)
|
||||||
func (c *Client) Token() string { return c.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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,3 +38,22 @@ func TestClient_URL_Good(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, srv.URL, client.URL())
|
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")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,17 @@ func newGiteaMux() *http.ServeMux {
|
||||||
jsonResponse(w, map[string]string{"version": "1.21.0"})
|
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.
|
// User repos listing.
|
||||||
mux.HandleFunc("/api/v1/user/repos", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/v1/user/repos", func(w http.ResponseWriter, r *http.Request) {
|
||||||
jsonResponse(w, []map[string]any{
|
jsonResponse(w, []map[string]any{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue