Skip to content

Commit 18a4333

Browse files
author
trunk bot
committed
1 parent b08188c commit 18a4333

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/main.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use gen::cli::{Cli, Subcommands};
44
use gen::config::Conf;
55
use gen::edit::change_file;
66
use gen::github::GitHub;
7-
use gen::process::{gh, git};
7+
use gen::process::{gh, git, try_git};
88
use rand::Rng;
99
use regex::Regex;
1010
use serde_json::to_string_pretty;
@@ -104,14 +104,19 @@ fn test_with_flakes(config: &Conf) -> bool {
104104
random_float > config.flake_rate
105105
}
106106

107-
fn create_pull_request(words: &[String]) -> String {
107+
fn create_pull_request(words: &[String]) -> Result<String, String> {
108108
let branch_name = format!("change/{}", words.join("-"));
109109
git(&["checkout", "-t", "-b", &branch_name]);
110110

111111
let commit_msg = format!("Moving words {}", words.join(", "));
112112
git(&["commit", "-am", &commit_msg]);
113-
git(&["push", "--set-upstream", "origin", "HEAD"]);
114-
113+
let result = try_git(&["push", "--set-upstream", "origin", "HEAD"]);
114+
if result.is_err() {
115+
git(&["checkout", "main"]);
116+
git(&["pull"]);
117+
return Err("could not push to origin".to_owned());
118+
}
119+
115120
let pr_url = gh(&[
116121
"pr",
117122
"create",
@@ -130,7 +135,7 @@ fn create_pull_request(words: &[String]) -> String {
130135
git(&["checkout", "main"]);
131136
git(&["pull"]);
132137

133-
pr_number.to_string()
138+
Ok(pr_number.to_string())
134139
}
135140

136141
fn run() -> anyhow::Result<()> {
@@ -195,8 +200,13 @@ fn run() -> anyhow::Result<()> {
195200
let max_impacted_deps = config.max_impacted_deps as u32; // Convert usize to u32
196201
let words = change_file(&filenames, max_impacted_deps); // Use the converted value
197202

198-
let pr = create_pull_request(&words);
203+
let pr_result = create_pull_request(&words);
204+
if pr_result.is_err() {
205+
println!("problem created pr for {:?}", words);
206+
continue;
207+
}
199208
let duration = start.elapsed();
209+
let pr = pr_result.unwrap();
200210
println!("created pr: {} in {:?}", pr, duration);
201211
prs.push(pr);
202212
}

src/process.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
use std::process::Command;
22

3-
fn exec(cmd: &str, args: &[&str]) -> String {
3+
fn exec(cmd: &str, args: &[&str]) -> Result<String, String> {
44
let output = Command::new(cmd)
55
.args(args)
66
.output()
77
.expect(&format!("Failed to execute {}", cmd));
88

99
if !output.status.success() {
10-
eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
1110
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
12-
panic!("Call to {} {} failed", cmd, args.join(" "));
11+
eprintln!("Call to {} {} failed", cmd, args.join(" "));
12+
return Err(String::from_utf8_lossy(&output.stderr).into_owned());
13+
} else {
14+
return Ok(String::from_utf8_lossy(&output.stdout).into_owned());
1315
}
14-
15-
String::from_utf8_lossy(&output.stdout).into_owned()
1616
}
1717

1818
pub fn gh(args: &[&str]) -> String {
19-
exec("gh", args)
19+
exec("gh", args).expect("gh exec failed")
2020
}
2121

2222
pub fn git(args: &[&str]) -> String {
23+
exec("git", args).expect("git exec failed")
24+
}
25+
26+
pub fn try_git(args: &[&str]) -> Result<String, String> {
2327
exec("git", args)
2428
}

0 commit comments

Comments
 (0)