|
1
|
//! Wire types for the sigit.si API. |
|
2
|
//! |
|
3
|
//! Field names and nullability mirror the Rails serializers in |
|
4
|
//! `app/controllers/api/v1/` and the MCP tool results in |
|
5
|
//! `app/services/mcp/tools.rb`. Every struct is `deny_unknown_fields`-free on |
|
6
|
//! purpose: the server may add fields, and an older `si` should keep working. |
|
7
|
|
|
8
|
use { |
|
9
|
chrono::{DateTime, Utc}, |
|
10
|
serde::{Deserialize, Serialize}, |
|
11
|
}; |
|
12
|
|
|
13
|
// ── Account ───────────────────────────────────────────────────────────────── |
|
14
|
|
|
15
|
/// `GET /api/v1/user` |
|
16
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
17
|
pub struct User { |
|
18
|
pub id: i64, |
|
19
|
pub username: String, |
|
20
|
pub display_name: Option<String>, |
|
21
|
pub avatar_url: Option<String>, |
|
22
|
pub email: String, |
|
23
|
pub created_at: DateTime<Utc>, |
|
24
|
} |
|
25
|
|
|
26
|
/// The result of `POST /api/v1/auth/sign_in`, shaped as an |
|
27
|
/// externally tagged enum: the bare string `"NotFound"`, `{"Ready":{…}}`, or |
|
28
|
/// `{"Incomplete":{…}}`. |
|
29
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
30
|
pub enum AccountStatus { |
|
31
|
NotFound, |
|
32
|
Ready { access_token: String }, |
|
33
|
Incomplete { status: u32 }, |
|
34
|
} |
|
35
|
|
|
36
|
// ── Repositories ──────────────────────────────────────────────────────────── |
|
37
|
|
|
38
|
/// `GET /api/v1/repos`, `POST /api/v1/repos` |
|
39
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
40
|
pub struct Repository { |
|
41
|
pub id: i64, |
|
42
|
pub name: String, |
|
43
|
pub full_name: String, |
|
44
|
pub description: Option<String>, |
|
45
|
pub default_branch: String, |
|
46
|
pub kind: Option<String>, |
|
47
|
pub is_private: bool, |
|
48
|
pub stars_count: i64, |
|
49
|
pub initialized: bool, |
|
50
|
pub updated_at: DateTime<Utc>, |
|
51
|
pub web_url: String, |
|
52
|
pub clone_url: String, |
|
53
|
} |
|
54
|
|
|
55
|
impl Repository { |
|
56
|
pub fn visibility(&self) -> &'static str { |
|
57
|
if self.is_private { |
|
58
|
"private" |
|
59
|
} else { |
|
60
|
"public" |
|
61
|
} |
|
62
|
} |
|
63
|
} |
|
64
|
|
|
65
|
// ── Webhooks ──────────────────────────────────────────────────────────────── |
|
66
|
|
|
67
|
/// `GET/POST /api/v1/repos/:owner/:repo/hooks` |
|
68
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
69
|
pub struct Hook { |
|
70
|
pub id: i64, |
|
71
|
pub url: String, |
|
72
|
pub events: Vec<String>, |
|
73
|
pub active: bool, |
|
74
|
pub last_status_code: Option<i32>, |
|
75
|
pub last_delivered_at: Option<DateTime<Utc>>, |
|
76
|
pub created_at: DateTime<Utc>, |
|
77
|
/// Returned exactly once, by `POST`. It is not retrievable afterwards. |
|
78
|
#[serde(default)] |
|
79
|
pub secret: Option<String>, |
|
80
|
} |
|
81
|
|
|
82
|
// ── Cloud Sessions ────────────────────────────────────────────────────────── |
|
83
|
|
|
84
|
/// `GET /api/v1/sessions`, `GET /api/v1/sessions/:id` |
|
85
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
86
|
pub struct CloudSession { |
|
87
|
pub id: i64, |
|
88
|
pub title: Option<String>, |
|
89
|
pub model: Option<String>, |
|
90
|
pub last_message_at: Option<DateTime<Utc>>, |
|
91
|
pub created_at: DateTime<Utc>, |
|
92
|
pub updated_at: DateTime<Utc>, |
|
93
|
/// Only present on the single-session `show` response. |
|
94
|
#[serde(default)] |
|
95
|
pub messages: Option<Vec<CloudMessage>>, |
|
96
|
} |
|
97
|
|
|
98
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
99
|
pub struct CloudMessage { |
|
100
|
pub id: i64, |
|
101
|
pub role: String, |
|
102
|
pub content: String, |
|
103
|
pub created_at: DateTime<Utc>, |
|
104
|
} |
|
105
|
|
|
106
|
/// `POST /api/v1/sessions/:id/messages` |
|
107
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
108
|
pub struct AppendedMessage { |
|
109
|
pub session: CloudSession, |
|
110
|
pub message: CloudMessage, |
|
111
|
} |
|
112
|
|
|
113
|
// ── Billing ───────────────────────────────────────────────────────────────── |
|
114
|
|
|
115
|
/// `GET /api/v1/billing` |
|
116
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
117
|
pub struct Billing { |
|
118
|
pub plan: String, |
|
119
|
pub status: String, |
|
120
|
pub entitled_to_cloud: bool, |
|
121
|
pub cloud_requests_used: i64, |
|
122
|
pub cloud_allowance: i64, |
|
123
|
pub current_period_end: Option<DateTime<Utc>>, |
|
124
|
} |
|
125
|
|
|
126
|
/// `POST /api/v1/billing/checkout`, `POST /api/v1/billing/portal` |
|
127
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
128
|
pub struct BillingUrl { |
|
129
|
pub url: String, |
|
130
|
} |
|
131
|
|
|
132
|
// ── Git credentials ───────────────────────────────────────────────────────── |
|
133
|
|
|
134
|
/// `POST /api/v1/git_credentials` — a short-lived, read-scoped git password. |
|
135
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
136
|
pub struct GitCredential { |
|
137
|
pub git_token: String, |
|
138
|
pub username: String, |
|
139
|
pub expires_at: DateTime<Utc>, |
|
140
|
} |
|
141
|
|
|
142
|
// ── Commit statuses ───────────────────────────────────────────────────────── |
|
143
|
|
|
144
|
/// One reported status on a commit, from `POST …/statuses/:sha` and the |
|
145
|
/// `statuses` list of the combined status. |
|
146
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
147
|
pub struct CommitStatus { |
|
148
|
pub id: i64, |
|
149
|
pub sha: String, |
|
150
|
/// `pending`, `success`, `failure` or `error`. |
|
151
|
pub state: String, |
|
152
|
pub context: String, |
|
153
|
pub description: Option<String>, |
|
154
|
pub target_url: Option<String>, |
|
155
|
pub created_at: DateTime<Utc>, |
|
156
|
pub updated_at: DateTime<Utc>, |
|
157
|
} |
|
158
|
|
|
159
|
/// `GET …/commits/:sha/status` — every context on a commit and the state they |
|
160
|
/// add up to. |
|
161
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
162
|
pub struct CombinedStatus { |
|
163
|
pub sha: String, |
|
164
|
pub state: String, |
|
165
|
pub total_count: u32, |
|
166
|
pub statuses: Vec<CommitStatus>, |
|
167
|
} |
|
168
|
|
|
169
|
// ── Agent runs ────────────────────────────────────────────────────────────── |
|
170
|
|
|
171
|
/// A siGit Code Cloud Agent run. `transcript_tail` is only present on the |
|
172
|
/// single-run read. |
|
173
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
174
|
pub struct AgentRun { |
|
175
|
pub id: i64, |
|
176
|
pub title: Option<String>, |
|
177
|
pub repo_full_name: String, |
|
178
|
pub trigger_kind: String, |
|
179
|
pub status: String, |
|
180
|
#[serde(default)] |
|
181
|
pub cancel_requested: bool, |
|
182
|
pub exit_reason: Option<String>, |
|
183
|
pub base_branch: Option<String>, |
|
184
|
pub head_branch: Option<String>, |
|
185
|
pub pull_request_url: Option<String>, |
|
186
|
pub issue_number: Option<i64>, |
|
187
|
pub pr_number: Option<i64>, |
|
188
|
#[serde(default)] |
|
189
|
pub inference_requests_used: i64, |
|
190
|
pub created_at: DateTime<Utc>, |
|
191
|
pub started_at: Option<DateTime<Utc>>, |
|
192
|
pub finished_at: Option<DateTime<Utc>>, |
|
193
|
#[serde(default, skip_serializing_if = "Option::is_none")] |
|
194
|
pub transcript_tail: Option<String>, |
|
195
|
} |
|
196
|
|
|
197
|
// ── Sites ─────────────────────────────────────────────────────────────────── |
|
198
|
|
|
199
|
/// A repository's published static site. |
|
200
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
201
|
pub struct Site { |
|
202
|
pub url: String, |
|
203
|
/// `publishing`, `live` or `failed`. |
|
204
|
pub state: String, |
|
205
|
pub error: Option<String>, |
|
206
|
pub last_published_at: Option<DateTime<Utc>>, |
|
207
|
#[serde(default)] |
|
208
|
pub files: i64, |
|
209
|
#[serde(default)] |
|
210
|
pub bytes: i64, |
|
211
|
} |
|
212
|
|
|
213
|
// ── MCP-backed resources ──────────────────────────────────────────────────── |
|
214
|
// |
|
215
|
// Issues, pull requests, and code search have no REST surface; they are served |
|
216
|
// by the MCP tools at `POST /api/v1/mcp`. These are the tool result shapes. |
|
217
|
|
|
218
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
219
|
pub struct RepositorySummary { |
|
220
|
pub full_name: String, |
|
221
|
pub description: Option<String>, |
|
222
|
pub default_branch: String, |
|
223
|
pub private: bool, |
|
224
|
pub url: String, |
|
225
|
} |
|
226
|
|
|
227
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
228
|
pub struct FileContents { |
|
229
|
pub repo: String, |
|
230
|
pub path: String, |
|
231
|
#[serde(rename = "ref")] |
|
232
|
pub git_ref: String, |
|
233
|
pub size: i64, |
|
234
|
pub content: String, |
|
235
|
} |
|
236
|
|
|
237
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
238
|
pub struct CodeHit { |
|
239
|
pub path: String, |
|
240
|
pub line: i64, |
|
241
|
pub snippet: String, |
|
242
|
} |
|
243
|
|
|
244
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
245
|
pub struct Comment { |
|
246
|
pub author: String, |
|
247
|
pub body: String, |
|
248
|
pub created_at: DateTime<Utc>, |
|
249
|
} |
|
250
|
|
|
251
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
252
|
pub struct IssueSummary { |
|
253
|
pub number: i64, |
|
254
|
pub title: String, |
|
255
|
pub state: String, |
|
256
|
pub author: String, |
|
257
|
pub url: String, |
|
258
|
} |
|
259
|
|
|
260
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
261
|
pub struct Issue { |
|
262
|
pub number: i64, |
|
263
|
pub title: String, |
|
264
|
pub state: String, |
|
265
|
pub author: String, |
|
266
|
pub body: Option<String>, |
|
267
|
pub url: String, |
|
268
|
#[serde(default)] |
|
269
|
pub comments: Vec<Comment>, |
|
270
|
} |
|
271
|
|
|
272
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
273
|
pub struct PullRequestSummary { |
|
274
|
pub number: i64, |
|
275
|
pub title: String, |
|
276
|
pub state: String, |
|
277
|
pub head: String, |
|
278
|
pub base: String, |
|
279
|
pub url: String, |
|
280
|
} |
|
281
|
|
|
282
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
283
|
pub struct PullRequest { |
|
284
|
pub number: i64, |
|
285
|
pub title: String, |
|
286
|
pub state: String, |
|
287
|
pub author: String, |
|
288
|
pub head: String, |
|
289
|
pub base: String, |
|
290
|
pub body: Option<String>, |
|
291
|
pub url: String, |
|
292
|
#[serde(default)] |
|
293
|
pub comments: Vec<Comment>, |
|
294
|
/// `None` when a branch no longer exists; see `diff_note`. |
|
295
|
#[serde(default)] |
|
296
|
pub diff: Option<String>, |
|
297
|
#[serde(default)] |
|
298
|
pub diff_truncated: bool, |
|
299
|
#[serde(default)] |
|
300
|
pub diff_note: Option<String>, |
|
301
|
} |
|
302
|
|
|
303
|
/// What `create_issue` / `create_pull_request` hand back. |
|
304
|
#[derive(Debug, Clone, Deserialize, Serialize)] |
|
305
|
pub struct CreatedRef { |
|
306
|
pub number: i64, |
|
307
|
pub state: String, |
|
308
|
pub url: String, |
|
309
|
} |
|
310
|
|
|
311
|
#[cfg(test)] |
|
312
|
mod tests { |
|
313
|
use super::*; |
|
314
|
|
|
315
|
#[test] |
|
316
|
fn account_status_reads_the_bare_not_found_string() { |
|
317
|
let status: AccountStatus = serde_json::from_str(r#""NotFound""#).unwrap(); |
|
318
|
assert!(matches!(status, AccountStatus::NotFound)); |
|
319
|
} |
|
320
|
|
|
321
|
#[test] |
|
322
|
fn account_status_reads_the_ready_envelope() { |
|
323
|
let status: AccountStatus = |
|
324
|
serde_json::from_str(r#"{"Ready":{"access_token":"abc123"}}"#).unwrap(); |
|
325
|
match status { |
|
326
|
AccountStatus::Ready { access_token } => assert_eq!(access_token, "abc123"), |
|
327
|
other => panic!("expected Ready, got {other:?}"), |
|
328
|
} |
|
329
|
} |
|
330
|
|
|
331
|
#[test] |
|
332
|
fn account_status_reads_the_incomplete_envelope() { |
|
333
|
let status: AccountStatus = |
|
334
|
serde_json::from_str(r#"{"Incomplete":{"status":1001}}"#).unwrap(); |
|
335
|
match status { |
|
336
|
AccountStatus::Incomplete { status } => assert_eq!(status, 1001), |
|
337
|
other => panic!("expected Incomplete, got {other:?}"), |
|
338
|
} |
|
339
|
} |
|
340
|
|
|
341
|
#[test] |
|
342
|
fn a_hook_without_a_secret_still_parses() { |
|
343
|
// Only the create response carries `secret`; index responses omit it. |
|
344
|
let hook: Hook = serde_json::from_str( |
|
345
|
r#"{"id":1,"url":"https://x.test","events":["push"],"active":true, |
|
346
|
"last_status_code":null,"last_delivered_at":null, |
|
347
|
"created_at":"2026-08-16T10:00:00.000Z"}"#, |
|
348
|
) |
|
349
|
.unwrap(); |
|
350
|
assert!(hook.secret.is_none()); |
|
351
|
assert!(hook.last_delivered_at.is_none()); |
|
352
|
} |
|
353
|
|
|
354
|
#[test] |
|
355
|
fn file_contents_maps_the_ref_keyword_field() { |
|
356
|
let file: FileContents = serde_json::from_str( |
|
357
|
r#"{"repo":"a/b","path":"README.md","ref":"main","size":3,"content":"hi\n"}"#, |
|
358
|
) |
|
359
|
.unwrap(); |
|
360
|
assert_eq!(file.git_ref, "main"); |
|
361
|
} |
|
362
|
|
|
363
|
#[test] |
|
364
|
fn a_pull_request_without_diff_fields_parses() { |
|
365
|
// `get_pull_request` omits diff_truncated unless it truncated. |
|
366
|
let pr: PullRequest = serde_json::from_str( |
|
367
|
r#"{"number":7,"title":"t","state":"open","author":"seto","head":"f","base":"main", |
|
368
|
"body":null,"url":"https://sigit.si/a/b/pull/7","comments":[],"diff":"@@"}"#, |
|
369
|
) |
|
370
|
.unwrap(); |
|
371
|
assert!(!pr.diff_truncated); |
|
372
|
assert_eq!(pr.diff.as_deref(), Some("@@")); |
|
373
|
} |
|
374
|
} |