From 9ce756a18834e21044f4fa07a05bfd7bdd764593 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Thu, 26 Dec 2024 14:39:21 +0000 Subject: [PATCH] fix issue with upsert --- server/src/command/auto_try.rs | 14 ++++++++++++++ server/src/database/pr.rs | 10 +++++----- server/src/webhook/pull_request.rs | 7 ++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/server/src/command/auto_try.rs b/server/src/command/auto_try.rs index cd32776..6df2047 100644 --- a/server/src/command/auto_try.rs +++ b/server/src/command/auto_try.rs @@ -1,6 +1,9 @@ +use anyhow::Context; +use diesel::OptionalExtension; use diesel_async::{AsyncPgConnection, RunQueryDsl}; use super::BrawlCommandContext; +use crate::database::ci_run::CiRun; use crate::database::pr::Pr; use crate::github::messages; use crate::github::models::PullRequest; @@ -36,6 +39,17 @@ async fn handle_with_pr( return Ok(()); } + let active_run = CiRun::active(context.repo.id(), context.pr_number) + .get_result(conn) + .await + .optional() + .context("fetch active run")?; + + if active_run.is_some_and(|r| !r.is_dry_run) { + context.repo.send_message(context.pr_number, &messages::error_no_body("Cannot enable auto-try while a merge is in progress, use `?brawl cancel` to cancel it first & then try again.")).await?; + return Ok(()); + } + let db_pr = Pr::find(context.repo.id(), context.pr_number).get_result(conn).await?; match (command.disable, db_pr.auto_try) { diff --git a/server/src/database/pr.rs b/server/src/database/pr.rs index cd8b574..8206793 100644 --- a/server/src/database/pr.rs +++ b/server/src/database/pr.rs @@ -137,14 +137,14 @@ impl<'a> Pr<'a> { author_id: pr.user.as_ref().map(|u| u.id.0 as i64).unwrap_or(user_id.0 as i64), assigned_ids: pr_assigned_ids(pr), status: pr_status(pr), - default_priority: None, merge_commit_sha: pr.merged_at.and_then(|_| pr.merge_commit_sha.as_deref().map(Cow::Borrowed)), target_branch: Cow::Borrowed(&pr.base.ref_field), source_branch: Cow::Borrowed(&pr.head.ref_field), latest_commit_sha: Cow::Borrowed(&pr.head.sha), - added_labels: Vec::new(), created_at: chrono::Utc::now(), updated_at: chrono::Utc::now(), + default_priority: None, + added_labels: Vec::new(), auto_try: false, } } @@ -172,14 +172,14 @@ impl<'a> Pr<'a> { title: Some(Cow::Borrowed(self.title.as_ref())), body: Some(Cow::Borrowed(self.body.as_ref())), merge_status: Some(self.merge_status), - added_labels: Some(self.added_labels.clone()), assigned_ids: Some(self.assigned_ids.clone()), status: Some(self.status), - default_priority: Some(self.default_priority), merge_commit_sha: Some(self.merge_commit_sha.as_deref().map(Cow::Borrowed)), target_branch: Some(Cow::Borrowed(self.target_branch.as_ref())), latest_commit_sha: Some(Cow::Borrowed(self.latest_commit_sha.as_ref())), - auto_try: Some(self.auto_try), + default_priority: None, + added_labels: None, + auto_try: None, updated_at: self.updated_at, }) .returning(Pr::as_select()) diff --git a/server/src/webhook/pull_request.rs b/server/src/webhook/pull_request.rs index 998953b..bc80fee 100644 --- a/server/src/webhook/pull_request.rs +++ b/server/src/webhook/pull_request.rs @@ -35,6 +35,7 @@ pub async fn handle_with_pr( let update = current.update_from(&pr); if update.needs_update() { update.query().execute(conn).await?; + let commit_head_changed = current.latest_commit_sha != pr.head.sha; update.update_pr(&mut current); // Fetch the active run (if there is one) @@ -44,7 +45,7 @@ pub async fn handle_with_pr( .optional() .context("fetch ci run")?; - match run { + match &run { Some(run) if !run.is_dry_run => { repo.merge_workflow().cancel(&run, repo, conn, ¤t).await?; repo.send_message( @@ -59,13 +60,13 @@ pub async fn handle_with_pr( ) .await?; } - Some(run) if current.auto_try => { + Some(run) if current.auto_try && commit_head_changed && run.is_dry_run => { repo.merge_workflow().cancel(&run, repo, conn, ¤t).await?; } _ => {} } - if current.auto_try { + if current.auto_try && commit_head_changed && run.is_none_or(|r| r.is_dry_run) { let run = CiRun::insert(repo.id(), pr.number) .base_ref(Base::from_pr(&pr)) .head_commit_sha(pr.head.sha.as_str().into())