|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use crate::PgDbClient; |
| 4 | +use crate::bors::handlers::{PullRequestData, deny_request, has_permission}; |
| 5 | +use crate::bors::merge_queue::MergeQueueSender; |
| 6 | +use crate::bors::{Comment, RepositoryState}; |
| 7 | +use crate::github::{GithubUser, PullRequestNumber}; |
| 8 | +use crate::permissions::PermissionType; |
| 9 | + |
| 10 | +pub(super) async fn command_retry( |
| 11 | + repo_state: Arc<RepositoryState>, |
| 12 | + db: Arc<PgDbClient>, |
| 13 | + pr: PullRequestData<'_>, |
| 14 | + author: &GithubUser, |
| 15 | + merge_queue_tx: &MergeQueueSender, |
| 16 | +) -> anyhow::Result<()> { |
| 17 | + if !has_permission(&repo_state, author, pr, PermissionType::Review).await? { |
| 18 | + deny_request(&repo_state, pr.number(), author, PermissionType::Review).await?; |
| 19 | + return Ok(()); |
| 20 | + } |
| 21 | + |
| 22 | + let pr_model = pr.db; |
| 23 | + |
| 24 | + if pr_model.is_stalled() { |
| 25 | + db.clear_auto_build(pr_model).await?; |
| 26 | + merge_queue_tx.notify().await?; |
| 27 | + } else { |
| 28 | + notify_of_invalid_retry_state(&repo_state, pr.number()).await?; |
| 29 | + } |
| 30 | + |
| 31 | + Ok(()) |
| 32 | +} |
| 33 | + |
| 34 | +async fn notify_of_invalid_retry_state( |
| 35 | + repo: &RepositoryState, |
| 36 | + pr_number: PullRequestNumber, |
| 37 | +) -> anyhow::Result<()> { |
| 38 | + repo.client |
| 39 | + .post_comment( |
| 40 | + pr_number, |
| 41 | + Comment::new(":exclamation: You can only retry pull requests that are approved and have a previously failed auto build".to_string()) |
| 42 | + ) |
| 43 | + .await?; |
| 44 | + Ok(()) |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use crate::tests::{BorsTester, Comment, User, run_test}; |
| 50 | + |
| 51 | + #[sqlx::test] |
| 52 | + async fn retry_command_insufficient_privileges(pool: sqlx::PgPool) { |
| 53 | + run_test(pool, async |tester: &mut BorsTester| { |
| 54 | + tester |
| 55 | + .post_comment(Comment::from("@bors retry").with_author(User::unprivileged())) |
| 56 | + .await?; |
| 57 | + insta::assert_snapshot!( |
| 58 | + tester.get_comment_text(()).await?, |
| 59 | + @"@unprivileged-user: :key: Insufficient privileges: not in review users" |
| 60 | + ); |
| 61 | + Ok(()) |
| 62 | + }) |
| 63 | + .await; |
| 64 | + } |
| 65 | + |
| 66 | + #[sqlx::test] |
| 67 | + async fn retry_invalid_state_error(pool: sqlx::PgPool) { |
| 68 | + run_test(pool, async |tester: &mut BorsTester| { |
| 69 | + tester.post_comment(Comment::from("@bors retry")).await?; |
| 70 | + insta::assert_snapshot!( |
| 71 | + tester.get_comment_text(()).await?, |
| 72 | + @":exclamation: You can only retry pull requests that are approved and have a previously failed auto build" |
| 73 | + ); |
| 74 | + Ok(()) |
| 75 | + }) |
| 76 | + .await; |
| 77 | + } |
| 78 | + |
| 79 | + #[sqlx::test] |
| 80 | + async fn retry_auto_build(pool: sqlx::PgPool) { |
| 81 | + run_test(pool, async |tester: &mut BorsTester| { |
| 82 | + tester.approve(()).await?; |
| 83 | + tester.start_auto_build(()).await?; |
| 84 | + tester |
| 85 | + .workflow_full_failure(tester.auto_branch().await) |
| 86 | + .await?; |
| 87 | + tester.expect_comments((), 1).await; |
| 88 | + tester.post_comment(Comment::from("@bors retry")).await?; |
| 89 | + tester.wait_for_pr((), |pr| pr.auto_build.is_none()).await?; |
| 90 | + tester.process_merge_queue().await; |
| 91 | + insta::assert_snapshot!( |
| 92 | + tester.get_comment_text(()).await?, |
| 93 | + @":hourglass: Testing commit pr-1-sha with merge merge-1-pr-1..." |
| 94 | + ); |
| 95 | + Ok(()) |
| 96 | + }) |
| 97 | + .await; |
| 98 | + } |
| 99 | +} |
0 commit comments