g1t/packages/contracts/src/repos.ts

394 lines14,675 bytesCodeBlame
1import type { User, Viewer } from "./identity";
2import type { Result } from "./result";
3
4export type Repo = {
5 id: string;
6 /** The slug of the workspace that owns it: the first URL segment. */
7 namespace: string;
8 name: string;
9 description: string | null;
10 isPrivate: boolean;
11 ownerId: string;
12 defaultBranch: string;
13 /** Set when this repo is a pull request's working copy of another repo. */
14 forkOf: string | null;
15 /**
16 * Whether the default branch is protected: it changes only by merging a
17 * pull request, and pushes to it are refused.
18 */
19 protected: boolean;
20 /** RFC 3339. */
21 createdAt: string;
22 /**
23 * Words that say what it is about, for search and Explore: lowercase
24 * letters, digits and hyphens, at most 20.
25 */
26 topics: string[];
27 /** Its home page, an http(s) address. */
28 website?: string | null;
29 /**
30 * RFC 3339: when it was archived. While archived it is read-only:
31 * pushes, merges, agents and workflows are refused, and issues and pull
32 * requests are locked. Null when it is not archived.
33 */
34 archivedAt?: string | null;
35};
36
37/** How long a deleted repository can be restored before it is purged. */
38export const RESTORE_DAYS = 30;
39
40/** A deleted repository, as its workspace's Recently deleted list shows it. */
41export type DeletedRepo = {
42 id: string;
43 namespace: string;
44 name: string;
45 description: string | null;
46 isPrivate: boolean;
47 /** RFC 3339. */
48 deletedAt: string;
49 /** The username of who deleted it. */
50 deletedBy: string;
51 /** RFC 3339: when it is purged, unless restored first. */
52 purgeAfter: string;
53};
54
55/** Whether a repository is archived or deleted, for services deciding whether to act on it. */
56export type RepoStatus = { archived: boolean; deleted: boolean };
57
58/** The most topics a repository has, and the longest topic. */
59export const MAX_TOPICS = 20;
60export const MAX_TOPIC_CHARS = 35;
61
62export type RepoPath = { namespace: string; name: string };
63
64export type Commit = {
65 hash: string;
66 treeHash: string;
67 message: string;
68 author: { name: string; email: string };
69 parents: string[];
70 /** RFC 3339. */
71 authoredAt: string;
72};
73
74export type TreeEntry = {
75 name: string;
76 hash: string;
77 kind: "tree" | "blob" | "symlink" | "gitlink" | "exec";
78};
79
80export type TreeView = {
81 repo: Repo;
82 ref: string;
83 path: string;
84 /** Null when the repo has no commits yet. */
85 head: Commit | null;
86 entries: TreeEntry[];
87 readme: { name: string; text: string | null } | null;
88};
89
90export type BlobView = {
91 repo: Repo;
92 ref: string;
93 path: string;
94 size: number;
95 /** Null when the file is binary or too large to show. */
96 text: string | null;
97};
98
99/** A git remote and a short-lived credential for it. */
100export type GitAccess = { remote: string; token: string };
101
102export type GitService = "git-upload-pack" | "git-receive-pack";
103
104export type CreateRepoInput = {
105 /** The workspace to create it in; the creator must be a member. */
106 namespace: string;
107 name: string;
108 description?: string | null;
109 isPrivate?: boolean;
110 /**
111 * The https address of a public git repository to copy the default branch
112 * of, such as `https://github.com/owner/repo`.
113 */
114 importUrl?: string;
115};
116
117/**
118 * Where repositories may be kept. `euAvailable`: an EU namespace takes new
119 * repositories, so a workspace may keep its data in the EU.
120 */
121export type StorageOptions = { euAvailable: boolean };
122
123/** Repositories: metadata, contents and git access. */
124export interface ReposApi {
125 get(path: RepoPath, viewer: Viewer): Promise<Result<Repo>>;
126 getById(id: string, viewer: Viewer): Promise<Result<Repo>>;
127 /**
128 * Of these ids (at most 500), the repositories the viewer can read, in
129 * one call. Forks and deleted repositories are left out.
130 */
131 readable(ids: string[], viewer: Viewer): Promise<Repo[]>;
132 /**
133 * The workspaces in which this account made a public repository, and so
134 * a public project anyone can see. By account id.
135 */
136 publicNamespaces(ownerId: string): Promise<string[]>;
137 /** Repos the viewer may see, newest first, optionally matching `query`. */
138 list(
139 viewer: Viewer,
140 options?: {
141 query?: string;
142 /** Only repos in this workspace. */
143 namespace?: string;
144 /** Only repos in workspaces the viewer belongs to. */
145 memberOnly?: boolean;
146 },
147 ): Promise<Repo[]>;
148 create(owner: User, input: CreateRepoInput): Promise<Result<Repo>>;
149 /**
150 * Changes whichever details are given. Members of the repository's
151 * workspace only. An empty description clears it.
152 */
153 update(
154 actor: User,
155 path: RepoPath,
156 changes: {
157 description?: string;
158 isPrivate?: boolean;
159 protected?: boolean;
160 topics?: string[];
161 /** An empty string clears it. */
162 website?: string;
163 },
164 ): Promise<Result<Repo>>;
165 /**
166 * Deletes the repository. Owners only, who type its full name as
167 * `confirm`. It is hidden at once and can be restored for
168 * `RESTORE_DAYS` days, then purged. Publishes `repo.deleted`.
169 */
170 delete(actor: User, path: RepoPath, confirm: string): Promise<Result<DeletedRepo>>;
171 /** A workspace's recently deleted repositories, newest first. Owners only; empty otherwise. */
172 deleted(viewer: Viewer, namespace: string): Promise<DeletedRepo[]>;
173 /** Brings a deleted repository back as it was. Owners only. Publishes `repo.restored`. */
174 restore(actor: User, path: RepoPath): Promise<Result<Repo>>;
175 /**
176 * Removes a deleted repository for good now, its git data with it, and
177 * frees its name. Owners only, who type its full name. Publishes `repo.purged`.
178 */
179 purge(actor: User, path: RepoPath, confirm: string): Promise<Result<boolean>>;
180 /**
181 * Renames the repository in its workspace. Owners only. The old path
182 * redirects until a repository is made there. Publishes `repo.renamed`.
183 */
184 rename(actor: User, path: RepoPath, name: string): Promise<Result<Repo>>;
185 /**
186 * Archives the repository (read-only) or unarchives it. Owners only.
187 * Publishes `repo.archived` or `repo.unarchived`.
188 */
189 archive(actor: User, path: RepoPath, archived: boolean): Promise<Result<Repo>>;
190 /**
191 * Makes the repository public or private. Owners only, who type its full
192 * name as `confirm`. Publishes `repo.updated` and `repo.visibility_changed`.
193 */
194 setVisibility(actor: User, path: RepoPath, isPrivate: boolean, confirm: string): Promise<Result<Repo>>;
195 /** Makes another existing branch the default. Members. */
196 setDefaultBranch(actor: User, path: RepoPath, branch: string): Promise<Result<Repo>>;
197 /**
198 * Renames a branch; pull requests from it follow, and addresses naming the
199 * old one redirect. Members; only owners rename the default branch.
200 */
201 renameBranch(actor: User, path: RepoPath, from: string, to: string): Promise<Result<Repo>>;
202 /** What a branch renamed away from `branch` is called now, or null. */
203 resolveBranch(repoId: string, branch: string): Promise<string | null>;
204 /** Whether a repository is archived or deleted; an unknown id answers as deleted. */
205 statusById(id: string): Promise<RepoStatus>;
206 /** What a workspace may choose about where its repositories are kept. */
207 storageOptions(): Promise<StorageOptions>;
208 /**
209 * Moves the repository to the workspace `to`, keeping its name, id and
210 * everything under it. The actor must own both workspaces. The old path
211 * redirects until a repository is made there. Publishes `repo.transferred`.
212 */
213 transfer(actor: User, path: RepoPath, to: string): Promise<Result<Repo>>;
214 /**
215 * Where a repository transferred away from `path` is now; null when
216 * `path` is a repository or never was one that moved. Callers check the
217 * viewer may see it there.
218 */
219 resolvePath(path: RepoPath): Promise<RepoPath | null>;
220
221 tree(path: RepoPath, viewer: Viewer, ref: string | null, treePath: string): Promise<Result<TreeView>>;
222 blob(path: RepoPath, viewer: Viewer, ref: string, filePath: string): Promise<Result<BlobView>>;
223 log(path: RepoPath, viewer: Viewer, ref: string | null, limit: number): Promise<Result<Commit[]>>;
224 /**
225 * Who last changed each line of a file as of `ref` (the default branch if
226 * null). Not found when the file is missing or is not text.
227 */
228 blame(path: RepoPath, viewer: Viewer, ref: string | null, filePath: string): Promise<Result<Blame>>;
229
230 /**
231 * A copy-on-write copy of `source`, hidden from listings, for one pull request
232 * to work in.
233 */
234 forkForPull(sourceId: string, pullId: string, actor: User): Promise<Result<Repo>>;
235
236 /**
237 * Authorizes a git operation and returns where to send it. Pushing to a
238 * repo that does not exist creates it in the pusher's own namespace.
239 */
240 gitAccess(path: RepoPath, viewer: Viewer, service: GitService): Promise<Result<GitAccess>>;
241
242 /** The repository's branches, default branch first. */
243 branches(path: RepoPath, viewer: Viewer): Promise<Result<Branch[]>>;
244
245 /** Which commit last changed each entry of a directory at `ref` (the default branch when null). */
246 lastCommits(path: RepoPath, viewer: Viewer, ref: string | null, treePath: string): Promise<Result<LastCommits>>;
247
248 /** The repository's tags, newest commit first, at most 100. */
249 tags(path: RepoPath, viewer: Viewer): Promise<Result<Tag[]>>;
250
251 /**
252 * Every file at `ref` (the default branch when null), at most `limit`
253 * (10,000 at most). No viewer: check access first.
254 */
255 listFiles(repoId: string, ref: string | null, limit: number): Promise<FileList>;
256
257 /** Blobs' bytes as standard base64, at most 100; `data` is null for one missing or over `maxBytes`. No viewer. */
258 rawBlobs(repoId: string, hashes: string[], maxBytes: number): Promise<RawBlob[]>;
259
260 /**
261 * Writes one file on a new branch made from the default branch's head, as
262 * one commit by `actor`, for a change g1t proposes on their behalf (a
263 * starter workflow). Refused unless they may push, when the branch exists,
264 * or when the file is already there.
265 */
266 commitFile(
267 repo: RepoPath,
268 actor: User,
269 file: { branch: string; path: string; content: string; message: string },
270 ): Promise<Result<{ branch: string; commit: string }>>;
271
272 /**
273 * Moves a repository's default branch to the head of a pull request's
274 * source: a fork (`sourceId` is the fork) or one of the repository's own
275 * branches (`sourceId` is the repository, and `branch` is required).
276 * Refused with "conflict" when the source is behind, since that would
277 * discard commits.
278 */
279 land(sourceId: string, actor: User, branch?: string | null): Promise<Result<{ commit: string; previous: string | null }>>;
280
281 /**
282 * What `head` changes relative to `base`. `head` is a branch or a commit
283 * and defaults to the default branch. With no `base`, a fork is compared
284 * with the last commit it shares with the repository it came from, and a
285 * branch with the point where it left the default branch.
286 */
287 compare(repoId: string, viewer: Viewer, base?: string | null, head?: string | null): Promise<Result<Comparison>>;
288
289 /**
290 * Services only, for the runner's sweep: up to `limit` queued nightly
291 * backups, each now running with a token of its own, so long as no more
292 * than `maxRunning` are then running. Empty when backups are off.
293 */
294 claimBackups(limit: number, maxRunning: number): Promise<BackupClaim[]>;
295
296 /**
297 * Services only: a backup's sandbox stopped before it reported, so the
298 * job is tried again later. Refused harmlessly once it has reported.
299 */
300 failBackup(jobId: string, token: string, error: string): Promise<Result<boolean>>;
301}
302
303/**
304 * A nightly backup to start (`g1t_contracts::backups`): the sandbox is
305 * given the job's id and token, and nothing else.
306 */
307export type BackupClaim = {
308 jobId: string;
309 token: string;
310 repoId: string;
311 path: RepoPath;
312};
313
314/** Lines `start` to `end` (inclusive, from 1) last changed by `commit`. */
315export type BlameRange = { start: number; end: number; commit: string };
316
317/** Who last changed each line of a file. */
318export type Blame = {
319 /** The commit the file was read at. */
320 head: string;
321 /** Every line, in order, in runs that share a commit. */
322 ranges: BlameRange[];
323 /** The commits the ranges name, each once. */
324 commits: Commit[];
325 /** True when the history was too long to read in full. */
326 partial: boolean;
327};
328
329/** A branch and the commit it points to. */
330export type Branch = { name: string; hash: string };
331
332/** Each entry's last commit; `complete` is false when some were not reached. */
333export type LastCommits = { entries: { name: string; commit: Commit }[]; complete: boolean };
334
335/** A tag, and its commit when it could be read. */
336export type Tag = { name: string; commit: Commit | null };
337
338/** Files at a commit, and whether there were more than were listed. */
339export type FileList = { commit: string | null; files: { path: string; hash: string | null }[]; truncated: boolean };
340
341/** One blob's bytes, standard base64; null when missing or too large. */
342export type RawBlob = { hash: string; size: number; data: string | null };
343
344/**
345 * What came of bringing a pull request up to date with the default branch
346 * without a sandbox. `needs_agent` pushed nothing: the runner's `update`
347 * merges it in a sandbox, with an agent if it conflicts.
348 */
349export type PullBranchUpdate =
350 | { outcome: "updated"; commit: string; previous: string }
351 | { outcome: "up_to_date"; commit: string }
352 | {
353 outcome: "needs_agent";
354 /**
355 * `overlap`: both sides changed some of the same files. `conflicting`:
356 * merging is known to conflict. `unsupported`: it could not be worked
357 * out without git, such as for a very large change.
358 */
359 reason: "overlap" | "conflicting" | "unsupported";
360 detail: string;
361 /** The files both changed, or that conflict, when known. */
362 paths: string[];
363 };
364
365export type DiffLine = {
366 kind: "context" | "add" | "delete";
367 /** Line number in the old file; null for added lines. */
368 old: number | null;
369 /** Line number in the new file; null for deleted lines. */
370 new: number | null;
371 text: string;
372};
373
374/** A run of changed lines with their surrounding context. */
375export type Hunk = { lines: DiffLine[] };
376
377export type FileDiff = {
378 path: string;
379 status: "added" | "modified" | "deleted";
380 additions: number;
381 deletions: number;
382 /** True when the file is binary or too large, so no lines are shown. */
383 binary: boolean;
384 hunks: Hunk[];
385};
386
387/** What changed between two commits. */
388export type Comparison = {
389 base: string | null;
390 head: string;
391 files: FileDiff[];
392 /** True when the change was too large to return in full. */
393 truncated: boolean;
394};