From 18a433331cecc416955e4dc0c710acce943953bc Mon Sep 17 00:00:00 2001 From: trunk bot Date: Fri, 29 Mar 2024 06:02:11 +0000 Subject: [PATCH] --- src/main.rs | 22 ++++++++++++++++------ src/process.rs | 16 ++++++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index a73f743dd880..82d81c981452 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use gen::cli::{Cli, Subcommands}; use gen::config::Conf; use gen::edit::change_file; use gen::github::GitHub; -use gen::process::{gh, git}; +use gen::process::{gh, git, try_git}; use rand::Rng; use regex::Regex; use serde_json::to_string_pretty; @@ -104,14 +104,19 @@ fn test_with_flakes(config: &Conf) -> bool { random_float > config.flake_rate } -fn create_pull_request(words: &[String]) -> String { +fn create_pull_request(words: &[String]) -> Result { let branch_name = format!("change/{}", words.join("-")); git(&["checkout", "-t", "-b", &branch_name]); let commit_msg = format!("Moving words {}", words.join(", ")); git(&["commit", "-am", &commit_msg]); - git(&["push", "--set-upstream", "origin", "HEAD"]); - + let result = try_git(&["push", "--set-upstream", "origin", "HEAD"]); + if result.is_err() { + git(&["checkout", "main"]); + git(&["pull"]); + return Err("could not push to origin".to_owned()); + } + let pr_url = gh(&[ "pr", "create", @@ -130,7 +135,7 @@ fn create_pull_request(words: &[String]) -> String { git(&["checkout", "main"]); git(&["pull"]); - pr_number.to_string() + Ok(pr_number.to_string()) } fn run() -> anyhow::Result<()> { @@ -195,8 +200,13 @@ fn run() -> anyhow::Result<()> { let max_impacted_deps = config.max_impacted_deps as u32; // Convert usize to u32 let words = change_file(&filenames, max_impacted_deps); // Use the converted value - let pr = create_pull_request(&words); + let pr_result = create_pull_request(&words); + if pr_result.is_err() { + println!("problem created pr for {:?}", words); + continue; + } let duration = start.elapsed(); + let pr = pr_result.unwrap(); println!("created pr: {} in {:?}", pr, duration); prs.push(pr); } diff --git a/src/process.rs b/src/process.rs index 7c9ebf31c5d8..64c2d82f2d16 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,24 +1,28 @@ use std::process::Command; -fn exec(cmd: &str, args: &[&str]) -> String { +fn exec(cmd: &str, args: &[&str]) -> Result { let output = Command::new(cmd) .args(args) .output() .expect(&format!("Failed to execute {}", cmd)); if !output.status.success() { - eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout)); eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr)); - panic!("Call to {} {} failed", cmd, args.join(" ")); + eprintln!("Call to {} {} failed", cmd, args.join(" ")); + return Err(String::from_utf8_lossy(&output.stderr).into_owned()); + } else { + return Ok(String::from_utf8_lossy(&output.stdout).into_owned()); } - - String::from_utf8_lossy(&output.stdout).into_owned() } pub fn gh(args: &[&str]) -> String { - exec("gh", args) + exec("gh", args).expect("gh exec failed") } pub fn git(args: &[&str]) -> String { + exec("git", args).expect("git exec failed") +} + +pub fn try_git(args: &[&str]) -> Result { exec("git", args) }