From 6381e06cbb33f3a86fd352c6157e8346a765c569 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Wed, 12 Jun 2024 11:10:38 +0800 Subject: [PATCH] feat: skip duplicated git fetch --- buildit-utils/src/github.rs | 38 +++++++++++++++++++++-------------- buildit-utils/src/lib.rs | 2 +- server/src/api.rs | 10 ++++++--- server/src/bot.rs | 1 + server/src/routes/pipeline.rs | 1 + 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/buildit-utils/src/github.rs b/buildit-utils/src/github.rs index dce8c01..2314e71 100644 --- a/buildit-utils/src/github.rs +++ b/buildit-utils/src/github.rs @@ -87,7 +87,7 @@ pub async fn open_pr( archs, } = openpr_request; - update_abbs(&git_ref, &abbs_path).await?; + update_abbs(&git_ref, &abbs_path, false).await?; let abbs_path_clone = abbs_path.clone(); let commits = task::spawn_blocking(move || get_commits(&abbs_path_clone)) @@ -304,7 +304,11 @@ fn get_commits(path: &Path) -> anyhow::Result> { /// Update ABBS tree commit logs #[tracing::instrument(skip(abbs_path))] -pub async fn update_abbs>(git_ref: &str, abbs_path: P) -> anyhow::Result<()> { +pub async fn update_abbs>( + git_ref: &str, + abbs_path: P, + skip_git_fetch: bool, +) -> anyhow::Result<()> { info!("Running git checkout -b stable ..."); let abbs_path = abbs_path.as_ref(); @@ -332,21 +336,25 @@ pub async fn update_abbs>(git_ref: &str, abbs_path: P) -> anyhow: print_stdout_and_stderr(&output); - info!("Running git fetch origin {git_ref} ..."); - - let output = process::Command::new("git") - .arg("fetch") - .arg("origin") - .arg(git_ref) - .current_dir(abbs_path) - .output() - .instrument(info_span!("git_fetch_origin")) - .await?; + if skip_git_fetch { + info!("Skippping git fetch ...") + } else { + info!("Running git fetch origin {git_ref} ..."); + + let output = process::Command::new("git") + .arg("fetch") + .arg("origin") + .arg(git_ref) + .current_dir(abbs_path) + .output() + .instrument(info_span!("git_fetch_origin")) + .await?; - print_stdout_and_stderr(&output); + print_stdout_and_stderr(&output); - if !output.status.success() { - bail!("Failed to fetch origin git-ref: {git_ref}"); + if !output.status.success() { + bail!("Failed to fetch origin git-ref: {git_ref}"); + } } info!("Running git reset origin/stable --hard ..."); diff --git a/buildit-utils/src/lib.rs b/buildit-utils/src/lib.rs index 3b0fc0a..68b7e48 100644 --- a/buildit-utils/src/lib.rs +++ b/buildit-utils/src/lib.rs @@ -50,7 +50,7 @@ pub async fn find_update_and_update_checksum( let _lock = ABBS_REPO_LOCK.lock().await; // switch to stable branch - update_abbs("stable", &abbs_path).await?; + update_abbs("stable", &abbs_path, false).await?; info!("Running aosc-findupdate ..."); diff --git a/server/src/api.rs b/server/src/api.rs index 342aa6e..1ab64ea 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -1,7 +1,6 @@ use crate::{ github::{get_crab_github_installation, get_packages_from_pr}, models::{Job, NewJob, NewPipeline, Pipeline, User, Worker}, - schema::jobs::github_check_run_id, DbPool, ALL_ARCH, ARGS, }; use anyhow::Context; @@ -60,6 +59,7 @@ pub async fn pipeline_new( packages: &str, archs: &str, source: &JobSource, + skip_git_fetch: bool, ) -> anyhow::Result { // sanitize archs arg let mut archs: Vec<&str> = archs.split(',').collect(); @@ -103,7 +103,7 @@ pub async fn pipeline_new( } let lock = ABBS_REPO_LOCK.lock().await; - update_abbs(git_branch, &ARGS.abbs_path) + update_abbs(git_branch, &ARGS.abbs_path, skip_git_fetch) .await .context("Failed to update ABBS tree")?; @@ -267,15 +267,18 @@ pub async fn pipeline_new_pr( // find lines starting with #buildit let packages = get_packages_from_pr(&pr); if !packages.is_empty() { + let mut skip_git_fetch = false; let archs = if let Some(archs) = archs { archs.to_string() } else { let path = &ARGS.abbs_path; let _lock = ABBS_REPO_LOCK.lock().await; - update_abbs(git_branch, &ARGS.abbs_path) + update_abbs(git_branch, &ARGS.abbs_path, false) .await .context("Failed to update ABBS tree")?; + // skip next git fetch in pipeline_new + skip_git_fetch = true; let resolved_packages = resolve_packages(&packages, path).context("Failed to resolve packages")?; @@ -291,6 +294,7 @@ pub async fn pipeline_new_pr( &packages.join(","), &archs, source, + skip_git_fetch, ) .await } else { diff --git a/server/src/bot.rs b/server/src/bot.rs index b27fbf2..e9726ea 100644 --- a/server/src/bot.rs +++ b/server/src/bot.rs @@ -131,6 +131,7 @@ async fn pipeline_new_and_report( packages, archs, &JobSource::Telegram(msg.chat.id.0), + false, ) .await { diff --git a/server/src/routes/pipeline.rs b/server/src/routes/pipeline.rs index 00d572f..f851b4d 100644 --- a/server/src/routes/pipeline.rs +++ b/server/src/routes/pipeline.rs @@ -37,6 +37,7 @@ pub async fn pipeline_new( &payload.packages, &payload.archs, &JobSource::Manual, + false, ) .await?; Ok(Json(PipelineNewResponse { id: pipeline.id }))