| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! The JSON bodies of the hub's routes, section by section of |
| 5 | //! `docs/hub.md`. Every field that holds a braid id is 64 hex digits on the |
| 6 | //! wire and an [`ObjectId`] here. |
| 7 | |
| 8 | use std::collections::BTreeMap; |
| 9 | |
| 10 | use braid_format::ObjectId; |
| 11 | use serde::{Deserialize, Serialize}; |
| 12 | |
| 13 | use crate::cards::RepoRef; |
| 14 | use crate::hex; |
| 15 | |
| 16 | /// An error body: `{"error", "detail"}`, with `ids` for `missing`, |
| 17 | /// `retry_after` for `rate_limited`, and `head` for `not_on_branch`. |
| 18 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 19 | pub struct ErrorBody { |
| 20 | /// The error's code: `unauthorized` (401), `forbidden` (403), |
| 21 | /// `not_found` (404), `method_not_allowed` (405), `exists` (409), |
| 22 | /// `invalid` (400), `missing` (409), `not_on_branch` (409), `too_large` |
| 23 | /// (413), `rate_limited` (429) or `internal` (500). |
| 24 | pub error: String, |
| 25 | /// What went wrong, for people. |
| 26 | pub detail: String, |
| 27 | /// With `missing`: the objects the hub needs. |
| 28 | #[serde(default, skip_serializing_if = "Vec::is_empty", with = "hex::ids")] |
| 29 | pub ids: Vec<ObjectId>, |
| 30 | /// With `rate_limited`: seconds to wait. |
| 31 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 32 | pub retry_after: Option<u64>, |
| 33 | /// With `not_on_branch`: where the ref points now, if it exists, as 64 |
| 34 | /// hex digits. Text rather than an [`ObjectId`], so that an error stays |
| 35 | /// small. |
| 36 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 37 | pub head: Option<String>, |
| 38 | } |
| 39 | |
| 40 | /// `GET /v1`: the protocol version the hub speaks, and the sizes a client |
| 41 | /// keeps its requests within. |
| 42 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 43 | pub struct Root { |
| 44 | /// The protocol version: 1. |
| 45 | pub version: u32, |
| 46 | /// What one request may carry or ask for. |
| 47 | pub limits: RootLimits, |
| 48 | } |
| 49 | |
| 50 | /// The sizes of section 12 a hub enforces, as `GET /v1` gives them. |
| 51 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 52 | pub struct RootLimits { |
| 53 | /// The largest request body, and so the largest object stream. |
| 54 | pub max_body: u64, |
| 55 | /// The largest object of a `PUT …/o/{id}`. |
| 56 | pub max_put: u64, |
| 57 | /// The largest JSON request body. |
| 58 | pub max_json: u64, |
| 59 | /// The most ids of one list: `lacks`, `want`, `have`, a stack. |
| 60 | pub max_ids: u64, |
| 61 | /// The most path prefixes of a `missing` request. |
| 62 | pub max_paths: u64, |
| 63 | /// The entries of a `missing` page without a `limit`. |
| 64 | pub missing_page: u64, |
| 65 | /// The most entries of a `missing` page. |
| 66 | pub missing_page_max: u64, |
| 67 | /// The most objects one `missing` walk lists. |
| 68 | pub missing_objects: u64, |
| 69 | /// The most entries of a tree listing. |
| 70 | pub tree_entries: u64, |
| 71 | /// The deepest tree listing. |
| 72 | pub tree_depth: u32, |
| 73 | /// The most repositories, tokens or collaborators a list holds. |
| 74 | pub max_list: u64, |
| 75 | /// The sizes of issues, change requests, topics and browsing, from a |
| 76 | /// hub that has them. |
| 77 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 78 | pub social: Option<SocialCaps>, |
| 79 | } |
| 80 | |
| 81 | /// The sizes of the social features and of browsing a hub enforces, as |
| 82 | /// `GET /v1` gives them in `limits.social`. |
| 83 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 84 | pub struct SocialCaps { |
| 85 | /// The longest title of an issue or a change request, in characters. |
| 86 | pub title: u64, |
| 87 | /// The largest body of an issue, a comment or a review, in bytes. |
| 88 | pub text: u64, |
| 89 | /// The most entries of one page of a list. |
| 90 | pub page: u64, |
| 91 | /// The most topics of a repository. |
| 92 | pub topics: u64, |
| 93 | /// The longest topic, in characters. |
| 94 | pub topic: u64, |
| 95 | /// The most labels of a repository. |
| 96 | pub labels: u64, |
| 97 | /// The most labels of one thread. |
| 98 | pub thread_labels: u64, |
| 99 | /// The most assignees of one thread. |
| 100 | pub assignees: u64, |
| 101 | /// The most comments of one thread: it locks itself at that many. |
| 102 | pub comments_per_thread: u64, |
| 103 | /// The most line comments of one review. |
| 104 | pub review_comments: u64, |
| 105 | /// The most mentions of one text that notify. |
| 106 | pub mentions: u64, |
| 107 | /// The most references one text links. |
| 108 | pub references: u64, |
| 109 | /// The most changesets of one change request. |
| 110 | pub request_changesets: u64, |
| 111 | /// The most bytes a change request from another repository brings. |
| 112 | pub request_bytes: u64, |
| 113 | /// The most entries of one page of a directory. |
| 114 | pub dir_page: u64, |
| 115 | /// The largest file shown as text, in bytes. |
| 116 | pub text_view: u64, |
| 117 | } |
| 118 | |
| 119 | // ---------------------------------------------------------------- section 2 |
| 120 | |
| 121 | /// `POST /v1/login/device` → the start of a device flow. |
| 122 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 123 | pub struct DeviceStart { |
| 124 | /// What to send back when polling. |
| 125 | pub poll: String, |
| 126 | /// The code the person types at `verification_uri`. |
| 127 | pub user_code: String, |
| 128 | /// Where the person goes. |
| 129 | pub verification_uri: String, |
| 130 | /// Seconds until the code expires. |
| 131 | pub expires_in: u64, |
| 132 | /// Seconds to wait between polls. |
| 133 | pub interval: u64, |
| 134 | } |
| 135 | |
| 136 | /// `POST /v1/login/device/poll` request. |
| 137 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 138 | pub struct DevicePollRequest { |
| 139 | /// From [`DeviceStart::poll`]. |
| 140 | pub poll: String, |
| 141 | } |
| 142 | |
| 143 | /// `POST /v1/login/device/poll` response. |
| 144 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 145 | #[serde(tag = "status", rename_all = "snake_case")] |
| 146 | pub enum DevicePoll { |
| 147 | /// The person has not approved yet. |
| 148 | Pending, |
| 149 | /// Poll less often. |
| 150 | SlowDown { |
| 151 | /// The new interval, in seconds. |
| 152 | interval: u64, |
| 153 | }, |
| 154 | /// The person refused. |
| 155 | Denied, |
| 156 | /// The code expired. |
| 157 | Expired, |
| 158 | /// Signed in. |
| 159 | Ok { |
| 160 | /// The new token, shown this once. |
| 161 | token: String, |
| 162 | /// The GitHub login. |
| 163 | login: String, |
| 164 | /// When the token expires. |
| 165 | expires: String, |
| 166 | }, |
| 167 | } |
| 168 | |
| 169 | // ---------------------------------------------------------------- section 3 |
| 170 | |
| 171 | /// A token's scope: rights on repositories. |
| 172 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 173 | pub struct Scope { |
| 174 | /// `owner/name`, `owner/*`, or `*`. |
| 175 | pub repo: String, |
| 176 | /// `read`, `write`, `land:<glob>`, `admin`. |
| 177 | pub rights: Vec<String>, |
| 178 | } |
| 179 | |
| 180 | /// What `GET /v1/me` says about the token that asked. |
| 181 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 182 | pub struct TokenInfo { |
| 183 | /// The token's public id. |
| 184 | pub id: String, |
| 185 | /// A note, for people. |
| 186 | #[serde(default)] |
| 187 | pub note: String, |
| 188 | /// Its scopes. |
| 189 | pub scopes: Vec<Scope>, |
| 190 | /// When it was made. |
| 191 | #[serde(default)] |
| 192 | pub created: String, |
| 193 | /// When it expires. |
| 194 | pub expires: String, |
| 195 | /// When it was last used, if ever. |
| 196 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 197 | pub last_used: Option<String>, |
| 198 | } |
| 199 | |
| 200 | /// `GET /v1/me`. |
| 201 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 202 | pub struct Me { |
| 203 | /// The GitHub login. |
| 204 | pub login: String, |
| 205 | /// The display name, if any. |
| 206 | #[serde(default)] |
| 207 | pub name: Option<String>, |
| 208 | /// The avatar, if any. |
| 209 | #[serde(default)] |
| 210 | pub avatar_url: Option<String>, |
| 211 | /// Whether the hub holds GitHub repository access for the account. |
| 212 | #[serde(default)] |
| 213 | pub github_repo_access: bool, |
| 214 | /// The token that asked, when one did. |
| 215 | #[serde(default)] |
| 216 | pub token: Option<TokenInfo>, |
| 217 | /// The account's settings. |
| 218 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 219 | pub settings: Option<MeSettings>, |
| 220 | /// The scopes of the GitHub access the account granted the hub, as |
| 221 | /// GitHub wrote them, when it granted any. |
| 222 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 223 | pub github_scope: Option<String>, |
| 224 | } |
| 225 | |
| 226 | /// The settings of an account, in `GET /v1/me`; `PATCH /v1/me` changes |
| 227 | /// them. |
| 228 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 229 | pub struct MeSettings { |
| 230 | /// What text and data mining new public repositories start with: |
| 231 | /// `allowed` or `reserved`. |
| 232 | pub tdm_default: String, |
| 233 | /// Whether the repositories the account starred show on its profile. |
| 234 | pub stars_public: bool, |
| 235 | } |
| 236 | |
| 237 | /// `POST /v1/tokens` request. |
| 238 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 239 | pub struct TokenCreate { |
| 240 | /// A note, for people. |
| 241 | #[serde(default)] |
| 242 | pub note: String, |
| 243 | /// The scopes. |
| 244 | pub scopes: Vec<Scope>, |
| 245 | /// Days until it expires, at most 366. |
| 246 | pub expires_in_days: u32, |
| 247 | } |
| 248 | |
| 249 | /// `POST /v1/tokens` response. |
| 250 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 251 | pub struct TokenCreated { |
| 252 | /// The new token, shown this once. |
| 253 | pub token: String, |
| 254 | /// Its public id. |
| 255 | pub id: String, |
| 256 | /// Its scopes. |
| 257 | pub scopes: Vec<Scope>, |
| 258 | /// When it expires. |
| 259 | pub expires: String, |
| 260 | } |
| 261 | |
| 262 | /// `GET /v1/tokens`. |
| 263 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 264 | pub struct TokenList { |
| 265 | /// The account's tokens. |
| 266 | pub tokens: Vec<TokenInfo>, |
| 267 | } |
| 268 | |
| 269 | // ---------------------------------------------------------------- section 4 |
| 270 | |
| 271 | /// `POST /v1/repos` request. |
| 272 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 273 | pub struct RepoCreate { |
| 274 | /// The repository's name; the owner is the caller. |
| 275 | pub name: String, |
| 276 | /// Private unless `false`. |
| 277 | #[serde(default = "yes")] |
| 278 | pub private: bool, |
| 279 | /// A description. |
| 280 | #[serde(default)] |
| 281 | pub description: String, |
| 282 | /// A git URL to take the history from first. |
| 283 | #[serde(default)] |
| 284 | pub import: Option<String>, |
| 285 | /// A git URL to publish the default branch to. |
| 286 | #[serde(default)] |
| 287 | pub mirror: Option<String>, |
| 288 | } |
| 289 | |
| 290 | fn yes() -> bool { |
| 291 | true |
| 292 | } |
| 293 | |
| 294 | /// A mirror's state. |
| 295 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 296 | pub struct MirrorInfo { |
| 297 | /// The git URL. |
| 298 | pub url: String, |
| 299 | /// When it was last synced, if ever. |
| 300 | #[serde(default)] |
| 301 | pub last_sync: Option<String>, |
| 302 | /// The last sync's outcome code (`pushed`, `up_to_date`, `diverged`, |
| 303 | /// …), if any. |
| 304 | #[serde(default)] |
| 305 | pub outcome: Option<String>, |
| 306 | /// More about it, for people. |
| 307 | #[serde(default)] |
| 308 | pub detail: Option<String>, |
| 309 | /// How the hub takes in what is pushed to the mirror: `off`, `ff-only` |
| 310 | /// or `merge`. Absent is `off`. |
| 311 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 312 | pub follow: Option<String>, |
| 313 | /// The GitHub repository's numeric id, which stays when it is renamed. |
| 314 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 315 | pub github_id: Option<u64>, |
| 316 | /// The git commit both sides held after the last publish, follow or |
| 317 | /// import, as 40 hex digits. |
| 318 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 319 | pub synced: Option<String>, |
| 320 | /// How the hub hears of pushes to the mirror, such as `webhook` or |
| 321 | /// `polling`. |
| 322 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 323 | pub hook: Option<String>, |
| 324 | } |
| 325 | |
| 326 | /// A repository, as `GET /v1/r/{owner}/{name}` and others return it. |
| 327 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 328 | pub struct RepoInfo { |
| 329 | /// The repository's id, which no other repository ever takes, not even |
| 330 | /// one made later under the same name. |
| 331 | #[serde(default, skip_serializing_if = "String::is_empty")] |
| 332 | pub id: String, |
| 333 | /// The owner's login, lowercase. |
| 334 | pub owner: String, |
| 335 | /// The name. |
| 336 | pub name: String, |
| 337 | /// Whether only those given access can read it. |
| 338 | pub private: bool, |
| 339 | /// A description. |
| 340 | #[serde(default)] |
| 341 | pub description: String, |
| 342 | /// The branch the mirror publishes and the web shows. |
| 343 | pub default_branch: String, |
| 344 | /// When it was made. |
| 345 | pub created: String, |
| 346 | /// The mirror, if any. |
| 347 | #[serde(default)] |
| 348 | pub mirror: Option<MirrorInfo>, |
| 349 | /// The caller's permission: `read`, `write` or `admin`. |
| 350 | pub permission: String, |
| 351 | /// Its topics: lowercase letters, digits and `-`. |
| 352 | #[serde(default)] |
| 353 | pub topics: Vec<String>, |
| 354 | /// The repository it was forked from, when the caller may read that |
| 355 | /// one. |
| 356 | #[serde(default)] |
| 357 | pub fork_of: Option<RepoRef>, |
| 358 | /// Text and data mining of its content: `allowed` or `reserved`. |
| 359 | #[serde(default, skip_serializing_if = "String::is_empty")] |
| 360 | pub tdm: String, |
| 361 | /// When a landing last moved one of its refs, if one ever did. |
| 362 | #[serde(default)] |
| 363 | pub pushed: Option<String>, |
| 364 | /// Its counts, where one repository is shown. |
| 365 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 366 | pub counts: Option<RepoCounts>, |
| 367 | /// The caller's own state on it, for a signed-in caller. |
| 368 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 369 | pub viewer: Option<RepoViewer>, |
| 370 | /// Its settings, for its admins. |
| 371 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 372 | pub settings: Option<RepoSettingsInfo>, |
| 373 | } |
| 374 | |
| 375 | /// The counts of a repository, as the caller may see them. |
| 376 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 377 | pub struct RepoCounts { |
| 378 | /// The accounts that starred it. |
| 379 | pub stars: u64, |
| 380 | /// Its forks the caller may see. |
| 381 | pub forks: u64, |
| 382 | /// The accounts that watch all of it. |
| 383 | pub watchers: u64, |
| 384 | /// Its open issues. |
| 385 | pub open_issues: u64, |
| 386 | /// Its open change requests. |
| 387 | pub open_requests: u64, |
| 388 | } |
| 389 | |
| 390 | /// What the caller itself does with a repository. |
| 391 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 392 | pub struct RepoViewer { |
| 393 | /// Whether it starred it. |
| 394 | pub starred: bool, |
| 395 | /// How it watches it: `all`, `participating` or `ignore`. |
| 396 | pub watching: String, |
| 397 | } |
| 398 | |
| 399 | /// The settings of a repository, as its admins see them. |
| 400 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 401 | pub struct RepoSettingsInfo { |
| 402 | /// Whether it takes issues. |
| 403 | pub issues: bool, |
| 404 | /// Who may open change requests: `readers`, `collaborators` or `off`. |
| 405 | pub requests_from: String, |
| 406 | /// Whether it may be forked. |
| 407 | pub allow_forks: bool, |
| 408 | /// The approvals a change request needs before it lands, 0 to 10. |
| 409 | pub require_approvals: u32, |
| 410 | /// How its mirror is followed (`off`, `ff-only` or `merge`), when it |
| 411 | /// has a mirror. |
| 412 | #[serde(default)] |
| 413 | pub mirror_follow: Option<String>, |
| 414 | } |
| 415 | |
| 416 | /// `GET /v1/repos`. |
| 417 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 418 | pub struct RepoList { |
| 419 | /// The repositories. |
| 420 | pub repos: Vec<RepoInfo>, |
| 421 | } |
| 422 | |
| 423 | /// `PATCH /v1/r/{owner}/{name}`: the fields to change. |
| 424 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 425 | pub struct RepoPatch { |
| 426 | /// New visibility. |
| 427 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 428 | pub private: Option<bool>, |
| 429 | /// New description. |
| 430 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 431 | pub description: Option<String>, |
| 432 | /// New mirror URL; an empty string removes the mirror. |
| 433 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 434 | pub mirror: Option<String>, |
| 435 | /// New default branch. |
| 436 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 437 | pub default_branch: Option<String>, |
| 438 | /// New topics, replacing the old ones. |
| 439 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 440 | pub topics: Option<Vec<String>>, |
| 441 | /// Text and data mining: `allowed` or `reserved`. |
| 442 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 443 | pub tdm: Option<String>, |
| 444 | /// Whether it takes issues. |
| 445 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 446 | pub issues: Option<bool>, |
| 447 | /// Who may open change requests: `readers`, `collaborators` or `off`. |
| 448 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 449 | pub requests_from: Option<String>, |
| 450 | /// Whether it may be forked. |
| 451 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 452 | pub allow_forks: Option<bool>, |
| 453 | /// The approvals a change request needs before it lands, 0 to 10. |
| 454 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 455 | pub require_approvals: Option<u32>, |
| 456 | /// How the mirror is followed: `off`, `ff-only` or `merge`. |
| 457 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 458 | pub mirror_follow: Option<String>, |
| 459 | } |
| 460 | |
| 461 | /// `PUT …/collaborators/{login}`. |
| 462 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 463 | pub struct Collaborator { |
| 464 | /// `read`, `write` or `admin`. |
| 465 | pub permission: String, |
| 466 | } |
| 467 | |
| 468 | /// One entry of `GET …/collaborators`. |
| 469 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 470 | pub struct CollaboratorEntry { |
| 471 | /// The collaborator's login, lowercase. |
| 472 | pub login: String, |
| 473 | /// `read`, `write` or `admin`. |
| 474 | pub permission: String, |
| 475 | } |
| 476 | |
| 477 | /// `GET …/collaborators`. |
| 478 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 479 | pub struct CollaboratorList { |
| 480 | /// Every collaborator, by login. |
| 481 | pub collaborators: Vec<CollaboratorEntry>, |
| 482 | } |
| 483 | |
| 484 | // ---------------------------------------------------------------- section 6 |
| 485 | |
| 486 | /// `POST …/lacks` request. |
| 487 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 488 | pub struct LacksRequest { |
| 489 | /// Ids to check, at most 10,000. |
| 490 | #[serde(with = "hex::ids")] |
| 491 | pub ids: Vec<ObjectId>, |
| 492 | } |
| 493 | |
| 494 | /// `POST …/lacks` response. |
| 495 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 496 | pub struct LacksResponse { |
| 497 | /// The ids the hub does not store. |
| 498 | #[serde(with = "hex::ids")] |
| 499 | pub lacks: Vec<ObjectId>, |
| 500 | } |
| 501 | |
| 502 | /// `POST …/objects` response. |
| 503 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 504 | pub struct Stored { |
| 505 | /// How many records were stored. |
| 506 | pub stored: u64, |
| 507 | } |
| 508 | |
| 509 | /// `POST …/missing` request. |
| 510 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 511 | pub struct MissingRequest { |
| 512 | /// Changesets wanted. |
| 513 | #[serde(with = "hex::ids")] |
| 514 | pub want: Vec<ObjectId>, |
| 515 | /// Changesets the client holds with all they reach. |
| 516 | #[serde(default, with = "hex::ids")] |
| 517 | pub have: Vec<ObjectId>, |
| 518 | /// `None` for every ancestor, `Some(0)` for the wanted changesets only, |
| 519 | /// `Some(n)` for n generations. |
| 520 | #[serde(default)] |
| 521 | pub history: Option<u32>, |
| 522 | /// Path prefixes to limit trees to. |
| 523 | #[serde(default)] |
| 524 | pub paths: Option<Vec<String>>, |
| 525 | /// Entries per page. |
| 526 | #[serde(default)] |
| 527 | pub limit: Option<u32>, |
| 528 | /// The cursor from the page before. |
| 529 | #[serde(default)] |
| 530 | pub after: Option<String>, |
| 531 | } |
| 532 | |
| 533 | /// One object a client is missing. |
| 534 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 535 | pub struct MissingEntry { |
| 536 | /// Its id. |
| 537 | #[serde(with = "hex::id")] |
| 538 | pub id: ObjectId, |
| 539 | /// Its kind's name. |
| 540 | pub kind: String, |
| 541 | /// Its size in bytes. |
| 542 | pub size: u64, |
| 543 | } |
| 544 | |
| 545 | /// `POST …/missing` response, as JSON. |
| 546 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 547 | pub struct MissingResponse { |
| 548 | /// The objects, a changeset before what it names. |
| 549 | pub missing: Vec<MissingEntry>, |
| 550 | /// The cursor for the next page, or `None` when done. |
| 551 | pub next: Option<String>, |
| 552 | } |
| 553 | |
| 554 | // ---------------------------------------------------------------- section 8 |
| 555 | |
| 556 | /// `GET …/refs`, and each `refs` event of the watch. |
| 557 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 558 | pub struct Refs { |
| 559 | /// The op log's sequence number. |
| 560 | pub seq: u64, |
| 561 | /// Every ref. |
| 562 | #[serde(with = "hex::id_map")] |
| 563 | pub refs: BTreeMap<String, ObjectId>, |
| 564 | } |
| 565 | |
| 566 | /// How a landing goes. |
| 567 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 568 | #[serde(rename_all = "kebab-case")] |
| 569 | pub enum LandMode { |
| 570 | /// Fast-forward, or rebase onto the head. |
| 571 | #[default] |
| 572 | Rebase, |
| 573 | /// Fast-forward only; refuse otherwise. |
| 574 | FfOnly, |
| 575 | } |
| 576 | |
| 577 | /// `POST …/land` request. |
| 578 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 579 | pub struct LandRequest { |
| 580 | /// The ref to land on. |
| 581 | #[serde(rename = "ref")] |
| 582 | pub reference: String, |
| 583 | /// The stack, oldest first. |
| 584 | #[serde(with = "hex::ids")] |
| 585 | pub changes: Vec<ObjectId>, |
| 586 | /// How. |
| 587 | #[serde(default)] |
| 588 | pub mode: LandMode, |
| 589 | /// Who is landing, for the op log. |
| 590 | #[serde(default)] |
| 591 | pub agent: Option<String>, |
| 592 | } |
| 593 | |
| 594 | /// Where one changeset of a stack went. |
| 595 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 596 | pub struct Placed { |
| 597 | /// The changeset as sent. |
| 598 | #[serde(with = "hex::id")] |
| 599 | pub from: ObjectId, |
| 600 | /// What holds it now, or `None` when it went nowhere. |
| 601 | #[serde(with = "hex::opt_id")] |
| 602 | pub to: Option<ObjectId>, |
| 603 | } |
| 604 | |
| 605 | /// `POST …/land` response. |
| 606 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 607 | pub struct LandResponse { |
| 608 | /// The ref. |
| 609 | #[serde(rename = "ref")] |
| 610 | pub reference: String, |
| 611 | /// `fast-forward`, `rebased`, `already-applied`, `conflicts` or |
| 612 | /// `refused`. |
| 613 | pub outcome: String, |
| 614 | /// The ref's head before. |
| 615 | #[serde(with = "hex::opt_id")] |
| 616 | pub head_before: Option<ObjectId>, |
| 617 | /// Its head after. |
| 618 | #[serde(with = "hex::opt_id")] |
| 619 | pub head: Option<ObjectId>, |
| 620 | /// Where each changeset of the stack went. |
| 621 | pub landed: Vec<Placed>, |
| 622 | /// Paths both sides changed, merged. |
| 623 | #[serde(default)] |
| 624 | pub merged_paths: Vec<String>, |
| 625 | /// With `conflicts`: each conflict, as `braid land --json` prints it |
| 626 | /// (see [`crate::json::conflict`]). |
| 627 | #[serde(default)] |
| 628 | pub conflicts: Vec<serde_json::Value>, |
| 629 | /// The changeset that conflicts. |
| 630 | #[serde(default, with = "hex::opt_id")] |
| 631 | pub change: Option<ObjectId>, |
| 632 | /// The git commit the head exports to, or `None` when the hub cannot |
| 633 | /// export it: never a commit the head would not export to. |
| 634 | #[serde(default)] |
| 635 | pub git_commit: Option<String>, |
| 636 | /// The op log operation that moved the ref, or `None` when it did not |
| 637 | /// move. |
| 638 | #[serde(default)] |
| 639 | pub operation: Option<u64>, |
| 640 | } |
| 641 | |
| 642 | // ---------------------------------------------------------------- section 9 |
| 643 | |
| 644 | /// `GET …/git/commit/{sha1}`. |
| 645 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 646 | pub struct GitCommit { |
| 647 | /// The changeset. |
| 648 | #[serde(with = "hex::id")] |
| 649 | pub changeset: ObjectId, |
| 650 | /// Its root tree. |
| 651 | #[serde(with = "hex::id")] |
| 652 | pub tree: ObjectId, |
| 653 | /// The git tree of the commit. |
| 654 | pub git_tree: String, |
| 655 | } |
| 656 | |
| 657 | /// `GET …/git/changeset/{id}`. |
| 658 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 659 | pub struct GitChangeset { |
| 660 | /// The changeset. |
| 661 | #[serde(with = "hex::id")] |
| 662 | pub changeset: ObjectId, |
| 663 | /// The git commit it exports to. |
| 664 | pub git_commit: String, |
| 665 | } |
| 666 | |
| 667 | // --------------------------------------------------------------- section 10 |
| 668 | |
| 669 | /// One entry of a tree listing. |
| 670 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 671 | pub struct TreeEntry { |
| 672 | /// The path, from the repository's root. |
| 673 | pub path: String, |
| 674 | /// `file`, `executable`, `symlink`, `tree` or `gitlink`. |
| 675 | pub kind: String, |
| 676 | /// The braid id (blob or tree), or the git commit of a gitlink. |
| 677 | pub id: String, |
| 678 | /// The size, for files and symlinks. |
| 679 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 680 | pub size: Option<u64>, |
| 681 | /// A directory below the requested depth. |
| 682 | #[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 683 | pub truncated: bool, |
| 684 | } |
| 685 | |
| 686 | /// `GET …/t/{tree}`. |
| 687 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 688 | pub struct TreeListing { |
| 689 | /// The tree listed. |
| 690 | #[serde(with = "hex::id")] |
| 691 | pub tree: ObjectId, |
| 692 | /// The prefix asked for. |
| 693 | pub prefix: String, |
| 694 | /// The entries. |
| 695 | pub entries: Vec<TreeEntry>, |
| 696 | } |
| 697 | |
| 698 | #[cfg(test)] |
| 699 | mod tests { |
| 700 | use super::*; |
| 701 | use braid_format::Kind; |
| 702 | |
| 703 | #[test] |
| 704 | fn device_poll_is_tagged_by_status() { |
| 705 | let ok = DevicePoll::Ok { |
| 706 | token: "brd_x".into(), |
| 707 | login: "ada".into(), |
| 708 | expires: "2027-01-01T00:00:00Z".into(), |
| 709 | }; |
| 710 | let text = serde_json::to_string(&ok).unwrap(); |
| 711 | assert!(text.contains("\"status\":\"ok\""), "{text}"); |
| 712 | assert_eq!(serde_json::from_str::<DevicePoll>(&text).unwrap(), ok); |
| 713 | assert_eq!( |
| 714 | serde_json::from_str::<DevicePoll>(r#"{"status":"slow_down","interval":10}"#).unwrap(), |
| 715 | DevicePoll::SlowDown { interval: 10 } |
| 716 | ); |
| 717 | } |
| 718 | |
| 719 | #[test] |
| 720 | fn land_request_on_the_wire() { |
| 721 | let id = ObjectId::hash(Kind::Changeset, b"x"); |
| 722 | let request = LandRequest { |
| 723 | reference: "main".into(), |
| 724 | changes: vec![id], |
| 725 | mode: LandMode::FfOnly, |
| 726 | agent: Some("agent-1".into()), |
| 727 | }; |
| 728 | let text = serde_json::to_string(&request).unwrap(); |
| 729 | assert!(text.contains("\"ref\":\"main\""), "{text}"); |
| 730 | assert!(text.contains("\"mode\":\"ff-only\""), "{text}"); |
| 731 | assert!(text.contains(&id.to_hex())); |
| 732 | assert_eq!(serde_json::from_str::<LandRequest>(&text).unwrap(), request); |
| 733 | // The mode defaults to rebase. |
| 734 | let bare: LandRequest = |
| 735 | serde_json::from_str(&format!(r#"{{"ref":"main","changes":["{id}"]}}"#)).unwrap(); |
| 736 | assert_eq!(bare.mode, LandMode::Rebase); |
| 737 | // A bad id is refused. |
| 738 | assert!( |
| 739 | serde_json::from_str::<LandRequest>(r#"{"ref":"main","changes":["xyz"]}"#).is_err() |
| 740 | ); |
| 741 | } |
| 742 | |
| 743 | #[test] |
| 744 | fn what_an_older_hub_sends_still_reads() { |
| 745 | let info: RepoInfo = serde_json::from_str( |
| 746 | r#"{"owner":"ada","name":"demo","private":true,"description":"","default_branch":"main","created":"2026-01-01T00:00:00Z","mirror":{"url":"https://github.com/ada/demo.git"},"permission":"read"}"#, |
| 747 | ) |
| 748 | .unwrap(); |
| 749 | assert!(info.id.is_empty() && info.topics.is_empty() && info.fork_of.is_none()); |
| 750 | assert!(info.counts.is_none() && info.viewer.is_none() && info.settings.is_none()); |
| 751 | let mirror = info.mirror.as_ref().unwrap(); |
| 752 | assert_eq!((&mirror.follow, mirror.github_id), (&None, None)); |
| 753 | // Written again, it says no more than it was told, but `topics`, |
| 754 | // `fork_of` and `pushed`. |
| 755 | let text = serde_json::to_string(&info).unwrap(); |
| 756 | for absent in [ |
| 757 | "\"id\"", "counts", "viewer", "settings", "tdm", "follow", "synced", |
| 758 | ] { |
| 759 | assert!(!text.contains(absent), "{absent}: {text}"); |
| 760 | } |
| 761 | let root: Root = serde_json::from_str( |
| 762 | r#"{"version":1,"limits":{"max_body":1,"max_put":1,"max_json":1,"max_ids":1,"max_paths":1,"missing_page":1,"missing_page_max":1,"missing_objects":1,"tree_entries":1,"tree_depth":1,"max_list":1}}"#, |
| 763 | ) |
| 764 | .unwrap(); |
| 765 | assert_eq!(root.limits.social, None); |
| 766 | let me: Me = serde_json::from_str(r#"{"login":"ada"}"#).unwrap(); |
| 767 | assert_eq!((me.settings, me.github_scope), (None, None)); |
| 768 | } |
| 769 | |
| 770 | #[test] |
| 771 | fn refs_and_repo_create_defaults() { |
| 772 | let id = ObjectId::hash(Kind::Changeset, b"y"); |
| 773 | let refs = Refs { |
| 774 | seq: 3, |
| 775 | refs: [("main".to_owned(), id)].into_iter().collect(), |
| 776 | }; |
| 777 | let text = serde_json::to_string(&refs).unwrap(); |
| 778 | assert_eq!(serde_json::from_str::<Refs>(&text).unwrap(), refs); |
| 779 | let create: RepoCreate = serde_json::from_str(r#"{"name":"demo"}"#).unwrap(); |
| 780 | assert!(create.private); |
| 781 | assert_eq!(create.import, None); |
| 782 | } |
| 783 | } |