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