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
266#[cfg(test)]
267mod tests {
268 use super::*;
269
270 #[test]
271 fn action_required_is_not_finished() {
272 assert!(!check_run_is_finished("completed", Some("action_required")));
273 assert!(check_run_is_finished("completed", Some("success")));
274 assert!(check_run_is_finished("completed", Some("failure")));
275 assert!(check_run_is_finished("completed", Some("skipped")));
276 assert!(!check_run_is_finished("completed", None));
277 assert!(!check_run_is_finished("in_progress", None));
278 assert!(!check_run_is_finished("waiting", None));
279 assert!(!check_conclusion_passed(Some("action_required")));
280 assert!(check_conclusion_passed(Some("success")));
281 assert!(check_conclusion_passed(Some("skipped")));
282 assert!(!check_conclusion_passed(Some("failure")));
283 }
284
285 #[test]
286 fn test_github_review_deserialization() {
287 let json = r#"{
288 "id": 789,
289 "user": {"login": "reviewer"},
290 "body": "Please fix this",
291 "state": "CHANGES_REQUESTED",
292 "html_url": "https://github.com/test/repo/pull/123#pullrequestreview-789",
293 "submitted_at": "2024-01-01T12:00:00Z",
294 "commit_id": "abc123"
295 }"#;
296
297 let review: GitHubReview = serde_json::from_str(json).unwrap();
298 assert_eq!(review.id, 789);
299 assert_eq!(review.user.login, "reviewer");
300 assert_eq!(review.body, Some("Please fix this".to_string()));
301 assert_eq!(review.state, "CHANGES_REQUESTED");
302 assert_eq!(review.submitted_at, "2024-01-01T12:00:00Z");
303 assert_eq!(review.commit_id, Some("abc123".to_string()));
304 }
305
306 #[test]
307 fn test_github_review_comment_deserialization() {
308 let json = r#"{
309 "id": 101112,
310 "user": {"login": "reviewer"},
311 "body": "This line needs fixing",
312 "path": "src/main.rs",
313 "line": 42,
314 "original_line": 42,
315 "commit_id": "abc123",
316 "original_commit_id": "abc123",
317 "html_url": "https://github.com/test/repo/pull/123#discussion_r101112",
318 "created_at": "2024-01-01T12:00:00Z",
319 "updated_at": "2024-01-01T12:00:00Z",
320 "in_reply_to_id": null,
321 "diff_hunk": "@@ -40,5 +40,5 @@ fn main() {\n let x = 1;\n- let y = 2;\n+ let y = 3;"
322 }"#;
323
324 let comment: GitHubReviewComment = serde_json::from_str(json).unwrap();
325 assert_eq!(comment.id, 101_112);
326 assert_eq!(comment.user.login, "reviewer");
327 assert_eq!(comment.body, "This line needs fixing");
328 assert_eq!(comment.path, "src/main.rs");
329 assert_eq!(comment.line, Some(42));
330 assert_eq!(comment.commit_id, "abc123");
331 assert_eq!(comment.in_reply_to_id, None);
332 assert!(comment.diff_hunk.is_some());
333 }
334
335 #[test]
336 fn github_check_run_deserializes_app_id() {
337 let json = r#"{
338 "id": 4,
339 "name": "ci/test",
340 "status": "completed",
341 "conclusion": "success",
342 "app": { "id": 42, "slug": "shipcheck", "name": "shipcheck" }
343 }"#;
344 let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
345 assert_eq!(run.name, "ci/test");
346 assert_eq!(run.app.unwrap().id, 42);
347 }
348
349 #[test]
350 fn github_check_run_deserializes_without_app() {
351 let json = r#"{
352 "id": 4,
353 "name": "ci/test",
354 "status": "completed",
355 "conclusion": "success"
356 }"#;
357 let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
358 assert!(run.app.is_none());
359 }
360
361 #[test]
362 fn test_github_branch_ref_deserialization() {
363 let json = r#"{
364 "ref": "main",
365 "sha": "abc123"
366 }"#;
367
368 let branch_ref: GitHubBranchRef = serde_json::from_str(json).unwrap();
369 assert_eq!(branch_ref.ref_, "main");
370 assert_eq!(branch_ref.sha, "abc123");
371 }
372
373 #[test]
374 fn test_pull_request_review_requests_changes() {
375 let review = PullRequestReview {
376 review: GitHubReview {
377 id: 1,
378 user: GitHubUser {
379 login: "reviewer".to_string(),
380 },
381 body: Some("Please fix".to_string()),
382 state: "CHANGES_REQUESTED".to_string(),
383 html_url: None,
384 submitted_at: "2024-01-01T00:00:00Z".to_string(),
385 commit_id: None,
386 },
387 comments: vec![],
388 };
389
390 assert!(review.requests_changes());
391 }
392
393 #[test]
394 fn test_pull_request_review_approved() {
395 let review = PullRequestReview {
396 review: GitHubReview {
397 id: 1,
398 user: GitHubUser {
399 login: "reviewer".to_string(),
400 },
401 body: Some("LGTM".to_string()),
402 state: "APPROVED".to_string(),
403 html_url: None,
404 submitted_at: "2024-01-01T00:00:00Z".to_string(),
405 commit_id: None,
406 },
407 comments: vec![],
408 };
409
410 assert!(!review.requests_changes());
411 }
412
413 #[test]
414 fn test_pull_request_review_actionable_comments() {
415 let review = PullRequestReview {
416 review: GitHubReview {
417 id: 1,
418 user: GitHubUser {
419 login: "reviewer".to_string(),
420 },
421 body: None,
422 state: "CHANGES_REQUESTED".to_string(),
423 html_url: None,
424 submitted_at: "2024-01-01T00:00:00Z".to_string(),
425 commit_id: None,
426 },
427 comments: vec![
428 GitHubReviewComment {
429 id: 1,
430 user: GitHubUser {
431 login: "reviewer".to_string(),
432 },
433 body: "Fix this".to_string(),
434 path: "src/main.rs".to_string(),
435 line: Some(10),
436 original_line: Some(10),
437 commit_id: "abc".to_string(),
438 original_commit_id: "abc".to_string(),
439 html_url: "url".to_string(),
440 created_at: "2024-01-01T00:00:00Z".to_string(),
441 updated_at: "2024-01-01T00:00:00Z".to_string(),
442 in_reply_to_id: None,
443 diff_hunk: None,
444 },
445 GitHubReviewComment {
446 id: 2,
447 user: GitHubUser {
448 login: "reviewer".to_string(),
449 },
450 body: String::new(), path: "src/lib.rs".to_string(),
452 line: Some(20),
453 original_line: Some(20),
454 commit_id: "abc".to_string(),
455 original_commit_id: "abc".to_string(),
456 html_url: "url".to_string(),
457 created_at: "2024-01-01T00:00:00Z".to_string(),
458 updated_at: "2024-01-01T00:00:00Z".to_string(),
459 in_reply_to_id: None,
460 diff_hunk: None,
461 },
462 GitHubReviewComment {
463 id: 3,
464 user: GitHubUser {
465 login: "reviewer".to_string(),
466 },
467 body: "Reply to this".to_string(),
468 path: "src/lib.rs".to_string(),
469 line: Some(30),
470 original_line: Some(30),
471 commit_id: "abc".to_string(),
472 original_commit_id: "abc".to_string(),
473 html_url: "url".to_string(),
474 created_at: "2024-01-01T00:00:00Z".to_string(),
475 updated_at: "2024-01-01T00:00:00Z".to_string(),
476 in_reply_to_id: Some(1), diff_hunk: None,
478 },
479 ],
480 };
481
482 let actionable = review.actionable_comments();
483 assert_eq!(actionable.len(), 1);
484 assert_eq!(actionable[0].id, 1);
485 assert_eq!(actionable[0].body, "Fix this");
486 }
487
488 #[test]
489 fn extract_issue_number_from_url_works() {
490 assert_eq!(
491 extract_issue_number_from_url("https://api.github.com/repos/owner/repo/issues/123"),
492 Some("123".into())
493 );
494 }
495
496 #[test]
497 fn extract_issue_number_from_url_empty() {
498 assert_eq!(extract_issue_number_from_url(""), None);
499 }
500}