-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
99 lines (82 loc) · 3.6 KB
/
admin.go
File metadata and controls
99 lines (82 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package forge
import (
"context"
"iter"
"dappco.re/go/core/forge/types"
)
// AdminService handles site administration operations.
// Unlike other services, AdminService does not embed Resource[T,C,U]
// because admin endpoints are heterogeneous.
type AdminService struct {
client *Client
}
func newAdminService(c *Client) *AdminService {
return &AdminService{client: c}
}
// ListUsers returns all users (admin only).
func (s *AdminService) ListUsers(ctx context.Context) ([]types.User, error) {
return ListAll[types.User](ctx, s.client, "/api/v1/admin/users", nil)
}
// IterUsers returns an iterator over all users (admin only).
func (s *AdminService) IterUsers(ctx context.Context) iter.Seq2[types.User, error] {
return ListIter[types.User](ctx, s.client, "/api/v1/admin/users", nil)
}
// CreateUser creates a new user (admin only).
func (s *AdminService) CreateUser(ctx context.Context, opts *types.CreateUserOption) (*types.User, error) {
var out types.User
if err := s.client.Post(ctx, "/api/v1/admin/users", opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// EditUser edits an existing user (admin only).
func (s *AdminService) EditUser(ctx context.Context, username string, opts map[string]any) error {
path := ResolvePath("/api/v1/admin/users/{username}", Params{"username": username})
return s.client.Patch(ctx, path, opts, nil)
}
// DeleteUser deletes a user (admin only).
func (s *AdminService) DeleteUser(ctx context.Context, username string) error {
path := ResolvePath("/api/v1/admin/users/{username}", Params{"username": username})
return s.client.Delete(ctx, path)
}
// RenameUser renames a user (admin only).
func (s *AdminService) RenameUser(ctx context.Context, username, newName string) error {
path := ResolvePath("/api/v1/admin/users/{username}/rename", Params{"username": username})
return s.client.Post(ctx, path, &types.RenameUserOption{NewName: newName}, nil)
}
// ListOrgs returns all organisations (admin only).
func (s *AdminService) ListOrgs(ctx context.Context) ([]types.Organization, error) {
return ListAll[types.Organization](ctx, s.client, "/api/v1/admin/orgs", nil)
}
// IterOrgs returns an iterator over all organisations (admin only).
func (s *AdminService) IterOrgs(ctx context.Context) iter.Seq2[types.Organization, error] {
return ListIter[types.Organization](ctx, s.client, "/api/v1/admin/orgs", nil)
}
// RunCron runs a cron task by name (admin only).
func (s *AdminService) RunCron(ctx context.Context, task string) error {
path := ResolvePath("/api/v1/admin/cron/{task}", Params{"task": task})
return s.client.Post(ctx, path, nil, nil)
}
// ListCron returns all cron tasks (admin only).
func (s *AdminService) ListCron(ctx context.Context) ([]types.Cron, error) {
return ListAll[types.Cron](ctx, s.client, "/api/v1/admin/cron", nil)
}
// IterCron returns an iterator over all cron tasks (admin only).
func (s *AdminService) IterCron(ctx context.Context) iter.Seq2[types.Cron, error] {
return ListIter[types.Cron](ctx, s.client, "/api/v1/admin/cron", nil)
}
// AdoptRepo adopts an unadopted repository (admin only).
func (s *AdminService) AdoptRepo(ctx context.Context, owner, repo string) error {
path := ResolvePath("/api/v1/admin/unadopted/{owner}/{repo}", Params{"owner": owner, "repo": repo})
return s.client.Post(ctx, path, nil, nil)
}
// GenerateRunnerToken generates an actions runner registration token.
func (s *AdminService) GenerateRunnerToken(ctx context.Context) (string, error) {
var out struct {
Token string `json:"token"`
}
if err := s.client.Get(ctx, "/api/v1/admin/runners/registration-token", &out); err != nil {
return "", err
}
return out.Token, nil
}