Skip to content

Commit 0012786

Browse files
authored
Merge pull request #2111 from Urgau/test-check-commits-trigger
Extract check-commits handler trigger logic and add tests for it
2 parents 3510332 + 8d94d24 commit 0012786

File tree

2 files changed

+184
-29
lines changed

2 files changed

+184
-29
lines changed

src/github.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ pub struct Label {
287287
/// and some don't. GitHub does include a few fields here, but they aren't
288288
/// needed at this time (merged_at, diff_url, html_url, patch_url, url).
289289
#[derive(Debug, serde::Deserialize)]
290+
#[cfg_attr(test, derive(Default))]
290291
pub struct PullRequestDetails {
291292
/// This is a slot to hold the diff for a PR.
292293
///

src/handlers/check_commits.rs

Lines changed: 183 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,42 @@ struct CheckCommitsState {
3939
last_labels: Vec<String>,
4040
}
4141

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+
}
4652

47-
let should_check = matches!(
53+
// Check opened, reopened, synchronized and ready-for-review PRs
54+
if matches!(
4855
event.action,
4956
IssuesAction::Opened
5057
| IssuesAction::Reopened
5158
| IssuesAction::Synchronize
5259
| IssuesAction::ReadyForReview
53-
) || event.has_base_changed();
60+
) {
61+
return true;
62+
}
5463

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;
5767
}
5868

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) {
6178
return Ok(());
6279
}
6380

@@ -309,36 +326,37 @@ fn dummy_commit_from_body(sha: &str, body: &str) -> GithubCommit {
309326
}
310327
}
311328

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+
&[
318339
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+
),
323343
r#":warning: **Warning** :warning:
324344
325345
* This line should NOT be intend with 4 spaces,
326346
but this line should!"#
327-
);
347+
);
328348

329-
assert_eq!(
330-
warning_from_warnings(&[
349+
assert_eq!(
350+
warning_from_warnings(&[
331351
r#"This is warning 1.
332352
333353
Look at this list:
334354
- 12
335-
- 13"#
336-
.to_string(),
355+
- 13"#.to_string(),
337356
r#"This is warning 2.
338357
- 123456789
339-
"#
340-
.to_string()
341-
]),
358+
"#.to_string()
359+
]),
342360
r#":warning: **Warning** :warning:
343361
344362
* This is warning 1.
@@ -348,5 +366,141 @@ r#":warning: **Warning** :warning:
348366
- 13
349367
* This is warning 2.
350368
- 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+
}
352506
}

0 commit comments

Comments
 (0)