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#[derive(Debug, Clone, PartialEq, Eq)]
216pub struct BlockedByIssue {
217 pub github_id: i64,
218 pub repo: String,
219 pub number: i64,
220 pub state: String,
221 pub state_reason: Option<String>,
222}
223
224#[derive(Debug, Clone, PartialEq, Eq)]
231pub enum BlockedByFetch {
232 Native(Vec<BlockedByIssue>),
233 Unavailable,
234 Failed(String),
235 Unreachable(String),
236}
237
238#[cfg(test)]
239impl PullRequestReview {
240 pub fn requests_changes(&self) -> bool {
242 self.review.state == "CHANGES_REQUESTED"
243 }
244}
245
246#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
248pub struct ReviewIssue {
249 pub file: String,
251 pub line: Option<u32>,
253 pub severity: String,
255 pub description: String,
257}
258
259const REVIEW_OWN_CHECKS: &[&str] = &["review-gate", "orch/review"];
261
262pub(crate) fn is_own_check(name: &str) -> bool {
263 REVIEW_OWN_CHECKS.contains(&name)
264}
265
266pub(crate) fn parse_pr_number_from_url(url: &str) -> Result<u64, String> {
278 let url = url.trim();
279 if url.is_empty() {
280 return Err("URL is empty".to_string());
281 }
282 if !url.starts_with("https://github.com/") {
283 return Err(format!(
284 "URL does not start with 'https://github.com/': {url}"
285 ));
286 }
287 if !url.contains("/pull/") {
288 return Err(format!("URL missing '/pull/' segment: {url}"));
289 }
290 let last_segment = url
291 .trim_end_matches('/')
292 .rsplit('/')
293 .next()
294 .ok_or_else(|| format!("No segment after '/pull/' in URL: {url}"))?;
295 let number_part = last_segment
296 .split(['?', '#'])
297 .next()
298 .filter(|s| !s.is_empty())
299 .ok_or_else(|| format!("PR number segment is empty in URL: {url}"))?;
300 let pr_num: u64 = number_part.parse().map_err(|_| {
301 format!("PR number is not a valid positive integer: '{number_part}' in URL: {url}")
302 })?;
303 if pr_num == 0 {
304 return Err(format!("PR number cannot be zero in URL: {url}"));
305 }
306 Ok(pr_num)
307}
308
309#[cfg(test)]
310mod tests {
311 use super::*;
312
313 #[test]
314 fn action_required_is_not_finished() {
315 assert!(!check_run_is_finished("completed", Some("action_required")));
316 assert!(check_run_is_finished("completed", Some("success")));
317 assert!(check_run_is_finished("completed", Some("failure")));
318 assert!(check_run_is_finished("completed", Some("skipped")));
319 assert!(!check_run_is_finished("completed", None));
320 assert!(!check_run_is_finished("in_progress", None));
321 assert!(!check_run_is_finished("waiting", None));
322 assert!(!check_conclusion_passed(Some("action_required")));
323 assert!(check_conclusion_passed(Some("success")));
324 assert!(check_conclusion_passed(Some("skipped")));
325 assert!(!check_conclusion_passed(Some("failure")));
326 }
327
328 #[test]
329 fn test_github_review_deserialization() {
330 let json = r#"{
331 "id": 789,
332 "user": {"login": "reviewer"},
333 "body": "Please fix this",
334 "state": "CHANGES_REQUESTED",
335 "html_url": "https://github.com/test/repo/pull/123#pullrequestreview-789",
336 "submitted_at": "2024-01-01T12:00:00Z",
337 "commit_id": "abc123"
338 }"#;
339
340 let review: GitHubReview = serde_json::from_str(json).unwrap();
341 assert_eq!(review.id, 789);
342 assert_eq!(review.user.login, "reviewer");
343 assert_eq!(review.body, Some("Please fix this".to_string()));
344 assert_eq!(review.state, "CHANGES_REQUESTED");
345 assert_eq!(review.submitted_at, "2024-01-01T12:00:00Z");
346 assert_eq!(review.commit_id, Some("abc123".to_string()));
347 }
348
349 #[test]
350 fn test_github_review_comment_deserialization() {
351 let json = r#"{
352 "id": 101112,
353 "user": {"login": "reviewer"},
354 "body": "This line needs fixing",
355 "path": "src/main.rs",
356 "line": 42,
357 "original_line": 42,
358 "commit_id": "abc123",
359 "original_commit_id": "abc123",
360 "html_url": "https://github.com/test/repo/pull/123#discussion_r101112",
361 "created_at": "2024-01-01T12:00:00Z",
362 "updated_at": "2024-01-01T12:00:00Z",
363 "in_reply_to_id": null,
364 "diff_hunk": "@@ -40,5 +40,5 @@ fn main() {\n let x = 1;\n- let y = 2;\n+ let y = 3;"
365 }"#;
366
367 let comment: GitHubReviewComment = serde_json::from_str(json).unwrap();
368 assert_eq!(comment.id, 101_112);
369 assert_eq!(comment.user.login, "reviewer");
370 assert_eq!(comment.body, "This line needs fixing");
371 assert_eq!(comment.path, "src/main.rs");
372 assert_eq!(comment.line, Some(42));
373 assert_eq!(comment.commit_id, "abc123");
374 assert_eq!(comment.in_reply_to_id, None);
375 assert!(comment.diff_hunk.is_some());
376 }
377
378 #[test]
379 fn github_check_run_deserializes_app_id() {
380 let json = r#"{
381 "id": 4,
382 "name": "ci/test",
383 "status": "completed",
384 "conclusion": "success",
385 "app": { "id": 42, "slug": "shipcheck", "name": "shipcheck" }
386 }"#;
387 let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
388 assert_eq!(run.name, "ci/test");
389 assert_eq!(run.app.unwrap().id, 42);
390 }
391
392 #[test]
393 fn github_check_run_deserializes_without_app() {
394 let json = r#"{
395 "id": 4,
396 "name": "ci/test",
397 "status": "completed",
398 "conclusion": "success"
399 }"#;
400 let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
401 assert!(run.app.is_none());
402 }
403
404 #[test]
405 fn test_github_branch_ref_deserialization() {
406 let json = r#"{
407 "ref": "main",
408 "sha": "abc123"
409 }"#;
410
411 let branch_ref: GitHubBranchRef = serde_json::from_str(json).unwrap();
412 assert_eq!(branch_ref.ref_, "main");
413 assert_eq!(branch_ref.sha, "abc123");
414 }
415
416 #[test]
417 fn test_pull_request_review_requests_changes() {
418 let review = PullRequestReview {
419 review: GitHubReview {
420 id: 1,
421 user: GitHubUser {
422 login: "reviewer".to_string(),
423 },
424 body: Some("Please fix".to_string()),
425 state: "CHANGES_REQUESTED".to_string(),
426 html_url: None,
427 submitted_at: "2024-01-01T00:00:00Z".to_string(),
428 commit_id: None,
429 },
430 comments: vec![],
431 };
432
433 assert!(review.requests_changes());
434 }
435
436 #[test]
437 fn test_pull_request_review_approved() {
438 let review = PullRequestReview {
439 review: GitHubReview {
440 id: 1,
441 user: GitHubUser {
442 login: "reviewer".to_string(),
443 },
444 body: Some("LGTM".to_string()),
445 state: "APPROVED".to_string(),
446 html_url: None,
447 submitted_at: "2024-01-01T00:00:00Z".to_string(),
448 commit_id: None,
449 },
450 comments: vec![],
451 };
452
453 assert!(!review.requests_changes());
454 }
455
456 #[test]
457 fn test_pull_request_review_actionable_comments() {
458 let review = PullRequestReview {
459 review: GitHubReview {
460 id: 1,
461 user: GitHubUser {
462 login: "reviewer".to_string(),
463 },
464 body: None,
465 state: "CHANGES_REQUESTED".to_string(),
466 html_url: None,
467 submitted_at: "2024-01-01T00:00:00Z".to_string(),
468 commit_id: None,
469 },
470 comments: vec![
471 GitHubReviewComment {
472 id: 1,
473 user: GitHubUser {
474 login: "reviewer".to_string(),
475 },
476 body: "Fix this".to_string(),
477 path: "src/main.rs".to_string(),
478 line: Some(10),
479 original_line: Some(10),
480 commit_id: "abc".to_string(),
481 original_commit_id: "abc".to_string(),
482 html_url: "url".to_string(),
483 created_at: "2024-01-01T00:00:00Z".to_string(),
484 updated_at: "2024-01-01T00:00:00Z".to_string(),
485 in_reply_to_id: None,
486 diff_hunk: None,
487 },
488 GitHubReviewComment {
489 id: 2,
490 user: GitHubUser {
491 login: "reviewer".to_string(),
492 },
493 body: String::new(), path: "src/lib.rs".to_string(),
495 line: Some(20),
496 original_line: Some(20),
497 commit_id: "abc".to_string(),
498 original_commit_id: "abc".to_string(),
499 html_url: "url".to_string(),
500 created_at: "2024-01-01T00:00:00Z".to_string(),
501 updated_at: "2024-01-01T00:00:00Z".to_string(),
502 in_reply_to_id: None,
503 diff_hunk: None,
504 },
505 GitHubReviewComment {
506 id: 3,
507 user: GitHubUser {
508 login: "reviewer".to_string(),
509 },
510 body: "Reply to this".to_string(),
511 path: "src/lib.rs".to_string(),
512 line: Some(30),
513 original_line: Some(30),
514 commit_id: "abc".to_string(),
515 original_commit_id: "abc".to_string(),
516 html_url: "url".to_string(),
517 created_at: "2024-01-01T00:00:00Z".to_string(),
518 updated_at: "2024-01-01T00:00:00Z".to_string(),
519 in_reply_to_id: Some(1), diff_hunk: None,
521 },
522 ],
523 };
524
525 let actionable = review.actionable_comments();
526 assert_eq!(actionable.len(), 1);
527 assert_eq!(actionable[0].id, 1);
528 assert_eq!(actionable[0].body, "Fix this");
529 }
530
531 #[test]
532 fn extract_issue_number_from_url_works() {
533 assert_eq!(
534 extract_issue_number_from_url("https://api.github.com/repos/owner/repo/issues/123"),
535 Some("123".into())
536 );
537 }
538
539 #[test]
540 fn extract_issue_number_from_url_empty() {
541 assert_eq!(extract_issue_number_from_url(""), None);
542 }
543}