mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
refactor: add services layer on the Gitea Go SDK (#1125)
- `internal/services`: typed Gitea workflows built on `gitea.dev/sdk`, independent of CLI flags and terminal I/O: issues, pull requests, reviews, comments, labels, milestones, releases and assets, repositories, branches, wiki, Actions (workflows, runs, jobs, logs, artifacts, secrets, variables), organizations, teams, webhooks, notifications, SSH keys, tracked time, admin users, search and status. - SDK types are mapped to tea's own output types, so `--json` stays a stable contract. Requests the SDK cannot express yet (e.g. pull list filters, multi-status run lists, org label archiving) go through small raw helpers, each marked `TODO(sdk)` with the missing SDK piece. - Targets Gitea 28 or newer: no version or API-schema probing. List helpers fill pages capped by `MAX_RESPONSE_ITEMS`. Nothing imports this package yet. Reviewed-on: https://gitea.com/gitea/tea/pulls/1125 Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
This commit is contained in:
1 parent
faea86cd94
commit
12e415a7a7
80 files changed
+9512
No files matched your search
@@ -0,0 +1,49 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
gitea "gitea.dev/sdk"
|
||||
"gitea.dev/tea/internal/transport"
|
||||
)
|
||||
|
||||
// accountCache holds each client's authenticated login per Sudo user; failed lookups are retried.
|
||||
var accountCache sync.Map
|
||||
|
||||
type accountKey struct {
|
||||
client *transport.Client
|
||||
sudo string
|
||||
}
|
||||
|
||||
// CurrentAccount verifies the credential against the selected Gitea installation and returns
|
||||
// the authenticated login. It asks the server once per client and Sudo user.
|
||||
func CurrentAccount(ctx context.Context, client *transport.Client) (string, error) {
|
||||
key := accountKey{client, client.Sudo}
|
||||
if cached, ok := accountCache.Load(key); ok {
|
||||
return cached.(string), nil
|
||||
}
|
||||
user, _, err := call(ctx, client, func(ctx context.Context, c *gitea.Client) (*gitea.User, *gitea.Response, error) {
|
||||
return c.Users.GetMyUserInfo(ctx)
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
account := login(user)
|
||||
if strings.TrimSpace(account) == "" {
|
||||
return "", errors.New("Gitea did not return an authenticated account")
|
||||
}
|
||||
accountCache.Store(key, account)
|
||||
return account, nil
|
||||
}
|
||||
|
||||
// MatchAccount reports whether selected matches the authenticated login.
|
||||
// An empty selection accepts any account.
|
||||
func MatchAccount(selected, authenticated string) bool {
|
||||
return selected == "" || strings.EqualFold(selected, authenticated)
|
||||
}
|
||||
Reference in new issue
Block a user