GET /api/v1/admin/users with a read:admin token in the Authorization header, paged until an empty page. Errors carry no body, URL or token. Refs: t81 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
119 lines
3.7 KiB
Go
119 lines
3.7 KiB
Go
// Package gitea reads the user list of a Managed Git instance (t81): one
|
|
// Gitea per customer, read with a token of scope read:admin.
|
|
package gitea
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// pageLimit is the page size asked for; Gitea may cap it lower
|
|
// (API.MAX_RESPONSE_ITEMS), so paging stops on an empty page, not a short one.
|
|
const (
|
|
pageLimit = 50
|
|
maxPages = 2000 // 100,000 users: a guard against a server that never ends
|
|
maxBody = 8 << 20
|
|
)
|
|
|
|
// User is the part of Gitea's admin user record the count uses.
|
|
type User struct {
|
|
Login string `json:"login"`
|
|
// LoginName is the user's name at its login source: for accounts that
|
|
// sign in through the tenant's Keycloak client, the Keycloak user id
|
|
// (sub). Local accounts (the CI bot) have none.
|
|
LoginName string `json:"login_name"`
|
|
LastLogin time.Time `json:"last_login"`
|
|
}
|
|
|
|
// SignedIn reports whether the user has signed in at least once. Gitea
|
|
// reports "never" as the Unix epoch (in the server's zone); the Go zero time
|
|
// covers a missing field.
|
|
func (u User) SignedIn() bool {
|
|
return !u.LastLogin.IsZero() && u.LastLogin.Unix() > 0
|
|
}
|
|
|
|
// Client calls Gitea instances. The zero value uses http.DefaultClient.
|
|
type Client struct {
|
|
HTTP *http.Client
|
|
}
|
|
|
|
func (c *Client) http() *http.Client {
|
|
if c != nil && c.HTTP != nil {
|
|
return c.HTTP
|
|
}
|
|
return http.DefaultClient
|
|
}
|
|
|
|
// ErrStatus is an unexpected HTTP status from Gitea. Its message carries the
|
|
// status only, never the response body.
|
|
type ErrStatus struct{ Code int }
|
|
|
|
func (e *ErrStatus) Error() string { return "gitea: status " + strconv.Itoa(e.Code) }
|
|
|
|
// ListUsers returns every user of the instance at baseURL
|
|
// (GET /api/v1/admin/users, paged). The token goes in the Authorization
|
|
// header only, so it never appears in a URL or an error.
|
|
func (c *Client) ListUsers(ctx context.Context, baseURL, token string) ([]User, error) {
|
|
if token == "" {
|
|
return nil, errors.New("gitea: no token")
|
|
}
|
|
base, err := url.Parse(strings.TrimRight(baseURL, "/") + "/api/v1/admin/users")
|
|
if err != nil || base.Host == "" {
|
|
return nil, errors.New("gitea: invalid instance url")
|
|
}
|
|
var out []User
|
|
for page := 1; page <= maxPages; page++ {
|
|
u := *base
|
|
u.RawQuery = url.Values{"page": {strconv.Itoa(page)}, "limit": {strconv.Itoa(pageLimit)}}.Encode()
|
|
users, err := c.page(ctx, u.String(), token)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("page %d: %w", page, err)
|
|
}
|
|
if len(users) == 0 {
|
|
return out, nil
|
|
}
|
|
out = append(out, users...)
|
|
}
|
|
return nil, errors.New("gitea: too many pages")
|
|
}
|
|
|
|
func (c *Client) page(ctx context.Context, u, token string) ([]User, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
|
if err != nil {
|
|
return nil, errors.New("gitea: build request")
|
|
}
|
|
req.Header.Set("Authorization", "token "+token)
|
|
req.Header.Set("Accept", "application/json")
|
|
resp, err := c.http().Do(req)
|
|
if err != nil {
|
|
// Not the *url.Error itself: it names the instance's URL, and the
|
|
// report this ends up in should name the service id only.
|
|
var ue *url.Error
|
|
if errors.As(err, &ue) && ue.Timeout() {
|
|
return nil, errors.New("gitea: request timed out")
|
|
}
|
|
if ctx.Err() != nil {
|
|
return nil, fmt.Errorf("gitea: %w", ctx.Err())
|
|
}
|
|
return nil, errors.New("gitea: request failed")
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, maxBody))
|
|
return nil, &ErrStatus{Code: resp.StatusCode}
|
|
}
|
|
var users []User
|
|
if err := json.NewDecoder(io.LimitReader(resp.Body, maxBody)).Decode(&users); err != nil {
|
|
return nil, errors.New("gitea: response is not a user list")
|
|
}
|
|
return users, nil
|
|
}
|