Skip to content

Commit f11c1f7

Browse files
committed
Add tests for PR conflict comments
1 parent a50a2ca commit f11c1f7

File tree

4 files changed

+113
-23
lines changed

4 files changed

+113
-23
lines changed

src/bors/handlers/pr_events.rs

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub(super) async fn handle_pull_request_opened(
119119
author: payload.pull_request.author.username.clone(),
120120
assignees,
121121
base_branch: payload.pull_request.base.name.clone(),
122-
mergeable_state: MergeableState::Unknown,
122+
mergeable_state: payload.pull_request.mergeable_state.clone().into(),
123123
pr_status,
124124
},
125125
)
@@ -361,6 +361,7 @@ async fn notify_of_pushed_pr(
361361

362362
#[cfg(test)]
363363
mod tests {
364+
use insta::assert_snapshot;
364365
use octocrab::params::checks::{CheckRunConclusion, CheckRunStatus};
365366

366367
use crate::bors::PullRequestStatus;
@@ -439,7 +440,7 @@ mod tests {
439440
tester.get_pr_copy(()).await.expect_unapproved();
440441
Ok(())
441442
})
442-
.await;
443+
.await;
443444
}
444445

445446
#[sqlx::test]
@@ -653,13 +654,13 @@ mod tests {
653654
run_test(pool, async |tester: &mut BorsTester| {
654655
let pr = tester
655656
.open_pr(default_repo_name(), |pr| {
656-
pr.mergeable_state = OctocrabMergeableState::Unknown
657+
pr.mergeable_state = OctocrabMergeableState::Unknown;
657658
})
658659
.await?;
659-
tester.push_to_branch(default_branch_name()).await?;
660+
tester.push_to_branch(default_branch_name(), "sha").await?;
660661
tester
661662
.modify_pr_state(pr.id(), |pr| {
662-
pr.mergeable_state = OctocrabMergeableState::Dirty
663+
pr.mergeable_state = OctocrabMergeableState::Dirty;
663664
})
664665
.await;
665666
tester
@@ -672,6 +673,86 @@ mod tests {
672673
.await;
673674
}
674675

676+
#[sqlx::test]
677+
async fn conflict_message_unknown_sha(pool: sqlx::PgPool) {
678+
run_test(pool, async |tester: &mut BorsTester| {
679+
let pr = tester
680+
.open_pr(default_repo_name(), |pr| {
681+
pr.mergeable_state = OctocrabMergeableState::Clean;
682+
})
683+
.await?;
684+
tester
685+
.modify_pr_state(pr.id(), |pr| {
686+
pr.mergeable_state = OctocrabMergeableState::Dirty;
687+
})
688+
.await;
689+
tester.push_to_branch(default_branch_name(), "sha").await?;
690+
assert_snapshot!(tester.get_next_comment_text(pr).await?, @":umbrella: The latest upstream changes made this pull request unmergeable. Please [resolve the merge conflicts](https://rustc-dev-guide.rust-lang.org/git.html#rebasing-and-conflicts).");
691+
692+
Ok(())
693+
})
694+
.await;
695+
}
696+
697+
#[sqlx::test]
698+
async fn conflict_message_unknown_sha_approved(pool: sqlx::PgPool) {
699+
run_test(pool, async |tester: &mut BorsTester| {
700+
let pr = tester
701+
.open_pr(default_repo_name(), |pr| {
702+
pr.mergeable_state = OctocrabMergeableState::Clean;
703+
})
704+
.await?;
705+
tester.approve(pr.id()).await?;
706+
tester
707+
.modify_pr_state(pr.id(), |pr| {
708+
pr.mergeable_state = OctocrabMergeableState::Dirty;
709+
})
710+
.await;
711+
tester.push_to_branch(default_branch_name(), "sha").await?;
712+
assert_snapshot!(tester.get_next_comment_text(pr.id()).await?, @r"
713+
:umbrella: The latest upstream changes made this pull request unmergeable. Please [resolve the merge conflicts](https://rustc-dev-guide.rust-lang.org/git.html#rebasing-and-conflicts).
714+
715+
This pull request was unapproved.
716+
");
717+
tester.get_pr_copy(pr.id()).await.expect_unapproved();
718+
719+
Ok(())
720+
})
721+
.await;
722+
}
723+
724+
#[sqlx::test]
725+
async fn conflict_message_known_sha(pool: sqlx::PgPool) {
726+
run_test(pool, async |tester: &mut BorsTester| {
727+
let pr2 = tester
728+
.open_pr(default_repo_name(), |pr| {
729+
pr.mergeable_state = OctocrabMergeableState::Clean;
730+
})
731+
.await?;
732+
733+
let pr1 = tester
734+
.open_pr(default_repo_name(), |_| {})
735+
.await?;
736+
tester.approve(pr1.id()).await?;
737+
tester.start_auto_build(pr1.id()).await?;
738+
tester.workflow_full_success(tester.auto_branch().await).await?;
739+
tester.process_merge_queue().await;
740+
tester.expect_comments(pr1.id(), 1).await;
741+
let sha = tester.auto_branch().await.get_sha().to_string();
742+
743+
tester
744+
.modify_pr_state(pr2.id(), |pr| {
745+
pr.mergeable_state = OctocrabMergeableState::Dirty;
746+
})
747+
.await;
748+
tester.push_to_branch(default_branch_name(), &sha).await?;
749+
assert_snapshot!(tester.get_next_comment_text(pr2.id()).await?, @":umbrella: The latest upstream changes (presumably #3) made this pull request unmergeable. Please [resolve the merge conflicts](https://rustc-dev-guide.rust-lang.org/git.html#rebasing-and-conflicts).");
750+
751+
Ok(())
752+
})
753+
.await;
754+
}
755+
675756
#[sqlx::test]
676757
async fn enqueue_prs_on_pr_opened(pool: sqlx::PgPool) {
677758
run_test(pool, async |tester: &mut BorsTester| {
@@ -752,7 +833,7 @@ mod tests {
752833
");
753834
Ok(())
754835
})
755-
.await;
836+
.await;
756837
gh.check_cancelled_workflows(default_repo_name(), &[123]);
757838
}
758839

@@ -777,7 +858,7 @@ mod tests {
777858
");
778859
Ok(())
779860
})
780-
.await;
861+
.await;
781862
}
782863

783864
#[sqlx::test]

src/server/webhook.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -487,22 +487,23 @@ mod tests {
487487
insta::assert_debug_snapshot!(
488488
check_webhook("webhook/push.json", "push").await,
489489
@r#"
490-
Ok(
491-
GitHubWebhook(
492-
Repository(
493-
PushToBranch(
494-
PushToBranch {
495-
repository: GithubRepoName {
496-
owner: "kobzol",
497-
name: "bors-kindergarten",
498-
},
499-
branch: "main",
490+
Ok(
491+
GitHubWebhook(
492+
Repository(
493+
PushToBranch(
494+
PushToBranch {
495+
repository: GithubRepoName {
496+
owner: "kobzol",
497+
name: "bors-kindergarten",
500498
},
501-
),
499+
branch: "main",
500+
sha: "bc7370e473896a94d40a7dff71f197a3ff0208f5",
501+
},
502502
),
503503
),
504-
)
505-
"#
504+
),
505+
)
506+
"#
506507
);
507508
}
508509

src/tests/mocks/pull_request.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ impl From<()> for PrIdentifier {
8989
}
9090
}
9191

92+
impl From<PullRequest> for PrIdentifier {
93+
fn from(pr: PullRequest) -> Self {
94+
pr.id()
95+
}
96+
}
97+
9298
pub enum CommentMsg {
9399
Comment(Comment),
94100
Close,
@@ -534,13 +540,15 @@ pub struct GitHubPushEventPayload {
534540
pub repository: GitHubRepository,
535541
#[serde(rename = "ref")]
536542
pub ref_field: String,
543+
pub after: String,
537544
}
538545

539546
impl GitHubPushEventPayload {
540-
pub fn new(branch_name: &str) -> Self {
547+
pub fn new(branch_name: &str, sha: &str) -> Self {
541548
GitHubPushEventPayload {
542549
repository: default_repo_name().into(),
543550
ref_field: format!("refs/heads/{branch_name}"),
551+
after: sha.to_string(),
544552
}
545553
}
546554
}

src/tests/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ impl BorsTester {
352352
.get_commit_message(branch.get_sha())
353353
}
354354

355-
pub async fn push_to_branch(&mut self, branch: &str) -> anyhow::Result<()> {
356-
self.send_webhook("push", GitHubPushEventPayload::new(branch))
355+
pub async fn push_to_branch(&mut self, branch: &str, sha: &str) -> anyhow::Result<()> {
356+
self.send_webhook("push", GitHubPushEventPayload::new(branch, sha))
357357
.await
358358
}
359359

0 commit comments

Comments
 (0)