|
| 1 | +use crate::bors::Comment; |
| 2 | +use crate::bors::RepositoryState; |
| 3 | +use crate::database::PgDbClient; |
| 4 | +use crate::github::PullRequest; |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +pub(super) async fn command_info( |
| 8 | + repo: Arc<RepositoryState>, |
| 9 | + pr: &PullRequest, |
| 10 | + db: Arc<PgDbClient>, |
| 11 | +) -> anyhow::Result<()> { |
| 12 | + // Geting PR info from database |
| 13 | + let pr_model = db |
| 14 | + .get_or_create_pull_request(repo.client.repository(), pr.number) |
| 15 | + .await?; |
| 16 | + |
| 17 | + // Building the info message |
| 18 | + let mut info_lines = Vec::new(); |
| 19 | + |
| 20 | + // Approval info |
| 21 | + if let Some(approved_by) = pr_model.approved_by { |
| 22 | + info_lines.push(format!("- **Approved by:** @{}", approved_by)); |
| 23 | + } else { |
| 24 | + info_lines.push("- **Not Approved:**".to_string()); |
| 25 | + } |
| 26 | + |
| 27 | + // Priority info |
| 28 | + if let Some(priority) = pr_model.priority { |
| 29 | + info_lines.push(format!("- **Priority:** {}", priority)); |
| 30 | + } else { |
| 31 | + info_lines.push("- **Priority:** Not set".to_string()); |
| 32 | + } |
| 33 | + |
| 34 | + // Build status |
| 35 | + if let Some(try_build) = pr_model.try_build { |
| 36 | + info_lines.push(format!("- **Try build branch:** {}", try_build.branch)); |
| 37 | + |
| 38 | + if let Ok(urls) = db.get_workflow_urls_for_build(&try_build).await { |
| 39 | + info_lines.extend( |
| 40 | + urls.into_iter() |
| 41 | + .map(|url| format!("- **Workflow URL:** {}", url)), |
| 42 | + ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Joining all lines |
| 47 | + let info = info_lines.join("\n"); |
| 48 | + |
| 49 | + // Post the comment |
| 50 | + repo.client |
| 51 | + .post_comment(pr.number, Comment::new(info)) |
| 52 | + .await?; |
| 53 | + |
| 54 | + Ok(()) |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use crate::tests::mocks::{BorsBuilder, User, World}; |
| 60 | + |
| 61 | + fn create_world_with_approve_config() -> World { |
| 62 | + let world = World::default(); |
| 63 | + world.default_repo().lock().set_config( |
| 64 | + r#" |
| 65 | +[labels] |
| 66 | +approve = ["+approved"] |
| 67 | +"#, |
| 68 | + ); |
| 69 | + world |
| 70 | + } |
| 71 | + |
| 72 | + #[sqlx::test] |
| 73 | + async fn info_for_unapproved_pr(pool: sqlx::PgPool) { |
| 74 | + BorsBuilder::new(pool) |
| 75 | + .world(World::default()) |
| 76 | + .run_test(|mut tester| async { |
| 77 | + tester.post_comment("@bors info").await?; |
| 78 | + assert_eq!( |
| 79 | + tester.get_comment().await?, |
| 80 | + "- **Not Approved:**\n- **Priority:** Not set" |
| 81 | + ); |
| 82 | + Ok(tester) |
| 83 | + }) |
| 84 | + .await; |
| 85 | + } |
| 86 | + |
| 87 | + #[sqlx::test] |
| 88 | + async fn info_for_approved_pr(pool: sqlx::PgPool) { |
| 89 | + BorsBuilder::new(pool) |
| 90 | + .world(create_world_with_approve_config()) |
| 91 | + .run_test(|mut tester| async { |
| 92 | + // First approve the PR |
| 93 | + tester.post_comment("@bors r+").await?; |
| 94 | + tester.expect_comments(1).await; |
| 95 | + |
| 96 | + // Then check info |
| 97 | + tester.post_comment("@bors info").await?; |
| 98 | + assert_eq!( |
| 99 | + tester.get_comment().await?, |
| 100 | + format!( |
| 101 | + "- **Approved by:** @{}\n- **Priority:** Not set", |
| 102 | + User::default_user().name |
| 103 | + ) |
| 104 | + ); |
| 105 | + Ok(tester) |
| 106 | + }) |
| 107 | + .await; |
| 108 | + } |
| 109 | + |
| 110 | + #[sqlx::test] |
| 111 | + async fn info_for_pr_with_priority(pool: sqlx::PgPool) { |
| 112 | + BorsBuilder::new(pool) |
| 113 | + .world(create_world_with_approve_config()) |
| 114 | + .run_test(|mut tester| async { |
| 115 | + // Set priority |
| 116 | + tester.post_comment("@bors p=5").await?; |
| 117 | + tester |
| 118 | + .wait_for(|| async { |
| 119 | + let pr = tester.get_default_pr().await?; |
| 120 | + Ok(pr.priority == Some(5)) |
| 121 | + }) |
| 122 | + .await?; |
| 123 | + |
| 124 | + // Check info |
| 125 | + tester.post_comment("@bors info").await?; |
| 126 | + assert_eq!( |
| 127 | + tester.get_comment().await?, |
| 128 | + "- **Not Approved:**\n- **Priority:** 5" |
| 129 | + ); |
| 130 | + Ok(tester) |
| 131 | + }) |
| 132 | + .await; |
| 133 | + } |
| 134 | + |
| 135 | + #[sqlx::test] |
| 136 | + async fn info_for_pr_with_try_build(pool: sqlx::PgPool) { |
| 137 | + BorsBuilder::new(pool) |
| 138 | + .world(create_world_with_approve_config()) |
| 139 | + .run_test(|mut tester| async { |
| 140 | + // Create a try build |
| 141 | + tester.post_comment("@bors try").await?; |
| 142 | + tester.expect_comments(1).await; |
| 143 | + |
| 144 | + // Check info |
| 145 | + tester.post_comment("@bors info").await?; |
| 146 | + assert_eq!( |
| 147 | + tester.get_comment().await?, |
| 148 | + "- **Not Approved:**\n- **Priority:** Not set\n- **Try build branch:** automation/bors/try" |
| 149 | + ); |
| 150 | + Ok(tester) |
| 151 | + }) |
| 152 | + .await; |
| 153 | + } |
| 154 | + |
| 155 | + #[sqlx::test] |
| 156 | + async fn info_for_pr_with_everything(pool: sqlx::PgPool) { |
| 157 | + BorsBuilder::new(pool) |
| 158 | + .world(create_world_with_approve_config()) |
| 159 | + .run_test(|mut tester| async { |
| 160 | + // Approve with priority |
| 161 | + tester.post_comment("@bors r+ p=10").await?; |
| 162 | + tester.expect_comments(1).await; |
| 163 | + |
| 164 | + // Create a try build |
| 165 | + tester.post_comment("@bors try").await?; |
| 166 | + tester.expect_comments(1).await; |
| 167 | + |
| 168 | + // Check info |
| 169 | + tester.post_comment("@bors info").await?; |
| 170 | + assert_eq!( |
| 171 | + tester.get_comment().await?, |
| 172 | + format!( |
| 173 | + "- **Approved by:** @{}\n- **Priority:** 10\n- **Try build branch:** automation/bors/try", |
| 174 | + User::default_user().name |
| 175 | + ) |
| 176 | + ); |
| 177 | + Ok(tester) |
| 178 | + }) |
| 179 | + .await; |
| 180 | + } |
| 181 | +} |
0 commit comments