OXYGENHUB
# 681db69eba17 oxygenhub / crates / hub-api / src / lib.rs
33.3 KiB · Rust · last changed AI assistants review proposals and draft their descriptions 1 month ago source blame history raw
8d2caf12 Andy McCall ↶ 1
//! Types of the OxygenHub JSON API (hub spec §12.1). Everything here is
d23464c0 Andy McCall ↶ 2
//! `serde` data with no behaviour, so clients in other languages can be
3
//! generated from it and the server can never drift from them.
4
//!
5
//! Shapes reuse `oxy --json`'s where one exists (hashes as lowercase hex,
6
//! times as milliseconds since the epoch), so tools written against the
7
//! command line work against the hub.
8
b64e2537 Andy McCall ↶ 9
use schemars::JsonSchema;
d23464c0 Andy McCall ↶ 10
use serde::{Deserialize, Serialize};
11
12
/// A repository as the API lists it.
b64e2537 Andy McCall ↶ 13
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 14
pub struct Repository {
15
    pub owner: String,
16
    pub name: String,
17
    pub description: String,
18
    pub default_bookmark: String,
19
    pub visibility: Visibility,
20
    /// Milliseconds since the epoch.
21
    pub created_ms: i64,
22
    pub updated_ms: i64,
23
    /// `https://…/{owner}/{name}`, what `oxy clone` takes.
24
    pub clone_url: String,
25
}
26
b64e2537 Andy McCall ↶ 27
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 28
#[serde(rename_all = "lowercase")]
29
pub enum Visibility {
30
    Public,
31
    Internal,
32
    Private,
33
}
34
35
impl Visibility {
36
    pub fn as_str(self) -> &'static str {
37
        match self {
38
            Visibility::Public => "public",
39
            Visibility::Internal => "internal",
40
            Visibility::Private => "private",
41
        }
42
    }
43
44
    pub fn parse(s: &str) -> Option<Visibility> {
45
        Some(match s {
46
            "public" => Visibility::Public,
47
            "internal" => Visibility::Internal,
48
            "private" => Visibility::Private,
49
            _ => return None,
50
        })
51
    }
