Skip to content

Commit

Permalink
fix issue with upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed Dec 26, 2024
1 parent 5d451ff commit 9ce756a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
14 changes: 14 additions & 0 deletions server/src/command/auto_try.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,6 +39,17 @@ async fn handle_with_pr<R: GitHubRepoClient>(
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) {
Expand Down
10 changes: 5 additions & 5 deletions server/src/database/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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())
Expand Down
7 changes: 4 additions & 3 deletions server/src/webhook/pull_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub async fn handle_with_pr<R: GitHubRepoClient>(
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)
Expand All @@ -44,7 +45,7 @@ pub async fn handle_with_pr<R: GitHubRepoClient>(
.optional()
.context("fetch ci run")?;

match run {
match &run {
Some(run) if !run.is_dry_run => {
repo.merge_workflow().cancel(&run, repo, conn, &current).await?;
repo.send_message(
Expand All @@ -59,13 +60,13 @@ pub async fn handle_with_pr<R: GitHubRepoClient>(
)
.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, &current).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())
Expand Down

0 comments on commit 9ce756a

Please sign in to comment.