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