|
| 1 | +//! A pull request closed without merging (or its branch deleted) closes its builds waiting for review; reopening it |
| 2 | +//! brings back the newest one. Both are idempotent: forges send the same event more than once. |
| 3 | +
|
| 4 | +use app::domain::builds::{self, CLOSED_BRANCH_DELETED, CLOSED_PULL_REQUEST, CreateParams}; |
| 5 | +use app::models::Build; |
| 6 | +use app::testing::fixtures; |
| 7 | +use laravel::db; |
| 8 | +use laravel::testing::prelude::*; |
| 9 | + |
| 10 | +async fn waiting_build(project: &app::models::Project, branch: &str, commit: &str) -> laravel::Result<i64> { |
| 11 | + let params: CreateParams = serde_json::from_value(serde_json::json!({ "commit": commit, "branch": branch }))?; |
| 12 | + let build = builds::create(project, ¶ms, None).await.map_err(|e| laravel::Error::msg(format!("{e:?}")))?; |
| 13 | + let id = build.id; |
| 14 | + db::query!("UPDATE builds SET state = 'completed', review_state = 'pending' WHERE id = {id}").update().await?; |
| 15 | + Ok(id) |
| 16 | +} |
| 17 | + |
| 18 | +#[laravel::test] |
| 19 | +async fn closing_a_branch_closes_its_waiting_builds_and_reopening_brings_back_the_newest( |
| 20 | + _app: Application, |
| 21 | +) -> laravel::Result<()> { |
| 22 | + let org = fixtures::organization("acme").await?; |
| 23 | + let project = fixtures::project(&org, "docs").await?; |
| 24 | + let older = waiting_build(&project, "feat", "a1").await?; |
| 25 | + let newer = waiting_build(&project, "feat", "a2").await?; |
| 26 | + let other = waiting_build(&project, "main", "b1").await?; |
| 27 | + |
| 28 | + assert_eq!(builds::close_branch(project.id, "feat", "pull_request").await?, 2); |
| 29 | + assert_eq!(builds::close_branch(project.id, "feat", "pull_request").await?, 0, "a repeated event changes nothing"); |
| 30 | + let closed = Build::find_or_fail(newer).await?; |
| 31 | + assert_eq!(closed.review_state, "closed"); |
| 32 | + assert_eq!(closed.warnings, serde_json::json!([CLOSED_PULL_REQUEST])); |
| 33 | + assert_eq!(Build::find_or_fail(other).await?.review_state, "pending", "another branch is left alone"); |
| 34 | + |
| 35 | + assert_eq!(builds::reopen_branch(project.id, "feat").await?, 1); |
| 36 | + let reopened = Build::find_or_fail(newer).await?; |
| 37 | + assert_eq!(reopened.review_state, "pending"); |
| 38 | + assert_eq!(reopened.warnings, serde_json::json!([])); |
| 39 | + assert_eq!(Build::find_or_fail(older).await?.review_state, "closed", "the newest covers the older ones"); |
| 40 | + assert_eq!(builds::reopen_branch(project.id, "feat").await?, 1, "the next closed one"); |
| 41 | + assert_eq!(builds::reopen_branch(project.id, "feat").await?, 0); |
| 42 | + |
| 43 | + builds::close_branch(project.id, "main", "branch_deleted").await?; |
| 44 | + assert_eq!(Build::find_or_fail(other).await?.warnings, serde_json::json!([CLOSED_BRANCH_DELETED])); |
| 45 | + Ok(()) |
| 46 | +} |
0 commit comments