Skip to content

Commit 7baea0d

Browse files
committed
app/: closing and reopening a branch's builds (sc-8900): close_branch (a pull request closed without merging, or the branch deleted) closes its builds waiting for review with today's warning; reopen_branch brings back the newest; both idempotent and fire BuildChanged; for the forge listeners once #74 lands
1 parent c523ae2 commit 7baea0d

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎app/src/domain/builds.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,53 @@ pub async fn wait_until_reviewable(
440440
}
441441
}
442442
}
443+
444+
/// The warning on a build closed because its pull request was closed without merging.
445+
pub const CLOSED_PULL_REQUEST: &str = "Closed without review: the pull request was closed without merging.";
446+
/// The same, for a build whose branch was deleted.
447+
pub const CLOSED_BRANCH_DELETED: &str = "Closed without review: the branch was deleted.";
448+
449+
/// A branch's pull request closed without merging (`why` `pull_request`) or the branch deleted (`branch_deleted`): its
450+
/// builds waiting for review in this project stop waiting, and their checks and pull request comment say so
451+
/// (`BuildChanged`). Idempotent: a closed build isn't closed again. Returns how many.
452+
///
453+
/// # Errors
454+
/// On a database error.
455+
pub async fn close_branch(project_id: i64, branch: &str, why: &str) -> laravel::Result<usize> {
456+
let note = if why == "branch_deleted" { CLOSED_BRANCH_DELETED } else { CLOSED_PULL_REQUEST };
457+
let ids: Vec<i64> = db::query!(
458+
"UPDATE builds SET review_state = 'closed', warnings = warnings || jsonb_build_array({note}::TEXT), \
459+
updated_at = now() \
460+
WHERE project_id = {project_id} AND branch = {branch} AND review_state = 'pending' RETURNING id"
461+
)
462+
.select_as()
463+
.await?;
464+
for id in &ids {
465+
crate::events::BuildChanged { build_id: *id }.dispatch().await?;
466+
}
467+
Ok(ids.len())
468+
}
469+
470+
/// A closed pull request reopened: its branch's newest closed build waits for review again (older ones stay closed:
471+
/// the newest covers them), without the closing warning. Idempotent: with no closed build, nothing changes. Returns
472+
/// how many (0 or 1).
473+
///
474+
/// # Errors
475+
/// On a database error.
476+
pub async fn reopen_branch(project_id: i64, branch: &str) -> laravel::Result<usize> {
477+
let (pull_request, deleted) = (CLOSED_PULL_REQUEST, CLOSED_BRANCH_DELETED);
478+
let id: Option<i64> = db::query!(
479+
"UPDATE builds SET review_state = 'pending', updated_at = now(), \
480+
warnings = COALESCE((SELECT jsonb_agg(w) FROM jsonb_array_elements(warnings) w \
481+
WHERE w NOT IN (to_jsonb({pull_request}::TEXT), to_jsonb({deleted}::TEXT))), '[]'::jsonb) \
482+
WHERE id = (SELECT id FROM builds WHERE project_id = {project_id} AND branch = {branch} \
483+
AND review_state = 'closed' ORDER BY id DESC LIMIT 1) \
484+
RETURNING id"
485+
)
486+
.scalar()
487+
.await?;
488+
if let Some(id) = id {
489+
crate::events::BuildChanged { build_id: id }.dispatch().await?;
490+
}
491+
Ok(usize::from(id.is_some()))
492+
}

‎app/tests/branches.rs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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, &params, 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

Comments
 (0)