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