diff --git a/app/src/domain/builds.rs b/app/src/domain/builds.rs index 7345b387..aedcfb46 100644 --- a/app/src/domain/builds.rs +++ b/app/src/domain/builds.rs @@ -440,3 +440,53 @@ pub async fn wait_until_reviewable( } } } + +/// The warning on a build closed because its pull request was closed without merging. +pub const CLOSED_PULL_REQUEST: &str = "Closed without review: the pull request was closed without merging."; +/// The same, for a build whose branch was deleted. +pub const CLOSED_BRANCH_DELETED: &str = "Closed without review: the branch was deleted."; + +/// A branch's pull request closed without merging (`why` `pull_request`) or the branch deleted (`branch_deleted`): its +/// builds waiting for review in this project stop waiting, and their checks and pull request comment say so +/// (`BuildChanged`). Idempotent: a closed build isn't closed again. Returns how many. +/// +/// # Errors +/// On a database error. +pub async fn close_branch(project_id: i64, branch: &str, why: &str) -> laravel::Result { + let note = if why == "branch_deleted" { CLOSED_BRANCH_DELETED } else { CLOSED_PULL_REQUEST }; + let ids: Vec = db::query!( + "UPDATE builds SET review_state = 'closed', warnings = warnings || jsonb_build_array({note}::TEXT), \ + updated_at = now() \ + WHERE project_id = {project_id} AND branch = {branch} AND review_state = 'pending' RETURNING id" + ) + .select_as() + .await?; + for id in &ids { + crate::events::BuildChanged { build_id: *id }.dispatch().await?; + } + Ok(ids.len()) +} + +/// A closed pull request reopened: its branch's newest closed build waits for review again (older ones stay closed: +/// the newest covers them), without the closing warning. Idempotent: with no closed build, nothing changes. Returns +/// how many (0 or 1). +/// +/// # Errors +/// On a database error. +pub async fn reopen_branch(project_id: i64, branch: &str) -> laravel::Result { + let (pull_request, deleted) = (CLOSED_PULL_REQUEST, CLOSED_BRANCH_DELETED); + let id: Option = db::query!( + "UPDATE builds SET review_state = 'pending', updated_at = now(), \ + warnings = COALESCE((SELECT jsonb_agg(w) FROM jsonb_array_elements(warnings) w \ + WHERE w NOT IN (to_jsonb({pull_request}::TEXT), to_jsonb({deleted}::TEXT))), '[]'::jsonb) \ + WHERE id = (SELECT id FROM builds WHERE project_id = {project_id} AND branch = {branch} \ + AND review_state = 'closed' ORDER BY id DESC LIMIT 1) \ + RETURNING id" + ) + .scalar() + .await?; + if let Some(id) = id { + crate::events::BuildChanged { build_id: id }.dispatch().await?; + } + Ok(usize::from(id.is_some())) +} diff --git a/app/tests/branches.rs b/app/tests/branches.rs new file mode 100644 index 00000000..34b51238 --- /dev/null +++ b/app/tests/branches.rs @@ -0,0 +1,46 @@ +//! A pull request closed without merging (or its branch deleted) closes its builds waiting for review; reopening it +//! brings back the newest one. Both are idempotent: forges send the same event more than once. + +use app::domain::builds::{self, CLOSED_BRANCH_DELETED, CLOSED_PULL_REQUEST, CreateParams}; +use app::models::Build; +use app::testing::fixtures; +use laravel::db; +use laravel::testing::prelude::*; + +async fn waiting_build(project: &app::models::Project, branch: &str, commit: &str) -> laravel::Result { + let params: CreateParams = serde_json::from_value(serde_json::json!({ "commit": commit, "branch": branch }))?; + let build = builds::create(project, ¶ms, None).await.map_err(|e| laravel::Error::msg(format!("{e:?}")))?; + let id = build.id; + db::query!("UPDATE builds SET state = 'completed', review_state = 'pending' WHERE id = {id}").update().await?; + Ok(id) +} + +#[laravel::test] +async fn closing_a_branch_closes_its_waiting_builds_and_reopening_brings_back_the_newest( + _app: Application, +) -> laravel::Result<()> { + let org = fixtures::organization("acme").await?; + let project = fixtures::project(&org, "docs").await?; + let older = waiting_build(&project, "feat", "a1").await?; + let newer = waiting_build(&project, "feat", "a2").await?; + let other = waiting_build(&project, "main", "b1").await?; + + assert_eq!(builds::close_branch(project.id, "feat", "pull_request").await?, 2); + assert_eq!(builds::close_branch(project.id, "feat", "pull_request").await?, 0, "a repeated event changes nothing"); + let closed = Build::find_or_fail(newer).await?; + assert_eq!(closed.review_state, "closed"); + assert_eq!(closed.warnings, serde_json::json!([CLOSED_PULL_REQUEST])); + assert_eq!(Build::find_or_fail(other).await?.review_state, "pending", "another branch is left alone"); + + assert_eq!(builds::reopen_branch(project.id, "feat").await?, 1); + let reopened = Build::find_or_fail(newer).await?; + assert_eq!(reopened.review_state, "pending"); + assert_eq!(reopened.warnings, serde_json::json!([])); + assert_eq!(Build::find_or_fail(older).await?.review_state, "closed", "the newest covers the older ones"); + assert_eq!(builds::reopen_branch(project.id, "feat").await?, 1, "the next closed one"); + assert_eq!(builds::reopen_branch(project.id, "feat").await?, 0); + + builds::close_branch(project.id, "main", "branch_deleted").await?; + assert_eq!(Build::find_or_fail(other).await?.warnings, serde_json::json!([CLOSED_BRANCH_DELETED])); + Ok(()) +}