package thin import ( "context" "errors" "slices" "amadan.net/oficina/equipo/internal/runner/forge" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/wire" "amadan.net/oficina/equipo/internal/wire/forgekey" ) // ErrForgeUse and ErrForgeDuplicate identify configuration errors separately // from a repository key rejected by routing. var ErrForgeUse = errors.New("invalid forge credential use") var ErrForgeDuplicate = errors.New("duplicate forge registration") // Use distinguishes credential authority for reads and writes. type Use uint8 const ( Read Use = iota + 1 Push ) // Proposal identifies the pushed task branch. Forge-specific metadata belongs // in a later protocol addition, when proposals are carried over the wire. type Proposal struct{ Repo, Branch string } type ProposalRef struct{ URL string } // Forges routes by the parsed key's host. Missing forges never fall back to // the hub. Hostless identity and creation namespaces remain the hub's until // a later protocol can advertise connections separately. type Forges struct{ byHost map[string]Forge } func NewForges(forges ...Forge) (*Forges, error) { f := &Forges{byHost: make(map[string]Forge)} for _, fg := range forges { if fg == nil || !forgekey.KnownHost(fg.Host()) { return nil, forgekey.ErrKey } if f.byHost[fg.Host()] != nil { return nil, ErrForgeDuplicate } f.byHost[fg.Host()] = fg } return f, nil } // Host is empty for a router: it cannot itself be registered as a forge. func (*Forges) Host() string { return "" } func (f *Forges) route(repo string) (Forge, string, error) { k, err := forgekey.Parse(repo) if err != nil { return nil, "", err } fg := f.byHost[k.Host] if fg == nil { return nil, "", forgekey.ErrKey } return fg, k.String(), nil } func (f *Forges) Remote(ctx context.Context, repo string, use Use) (workspace.Remote, error) { if use != Read && use != Push { return workspace.Remote{}, errors.Join(workspace.ErrGitPassword, ErrForgeUse) } fg, key, err := f.route(repo) if err != nil { return workspace.Remote{}, errors.Join(workspace.ErrGitPassword, err) } return fg.Remote(ctx, key, use) } func (f *Forges) Exists(ctx context.Context, repo string) (bool, error) { fg, key, err := f.route(repo) if err != nil { return false, errors.Join(ErrNoCreate, err) } return fg.Exists(ctx, key) } func (f *Forges) Create(ctx context.Context, repo string) error { fg, key, err := f.route(repo) if err != nil { return errors.Join(ErrNoCreate, err) } return fg.Create(ctx, key) } func (f *Forges) Propose(ctx context.Context, p Proposal) (ProposalRef, error) { fg, key, err := f.route(p.Repo) if err != nil { return ProposalRef{}, err } p.Repo = key return fg.Propose(ctx, p) } func (f *Forges) Merge(ctx context.Context, repo, branch string, req forge.MergeRequest) (forge.MergeResult, error) { fg, key, err := f.route(repo) if err != nil { return forge.MergeResult{}, forge.NotSent(repo, errors.Join(forge.ErrMergeRefused, err)) } return fg.Merge(ctx, key, branch, req) } // mergePreflighter is a forge that can refuse a merge on the runner // before the remote is read (forge.BranchMerge.Preflight). type mergePreflighter interface { MergePreflight(ctx context.Context, repo string) error } // MergePreflight routes to the repository's forge, when it has one. func (f *Forges) MergePreflight(ctx context.Context, repo string) error { fg, key, err := f.route(repo) if err != nil { return forge.NotSent(repo, errors.Join(forge.ErrMergeRefused, err)) } if p, ok := fg.(mergePreflighter); ok { return p.MergePreflight(ctx, key) } return nil } func (f *Forges) catalog(list func(Forge) []string) []string { var out []string for host, fg := range f.byHost { for _, repo := range list(fg) { k, err := forgekey.Parse(repo) if err == nil && k.Host == host { out = append(out, k.String()) } } } slices.Sort(out) return slices.Compact(out) } func (f *Forges) Repos() []string { return f.catalog(func(fg Forge) []string { return fg.Repos() }) } func (f *Forges) Reachable(ctx context.Context) []string { return f.catalog(func(fg Forge) []string { return fg.Reachable(ctx) }) } func (f *Forges) Recheck() { for _, fg := range f.byHost { fg.Recheck() } } func (f *Forges) Agent() string { if fg := f.byHost[forgekey.Hub]; fg != nil { return fg.Agent() } return "" } func (f *Forges) CreateIn(ctx context.Context) []string { if fg := f.byHost[forgekey.Hub]; fg != nil { return fg.CreateIn(ctx) } return nil } func (f *Forges) CIHosted(ctx context.Context, repo string) (bool, error) { fg, key, err := f.route(repo) if err != nil { return false, errors.Join(forge.ErrCIRead, err) } if ci, ok := fg.(HostedCI); ok { return ci.CIHosted(ctx, key) } return false, nil } func (f *Forges) CIStatus(ctx context.Context, repo, branch, logs string) (forge.CIStatus, error) { fg, key, err := f.route(repo) if err != nil { return forge.CIStatus{}, errors.Join(forge.ErrCIRead, err) } if ci, ok := fg.(HostedCI); ok { return ci.CIStatus(ctx, key, branch, logs) } return forge.CIStatus{}, forge.ErrCIRead } // AccessCommand still carries hub namespace/repository targets. No other host // is admitted by this wire contract. func (f *Forges) RequestAccess(ctx context.Context, c wire.AccessCommand) (wire.AccessReport, error) { if af, ok := f.byHost[forgekey.Hub].(AccessForge); ok { return af.RequestAccess(ctx, c) } return wire.AccessReport{}, errAccess } func (f *Forges) PollAccess(ctx context.Context, id string, c wire.AccessCommand) (wire.AccessReport, error) { if af, ok := f.byHost[forgekey.Hub].(AccessForge); ok { return af.PollAccess(ctx, id, c) } return wire.AccessReport{}, errAccess } func (*fileForge) Host() string { return forgekey.Hub } func (*fileForge) Propose(_ context.Context, p Proposal) (ProposalRef, error) { k, err := forgekey.Parse(p.Repo) if err != nil || k.Host != forgekey.Hub || !wire.ValidTaskBranch(p.Branch) { return ProposalRef{}, forgekey.ErrKey } return ProposalRef{URL: forgekey.HubBranchURL(k.String(), p.Branch)}, nil }