@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

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 // ── Models ──────────────────────────────────────────────────────────────────
214
215 /// One entry of `GET /api/v1/models`, the models you can pick for a session.
216 #[derive(Debug, Clone, Deserialize, Serialize)]
217 pub struct Model {
218 pub id: String,
219 pub display_name: Option<String>,
220 pub capability: Option<String>,
221 #[serde(default)]
222 pub vision: bool,
223 /// Set on the model the calling client uses when none is named.
224 #[serde(default)]
225 pub default: bool,
226 }
227
228 // ── MCP-backed resources ────────────────────────────────────────────────────
229 //
230 // Issues, pull requests, and code search have no REST surface; they are served
231 // by the MCP tools at `POST /api/v1/mcp`. These are the tool result shapes.
232
233 #[derive(Debug, Clone, Deserialize, Serialize)]
234 pub struct RepositorySummary {
235 pub full_name: String,
236 pub description: Option<String>,
237 pub default_branch: String,
238 pub private: bool,
239 pub url: String,
240 }
241
242 #[derive(Debug, Clone, Deserialize, Serialize)]
243 pub struct FileContents {
244 pub repo: String,
245 pub path: String,
246 #[serde(rename = "ref")]
247 pub git_ref: String,
248 pub size: i64,
249 pub content: String,
250 }
251
252 #[derive(Debug, Clone, Deserialize, Serialize)]
253 pub struct CodeHit {
254 pub path: String,
255 pub line: i64,
256 pub snippet: String,
257 }
258
259 #[derive(Debug, Clone, Deserialize, Serialize)]
260 pub struct Comment {
261 pub author: String,
262 pub body: String,
263 pub created_at: DateTime<Utc>,
264 }
265
266 #[derive(Debug, Clone, Deserialize, Serialize)]
267 pub struct IssueSummary {
268 pub number: i64,
269 pub title: String,
270 pub state: String,
271 pub author: String,
272 pub url: String,
273 }
274
275 #[derive(Debug, Clone, Deserialize, Serialize)]
276 pub struct Issue {
277 pub number: i64,
278 pub title: String,
279 pub state: String,
280 pub author: String,
281 pub body: Option<String>,
282 pub url: String,
283 #[serde(default)]
284 pub comments: Vec<Comment>,
285 }
286
287 #[derive(Debug, Clone, Deserialize, Serialize)]
288 pub struct PullRequestSummary {
289 pub number: i64,
290 pub title: String,
291 pub state: String,
292 pub head: String,
293 pub base: String,
294 pub url: String,
295 }
296
297 #[derive(Debug, Clone, Deserialize, Serialize)]
298 pub struct PullRequest {
299 pub number: i64,
300 pub title: String,
301 pub state: String,
302 pub author: String,
303 pub head: String,
304 pub base: String,
305 pub body: Option<String>,
306 pub url: String,
307 #[serde(default)]
308 pub comments: Vec<Comment>,
309 /// `None` when a branch no longer exists; see `diff_note`.
310 #[serde(default)]
311 pub diff: Option<String>,
312 #[serde(default)]
313 pub diff_truncated: bool,
314 #[serde(default)]
315 pub diff_note: Option<String>,
316 }
317
318 /// What `create_issue` / `create_pull_request` hand back.
319 #[derive(Debug, Clone, Deserialize, Serialize)]
320 pub struct CreatedRef {
321 pub number: i64,
322 pub state: String,
323 pub url: String,
324 }
325
326 #[cfg(test)]
327 mod tests {
328 use super::*;
329
330 #[test]
331 fn account_status_reads_the_bare_not_found_string() {
332 let status: AccountStatus = serde_json::from_str(r#""NotFound""#).unwrap();
333 assert!(matches!(status, AccountStatus::NotFound));
334 }
335
336 #[test]
337 fn account_status_reads_the_ready_envelope() {
338 let status: AccountStatus =
339 serde_json::from_str(r#"{"Ready":{"access_token":"abc123"}}"#).unwrap();
340 match status {
341 AccountStatus::Ready { access_token } => assert_eq!(access_token, "abc123"),
342 other => panic!("expected Ready, got {other:?}"),
343 }
344 }
345
346 #[test]
347 fn account_status_reads_the_incomplete_envelope() {
348 let status: AccountStatus =
349 serde_json::from_str(r#"{"Incomplete":{"status":1001}}"#).unwrap();
350 match status {
351 AccountStatus::Incomplete { status } => assert_eq!(status, 1001),
352 other => panic!("expected Incomplete, got {other:?}"),
353 }
354 }
355
356 #[test]
357 fn a_hook_without_a_secret_still_parses() {
358 // Only the create response carries `secret`; index responses omit it.
359 let hook: Hook = serde_json::from_str(
360 r#"{"id":1,"url":"https://x.test","events":["push"],"active":true,
361 "last_status_code":null,"last_delivered_at":null,
362 "created_at":"2026-08-16T10:00:00.000Z"}"#,
363 )
364 .unwrap();
365 assert!(hook.secret.is_none());
366 assert!(hook.last_delivered_at.is_none());
367 }
368
369 #[test]
370 fn file_contents_maps_the_ref_keyword_field() {
371 let file: FileContents = serde_json::from_str(
372 r#"{"repo":"a/b","path":"README.md","ref":"main","size":3,"content":"hi\n"}"#,
373 )
374 .unwrap();
375 assert_eq!(file.git_ref, "main");
376 }
377
378 #[test]
379 fn a_pull_request_without_diff_fields_parses() {
380 // `get_pull_request` omits diff_truncated unless it truncated.
381 let pr: PullRequest = serde_json::from_str(
382 r#"{"number":7,"title":"t","state":"open","author":"seto","head":"f","base":"main",
383 "body":null,"url":"https://sigit.si/a/b/pull/7","comments":[],"diff":"@@"}"#,
384 )
385 .unwrap();
386 assert!(!pr.diff_truncated);
387 assert_eq!(pr.diff.as_deref(), Some("@@"));
388 }
389 }