| 1 | use std::time::SystemTime; |
| 2 | |
| 3 | use super::RepositoryResult; |
| 4 | use crate::domain::{IssueNumber, OrgId, RepoId, RepoName, Repository}; |
| 5 | |
| 6 | /// Persistence for [`Repository`]. |
| 7 | pub trait RepoRepository: Send + Sync { |
| 8 | /// Allocates the next number in a repository's sequence. |
| 9 | /// |
| 10 | /// **One sequence per repository, shared by issues and pull requests**, so `#4` |
| 11 | /// means one thing on a repository whatever kind of thing it is. It lives on this |
| 12 | /// port rather than on either of theirs because it belongs to the repository: while |
| 13 | /// it sat on `IssueRepository`, opening a pull request meant holding the issue port |
| 14 | /// for no other reason, and "the number sequence" was reachable only through one of |
| 15 | /// the two things that draw from it. |
| 16 | /// |
| 17 | /// Allocation has to be atomic: two concurrent creations must not both receive the |
| 18 | /// same number, and the unique `(repo_id, number)` constraint means the loser would |
| 19 | /// fail its insert rather than quietly overwrite. Numbers are consumed even when the |
| 20 | /// thing being numbered is never saved — a gap in the sequence is cheaper than a |
| 21 | /// reused number. |
| 22 | /// |
| 23 | /// The type is [`IssueNumber`] because issues were numbered first; it is the |
| 24 | /// repository's number, and `PullRequest` carries the same one. |
| 25 | fn next_number( |
| 26 | &self, |
| 27 | repo_id: &RepoId, |
| 28 | ) -> impl Future<Output = RepositoryResult<IssueNumber>> + Send; |
| 29 | |
| 30 | fn find_by_id( |
| 31 | &self, |
| 32 | id: &RepoId, |
| 33 | ) -> impl Future<Output = RepositoryResult<Option<Repository>>> + Send; |
| 34 | |
| 35 | /// Looks a repository up by its owner and name — the `/{handle}/repos/{name}` pair. |
| 36 | fn find_by_org_and_name( |
| 37 | &self, |
| 38 | org_id: &OrgId, |
| 39 | name: &RepoName, |
| 40 | ) -> impl Future<Output = RepositoryResult<Option<Repository>>> + Send; |
| 41 | |
| 42 | /// Every repository owned by an organisation, most recently updated first, ties |
| 43 | /// broken by name. |
| 44 | /// |
| 45 | /// Recency rather than alphabetical because the profile is a portfolio: alphabetical |
| 46 | /// is a filing rule and puts `dotfiles` above the thing being built. The name |
| 47 | /// tiebreak is what keeps a listing stable when several repositories share a second |
| 48 | /// — every repository that existed before `updated_at` did shares one exactly. |
| 49 | /// |
| 50 | /// Returns them all regardless of visibility. Filtering is an authorization |
| 51 | /// decision and belongs to the use case, so that the page and `/api` cannot end up |
| 52 | /// applying different rules. |
| 53 | fn list_by_org( |
| 54 | &self, |
| 55 | org_id: &OrgId, |
| 56 | ) -> impl Future<Output = RepositoryResult<Vec<Repository>>> + Send; |
| 57 | |
| 58 | /// Inserts or replaces a repository. |
| 59 | /// |
| 60 | /// The owning organisation must already exist; the foreign key runs that direction. |
| 61 | fn save(&self, repo: &Repository) -> impl Future<Output = RepositoryResult<()>> + Send; |
| 62 | |
| 63 | /// Moves a repository's `updated_at` to `now`, doing nothing if it is gone. |
| 64 | /// |
| 65 | /// The time is supplied rather than read here, so a test can say when a push |
| 66 | /// happened instead of racing the clock — and so the two implementations cannot |
| 67 | /// disagree about which clock they read. |
| 68 | /// |
| 69 | /// Succeeding on a missing row keeps the caller out of the business of a repository |
| 70 | /// deleted mid-push; there is nothing useful it could do about it either way. |
| 71 | fn touch( |
| 72 | &self, |
| 73 | id: &RepoId, |
| 74 | now: SystemTime, |
| 75 | ) -> impl Future<Output = RepositoryResult<()>> + Send; |
| 76 | |
| 77 | /// Marks every repository still `importing` as failed, with `reason`, and reports |
| 78 | /// how many there were. |
| 79 | /// |
| 80 | /// **Called once on boot.** An import runs in a background task, so a process that |
| 81 | /// stops — a restart, a deploy, a crash — leaves rows claiming to be mid-copy with |
| 82 | /// nothing copying them. Without this sweep such a repository shows an importing |
| 83 | /// panel forever and cannot even be deleted and recreated under the same name |
| 84 | /// without the owner guessing what happened. |
| 85 | /// |
| 86 | /// A blanket update rather than a per-row read-modify-write: nothing is running yet |
| 87 | /// when it is called, so there is no import it could stamp on. |
| 88 | fn fail_interrupted_imports( |
| 89 | &self, |
| 90 | reason: &str, |
| 91 | ) -> impl Future<Output = RepositoryResult<u64>> + Send; |
| 92 | |
| 93 | /// Deletes a repository, succeeding if there was nothing to delete. |
| 94 | /// |
| 95 | /// A delete rather than a flag, for the same reason token revocation is: a row that |
| 96 | /// lingers is a repository that stops appearing only as long as every read remembers |
| 97 | /// to check. The bare repo on disk is a separate write and not this port's business. |
| 98 | /// |
| 99 | /// Succeeding on a missing row keeps the caller idempotent — two clicks on a delete |
| 100 | /// button must not turn the second one into an error. |
| 101 | fn delete(&self, id: &RepoId) -> impl Future<Output = RepositoryResult<()>> + Send; |
| 102 | |
| 103 | /// How many repositories exist, across every organisation. |
| 104 | /// |
| 105 | /// Instance-wide rather than per-org, because the only caller is the operator |
| 106 | /// surface describing the whole machine. Counts private and importing |
| 107 | /// repositories too: it is a size, not a listing, and no visibility rule applies |
| 108 | /// to a number only an operator can read. |
| 109 | /// |
| 110 | /// Also a backup manifest's number: rows, not directories — the two can disagree, and |
| 111 | /// which of them a restore brought back is exactly what the number is there to show. |
| 112 | fn count(&self) -> impl Future<Output = RepositoryResult<u64>> + Send; |
| 113 | } |