Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
trunk bot committed Mar 29, 2024
1 parent b08188c commit 18a4333
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
22 changes: 16 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> {
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",
Expand All @@ -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<()> {
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 10 additions & 6 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
use std::process::Command;

fn exec(cmd: &str, args: &[&str]) -> String {
fn exec(cmd: &str, args: &[&str]) -> Result<String, String> {
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<String, String> {
exec("git", args)
}

0 comments on commit 18a4333

Please sign in to comment.