52
}
53
54
/// A bookmark: a name for a set of heads (spec §10.1).
b64e2537 Andy McCall ↶ 55
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 56
pub struct Bookmark {
57
    pub name: String,
58
    pub heads: Vec<String>,
59
    /// Number of changes in the bookmark's state.
60
    pub changes: usize,
61
    pub default: bool,
62
}
63
64
/// A tag: a name for one state, with its attestation when signed.
b64e2537 Andy McCall ↶ 65
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 66
pub struct Tag {
67
    pub name: String,
68
    pub heads: Vec<String>,
69
    pub message: String,
70
    pub signer: Option<String>,
71
}
72
73
/// One change in a history listing.
b64e2537 Andy McCall ↶ 74
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 75
pub struct ChangeSummary {
76
    pub hash: String,
77
    pub message: String,
78
    pub author: Author,
79
    pub when_ms: u64,
80
    pub deps: Vec<String>,
81
    pub head: bool,
82
    pub signature: Signature,
8933faff Andy McCall ↶ 83
    /// Signed for the author by a delegate (an agent), when it was.
84
    #[serde(default, skip_serializing_if = "Option::is_none")]
85
    pub via: Option<Via>,
86
}
87
88
/// A delegate that signed a change for its author (Oxygen spec §14.2).
89
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
90
pub struct Via {
91
    pub name: String,
92
    pub key: String,
93
    /// The delegation's scope, `changes` for an agent.
94
    pub scope: String,
95
    pub expires_ms: Option<u64>,
96
    /// The delegation has since been revoked or withdrawn.
97
    pub revoked: bool,
d23464c0 Andy McCall ↶ 98
}
99
100
/// Who wrote a change: the linked account when the signing key is linked,
101
/// else the key alone.
b64e2537 Andy McCall ↶ 102
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 103
pub struct Author {
104
    pub name: String,
105
    pub email: String,
106
    pub key: String,
107
    /// The hub account this key is linked to, when any.
108
    pub account: Option<String>,
760a7dd3 Andy McCall ↶ 109
    /// Set when the change is signed by a key no account has linked, and
110
    /// says how its owner links it.
111
    #[serde(default, skip_serializing_if = "Option::is_none")]
112
    pub note: Option<String>,
113
}
114
115
impl Author {
116
    /// What `note` says for a key that no account has linked.
117
    pub const UNLINKED: &'static str =
118
        "signed by a key not linked to any account; its owner links it under Settings → Keys (/settings/keys)";
119
120
    /// The note for an author with this key and account.
121
    pub fn note_for(key: &str, account: &Option<String>) -> Option<String> {
122
        (!key.is_empty() && account.is_none()).then(|| Self::UNLINKED.to_string())
123
    }
d23464c0 Andy McCall ↶ 124
}
125
126
/// Signature status of a change as `oxy verify` reports it.
b64e2537 Andy McCall ↶ 127
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 128
#[serde(tag = "status", rename_all = "lowercase")]
129
pub enum Signature {
130
    Unsigned,
131
    Valid { key: String, sealed: bool, revoked: bool },
132
    Invalid { reason: String },
133
}
134
135
/// An RFC 7807 problem response.
b64e2537 Andy McCall ↶ 136
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
d23464c0 Andy McCall ↶ 137
pub struct Problem {
138
    pub status: u16,
139
    pub title: String,
140
    pub detail: String,
141
}
142
143
#[cfg(test)]
144
mod tests {
145
    use super::*;
146
147
    #[test]
148
    fn visibility_round_trips() {
149
        for v in [Visibility::Public, Visibility::Internal, Visibility::Private] {
150
            assert_eq!(Visibility::parse(v.as_str()), Some(v));
151
        }
152
        assert_eq!(Visibility::parse("secret"), None);
153
    }
154
}
de505df5 Andy McCall ↶ 155
156
// ---------------------------------------------------------------- proposals
157
158
/// A proposal: a request to add changes to a target bookmark (hub spec §7).
b64e2537 Andy McCall ↶ 159
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
de505df5 Andy McCall ↶ 160
pub struct Proposal {
161
    pub number: i64,
162
    pub title: String,
163
    pub body: String,
164
    pub author: String,
165
    pub state: ProposalState,
166
    pub target: String,
167
    pub source: ProposalSource,
168
    /// The changes the proposal adds, dependency order, newest last.
169
    pub changes: Vec<ChangeSummary>,
170
    /// Heads of the projected state (target plus the changes), when computed.
171
    pub projected_heads: Vec<String>,
172
    pub projected_state: Option<String>,
173
    pub conflicted: bool,
174
    pub approvals: Vec<Approval>,
175
    pub landable: bool,
176
    pub blockers: Vec<String>,
177
    pub created_ms: i64,
178
    pub updated_ms: i64,
179
    /// `https://hub/{owner}/{name}/proposal/{number}`.
180
    pub url: String,
181
    /// Where `oxy` fetches the changes from.
182
    pub repository_url: String,
183
    /// The fork's clone URL when the source is a fork.
184
    pub source_url: Option<String>,
0b20f1ec Andy McCall ↶ 185
    /// Who landed it, how and when, once landed.
186
    #[serde(default)]
187
    pub landed: Option<Landed>,
cc1cd197 Andy McCall ↶ 188
    /// Asked to review and not yet reviewing, in the order asked.
189
    #[serde(default)]
190
    pub requested_reviewers: Vec<String>,
2291f8cc Andy McCall ↶ 191
    #[serde(default)]
192
    pub labels: Vec<Label>,
0b20f1ec Andy McCall ↶ 193
}
194
195
/// How a proposal was landed.
196
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
197
pub struct Landed {
198
    pub by: String,
199
    /// `web`, or `api (token NAME)`.
200
    pub via: String,
201
    pub when_ms: i64,
de505df5 Andy McCall ↶ 202
}
203
b64e2537 Andy McCall ↶ 204
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
de505df5 Andy McCall ↶ 205
#[serde(rename_all = "lowercase")]
206
pub enum ProposalState {
207
    Draft,
208
    Open,
209
    Landed,
210
    Closed,
211
}
212
213
impl ProposalState {
214
    pub fn as_str(self) -> &'static str {
215
        match self {
216
            ProposalState::Draft => "draft",
217
            ProposalState::Open => "open",
218
            ProposalState::Landed => "landed",
219
            ProposalState::Closed => "closed",
220
        }
221
    }
222
223
    pub fn parse(s: &str) -> Option<ProposalState> {
224
        Some(match s {
225
            "draft" => ProposalState::Draft,
226
            "open" => ProposalState::Open,
227
            "landed" => ProposalState::Landed,
228
            "closed" => ProposalState::Closed,
229
            _ => return None,
230
        })
231
    }
