oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "encoding/json"
6 "path"
7 "slices"
8 "strings"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// AccessCatalog includes a verified grant immediately. Describing its clone
14// can finish later; access and the original request do not wait for git.
15func (x *Index) AccessCatalog(ctx context.Context, state *State) []wire.RepoInfo {
16 catalog := x.Catalog()
17 grants, err := state.approvedAccess(ctx)
18 if err != nil || len(grants) == 0 {
19 return catalog
20 }
21 reachable := x.Forge.Reachable(ctx)
22 for _, repo := range reachable {
23 if len(catalog) >= wire.MaxCatalog {
24 break
25 }
26 if slices.ContainsFunc(catalog, func(e wire.RepoInfo) bool { return e.Repo == repo }) {
27 continue
28 }
29 if slices.ContainsFunc(grants, func(c wire.AccessCommand) bool { return accessCovers(c.Target, repo) }) {
30 catalog = append(catalog, wire.RepoInfo{Repo: repo, Names: []string{path.Base(repo)}, Hints: []string{}})
31 }
32 }
33 return catalog
34}
35
36func accessCovers(target, repo string) bool {
37 repo = strings.TrimPrefix(repo, "amadan.net/")
38 return repo == target || !strings.Contains(target, "/") && strings.HasPrefix(repo, target+"/")
39}
40
41func (s *State) approvedAccess(ctx context.Context) ([]wire.AccessCommand, error) {
42 rows, err := s.db.QueryContext(ctx, "SELECT command FROM access_requests WHERE state = 'approved' ORDER BY rowid DESC LIMIT ?", wire.MaxCatalog)
43 if err != nil {
44 return nil, err
45 }
46 defer rows.Close()
47 var out []wire.AccessCommand
48 for rows.Next() {
49 var raw string
50 if err := rows.Scan(&raw); err != nil {
51 return nil, err
52 }
53 var c wire.AccessCommand
54 if err := json.Unmarshal([]byte(raw), &c); err != nil {
55 return nil, err
56 }
57 out = append(out, c)
58 }
59 return out, rows.Err()
60}
61
62// Grant-driven exploration belongs to the resumed request. Only new
63// namespaces fully covered by this person's approved requests are folded in.
64func (s *settler) foldAccess(ctx context.Context, person string, old, next []string) bool {
65 grants, err := s.State.approvedAccess(ctx)
66 if err != nil {
67 return false
68 }
69 for _, ns := range old {
70 if !slices.Contains(next, ns) {
71 return false
72 }
73 }
74 for _, ns := range next {
75 if slices.Contains(old, ns) {
76 continue
77 }
78 if !slices.ContainsFunc(grants, func(c wire.AccessCommand) bool {
79 target, _, _ := strings.Cut(c.Target, "/")
80 return c.Person == person && target == ns
81 }) {
82 return false
83 }
84 }
85 return true
86}
87