package thin import ( "context" "encoding/json" "path" "slices" "strings" "amadan.net/oficina/equipo/internal/wire" ) // AccessCatalog includes a verified grant immediately. Describing its clone // can finish later; access and the original request do not wait for git. func (x *Index) AccessCatalog(ctx context.Context, state *State) []wire.RepoInfo { catalog := x.Catalog() grants, err := state.approvedAccess(ctx) if err != nil || len(grants) == 0 { return catalog } reachable := x.Forge.Reachable(ctx) for _, repo := range reachable { if len(catalog) >= wire.MaxCatalog { break } if slices.ContainsFunc(catalog, func(e wire.RepoInfo) bool { return e.Repo == repo }) { continue } if slices.ContainsFunc(grants, func(c wire.AccessCommand) bool { return accessCovers(c.Target, repo) }) { catalog = append(catalog, wire.RepoInfo{Repo: repo, Names: []string{path.Base(repo)}, Hints: []string{}}) } } return catalog } func accessCovers(target, repo string) bool { repo = strings.TrimPrefix(repo, "amadan.net/") return repo == target || !strings.Contains(target, "/") && strings.HasPrefix(repo, target+"/") } func (s *State) approvedAccess(ctx context.Context) ([]wire.AccessCommand, error) { rows, err := s.db.QueryContext(ctx, "SELECT command FROM access_requests WHERE state = 'approved' ORDER BY rowid DESC LIMIT ?", wire.MaxCatalog) if err != nil { return nil, err } defer rows.Close() var out []wire.AccessCommand for rows.Next() { var raw string if err := rows.Scan(&raw); err != nil { return nil, err } var c wire.AccessCommand if err := json.Unmarshal([]byte(raw), &c); err != nil { return nil, err } out = append(out, c) } return out, rows.Err() } // Grant-driven exploration belongs to the resumed request. Only new // namespaces fully covered by this person's approved requests are folded in. func (s *settler) foldAccess(ctx context.Context, person string, old, next []string) bool { grants, err := s.State.approvedAccess(ctx) if err != nil { return false } for _, ns := range old { if !slices.Contains(next, ns) { return false } } for _, ns := range next { if slices.Contains(old, ns) { continue } if !slices.ContainsFunc(grants, func(c wire.AccessCommand) bool { target, _, _ := strings.Cut(c.Target, "/") return c.Person == person && target == ns }) { return false } } return true }