232
}
233
b64e2537 Andy McCall ↶ 234
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
de505df5 Andy McCall ↶ 235
#[serde(tag = "kind", rename_all = "lowercase")]
236
pub enum ProposalSource {
237
    /// A bookmark of the same repository.
238
    Bookmark { bookmark: String },
239
    /// A bookmark of a fork.
240
    Fork { owner: String, name: String, bookmark: String },
241
    /// An explicit list of change hashes.
242
    Changes { hashes: Vec<String> },
243
}
244
245
/// A reviewer's approval, counted only when backed by a seal over every
246
/// change of the proposal signed with a key linked to the reviewer.
b64e2537 Andy McCall ↶ 247
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
de505df5 Andy McCall ↶ 248
pub struct Approval {
249
    pub account: String,
250
    pub sealed: bool,
251
    pub key: Option<String>,
252
}
b64e2537 Andy McCall ↶ 253
254
// ------------------------------------------------------------------ issues
255
256
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
257
pub struct Issue {
258
    pub number: i64,
259
    pub title: String,
260
    pub body: String,
261
    pub author: String,
262
    pub state: String,
263
    pub reason: String,
264
    pub labels: Vec<Label>,
265
    pub assignees: Vec<String>,
266
    pub milestone: Option<String>,
267
    pub comments: i64,
268
    pub locked: bool,
269
    pub pinned: bool,
270
    pub created_ms: i64,
271
    pub updated_ms: i64,
272
    pub closed_ms: Option<i64>,
273
    pub url: String,
274
}
275
276
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
277
pub struct Label {
278
    pub name: String,
279
    pub colour: String,
280
    pub description: String,
281
}
282
283
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
284
pub struct Milestone {
285
    pub title: String,
286
    pub description: String,
287
    pub due_ms: Option<i64>,
288
    pub state: String,
289
    pub open: i64,
290
    pub closed: i64,
291
}
292
5ea4872f Andy McCall ↶ 293
/// `POST /repos/{o}/{n}/milestones`: create a milestone, or change the
294
/// description and due date of the one with that title (triage).
b64e2537 Andy McCall ↶ 295
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
5ea4872f Andy McCall ↶ 296
pub struct NewMilestone {
297
    pub title: String,
298
    #[serde(default)]
299
    pub description: String,
300
    /// `YYYY-MM-DD`, UTC; absent for no due date.
301
    #[serde(default)]
302
    pub due: Option<String>,
303
}
304
305
/// `PATCH /repos/{o}/{n}/milestones/{title}`: only the fields given change
306
/// (triage).
307
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
308
pub struct MilestoneEdit {
309
    #[serde(default)]
310
    pub title: Option<String>,
311
    #[serde(default)]
312
    pub description: Option<String>,
313
    /// `YYYY-MM-DD`; the empty string clears the due date.
314
    #[serde(default)]
315
    pub due: Option<String>,
316
    /// `open` or `closed`.
317
    #[serde(default)]
318
    pub state: Option<String>,
319
}
320
321
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
b64e2537 Andy McCall ↶ 322
pub struct Comment {
323
    pub id: i64,
324
    pub author: String,
325
    pub body: String,
326
    pub created_ms: i64,
327
    /// For proposal comments: the file and the atom the comment anchors to.
328
    pub path: Option<String>,
329
    pub anchor: Option<Atom>,
330
    pub resolved: bool,
331
}
332
42a0ba4b Andy McCall ↶ 333
/// One entry in the caller's inbox (hub spec §10).
334
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
335
pub struct Notification {
336
    pub id: i64,
337
    pub at_ms: i64,
338
    /// `issue.opened`, `proposal.comment`, `mention`, `review`, …
339
    pub kind: String,
340
    pub title: String,
341
    /// The page on the hub, absolute.
342
    pub url: String,
343
    pub actor: String,
344
    pub read: bool,
345
}
346
347
/// `GET /notifications`: the unread count and the newest entries, unread first.
348
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
349
pub struct Inbox {
350
    pub unread: i64,
351
    pub items: Vec<Notification>,
352
}
353
3f02e5ea Andy McCall ↶ 354
/// A file a proposal changes, between the target's state and the
355
/// projected state (hub spec §7.2). Fetch either side's bytes from
356
/// `/proposals/{number}/content/{base|head}/{path}`.
b64e2537 Andy McCall ↶ 357
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
3f02e5ea Andy McCall ↶ 358
pub struct ProposalFile {
359
    pub path: String,
360
    /// For a rename, the path in the target's state.
361
    pub from: Option<String>,
362
    /// `added`, `removed`, `modified`, `renamed`, `mode` or `replaced`.
363
    pub kind: String,
364
    pub added: usize,
365
    pub removed: usize,
366
}
367
368
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
b64e2537 Andy McCall ↶ 369
pub struct Atom {
370
    pub change: String,
371
    pub index: u32,
372
}
373
374
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
375
pub struct Review {
376
    pub id: i64,
377
    pub author: String,
378
    pub verdict: String,
379
    pub body: String,
380
    pub sealed: bool,
381
    pub created_ms: i64,
7d2eb590 Andy McCall ↶ 382
    /// The AI assistant that answered for `author`, empty for a person's
383
    /// own review (hub spec §13.4).
384
    #[serde(default, skip_serializing_if = "String::is_empty")]
385
    pub via: String,
b64e2537 Andy McCall ↶ 386
}
387
388
// ------------------------------------------------------------- repository
389
390
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
391
pub struct TreeEntry {
392
    pub name: String,
393
    pub path: String,
394
    pub kind: String,
395
    pub exec: bool,
396
    pub size: Option<u64>,
397
    pub conflicted: bool,
398
}
399
400
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
401
pub struct BlameLine {
402
    pub line: usize,
403
    pub change: String,
404
    pub index: u32,
405
    pub author: String,
406
    pub when_ms: u64,
407
    pub text: String,
408
}
409
410
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
411
pub struct FileChange {
412
    pub path: String,
413
    pub kind: String,
414
    pub from: Option<String>,
415
    pub added: usize,
416
    pub removed: usize,
417
}
418
419
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
420
pub struct ChangeDetail {
421
    pub change: ChangeSummary,
422
    pub body: String,
423
    pub dependents: Vec<String>,
424
    pub in_bookmarks: Vec<String>,
425
    pub in_tags: Vec<String>,
426
    pub files: Vec<FileChange>,
427
}
428
429
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
430
pub struct History {
431
    pub reference: String,
432
    pub heads: Vec<String>,
433
    pub total: usize,
434
    pub offset: usize,
435
    pub changes: Vec<ChangeSummary>,
436
}
437
438
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
439
pub struct Conflict {
440
    pub bookmark: String,
441
    pub path: String,
442
    pub kind: String,
443
    pub contributors: Vec<String>,
444
}
445
446
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
447
pub struct Push {
448
    pub at_ms: i64,
449
    pub pusher: String,
450
    pub reference: String,
451
    pub ok: bool,
452
    pub reason: String,
453
    pub old_state: String,
454
    pub new_state: String,
455
    pub heads: i64,
456
    pub changes: i64,
457
}
458
459
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
460
pub struct Trust {
461
    pub policy: Option<Policy>,
462
    pub signed: usize,
463
    pub sealed: usize,
464
    pub unsigned: usize,
465
    pub invalid: Vec<String>,
466
    pub hub_key: String,
467
}
468
469
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
470
pub struct Policy {
471
    pub serial: u64,
472
    pub expires_ms: u64,
473
    pub roots: Vec<String>,
474
    pub root_threshold: u32,
475
    pub rules: Vec<Rule>,
476
}
477
478
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
479
pub struct Rule {
480
    pub pattern: String,
481
    pub keys: Vec<String>,
482
    pub threshold: u32,
483
    pub require: String,
484
    pub allow_conflicts: bool,
485
}
486
487
// ---------------------------------------------------------- hooks, checks
488
489
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
490
pub struct Hook {
491
    pub id: i64,
492
    pub url: String,
493
    pub events: String,
494
    pub active: bool,
495
    pub created_ms: i64,
496
}
497
498
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
499
pub struct NewHook {
500
    pub url: String,
501
    pub secret: String,
502
    #[serde(default = "star")]
503
    pub events: String,
504
}
505
506
fn star() -> String {
507
    "*".into()
508
}
509
90326c96 Andy McCall ↶ 510
/// An OxygenFlow run (hub spec §13.2).
511
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
512
pub struct FlowRun {
513
    /// The run's number within its repository.
514
    pub number: i64,
515
    pub bookmark: String,
516
    /// The state's hash, as checks are keyed.
517
    pub state: String,
518
    /// Its heads, full change hashes.
519
    pub heads: Vec<String>,
520
    /// `queued`, `running`, `passed`, `failed` or `error` (an invalid pipeline).
521
    pub status: String,
522
    /// `push`, `manual` or `rerun:N`.
523
    pub trigger: String,
524
    pub actor: String,
525
    pub pipeline_hash: String,
526
    /// An invalid pipeline's problems.
527
    pub error: String,
528
    pub created_ms: i64,
529
    pub finished_ms: Option<i64>,
530
    pub url: String,
bf3d0754 Andy McCall ↶ 531
    /// For a run waiting for a runner, its first queued job's 1-based
532
    /// place among all queued jobs.
533
    #[serde(default, skip_serializing_if = "Option::is_none")]
534
    pub queue_position: Option<i64>,
90326c96 Andy McCall ↶ 535
    /// Filled by the single-run endpoint.
536
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
537
    pub jobs: Vec<FlowJob>,
538
}
539
540
/// A job of a run.
541
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
542
pub struct FlowJob {
543
    pub name: String,
544
    /// `queued`, `running`, `passed`, `failed`, `skipped` or `error`.
545
    pub status: String,
546
    pub depends_on: Vec<String>,
547
    pub runner: String,
548
    pub exit_code: Option<i64>,
549
    pub error: String,
550
    pub started_ms: Option<i64>,
551
    pub finished_ms: Option<i64>,
552
    /// Log lines stored so far.
553
    pub log_lines: i64,
554
}
555
556
/// A line of a job's log.
557
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
558
pub struct FlowLogLine {
559
    /// Increasing within the job; pass the last one as `after` for more.
560
    pub seq: i64,
561
    /// Milliseconds since the job started.
562
    pub t: i64,
563
    /// `stdout`, `stderr` or `flow` (the runner's own notes).
564
    pub stream: String,
565
    pub text: String,
566
}
567
b64e2537 Andy McCall ↶ 568
/// A check on a state hash (hub spec §13.1).
569
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
570
pub struct Check {
571
    pub state: String,
572
    pub name: String,
573
    pub status: CheckStatus,
574
    pub summary: String,
575
    pub url: String,
576
    pub reporter: String,
577
    /// The seal attestation's reference when the checker sealed the changes.
578
    pub seal: Option<String>,
579
    pub started_ms: i64,
580
    pub finished_ms: Option<i64>,
581
}
582
583
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
584
#[serde(rename_all = "lowercase")]
585
pub enum CheckStatus {
586
    Queued,
587
    Running,
588
    Passed,
589
    Failed,
590
    Cancelled,
591
    Skipped,
592
}
593
594
impl CheckStatus {
595
    pub fn as_str(self) -> &'static str {
596
        match self {
597
            CheckStatus::Queued => "queued",
598
            CheckStatus::Running => "running",
599
            CheckStatus::Passed => "passed",
600
            CheckStatus::Failed => "failed",
601
            CheckStatus::Cancelled => "cancelled",
602
            CheckStatus::Skipped => "skipped",
603
        }
604
    }
