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