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,81 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/tea/internal/transport"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCurrentAccountValidatesIdentityAndPropagatesErrors(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
body string
|
||||
status int
|
||||
want string
|
||||
}{
|
||||
{`{"login":"Alice"}`, 200, "Alice"},
|
||||
{`{"login":" "}`, 200, ""},
|
||||
{`invalid`, 200, ""},
|
||||
{`{"message":"do not expose this"}`, 401, ""},
|
||||
} {
|
||||
t.Run(tc.body, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, "/base/api/v1/user", r.URL.Path)
|
||||
require.Equal(t, http.MethodGet, r.Method)
|
||||
require.Equal(t, "token secret", r.Header.Get("Authorization"))
|
||||
w.WriteHeader(tc.status)
|
||||
_, _ = w.Write([]byte(tc.body))
|
||||
}))
|
||||
defer server.Close()
|
||||
account, err := CurrentAccount(context.Background(), transport.New(server.URL+"/base", "secret", false))
|
||||
if tc.want != "" {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.want, account)
|
||||
} else {
|
||||
require.Error(t, err)
|
||||
require.Empty(t, account)
|
||||
require.NotContains(t, err.Error(), "secret")
|
||||
require.NotContains(t, err.Error(), "do not expose this")
|
||||
}
|
||||
})
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := CurrentAccount(ctx, transport.New("https://example.org", "secret", false))
|
||||
require.True(t, errors.Is(err, context.Canceled))
|
||||
}
|
||||
|
||||
func TestCurrentAccountAsksOncePerClientAndSudoUser(t *testing.T) {
|
||||
requests := 0
|
||||
client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requests++
|
||||
login := "alice"
|
||||
if sudo := r.Header.Get("Sudo"); sudo != "" {
|
||||
login = sudo
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"login":"` + login + `"}`))
|
||||
}))
|
||||
for range 2 {
|
||||
account, err := CurrentAccount(context.Background(), client)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "alice", account)
|
||||
}
|
||||
client.Sudo = "bob"
|
||||
account, err := CurrentAccount(context.Background(), client)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "bob", account)
|
||||
require.Equal(t, 2, requests)
|
||||
}
|
||||
|
||||
func TestMatchAccount(t *testing.T) {
|
||||
require.True(t, MatchAccount("", "Alice"))
|
||||
require.True(t, MatchAccount("alice", "Alice"))
|
||||
require.False(t, MatchAccount("bob", "Alice"))
|
||||
}
|
||||
Reference in new issue
Block a user