1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct GitHubIssue {
7 pub number: u64,
8 #[serde(default)]
10 pub id: u64,
11 pub title: String,
12 pub body: Option<String>,
13 pub state: String,
14 pub labels: Vec<GitHubLabel>,
15 pub user: GitHubUser,
16 pub created_at: String,
17 pub updated_at: String,
18 pub html_url: String,
19 pub node_id: Option<String>,
20 pub pull_request: Option<serde_json::Value>,
22 #[serde(default)]
25 pub author_association: Option<String>,
26 #[serde(default)]
28 pub state_reason: Option<String>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct GitHubLabel {
33 pub name: String,
34 pub color: Option<String>,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct GitHubUser {
39 pub login: String,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct GitHubPullRequest {
49 pub number: u64,
50 pub title: String,
51 pub body: Option<String>,
52 pub state: String,
53 pub head: GitHubBranchRef,
54 pub base: GitHubBranchRef,
55 pub mergeable: Option<bool>,
56 pub mergeable_state: Option<String>,
57 pub merged: Option<bool>,
58 pub html_url: String,
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct GitHubCheckRun {
63 pub id: u64,
64 pub name: String,
65 pub status: String,
66 pub conclusion: Option<String>,
67 #[serde(default)]
69 pub app: Option<GitHubApp>,
70}
71
72impl GitHubCheckRun {
73 pub(crate) fn is_finished(&self) -> bool {
74 check_run_is_finished(&self.status, self.conclusion.as_deref())
75 }
76
77 pub(crate) fn is_passed(&self) -> bool {
78 check_conclusion_passed(self.conclusion.as_deref())
79 }
80}
81
82pub(crate) fn check_run_is_finished(status: &str, conclusion: Option<&str>) -> bool {
84 status == "completed" && conclusion.is_some_and(|c| c != "action_required")
85}
86
87pub(crate) fn check_conclusion_passed(conclusion: Option<&str>) -> bool {
88 matches!(conclusion, Some("success" | "neutral" | "skipped"))
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct GitHubApp {
94 pub id: i64,
95}
96
97#[derive(Debug, Clone, PartialEq, Eq, Hash)]
99pub struct RequiredStatusCheck {
100 pub context: String,
101 pub integration_id: Option<i64>,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct GitHubAnnotation {
112 pub annotation_level: Option<String>,
113 pub message: Option<String>,
114 pub title: Option<String>,
115 pub path: Option<String>,
116 pub start_line: Option<u64>,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct GitHubBranchRef {
121 #[serde(rename = "ref")]
122 pub ref_: String,
123 pub sha: String,
124 #[serde(default)]
126 pub repo: Option<GitHubRepoRef>,
127}
128
129#[derive(Debug, Clone, Serialize, Deserialize)]
130pub struct GitHubRepoRef {
131 pub full_name: String,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct GitHubComment {
136 pub id: u64,
137 pub body: String,
138 pub user: GitHubUser,
139 pub created_at: String,
140 pub updated_at: Option<String>,
141 pub html_url: Option<String>,
142 pub issue_url: Option<String>,
145 #[serde(default)]
150 pub author_association: Option<String>,
151}
152
153pub fn extract_issue_number_from_url(url: &str) -> Option<String> {
158 url.rsplit('/')
159 .next()
160 .filter(|s| !s.is_empty() && s.chars().all(|c| c.is_ascii_digit()))
161 .map(String::from)
162}
163
164#[derive(Debug, Clone, Serialize, Deserialize)]
166pub struct GitHubReview {
167 pub id: u64,
168 pub user: GitHubUser,
169 pub body: Option<String>,
170 pub state: String,
171 pub html_url: Option<String>,
172 pub submitted_at: String,
173 pub commit_id: Option<String>,
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct GitHubReviewComment {
179 pub id: u64,
180 pub user: GitHubUser,
181 pub body: String,
182 pub path: String,
183 pub line: Option<u32>,
184 pub original_line: Option<u32>,
185 pub commit_id: String,
186 pub original_commit_id: String,
187 pub html_url: String,
188 pub created_at: String,
189 pub updated_at: String,
190 pub in_reply_to_id: Option<u64>,
191 pub diff_hunk: Option<String>,
192}
193
194#[derive(Debug, Clone)]
196pub struct PullRequestReview {
197 pub review: GitHubReview,
198 pub comments: Vec<GitHubReviewComment>,
199}
200
201impl PullRequestReview {
202 pub fn actionable_comments(&self) -> Vec<&GitHubReviewComment> {
204 self.comments
205 .iter()
206 .filter(|c| !c.body.trim().is_empty() && c.in_reply_to_id.is_none())
207 .collect()
208 }
209}
210
211#[cfg(test)]
212impl PullRequestReview {
213 pub fn requests_changes(&self) -> bool {
215 self.review.state == "CHANGES_REQUESTED"
216 }
217}
218
219#[cfg(test)]
220mod tests {
221 use super::*;
222
223 #[test]
224 fn action_required_is_not_finished() {
225 assert!(!check_run_is_finished("completed", Some("action_required")));
226 assert!(check_run_is_finished("completed", Some("success")));
227 assert!(check_run_is_finished("completed", Some("failure")));
228 assert!(check_run_is_finished("completed", Some("skipped")));
229 assert!(!check_run_is_finished("completed", None));
230 assert!(!check_run_is_finished("in_progress", None));
231 assert!(!check_run_is_finished("waiting", None));
232 assert!(!check_conclusion_passed(Some("action_required")));
233 assert!(check_conclusion_passed(Some("success")));
234 assert!(check_conclusion_passed(Some("skipped")));
235 assert!(!check_conclusion_passed(Some("failure")));
236 }
237
238 #[test]
239 fn test_github_review_deserialization() {
240 let json = r#"{
241 "id": 789,
242 "user": {"login": "reviewer"},
243 "body": "Please fix this",
244 "state": "CHANGES_REQUESTED",
245 "html_url": "https://github.com/test/repo/pull/123#pullrequestreview-789",
246 "submitted_at": "2024-01-01T12:00:00Z",
247 "commit_id": "abc123"
248 }"#;
249
250 let review: GitHubReview = serde_json::from_str(json).unwrap();
251 assert_eq!(review.id, 789);
252 assert_eq!(review.user.login, "reviewer");
253 assert_eq!(review.body, Some("Please fix this".to_string()));
254 assert_eq!(review.state, "CHANGES_REQUESTED");
255 assert_eq!(review.submitted_at, "2024-01-01T12:00:00Z");
256 assert_eq!(review.commit_id, Some("abc123".to_string()));
257 }
258
259 #[test]
260 fn test_github_review_comment_deserialization() {
261 let json = r#"{
262 "id": 101112,
263 "user": {"login": "reviewer"},
264 "body": "This line needs fixing",
265 "path": "src/main.rs",
266 "line": 42,
267 "original_line": 42,
268 "commit_id": "abc123",
269 "original_commit_id": "abc123",
270 "html_url": "https://github.com/test/repo/pull/123#discussion_r101112",
271 "created_at": "2024-01-01T12:00:00Z",
272 "updated_at": "2024-01-01T12:00:00Z",
273 "in_reply_to_id": null,
274 "diff_hunk": "@@ -40,5 +40,5 @@ fn main() {\n let x = 1;\n- let y = 2;\n+ let y = 3;"
275 }"#;
276
277 let comment: GitHubReviewComment = serde_json::from_str(json).unwrap();
278 assert_eq!(comment.id, 101_112);
279 assert_eq!(comment.user.login, "reviewer");
280 assert_eq!(comment.body, "This line needs fixing");
281 assert_eq!(comment.path, "src/main.rs");
282 assert_eq!(comment.line, Some(42));
283 assert_eq!(comment.commit_id, "abc123");
284 assert_eq!(comment.in_reply_to_id, None);
285 assert!(comment.diff_hunk.is_some());
286 }
287
288 #[test]
289 fn github_check_run_deserializes_app_id() {
290 let json = r#"{
291 "id": 4,
292 "name": "ci/test",
293 "status": "completed",
294 "conclusion": "success",
295 "app": { "id": 42, "slug": "shipcheck", "name": "shipcheck" }
296 }"#;
297 let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
298 assert_eq!(run.name, "ci/test");
299 assert_eq!(run.app.unwrap().id, 42);
300 }
301
302 #[test]
303 fn github_check_run_deserializes_without_app() {
304 let json = r#"{
305 "id": 4,
306 "name": "ci/test",
307 "status": "completed",
308 "conclusion": "success"
309 }"#;
310 let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
311 assert!(run.app.is_none());
312 }
313
314 #[test]
315 fn test_github_branch_ref_deserialization() {
316 let json = r#"{
317 "ref": "main",
318 "sha": "abc123"
319 }"#;
320
321 let branch_ref: GitHubBranchRef = serde_json::from_str(json).unwrap();
322 assert_eq!(branch_ref.ref_, "main");
323 assert_eq!(branch_ref.sha, "abc123");
324 }
325
326 #[test]
327 fn test_pull_request_review_requests_changes() {
328 let review = PullRequestReview {
329 review: GitHubReview {
330 id: 1,
331 user: GitHubUser {
332 login: "reviewer".to_string(),
333 },
334 body: Some("Please fix".to_string()),
335 state: "CHANGES_REQUESTED".to_string(),
336 html_url: None,
337 submitted_at: "2024-01-01T00:00:00Z".to_string(),
338 commit_id: None,
339 },
340 comments: vec![],
341 };
342
343 assert!(review.requests_changes());
344 }
345
346 #[test]
347 fn test_pull_request_review_approved() {
348 let review = PullRequestReview {
349 review: GitHubReview {
350 id: 1,
351 user: GitHubUser {
352 login: "reviewer".to_string(),
353 },
354 body: Some("LGTM".to_string()),
355 state: "APPROVED".to_string(),
356 html_url: None,
357 submitted_at: "2024-01-01T00:00:00Z".to_string(),
358 commit_id: None,
359 },
360 comments: vec![],
361 };
362
363 assert!(!review.requests_changes());
364 }
365
366 #[test]
367 fn test_pull_request_review_actionable_comments() {
368 let review = PullRequestReview {
369 review: GitHubReview {
370 id: 1,
371 user: GitHubUser {
372 login: "reviewer".to_string(),
373 },
374 body: None,
375 state: "CHANGES_REQUESTED".to_string(),
376 html_url: None,
377 submitted_at: "2024-01-01T00:00:00Z".to_string(),
378 commit_id: None,
379 },
380 comments: vec![
381 GitHubReviewComment {
382 id: 1,
383 user: GitHubUser {
384 login: "reviewer".to_string(),
385 },
386 body: "Fix this".to_string(),
387 path: "src/main.rs".to_string(),
388 line: Some(10),
389 original_line: Some(10),
390 commit_id: "abc".to_string(),
391 original_commit_id: "abc".to_string(),
392 html_url: "url".to_string(),
393 created_at: "2024-01-01T00:00:00Z".to_string(),
394 updated_at: "2024-01-01T00:00:00Z".to_string(),
395 in_reply_to_id: None,
396 diff_hunk: None,
397 },
398 GitHubReviewComment {
399 id: 2,
400 user: GitHubUser {
401 login: "reviewer".to_string(),
402 },
403 body: String::new(), path: "src/lib.rs".to_string(),
405 line: Some(20),
406 original_line: Some(20),
407 commit_id: "abc".to_string(),
408 original_commit_id: "abc".to_string(),
409 html_url: "url".to_string(),
410 created_at: "2024-01-01T00:00:00Z".to_string(),
411 updated_at: "2024-01-01T00:00:00Z".to_string(),
412 in_reply_to_id: None,
413 diff_hunk: None,
414 },
415 GitHubReviewComment {
416 id: 3,
417 user: GitHubUser {
418 login: "reviewer".to_string(),
419 },
420 body: "Reply to this".to_string(),
421 path: "src/lib.rs".to_string(),
422 line: Some(30),
423 original_line: Some(30),
424 commit_id: "abc".to_string(),
425 original_commit_id: "abc".to_string(),
426 html_url: "url".to_string(),
427 created_at: "2024-01-01T00:00:00Z".to_string(),
428 updated_at: "2024-01-01T00:00:00Z".to_string(),
429 in_reply_to_id: Some(1), diff_hunk: None,
431 },
432 ],
433 };
434
435 let actionable = review.actionable_comments();
436 assert_eq!(actionable.len(), 1);
437 assert_eq!(actionable[0].id, 1);
438 assert_eq!(actionable[0].body, "Fix this");
439 }
440
441 #[test]
442 fn extract_issue_number_from_url_works() {
443 assert_eq!(
444 extract_issue_number_from_url("https://api.github.com/repos/owner/repo/issues/123"),
445 Some("123".into())
446 );
447 }
448
449 #[test]
450 fn extract_issue_number_from_url_empty() {
451 assert_eq!(extract_issue_number_from_url(""), None);
452 }
453}