@@ -39,25 +39,42 @@ struct CheckCommitsState {
39
39
last_labels : Vec < String > ,
40
40
}
41
41
42
- pub ( super ) async fn handle ( ctx : & Context , event : & Event , config : & Config ) -> anyhow:: Result < ( ) > {
43
- let Event :: Issue ( event) = event else {
44
- return Ok ( ( ) ) ;
45
- } ;
42
+ fn should_handle_event ( event : & IssuesEvent ) -> bool {
43
+ // Reject non-PR
44
+ if !event. issue . is_pr ( ) {
45
+ return false ;
46
+ }
47
+
48
+ // Reject rollups and draft pr
49
+ if event. issue . title . starts_with ( "Rollup of" ) || event. issue . draft {
50
+ return false ;
51
+ }
46
52
47
- let should_check = matches ! (
53
+ // Check opened, reopened, synchronized and ready-for-review PRs
54
+ if matches ! (
48
55
event. action,
49
56
IssuesAction :: Opened
50
57
| IssuesAction :: Reopened
51
58
| IssuesAction :: Synchronize
52
59
| IssuesAction :: ReadyForReview
53
- ) || event. has_base_changed ( ) ;
60
+ ) {
61
+ return true ;
62
+ }
54
63
55
- if !should_check || !event. issue . is_pr ( ) {
56
- return Ok ( ( ) ) ;
64
+ // Also check PRs that changed their base branch (master -> beta)
65
+ if event. has_base_changed ( ) {
66
+ return true ;
57
67
}
58
68
59
- // Don't ping on rollups or draft PRs.
60
- if event. issue . title . starts_with ( "Rollup of" ) || event. issue . draft {
69
+ false
70
+ }
71
+
72
+ pub ( super ) async fn handle ( ctx : & Context , event : & Event , config : & Config ) -> anyhow:: Result < ( ) > {
73
+ let Event :: Issue ( event) = event else {
74
+ return Ok ( ( ) ) ;
75
+ } ;
76
+
77
+ if !should_handle_event ( event) {
61
78
return Ok ( ( ) ) ;
62
79
}
63
80
@@ -350,3 +367,139 @@ r#":warning: **Warning** :warning:
350
367
- 123456789"#
351
368
) ;
352
369
}
370
+
371
+ #[ cfg( test) ]
372
+ fn make_opened_issues_event ( ) -> IssuesEvent {
373
+ IssuesEvent {
374
+ action : IssuesAction :: Opened ,
375
+ issue : crate :: github:: Issue {
376
+ number : 123 ,
377
+ body : "My PR body" . to_string ( ) ,
378
+ created_at : Default :: default ( ) ,
379
+ updated_at : Default :: default ( ) ,
380
+ merge_commit_sha : Default :: default ( ) ,
381
+ title : "Some title" . to_string ( ) ,
382
+ html_url : Default :: default ( ) ,
383
+ user : crate :: github:: User {
384
+ login : "user" . to_string ( ) ,
385
+ id : 654123 ,
386
+ } ,
387
+ labels : Default :: default ( ) ,
388
+ assignees : Default :: default ( ) ,
389
+ pull_request : Some ( Default :: default ( ) ) ,
390
+ merged : false ,
391
+ draft : false ,
392
+ comments : Default :: default ( ) ,
393
+ comments_url : Default :: default ( ) ,
394
+ repository : Default :: default ( ) ,
395
+ base : Some ( crate :: github:: CommitBase {
396
+ sha : "fake-sha" . to_string ( ) ,
397
+ git_ref : "master" . to_string ( ) ,
398
+ repo : None ,
399
+ } ) ,
400
+ head : Some ( crate :: github:: CommitBase {
401
+ sha : "fake-sha" . to_string ( ) ,
402
+ git_ref : "master" . to_string ( ) ,
403
+ repo : None ,
404
+ } ) ,
405
+ state : crate :: github:: IssueState :: Open ,
406
+ milestone : None ,
407
+ mergeable : None ,
408
+ author_association : octocrab:: models:: AuthorAssociation :: Contributor ,
409
+ } ,
410
+ changes : None ,
411
+ repository : crate :: github:: Repository {
412
+ full_name : "rust-lang/rust" . to_string ( ) ,
413
+ default_branch : "master" . to_string ( ) ,
414
+ fork : false ,
415
+ parent : None ,
416
+ } ,
417
+ sender : crate :: github:: User {
418
+ login : "rustbot" . to_string ( ) ,
419
+ id : 987654 ,
420
+ } ,
421
+ }
422
+ }
423
+
424
+ #[ test]
425
+ fn test_pr_closed ( ) {
426
+ let mut event = make_opened_issues_event ( ) ;
427
+ event. action = IssuesAction :: Closed ;
428
+ assert ! ( !should_handle_event( & event) ) ;
429
+ }
430
+
431
+ #[ test]
432
+ fn test_pr_opened ( ) {
433
+ let event = make_opened_issues_event ( ) ;
434
+ assert ! ( should_handle_event( & event) ) ;
435
+ }
436
+
437
+ #[ test]
438
+ fn test_not_pr ( ) {
439
+ let mut event = make_opened_issues_event ( ) ;
440
+ event. issue . pull_request = None ;
441
+ assert ! ( !should_handle_event( & event) ) ;
442
+ }
443
+
444
+ #[ test]
445
+ fn test_pr_rollup ( ) {
446
+ let mut event = make_opened_issues_event ( ) ;
447
+ event. issue . title = "Rollup of 6 pull requests" . to_string ( ) ;
448
+ assert ! ( !should_handle_event( & event) ) ;
449
+ }
450
+
451
+ #[ test]
452
+ fn test_pr_draft ( ) {
453
+ let mut event = make_opened_issues_event ( ) ;
454
+ event. issue . draft = true ;
455
+ assert ! ( !should_handle_event( & event) ) ;
456
+ }
457
+
458
+ #[ test]
459
+ fn test_pr_ready ( ) {
460
+ let mut event = make_opened_issues_event ( ) ;
461
+ event. action = IssuesAction :: ReadyForReview ;
462
+ assert ! ( should_handle_event( & event) ) ;
463
+ }
464
+
465
+ #[ test]
466
+ fn test_pr_reopened ( ) {
467
+ let mut event = make_opened_issues_event ( ) ;
468
+ event. action = IssuesAction :: Reopened ;
469
+ assert ! ( should_handle_event( & event) ) ;
470
+ }
471
+
472
+ #[ test]
473
+ fn test_pr_synchronized ( ) {
474
+ let mut event = make_opened_issues_event ( ) ;
475
+ event. action = IssuesAction :: Synchronize ;
476
+ assert ! ( should_handle_event( & event) ) ;
477
+ }
478
+
479
+ #[ test]
480
+ fn test_pr_edited_title ( ) {
481
+ let mut event = make_opened_issues_event ( ) ;
482
+ event. action = IssuesAction :: Edited ;
483
+ event. changes = Some ( crate :: github:: Changes {
484
+ base : None ,
485
+ body : None ,
486
+ title : Some ( crate :: github:: ChangeInner {
487
+ from : "Previous title" . to_string ( ) ,
488
+ } ) ,
489
+ } ) ;
490
+ assert ! ( !should_handle_event( & event) ) ;
491
+ }
492
+
493
+ #[ test]
494
+ fn test_pr_edited_base ( ) {
495
+ let mut event = make_opened_issues_event ( ) ;
496
+ event. action = IssuesAction :: Edited ;
497
+ event. changes = Some ( crate :: github:: Changes {
498
+ title : None ,
499
+ body : None ,
500
+ base : Some ( crate :: github:: ChangeInner {
501
+ from : "master" . to_string ( ) ,
502
+ } ) ,
503
+ } ) ;
504
+ assert ! ( should_handle_event( & event) ) ;
505
+ }
0 commit comments