605
606
    pub fn parse(s: &str) -> Option<CheckStatus> {
607
        Some(match s {
608
            "queued" => CheckStatus::Queued,
609
            "running" => CheckStatus::Running,
610
            "passed" => CheckStatus::Passed,
611
            "failed" => CheckStatus::Failed,
612
            "cancelled" => CheckStatus::Cancelled,
613
            "skipped" => CheckStatus::Skipped,
614
            _ => return None,
615
        })
616
    }
617
}
618
619
/// What a checker posts.
620
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
621
pub struct NewCheck {
622
    pub state: String,
623
    pub name: String,
624
    pub status: CheckStatus,
625
    #[serde(default)]
626
    pub summary: String,
627
    #[serde(default)]
628
    pub url: String,
629
    /// Hex of a seal attestation reference the checker pushed, if any.
630
    #[serde(default)]
631
    pub seal: Option<String>,
632
}
633
634
// ------------------------------------------------------------- accounts
635
636
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
637
pub struct User {
638
    pub username: String,
639
    pub display_name: String,
640
    pub is_admin: bool,
641
    pub keys: Vec<String>,
642
    pub created_ms: i64,
643
}
644
645
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
646
pub struct Organisation {
647
    pub name: String,
648
    pub display_name: String,
649
    pub description: String,
650
    pub members: Vec<String>,
651
}
652
653
/// Forms accepted by the write endpoints.
654
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
655
pub struct NewRepository {
656
    pub owner: String,
657
    pub name: String,
658
    #[serde(default)]
659
    pub description: String,
660
    #[serde(default = "public")]
661
    pub visibility: Visibility,
662
}
663
664
fn public() -> Visibility {
665
    Visibility::Public
666
}
667
668
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
669
pub struct NewIssue {
670
    pub title: String,
671
    #[serde(default)]
672
    pub body: String,
76f262e1 Andy McCall ↶ 673
    /// Label names; each must exist. Needs triage.
674
    #[serde(default)]
675
    pub labels: Vec<String>,
676
    /// Usernames. Needs triage.
677
    #[serde(default)]
678
    pub assignees: Vec<String>,
679
    /// A milestone's title. Needs triage.
680
    #[serde(default)]
681
    pub milestone: Option<String>,
b64e2537 Andy McCall ↶ 682
}
683
76f262e1 Andy McCall ↶ 684
/// `PATCH /repos/{o}/{n}/issues/{number}`: only the fields given change.
685
/// Title and body: the author or triage. The rest: triage.
686
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
687
pub struct IssueEdit {
688
    #[serde(default)]
689
    pub title: Option<String>,
690
    #[serde(default)]
691
    pub body: Option<String>,
692
    /// The whole set of labels.
693
    #[serde(default)]
694
    pub labels: Option<Vec<String>>,
695
    /// The whole set of assignees.
696
    #[serde(default)]
697
    pub assignees: Option<Vec<String>>,
698
    /// A milestone's title; the empty string clears it.
699
    #[serde(default)]
700
    pub milestone: Option<String>,
701
}
702
961c10be Andy McCall ↶ 703
/// The checks a proposal needs, passed, to land on the repository's
704
/// default bookmark (hub spec §13.1). `GET` and `PUT
705
/// /repos/{o}/{n}/checks/required`.
706
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
707
pub struct RequiredChecks {
708
    pub names: Vec<String>,
709
}
710
ef20fdc1 Andy McCall ↶ 711
/// A project (hub spec §8.2).
712
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
713
pub struct Project {
714
    pub owner: String,
715
    pub number: i64,
716
    pub title: String,
717
    pub description: String,
718
    pub public: bool,
719
    pub closed: bool,
720
    pub updated_ms: i64,
721
    /// The board on the hub, absolute.
722
    pub url: String,
723
}
724
725
/// A card as the caller may see it.
726
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
727
pub struct ProjectCard {
728
    pub id: i64,
729
    /// `issue`, `proposal` or `note`.
730
    pub kind: String,
731
    /// `owner/name#N`; empty for a note.
732
    pub reference: String,
733
    /// The issue's or proposal's title, or the note.
734
    pub title: String,
735
    /// The issue's or proposal's state; empty for a note.
736
    pub state: String,
737
}
738
739
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
740
pub struct ProjectColumn {
741
    pub id: i64,
742
    pub name: String,
743
    pub cards: Vec<ProjectCard>,
744
}
745
746
/// `GET /projects/{owner}/{n}`.
747
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
748
pub struct Board {
749
    pub project: Project,
750
    pub columns: Vec<ProjectColumn>,
751
}
752
753
/// `POST /projects/{owner}`.
754
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
755
pub struct NewProject {
756
    pub title: String,
757
    #[serde(default)]
758
    pub description: String,
759
    #[serde(default)]
760
    pub private: bool,
761
}
762
763
/// `PATCH /projects/{owner}/{n}`: only the fields given change.
764
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
765
pub struct ProjectEdit {
766
    #[serde(default)]
767
    pub title: Option<String>,
768
    #[serde(default)]
769
    pub description: Option<String>,
770
    #[serde(default)]
771
    pub private: Option<bool>,
772
    #[serde(default)]
773
    pub closed: Option<bool>,
774
}
775
776
/// `POST /projects/{owner}/{n}/cards`: a reference (`repo#12`) or a note,
777
/// into the column named (by id or name; the first column when absent).
778
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
779
pub struct NewCard {
780
    #[serde(default)]
781
    pub column: Option<String>,
782
    #[serde(default)]
783
    pub reference: String,
784
    #[serde(default)]
785
    pub note: String,
786
}
787
788
/// `PATCH /projects/{owner}/{n}/cards/{id}`: move to a column (id or name).
789
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
790
pub struct CardMove {
791
    pub column: String,
792
}
793
76f262e1 Andy McCall ↶ 794
/// `POST /repos/{o}/{n}/labels`: create a label, or change one of that name.
795
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
796
pub struct NewLabel {
797
    pub name: String,
798
    /// `#rrggbb`; a default when absent.
799
    #[serde(default)]
800
    pub colour: Option<String>,
801
    #[serde(default)]
802
    pub description: String,
803
}
804
b64e2537 Andy McCall ↶ 805
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
806
pub struct NewComment {
807
    pub body: String,
3f4b0ea6 Andy McCall ↶ 808
    /// For a proposal: anchor the comment to this file's line (1-based) of
809
    /// the projected state; the hub records the line's atom.
810
    #[serde(default)]
811
    pub path: Option<String>,
812
    #[serde(default)]
813
    pub line: Option<usize>,
b64e2537 Andy McCall ↶ 814
}
815
816
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
817
pub struct NewProposal {
818
    pub title: String,
819
    #[serde(default)]
820
    pub body: String,
821
    pub target: String,
822
    /// A bookmark here, or `owner/name:bookmark` of a fork.
823
    pub source: String,
824
    #[serde(default)]
825
    pub draft: bool,
cc1cd197 Andy McCall ↶ 826
}
827
828
/// `POST .../proposals/{number}/reviewers`: accounts to ask for a review.
829
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
830
pub struct ReviewRequest {
831
    pub reviewers: Vec<String>,
7d2eb590 Andy McCall ↶ 832
}
833
834
/// `POST .../proposals/{number}/review-requests`: ask an AI assistant the
835
/// repository may use (hub spec §13.4) to review; it answers as a `comment`
836
/// review via the assistant.
837
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
838
pub struct AssistantRequest {
839
    pub assistant: String,
b64e2537 Andy McCall ↶ 840
}
841
842
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
843
pub struct NewReview {
844
    pub verdict: String,
845
    #[serde(default)]
846
    pub body: String,
847
}
848
849
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
850
pub struct IssueStateChange {
851
    /// `open` or `closed`.
852
    pub state: String,
853
    #[serde(default)]
854
    pub reason: String,
855
}
856
857
/// A webhook payload's envelope (hub spec §12.2).
858
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
859
pub struct WebhookEnvelope {
860
    pub event: String,
861
    pub delivery: String,
862
    pub repository: Option<Repository>,
863
    pub actor: String,
864
    pub at_ms: i64,
865
    pub data: serde_json::Value,
866
}
867
868
/// A server-sent event on a proposal's stream.
869
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
870
pub struct ProposalEvent {
871
    pub number: i64,
872
    pub kind: String,
873
    pub check: Option<Check>,
874
}
875
3f4b0ea6 Andy McCall ↶ 876
// --------------------------------------------------------------- releases
877
878
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
879
pub struct Release {
880
    pub tag: String,
881
    pub title: String,
882
    pub notes: String,
883
    pub prerelease: bool,
884
    pub latest: bool,
885
    pub author: String,
886
    pub heads: Vec<String>,
887
    pub signer: Option<String>,
888
    pub assets: Vec<ReleaseAsset>,
889
    pub archives: Vec<String>,
890
    pub created_ms: i64,
891
    pub url: String,
892
}
893
894
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
895
pub struct ReleaseAsset {
896
    pub name: String,
897
    pub size: i64,
898
    /// BLAKE3 of the content, hex.
899
    pub hash: String,
900
    pub content_type: String,
901
    pub downloads: i64,
902
    pub url: String,
10708ab8 Andy McCall ↶ 903
    /// The OxygenFlow run and job whose artifact has this content, if any.
904
    #[serde(default, skip_serializing_if = "Option::is_none")]
905
    pub built_by: Option<AssetBuild>,
906
}
907
908
/// Where a release asset was built (hub spec §9): a job's artifact with the
909
/// same BLAKE3, and that job's signed receipt when one was kept.
910
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
911
pub struct AssetBuild {
912
    pub run: i64,
913
    pub job: String,
914
    pub artifact: String,
915
    pub run_url: String,
916
    /// The receipt attestation, hex; `None` when the receipt was not signed.
917
    pub receipt: Option<String>,
918
    pub receipt_url: Option<String>,
919
    pub signer: Option<String>,
3f4b0ea6 Andy McCall ↶ 920
}
921
739a763a Andy McCall ↶ 922
/// `PATCH /repos/{o}/{n}/proposals/{number}`: what is given changes,
923
/// the rest stays (oxygenhub#76).
924
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
925
pub struct ProposalEdit {
926
    #[serde(default, skip_serializing_if = "Option::is_none")]
927
    pub title: Option<String>,
928
    #[serde(default, skip_serializing_if = "Option::is_none")]
929
    pub body: Option<String>,
2291f8cc Andy McCall ↶ 930
    /// The whole set of labels, by name (triage); oxygenhub#80.
931
    #[serde(default, skip_serializing_if = "Option::is_none")]
932
    pub labels: Option<Vec<String>>,
739a763a Andy McCall ↶ 933
}
934
ced6d3af Andy McCall ↶ 935
/// `PATCH /repos/{owner}/{name}/releases/{tag}`: what to change; fields
936
/// left out stay as they are.
937
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
938
pub struct ReleaseEdit {
939
    #[serde(default, skip_serializing_if = "Option::is_none")]
940
    pub title: Option<String>,
941
    #[serde(default, skip_serializing_if = "Option::is_none")]
942
    pub notes: Option<String>,
943
    #[serde(default, skip_serializing_if = "Option::is_none")]
944
    pub prerelease: Option<bool>,
945
}
946
3f4b0ea6 Andy McCall ↶ 947
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
948
pub struct NewRelease {
949
    pub tag: String,
950
    pub title: String,
951
    #[serde(default)]
952
    pub notes: Option<String>,
953
    #[serde(default)]
954
    pub prerelease: bool,
955
}
956
b64e2537 Andy McCall ↶ 957
/// Every type of the API, for schema generation.
958
pub fn schemas() -> Vec<(&'static str, schemars::Schema)> {
959
    vec![
960
        ("Repository", schemars::schema_for!(Repository)),
961
        ("Visibility", schemars::schema_for!(Visibility)),
962
        ("Bookmark", schemars::schema_for!(Bookmark)),
963
        ("Tag", schemars::schema_for!(Tag)),
964
        ("ChangeSummary", schemars::schema_for!(ChangeSummary)),
965
        ("Author", schemars::schema_for!(Author)),
966
        ("Signature", schemars::schema_for!(Signature)),
967
        ("Problem", schemars::schema_for!(Problem)),
968
        ("Proposal", schemars::schema_for!(Proposal)),
969
        ("ProposalState", schemars::schema_for!(ProposalState)),
970
        ("ProposalSource", schemars::schema_for!(ProposalSource)),
971
        ("Approval", schemars::schema_for!(Approval)),
972
        ("Issue", schemars::schema_for!(Issue)),
973
        ("Label", schemars::schema_for!(Label)),
974
        ("Milestone", schemars::schema_for!(Milestone)),
5ea4872f Andy McCall ↶ 975
        ("NewMilestone", schemars::schema_for!(NewMilestone)),
976
        ("MilestoneEdit", schemars::schema_for!(MilestoneEdit)),
b64e2537 Andy McCall ↶ 977
        ("Comment", schemars::schema_for!(Comment)),
3f02e5ea Andy McCall ↶ 978
        ("ProposalFile", schemars::schema_for!(ProposalFile)),
42a0ba4b Andy McCall ↶ 979
        ("Notification", schemars::schema_for!(Notification)),
980
        ("Inbox", schemars::schema_for!(Inbox)),
b64e2537 Andy McCall ↶ 981
        ("Atom", schemars::schema_for!(Atom)),
982
        ("Review", schemars::schema_for!(Review)),
983
        ("TreeEntry", schemars::schema_for!(TreeEntry)),
984
        ("BlameLine", schemars::schema_for!(BlameLine)),
985
        ("FileChange", schemars::schema_for!(FileChange)),
986
        ("ChangeDetail", schemars::schema_for!(ChangeDetail)),
987
        ("History", schemars::schema_for!(History)),
988
        ("Conflict", schemars::schema_for!(Conflict)),
989
        ("Push", schemars::schema_for!(Push)),
990
        ("Trust", schemars::schema_for!(Trust)),
991
        ("Policy", schemars::schema_for!(Policy)),
992
        ("Rule", schemars::schema_for!(Rule)),
993
        ("Hook", schemars::schema_for!(Hook)),
994
        ("NewHook", schemars::schema_for!(NewHook)),
995
        ("Check", schemars::schema_for!(Check)),
996
        ("CheckStatus", schemars::schema_for!(CheckStatus)),
997
        ("NewCheck", schemars::schema_for!(NewCheck)),
998
        ("User", schemars::schema_for!(User)),
999
        ("Organisation", schemars::schema_for!(Organisation)),
1000
        ("NewRepository", schemars::schema_for!(NewRepository)),
1001
        ("NewIssue", schemars::schema_for!(NewIssue)),
76f262e1 Andy McCall ↶ 1002
        ("IssueEdit", schemars::schema_for!(IssueEdit)),
2291f8cc Andy McCall ↶ 1003
        ("ReleaseEdit", schemars::schema_for!(ReleaseEdit)),
1004
        ("ProposalEdit", schemars::schema_for!(ProposalEdit)),
76f262e1 Andy McCall ↶ 1005
        ("NewLabel", schemars::schema_for!(NewLabel)),
961c10be Andy McCall ↶ 1006
        ("RequiredChecks", schemars::schema_for!(RequiredChecks)),
ef20fdc1 Andy McCall ↶ 1007
        ("Project", schemars::schema_for!(Project)),
1008
        ("ProjectCard", schemars::schema_for!(ProjectCard)),
1009
        ("ProjectColumn", schemars::schema_for!(ProjectColumn)),
1010
        ("Board", schemars::schema_for!(Board)),
1011
        ("NewProject", schemars::schema_for!(NewProject)),
1012
        ("ProjectEdit", schemars::schema_for!(ProjectEdit)),
1013
        ("NewCard", schemars::schema_for!(NewCard)),
1014
        ("CardMove", schemars::schema_for!(CardMove)),
b64e2537 Andy McCall ↶ 1015
        ("NewComment", schemars::schema_for!(NewComment)),
1016
        ("NewProposal", schemars::schema_for!(NewProposal)),
1017
        ("NewReview", schemars::schema_for!(NewReview)),
1018
        ("IssueStateChange", schemars::schema_for!(IssueStateChange)),
1019
        ("WebhookEnvelope", schemars::schema_for!(WebhookEnvelope)),
1020
        ("ProposalEvent", schemars::schema_for!(ProposalEvent)),
3f4b0ea6 Andy McCall ↶ 1021
        ("Release", schemars::schema_for!(Release)),
1022
        ("ReleaseAsset", schemars::schema_for!(ReleaseAsset)),
1023
        ("NewRelease", schemars::schema_for!(NewRelease)),
b64e2537 Andy McCall ↶ 1024
    ]
1025
}
b66e70b7 Andy McCall ↶ 1026
1027
/// An OxygenFlow runner (`GET /api/v1/runners`, site administrators).
1028
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
1029
pub struct Runner {
1030
    pub id: i64,
1031
    pub name: String,
1032
    /// `online`, `offline`, `draining` or `disabled`.
1033
    pub status: String,
1034
    /// What it serves: empty for the whole site, an owner, or `owner/name`.
1035
    pub serves: String,
1036
    pub arch: String,
1037
    pub os: String,
1038
    pub version: String,
1039
    pub labels: Vec<String>,
1040
    pub max_jobs: i64,
1041
    pub memory_mib: Option<i64>,
1042
    pub cpus: Option<i64>,
1043
    /// The key that signs its receipts (KeyId hex), empty until bound.
1044
    pub key: String,
1045
    /// `registered` or `config` (a hub.toml token).
1046
    pub source: String,
1047
    pub last_seen_ms: Option<i64>,
1048
    /// Jobs it is running now.
1049
    pub running: i64,
1050
}
1051
1052
/// A job a runner ran or is running.
1053
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
1054
pub struct RunnerJob {
1055
    pub repo: String,
1056
    pub run: i64,
1057
    pub job: String,
1058
    pub status: String,
1059
    pub started_ms: Option<i64>,
1060
    pub finished_ms: Option<i64>,
1061
}
1062
1063
/// A job in the queue: running ones first, then queued ones in the order
1064
/// runners claim them.
1065
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
1066
pub struct QueueItem {
1067
    pub repo: String,
1068
    pub run: i64,
1069
    pub job: String,
1070
    /// `running` or `queued`.
1071
    pub status: String,
1072
    /// The runner holding it, when running.
1073
    pub runner: String,
1074
}