coverage~bigbes/sr-ht-doltede3b0bbweb/adapters.go

Coverage
0.0% 0/32 statements
Δ
Blob
4e0631e
1 package web
2
3 import (
4 "context"
5 "time"
6
7 "sourcecraft.dev/bigbes/sr-ht-core/auth"
8 "sourcecraft.dev/bigbes/sr-ht-core/client"
9
10 "sourcecraft.dev/bigbes/sr-ht-dolt/authn"
11 "sourcecraft.dev/bigbes/sr-ht-dolt/browse"
12 "sourcecraft.dev/bigbes/sr-ht-dolt/core"
13 "sourcecraft.dev/bigbes/sr-ht-dolt/db"
14 )
15
16 // This file holds the production adapters that satisfy the small interfaces in
17 // deps.go over the real db, browse and core-go layers. Tests do not use these;
18 // they inject their own fakes. Each adapter is a zero-value struct with a
19 // compile-time interface assertion so a signature drift in a committed package
20 // breaks the build here rather than at Phase-3 wiring time.
21
22 // DBAdapter satisfies RepoStore over the request-scoped db.Store. It holds no
23 // connection: every method builds a Store from the *sql.DB the core-go database
24 // middleware installed in ctx (db.FromContext), so one value serves all
25 // requests and leaks nothing.
26 type DBAdapter struct{}
27
28 var _ RepoStore = DBAdapter{}
29
30 0 func (DBAdapter) CreateRepo(ctx context.Context, r *core.Repo) (*core.Repo, error) {
31 0 return db.FromContext(ctx).CreateRepo(ctx, r)
32 0 }
33
34 0 func (DBAdapter) GetRepoByOwnerAndName(ctx context.Context, owner, name string) (*core.Repo, error) {
35 0 return db.FromContext(ctx).GetRepoByOwnerAndName(ctx, owner, name)
36 0 }
37
38 0 func (DBAdapter) ListReposByOwner(ctx context.Context, owner string, viewer *core.Caller) ([]*core.Repo, error) {
39 0 return db.FromContext(ctx).ListReposByOwner(ctx, owner, viewer)
40 0 }
41
42 0 func (DBAdapter) ListReposForViewer(ctx context.Context, viewer *core.Caller) ([]*core.Repo, error) {
43 0 return db.FromContext(ctx).ListReposForViewer(ctx, viewer)
44 0 }
45
46 0 func (DBAdapter) ListReposForDashboard(ctx context.Context, userID int) ([]*core.Repo, error) {
47 0 return db.FromContext(ctx).ListReposForDashboard(ctx, userID)
48 0 }
49
50 0 func (DBAdapter) UpdateRepo(ctx context.Context, id int, description string, visibility core.Visibility) error {
51 0 return db.FromContext(ctx).UpdateRepo(ctx, id, description, visibility)
52 0 }
53
54 0 func (DBAdapter) RenameRepo(ctx context.Context, id int, name, path string) error {
55 0 return db.FromContext(ctx).RenameRepo(ctx, id, name, path)
56 0 }
57
58 0 func (DBAdapter) DeleteRepo(ctx context.Context, id int) error {
59 0 return db.FromContext(ctx).DeleteRepo(ctx, id)
60 0 }
61
62 0 func (DBAdapter) EffectiveAccess(ctx context.Context, userID, repoID int) (*core.AccessMode, error) {
63 0 return db.FromContext(ctx).EffectiveAccess(ctx, userID, repoID)
64 0 }
65
66 0 func (DBAdapter) ListACL(ctx context.Context, repoID int) ([]*db.ACLEntry, error) {
67 0 return db.FromContext(ctx).ListACL(ctx, repoID)
68 0 }
69
70 0 func (DBAdapter) UpsertACL(ctx context.Context, repoID, userID int, mode core.AccessMode) error {
71 0 return db.FromContext(ctx).UpsertACL(ctx, repoID, userID, mode)
72 0 }
73
74 0 func (DBAdapter) DeleteACL(ctx context.Context, repoID, userID int) error {
75 0 return db.FromContext(ctx).DeleteACL(ctx, repoID, userID)
76 0 }
77
78 0 func (DBAdapter) InsertKey(ctx context.Context, userID int, kid string, pubkey []byte, comment string) (*db.DoltKey, error) {
79 0 return db.FromContext(ctx).InsertKey(ctx, userID, kid, pubkey, comment)
80 0 }
81
82 0 func (DBAdapter) ListKeysByUser(ctx context.Context, userID int) ([]*db.DoltKey, error) {
83 0 return db.FromContext(ctx).ListKeysByUser(ctx, userID)
84 0 }
85
86 0 func (DBAdapter) DeleteKey(ctx context.Context, id, userID int) error {
87 0 return db.FromContext(ctx).DeleteKey(ctx, id, userID)
88 0 }
89
90 // BrowseAdapter satisfies BrowseOpener over browse.Open. The returned *browse.DB
91 // already implements every BrowseSession method plus Close.
92 type BrowseAdapter struct{}
93
94 var _ BrowseOpener = BrowseAdapter{}
95
96 0 func (BrowseAdapter) Open(ctx context.Context, diskPath string) (BrowseSession, error) {
97 0 dbh, err := browse.Open(ctx, diskPath)
98 0 if err != nil {
99 0 return nil, err
100 0 }
101 0 return dbh, nil
102 }
103
104 // gitDescribeTimeout bounds the git.sr.ht GraphQL lookup. It must stay well
105 // under dolt-git-hook's 5s client timeout: the lookup runs inside the hook's
106 // POST to /internal/repos, so a slow git.sr.ht API must not push the whole
107 // request past what the hook will wait for.
108 const gitDescribeTimeout = 3 * time.Second
109
110 // GitDescriptionResolver satisfies GitDescriber over git.sr.ht's GraphQL API,
111 // queried with internal auth in the owner's name — the same service-to-service
112 // network-key trust dolt-git-hook uses to reach us, pointed the other way.
113 type GitDescriptionResolver struct{}
114
115 var _ GitDescriber = GitDescriptionResolver{}
116
117 0 func (GitDescriptionResolver) Description(ctx context.Context, owner, name string) (string, bool) {
118 0 ctx, cancel := context.WithTimeout(ctx, gitDescribeTimeout)
119 0 defer cancel()
120 0
121 0 var resp struct {
122 0 Me struct {
123 0 Repository *struct {
124 0 Description *string `json:"description"`
125 0 } `json:"repository"`
126 0 } `json:"me"`
127 0 }
128 0 err := client.Do(ctx, owner, "git.sr.ht", client.GraphQLQuery{
129 0 Query: `query($name: String!) { me { repository(name: $name) { description } } }`,
130 0 Variables: map[string]any{"name": name},
131 0 }, &resp)
132 0 if err != nil || resp.Me.Repository == nil {
133 0 return "", false
134 0 }
135 0 if resp.Me.Repository.Description == nil {
136 0 return "", true
137 0 }
138 0 return *resp.Me.Repository.Description, true
139 }
140
141 // MetaUserResolver satisfies UserResolver via core-go's auth.LookupUser, which
142 // mirrors the meta.sr.ht profile into the local user table and yields the
143 // account's UserID. Used to resolve an ACL grantee by username.
144 type MetaUserResolver struct{}
145
146 var _ UserResolver = MetaUserResolver{}
147
148 0 func (MetaUserResolver) LookupUser(ctx context.Context, username string) (*core.Caller, error) {
149 0 var ac auth.AuthContext
150 0 if err := auth.LookupUser(ctx, username, &ac); err != nil {
151 0 return nil, err
152 0 }
153 0 return authn.AsCoreCaller(&ac), nil
154 }