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 "errors"
6 "slices"
7
8 "amadan.net/oficina/equipo/internal/runner/forge"
9 "amadan.net/oficina/equipo/internal/runner/workspace"
10 "amadan.net/oficina/equipo/internal/wire"
11 "amadan.net/oficina/equipo/internal/wire/forgekey"
12)
13
14// ErrForgeUse and ErrForgeDuplicate identify configuration errors separately
15// from a repository key rejected by routing.
16var ErrForgeUse = errors.New("invalid forge credential use")
17var ErrForgeDuplicate = errors.New("duplicate forge registration")
18
19// Use distinguishes credential authority for reads and writes.
20type Use uint8
21
22const (
23 Read Use = iota + 1
24 Push
25)
26
27// Proposal identifies the pushed task branch. Forge-specific metadata belongs
28// in a later protocol addition, when proposals are carried over the wire.
29type Proposal struct{ Repo, Branch string }
30type ProposalRef struct{ URL string }
31
32// Forges routes by the parsed key's host. Missing forges never fall back to
33// the hub. Hostless identity and creation namespaces remain the hub's until
34// a later protocol can advertise connections separately.
35type Forges struct{ byHost map[string]Forge }
36
37func NewForges(forges ...Forge) (*Forges, error) {
38 f := &Forges{byHost: make(map[string]Forge)}
39 for _, fg := range forges {
40 if fg == nil || !forgekey.KnownHost(fg.Host()) {
41 return nil, forgekey.ErrKey
42 }
43 if f.byHost[fg.Host()] != nil {
44 return nil, ErrForgeDuplicate
45 }
46 f.byHost[fg.Host()] = fg
47 }
48 return f, nil
49}
50
51// Host is empty for a router: it cannot itself be registered as a forge.
52func (*Forges) Host() string { return "" }
53func (f *Forges) route(repo string) (Forge, string, error) {
54 k, err := forgekey.Parse(repo)
55 if err != nil {
56 return nil, "", err
57 }
58 fg := f.byHost[k.Host]
59 if fg == nil {
60 return nil, "", forgekey.ErrKey
61 }
62 return fg, k.String(), nil
63}
64func (f *Forges) Remote(ctx context.Context, repo string, use Use) (workspace.Remote, error) {
65 if use != Read && use != Push {
66 return workspace.Remote{}, errors.Join(workspace.ErrGitPassword, ErrForgeUse)
67 }
68 fg, key, err := f.route(repo)
69 if err != nil {
70 return workspace.Remote{}, errors.Join(workspace.ErrGitPassword, err)
71 }
72 return fg.Remote(ctx, key, use)
73}
74func (f *Forges) Exists(ctx context.Context, repo string) (bool, error) {
75 fg, key, err := f.route(repo)
76 if err != nil {
77 return false, errors.Join(ErrNoCreate, err)
78 }
79 return fg.Exists(ctx, key)
80}
81func (f *Forges) Create(ctx context.Context, repo string) error {
82 fg, key, err := f.route(repo)
83 if err != nil {
84 return errors.Join(ErrNoCreate, err)
85 }
86 return fg.Create(ctx, key)
87}
88func (f *Forges) Propose(ctx context.Context, p Proposal) (ProposalRef, error) {
89 fg, key, err := f.route(p.Repo)
90 if err != nil {
91 return ProposalRef{}, err
92 }
93 p.Repo = key
94 return fg.Propose(ctx, p)
95}
96func (f *Forges) Merge(ctx context.Context, repo, branch string, req forge.MergeRequest) (forge.MergeResult, error) {
97 fg, key, err := f.route(repo)
98 if err != nil {
99 return forge.MergeResult{}, forge.NotSent(repo, errors.Join(forge.ErrMergeRefused, err))
100 }
101 return fg.Merge(ctx, key, branch, req)
102}
103
104// mergePreflighter is a forge that can refuse a merge on the runner
105// before the remote is read (forge.BranchMerge.Preflight).
106type mergePreflighter interface {
107 MergePreflight(ctx context.Context, repo string) error
108}
109
110// MergePreflight routes to the repository's forge, when it has one.
111func (f *Forges) MergePreflight(ctx context.Context, repo string) error {
112 fg, key, err := f.route(repo)
113 if err != nil {
114 return forge.NotSent(repo, errors.Join(forge.ErrMergeRefused, err))
115 }
116 if p, ok := fg.(mergePreflighter); ok {
117 return p.MergePreflight(ctx, key)
118 }
119 return nil
120}
121
122func (f *Forges) catalog(list func(Forge) []string) []string {
123 var out []string
124 for host, fg := range f.byHost {
125 for _, repo := range list(fg) {
126 k, err := forgekey.Parse(repo)
127 if err == nil && k.Host == host {
128 out = append(out, k.String())
129 }
130 }
131 }
132 slices.Sort(out)
133 return slices.Compact(out)
134}
135func (f *Forges) Repos() []string { return f.catalog(func(fg Forge) []string { return fg.Repos() }) }
136func (f *Forges) Reachable(ctx context.Context) []string {
137 return f.catalog(func(fg Forge) []string { return fg.Reachable(ctx) })
138}
139func (f *Forges) Recheck() {
140 for _, fg := range f.byHost {
141 fg.Recheck()
142 }
143}
144func (f *Forges) Agent() string {
145 if fg := f.byHost[forgekey.Hub]; fg != nil {
146 return fg.Agent()
147 }
148 return ""
149}
150func (f *Forges) CreateIn(ctx context.Context) []string {
151 if fg := f.byHost[forgekey.Hub]; fg != nil {
152 return fg.CreateIn(ctx)
153 }
154 return nil
155}
156func (f *Forges) CIHosted(ctx context.Context, repo string) (bool, error) {
157 fg, key, err := f.route(repo)
158 if err != nil {
159 return false, errors.Join(forge.ErrCIRead, err)
160 }
161 if ci, ok := fg.(HostedCI); ok {
162 return ci.CIHosted(ctx, key)
163 }
164 return false, nil
165}
166func (f *Forges) CIStatus(ctx context.Context, repo, branch, logs string) (forge.CIStatus, error) {
167 fg, key, err := f.route(repo)
168 if err != nil {
169 return forge.CIStatus{}, errors.Join(forge.ErrCIRead, err)
170 }
171 if ci, ok := fg.(HostedCI); ok {
172 return ci.CIStatus(ctx, key, branch, logs)
173 }
174 return forge.CIStatus{}, forge.ErrCIRead
175}
176
177// AccessCommand still carries hub namespace/repository targets. No other host
178// is admitted by this wire contract.
179func (f *Forges) RequestAccess(ctx context.Context, c wire.AccessCommand) (wire.AccessReport, error) {
180 if af, ok := f.byHost[forgekey.Hub].(AccessForge); ok {
181 return af.RequestAccess(ctx, c)
182 }
183 return wire.AccessReport{}, errAccess
184}
185func (f *Forges) PollAccess(ctx context.Context, id string, c wire.AccessCommand) (wire.AccessReport, error) {
186 if af, ok := f.byHost[forgekey.Hub].(AccessForge); ok {
187 return af.PollAccess(ctx, id, c)
188 }
189 return wire.AccessReport{}, errAccess
190}
191
192func (*fileForge) Host() string { return forgekey.Hub }
193func (*fileForge) Propose(_ context.Context, p Proposal) (ProposalRef, error) {
194 k, err := forgekey.Parse(p.Repo)
195 if err != nil || k.Host != forgekey.Hub || !wire.ValidTaskBranch(p.Branch) {
196 return ProposalRef{}, forgekey.ErrKey
197 }
198 return ProposalRef{URL: forgekey.HubBranchURL(k.String(), p.Branch)}, nil
199}
200