1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3// Package ports declares every interface that crosses a layer, and the
4// parameter and result types of the use case interfaces.
5//
6// Outbound ports (ports.go) are what the use cases need from the outside
7// world: adapters implement them. Inbound ports (services.go) are the use
8// cases as the adapters that serve requests see them: the types of package usecase
9// implement them. Adapters and use cases depend on this package and on domain,
10// never on each other; internal/app wires the implementations. All the mocks are generated in
11// ports/mocks.
12package ports
13
14import (
15 "context"
16 "fmt"
17 "io"
18 "time"
19
20 "bnema.dev/ermite/internal/domain"
21)
22
23//go:generate go tool mockery
24
25// RepoMetaStore keeps repository metadata. It is the source of truth for
26// everything ermite owns about a repository except its Git data.
27type RepoMetaStore interface {
28 // Create stores repo. It returns domain.ErrAlreadyExists if a repository
29 // with that name exists, including when a concurrent Create wins.
30 Create(ctx context.Context, repo domain.Repo) error
31 // Get returns the repository, or domain.ErrNotFound.
32 Get(ctx context.Context, name domain.RepoName) (domain.Repo, error)
33 // List returns every repository ordered by name.
34 List(ctx context.Context) ([]domain.Repo, error)
35 // Update replaces the stored repository. It returns domain.ErrNotFound if
36 // there is none and domain.ErrConflict if another write changed the
37 // stored repository since it was read.
38 Update(ctx context.Context, repo domain.Repo) error
39 // Delete removes the repository and everything keyed to it: its merge
40 // requests, their comments and read positions, and its number sequence.
41 // A repository created again under the same name starts empty, with its
42 // numbers starting at 1. Deleting a missing one is not an error.
43 Delete(ctx context.Context, name domain.RepoName) error
44}
45
46// StagedID identifies a repository being created. It is opaque to callers.
47type StagedID string
48
49// RepoDirs is the result of listing the repository directories.
50type RepoDirs struct {
51 // Names are the directories that hold a valid repository name.
52 Names []domain.RepoName
53 // Invalid are the entries that cannot be a repository directory.
54 Invalid []string
55}
56
57// GitRepos manages bare repository directories.
58type GitRepos interface {
59 // Stage creates an empty bare repository outside the public directory
60 // and returns its identifier.
61 Stage(ctx context.Context) (StagedID, error)
62 // Publish moves a staged repository to its final place under name. It
63 // returns domain.ErrAlreadyExists if a directory for name exists; the
64 // staged repository is left in place for Discard.
65 Publish(ctx context.Context, id StagedID, name domain.RepoName) error
66 // Discard removes a staged repository. A missing one is not an error.
67 Discard(ctx context.Context, id StagedID) error
68 // PurgeStaging removes every staged repository left by a previous run.
69 PurgeStaging(ctx context.Context) error
70 // List returns the repository directories.
71 List(ctx context.Context) (RepoDirs, error)
72 // Trash moves the repository directory to the trash, where it can be
73 // recovered by hand. It returns domain.ErrNotFound if there is none.
74 Trash(ctx context.Context, name domain.RepoName) error
75 // DefaultBranch returns the branch HEAD points to (without refs/heads/)
76 // and whether that branch exists. It returns domain.ErrNotFound if there
77 // is no such repository.
78 DefaultBranch(ctx context.Context, name domain.RepoName) (branch string, exists bool, err error)
79 // HasBranch reports whether refs/heads/<branch> exists. A name that
80 // cannot be a branch does not exist. It returns domain.ErrNotFound if
81 // there is no such repository.
82 HasBranch(ctx context.Context, name domain.RepoName, branch string) (bool, error)
83 // HeadBranch returns the short name of the branch HEAD points to, whether
84 // or not that branch exists, and "" when HEAD is detached. It reads no
85 // refs, so it is cheap enough for a listing. It returns
86 // domain.ErrNotFound if there is no such repository.
87 HeadBranch(ctx context.Context, name domain.RepoName) (string, error)
88 // SetDefaultBranch points HEAD to the branch, which need not exist yet.
89 SetDefaultBranch(ctx context.Context, name domain.RepoName, branch string) error
90 // Maintain refreshes the commit-graph of the repository.
91 Maintain(ctx context.Context, name domain.RepoName) error
92}
93
94// RepoFreshener is implemented by a RepoReader that keeps repositories open
95// between reads. The use cases that write to a repository (a push, the
96// maintenance that follows it) call Refresh so that the reader picks the change
97// up at once instead of when its own timer says so. It is optional: reads
98// stay correct without it.
99type RepoFreshener interface {
100 // Refresh tells the reader that the repository changed on disk. It does
101 // not fail: a reader that cannot refresh finds out on its next read.
102 Refresh(ctx context.Context, name domain.RepoName)
103}
104
105// CommitQuery selects a page of history.
106type CommitQuery struct {
107 // Revision is where the history starts; the zero value is HEAD.
108 Revision domain.Revision
109 // Path restricts the history to the commits that changed this file or
110 // directory; the root means the whole history.
111 Path domain.TreePath
112 // Skip is the number of commits of the listing to leave out. A caller that
113 // pages passes the commit it got in CommitPage.Head as Revision and the
114 // number of commits already read, so that every page walks the same
115 // history even when the revision moves.
116 Skip int
117 // Limit is the number of commits wanted, 1 to MaxCommitLimit.
118 Limit int
119}
120
121// MaxCommitLimit is the most commits one CommitQuery may ask for.
122const MaxCommitLimit = 1000
123
124// Validate checks the page size and the offset of the query. It returns an
125// error matching domain.ErrInvalidPageSize.
126func (q CommitQuery) Validate() error {
127 if q.Limit < 1 || q.Limit > MaxCommitLimit || q.Skip < 0 {
128 return fmt.Errorf("list commits: %w: limit %d, skip %d", domain.ErrInvalidPageSize, q.Limit, q.Skip)
129 }
130 return nil
131}
132
133// CommitPage is a page of history, newest first.
134type CommitPage struct {
135 // Head is the commit the revision resolved to.
136 Head domain.ObjectID
137 // Commits has at most Limit commits.
138 Commits []domain.Commit
139 // HasMore tells that commits follow the last one.
140 HasMore bool
141}
142
143// RepoReader reads the Git data of repositories: refs, trees, blobs, history
144// and diffs. A repository that has no directory gives domain.ErrNotFound; so
145// do an unknown revision, an empty repository (which has no commit) and a
146// path that does not exist at the revision.
147type RepoReader interface {
148 // ListRefs returns HEAD, then the branches, then the tags, the last two
149 // ordered by name. HEAD is always listed: when its branch does not exist
150 // yet its ShortName and ids are empty.
151 ListRefs(ctx context.Context, repo domain.RepoName) ([]domain.Ref, error)
152 // ResolveRevision turns a revision into the commit it designates. A name
153 // that is both a branch and a tag means the branch.
154 ResolveRevision(ctx context.Context, repo domain.RepoName, rev domain.Revision) (domain.ResolvedRevision, error)
155 // ResolveRefPath splits a path such as "feature/x/dir/file" into the
156 // longest leading part that names a branch or a tag and the rest. When no
157 // ref matches, the first segment may be a commit id (full, or abbreviated
158 // to MinAbbrevLen or more). It returns domain.ErrNotFound if nothing
159 // matches.
160 ResolveRefPath(ctx context.Context, repo domain.RepoName, path string) (domain.RefPath, error)
161 // GetTree lists the directory at path, at most domain.MaxTreeEntries
162 // entries (Tree.Truncated tells). With includeLastCommit, the first
163 // domain.MaxLastCommitEntries entries get their last commit. It returns
164 // domain.ErrNotDirectory if path names something else.
165 GetTree(ctx context.Context, repo domain.RepoName, rev domain.Revision, path domain.TreePath, includeLastCommit bool) (domain.Tree, error)
166 // ListPaths lists the file and symlink paths of rev recursively in tree
167 // order, at most maxPaths paths and maxBytes bytes of paths (PathList.Truncated
168 // tells), skipping submodules and paths that are not addressable
169 // (domain.IsAddressablePath).
170 ListPaths(ctx context.Context, repo domain.RepoName, rev domain.Revision, maxPaths, maxBytes int) (domain.PathList, error)
171 // OpenBlob returns the description of the file at path and a reader of its
172 // content, which streams without holding the whole file in memory. The
173 // caller must close the reader. It returns domain.ErrNotFile if path names
174 // something else.
175 OpenBlob(ctx context.Context, repo domain.RepoName, rev domain.Revision, path domain.TreePath) (domain.BlobInfo, io.ReadCloser, error)
176 // ListCommits returns a page of history.
177 ListCommits(ctx context.Context, repo domain.RepoName, q CommitQuery) (CommitPage, error)
178 // GetCommit returns a commit with its statistics and its diff against the
179 // first parent, bounded by limits.
180 GetCommit(ctx context.Context, repo domain.RepoName, rev domain.Revision, limits domain.DiffLimits) (domain.CommitDetail, error)
181}
182
183// HTTPBackend tells how to run `git http-backend` for one request.
184type HTTPBackend struct {
185 // Path is the git-http-backend executable.
186 Path string
187 // Dir is the working directory of the process.
188 Dir string
189 // Env is the complete environment of the process: the isolated Git
190 // environment, GIT_PROJECT_ROOT and the Git configuration of the request
191 // (GIT_CONFIG_COUNT/KEY/VALUE). The caller may append to it.
192 Env []string
193}
194
195// GitBackend provides the Git side of smart HTTP.
196type GitBackend interface {
197 // HTTPBackend returns how to run git http-backend. For a push it also
198 // enables http.receivepack and sets REMOTE_USER; a read request cannot
199 // reach receive-pack.
200 HTTPBackend(ctx context.Context, push bool) (HTTPBackend, error)
201}
202
203// GitArchive writes the archive of a commit.
204type GitArchive interface {
205 // Archive writes the tree of the commit as a gzip-compressed tar to w, all
206 // the paths below the directory prefix. It writes nothing and returns
207 // domain.ErrNotFound when the repository or the commit does not exist.
208 Archive(ctx context.Context, repo domain.RepoName, commit domain.ObjectID, prefix string, w io.Writer) error
209}
210
211// TokenStore keeps the stored tokens and the index from secret hash to token.
212// The secret itself is never stored.
213type TokenStore interface {
214 // Create stores the token and its hash index in one write. It returns
215 // domain.ErrAlreadyExists if the id or the hash is taken.
216 Create(ctx context.Context, token domain.Token) error
217 // Get returns the token with that id, or domain.ErrNotFound.
218 Get(ctx context.Context, id string) (domain.Token, error)
219 // FindByHash returns the token whose secret has that hash, or
220 // domain.ErrNotFound.
221 FindByHash(ctx context.Context, hash domain.TokenHash) (domain.Token, error)
222 // List returns every token, oldest first.
223 List(ctx context.Context) ([]domain.Token, error)
224 // Revoke removes the token and its hash index in one write, or returns
225 // domain.ErrNotFound.
226 Revoke(ctx context.Context, id string) error
227 // Touch records that the token was used at the given time. It returns
228 // domain.ErrNotFound if the token is gone.
229 Touch(ctx context.Context, id string, at time.Time) error
230}
231
232// MergeRequestStore keeps the merge requests of the repositories, their
233// comments and the read position of the reviewer. A repository has no entry
234// until its first merge request. Create, SaveComment and SetRead return
235// domain.ErrNotFound when the repository does not exist, so that deleting a
236// repository leaves nothing for one created again under its name.
237type MergeRequestStore interface {
238 // Create stores the merge request under the next number of its
239 // repository, which it sets, and returns it. Numbers start at 1 and are
240 // never reused, even by a concurrent Create.
241 Create(ctx context.Context, mr domain.MergeRequest) (domain.MergeRequest, error)
242 // Get returns the merge request, or domain.ErrNotFound. Its Version
243 // tells Update which state it was read at.
244 Get(ctx context.Context, repo domain.RepoName, number int64) (domain.MergeRequest, error)
245 // List returns every merge request of the repository, highest number
246 // first.
247 List(ctx context.Context, repo domain.RepoName) ([]domain.MergeRequest, error)
248 // ListOpen returns the open merge requests of the repository, highest
249 // number first, reading only the open ones (through an index).
250 ListOpen(ctx context.Context, repo domain.RepoName) ([]domain.MergeRequest, error)
251 // CountOpen returns the number of open merge requests of the repository
252 // from the index of keys: no request is read.
253 CountOpen(ctx context.Context, repo domain.RepoName) (int, error)
254 // OpenBySource returns the numbers of the open merge requests whose source
255 // branch is one of branches (short names), from an index of keys: a push
256 // looks up the branches it moved, whatever the number of merge requests the
257 // repository ever had. No request is read. It stops after limit numbers (a
258 // limit of 0 or less means none) and reports truncated when there are more,
259 // without counting them; the context is checked between keys, and a
260 // cancelled one ends the lookup with its error and what was found so far.
261 OpenBySource(ctx context.Context, repo domain.RepoName, branches []string, limit int) (numbers []int64, truncated bool, err error)
262 // OpenByTarget is OpenBySource for the open merge requests whose target
263 // branch is one of branches.
264 OpenByTarget(ctx context.Context, repo domain.RepoName, branches []string, limit int) (numbers []int64, truncated bool, err error)
265 // Update replaces the stored merge request. It returns domain.ErrNotFound
266 // if there is none and domain.ErrConflict if another write changed it
267 // since mr was read.
268 Update(ctx context.Context, mr domain.MergeRequest) error
269 // ListComments returns the comments of the merge request, oldest first.
270 ListComments(ctx context.Context, repo domain.RepoName, number int64) ([]domain.Comment, error)
271 // GetComment returns one comment, or domain.ErrNotFound.
272 GetComment(ctx context.Context, repo domain.RepoName, number int64, id int64) (domain.Comment, error)
273 // SaveComment writes a comment and the merge request it belongs to in one
274 // batch: a new comment (Version 0) must not exist yet, an existing one
275 // must be unchanged since it was read, and so must mr. It returns
276 // domain.ErrConflict otherwise. The caller sets mr.CommentSeq and the
277 // comment's ID for a new comment, and mr.UpdatedAt.
278 SaveComment(ctx context.Context, mr domain.MergeRequest, c domain.Comment) error
279 // GetRead returns the head the reviewer last marked read, or the zero
280 // ObjectID when there is none.
281 GetRead(ctx context.Context, repo domain.RepoName, number int64) (domain.ObjectID, error)
282 // SetRead records the head the reviewer has read. It returns
283 // domain.ErrNotFound when the repository does not exist, and
284 // domain.ErrConflict when concurrent writes to the repository keep it
285 // from landing.
286 SetRead(ctx context.Context, repo domain.RepoName, number int64, head domain.ObjectID) error
287}
288
289// MergeGit is what merge requests need from the Git repositories beyond
290// RepoReader. Every method returns domain.ErrNotFound when the repository has
291// no directory.
292type MergeGit interface {
293 // BranchHead returns the commit refs/heads/<branch> points to, or
294 // domain.ErrNotFound when there is no such branch.
295 BranchHead(ctx context.Context, repo domain.RepoName, branch string) (domain.ObjectID, error)
296 // HasCommit reports whether the repository holds that commit.
297 HasCommit(ctx context.Context, repo domain.RepoName, id domain.ObjectID) (bool, error)
298 // MergeBase returns the best common ancestor of two commits, or
299 // domain.ErrNotFound when they share no history.
300 MergeBase(ctx context.Context, repo domain.RepoName, a, b domain.ObjectID) (domain.ObjectID, error)
301 // IsAncestor reports whether ancestor is reachable from descendant, a
302 // commit being its own ancestor.
303 IsAncestor(ctx context.Context, repo domain.RepoName, ancestor, descendant domain.ObjectID) (bool, error)
304 // ListRange returns the commits reachable from head and not from exclude
305 // ("exclude..head"), newest first, at most limit; more is true when the
306 // range holds more. A zero exclude means the whole history of head.
307 ListRange(ctx context.Context, repo domain.RepoName, exclude, head domain.ObjectID, limit int) (commits []domain.Commit, more bool, err error)
308 // DiffCommits returns the statistics and the diff from commit base to
309 // commit head (renames detected), bounded by limits.
310 DiffCommits(ctx context.Context, repo domain.RepoName, base, head domain.ObjectID, limits domain.DiffLimits) (domain.DiffStats, domain.Diff, error)
311 // CommitTree returns the tree of a commit.
312 CommitTree(ctx context.Context, repo domain.RepoName, id domain.ObjectID) (domain.ObjectID, error)
313 // WriteCommit stores a commit object, given as the text of the object,
314 // and returns its id.
315 WriteCommit(ctx context.Context, repo domain.RepoName, object []byte) (domain.ObjectID, error)
316 // UpdateBranch moves refs/heads/<branch> from old to new. It returns
317 // domain.ErrConflict when the branch no longer points to old.
318 UpdateBranch(ctx context.Context, repo domain.RepoName, branch string, old, new domain.ObjectID) error
319 // DeleteBranch deletes refs/heads/<branch> if it still points to old. It
320 // returns domain.ErrConflict when the branch moved or is gone.
321 DeleteBranch(ctx context.Context, repo domain.RepoName, branch string, old domain.ObjectID) error
322}
323
324// CommitSigner signs the commits ermite creates (ADR 0022). The private key
325// never leaves it.
326type CommitSigner interface {
327 // PublicKey returns the public key and its fingerprint.
328 PublicKey() domain.SigningPublicKey
329 // Sign signs the text of a commit object and returns the armored SSH
330 // signature (namespace "git") to put in its gpgsig header.
331 Sign(payload []byte) (string, error)
332}
333
334// SessionStore keeps what the browser sessions of the owner (ADR 0018) need
335// to survive a restart: the key that signs them and the epoch that voids them.
336type SessionStore interface {
337 // Key returns the 32-byte secret that signs the session cookies. The first
338 // call on an empty store generates and stores it; a restart keeps it.
339 Key(ctx context.Context) ([]byte, error)
340 // Epoch returns the current epoch, 0 until the first BumpEpoch. A session
341 // is valid only while its epoch is the current one.
342 Epoch(ctx context.Context) (uint64, error)
343 // BumpEpoch increments the epoch, which ends every session, and returns
344 // the new value.
345 BumpEpoch(ctx context.Context) (uint64, error)
346}
347
348// PasskeyStore keeps the owner's passkeys (ADR 0026) and the WebAuthn user
349// handle they belong to.
350type PasskeyStore interface {
351 // UserHandle returns the 32-byte WebAuthn user handle of the owner. The
352 // first call on an empty store generates and stores it; it never changes.
353 UserHandle(ctx context.Context) ([]byte, error)
354 // List returns every passkey, oldest first.
355 List(ctx context.Context) ([]domain.Passkey, error)
356 // Get returns the passkey with that credential id, or domain.ErrNotFound.
357 Get(ctx context.Context, id []byte) (domain.Passkey, error)
358 // Create stores the passkey. It returns domain.ErrAlreadyExists if the
359 // credential id is taken. It does not bound the number of passkeys: the
360 // use case checks domain.MaxPasskeys.
361 Create(ctx context.Context, p domain.Passkey) error
362 // Update replaces the stored passkey (sign count, backup state, last
363 // use), or returns domain.ErrNotFound.
364 Update(ctx context.Context, p domain.Passkey) error
365 // Delete removes the passkey, or returns domain.ErrNotFound.
366 Delete(ctx context.Context, id []byte) error
367}
368
369// PasskeyVerifier runs the WebAuthn ceremonies (ADR 0026). It hides the
370// WebAuthn library: the ceremony data it hands out is opaque to the caller,
371// which keeps it between the begin and the finish of a ceremony and passes it
372// back untouched. Every verification failure is domain.ErrUnauthenticated,
373// wrapped with a reason meant for logs, not for callers.
374type PasskeyVerifier interface {
375 // BeginRegistration starts the registration of a passkey for the owner.
376 // options is the JSON of the credential creation options for the browser
377 // ({"publicKey": {...}}); existing passkeys are excluded.
378 BeginRegistration(userHandle []byte, existing []domain.Passkey) (options []byte, ceremony []byte, err error)
379 // FinishRegistration checks the browser's response (the JSON of
380 // PublicKeyCredential.toJSON()) and returns the new passkey, without a name
381 // or creation time.
382 FinishRegistration(userHandle []byte, ceremony, response []byte) (domain.Passkey, error)
383 // BeginLogin starts a discoverable login. options is the JSON of the
384 // credential request options for the browser ({"publicKey": {...}}).
385 BeginLogin() (options []byte, ceremony []byte, err error)
386 // FinishLogin checks the browser's response and returns the stored passkey
387 // updated with the sign count and backup state the response reports. lookup
388 // returns domain.ErrNotFound for an unknown credential id. A counter that
389 // does not advance is not an error here: the returned SignCount is then the
390 // stored one, and the caller, which holds the lock that orders the logins,
391 // refuses it.
392 FinishLogin(userHandle []byte, ceremony, response []byte, lookup func(id []byte) (domain.Passkey, error)) (domain.Passkey, error)
393}
394
395// EventBus fans events out to in-process subscribers.
396type EventBus interface {
397 // Publish hands the event to every subscriber without blocking. A
398 // subscriber that cannot keep up loses events and receives a
399 // domain.EventGap.
400 Publish(ctx context.Context, ev domain.Event)
401 // Subscribe returns a channel of events published from now on. The
402 // channel is closed when ctx ends.
403 Subscribe(ctx context.Context) <-chan domain.Event
404}
405
406// Clock tells the time.
407type Clock interface {
408 Now() time.Time
409}