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