@@ -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
@@ -309,36 +326,37 @@ fn dummy_commit_from_body(sha: &str, body: &str) -> GithubCommit {
309
326
}
310
327
}
311
328
312
- #[ test]
313
- #[ rustfmt:: skip]
314
- fn test_warning_from_warnings ( ) {
315
- assert_eq ! (
316
- warning_from_warnings(
317
- & [
329
+ #[ cfg( test) ]
330
+ mod tests {
331
+ use super :: * ;
332
+
333
+ #[ test]
334
+ #[ rustfmt:: skip]
335
+ fn test_warning_from_warnings ( ) {
336
+ assert_eq ! (
337
+ warning_from_warnings(
338
+ & [
318
339
r#"This line should NOT be intend with 4 spaces,
319
- but this line should!"#
320
- . to_string( )
321
- ]
322
- ) ,
340
+ but this line should!"# . to_string( )
341
+ ]
342
+ ) ,
323
343
r#":warning: **Warning** :warning:
324
344
325
345
* This line should NOT be intend with 4 spaces,
326
346
but this line should!"#
327
- ) ;
347
+ ) ;
328
348
329
- assert_eq ! (
330
- warning_from_warnings( & [
349
+ assert_eq ! (
350
+ warning_from_warnings( & [
331
351
r#"This is warning 1.
332
352
333
353
Look at this list:
334
354
- 12
335
- - 13"#
336
- . to_string( ) ,
355
+ - 13"# . to_string( ) ,
337
356
r#"This is warning 2.
338
357
- 123456789
339
- "#
340
- . to_string( )
341
- ] ) ,
358
+ "# . to_string( )
359
+ ] ) ,
342
360
r#":warning: **Warning** :warning:
343
361
344
362
* This is warning 1.
@@ -348,5 +366,141 @@ r#":warning: **Warning** :warning:
348
366
- 13
349
367
* This is warning 2.
350
368
- 123456789"#
351
- ) ;
369
+ ) ;
370
+ }
371
+
372
+ fn make_opened_pr_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_pr_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_pr_event ( ) ;
434
+ assert ! ( should_handle_event( & event) ) ;
435
+ }
436
+
437
+ #[ test]
438
+ fn test_not_pr ( ) {
439
+ let mut event = make_opened_pr_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_pr_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_pr_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_pr_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_pr_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_pr_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_pr_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_pr_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
+ }
352
506
}
0 commit comments