From 775f20fc136c5532838b1b07cac5b3e59c1a5cbf Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 9 Dec 2020 16:07:11 -0800 Subject: [PATCH] glacier: Append random sequence to branch name to make it unique Sometimes the user (me) will make a mistake (e.g. leave out `/rust-play/` from the URL) when invoking `@rustbot glacier` that means the automatically-created PR has to be closed. However, there was no way to try again, because the branch name would conflict and triagebot would crash. Now, we append an 8-character random alphanumeric string to the branch name so that the branch names are unique and you can try again. This adds a dependency on `rand`, which is probably the most downloaded crate all-time, but we already depended on it through other crates. --- Cargo.lock | 1 + Cargo.toml | 1 + src/handlers/glacier.rs | 20 +++++++++++++------- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c07bfd179..cdfe9a4cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1919,6 +1919,7 @@ dependencies = [ "openssl", "parser", "postgres-native-tls", + "rand", "regex", "reqwest", "rust_team_data", diff --git a/Cargo.toml b/Cargo.toml index 509d1c2e9..22c9dd282 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ native-tls = "0.2" serde_path_to_error = "0.1.2" octocrab = "0.5" comrak = "0.8.2" +rand = "0.7" [dependencies.serde] version = "1" diff --git a/src/handlers/glacier.rs b/src/handlers/glacier.rs index d98fb489d..9cddfb828 100644 --- a/src/handlers/glacier.rs +++ b/src/handlers/glacier.rs @@ -5,6 +5,7 @@ use crate::{config::GlacierConfig, github::Event, handlers::Context}; use octocrab::models::Object; use octocrab::params::repos::Reference; use parser::command::glacier::GlacierCommand; +use rand::Rng as _; pub(super) async fn handle_command( ctx: &Context, @@ -46,17 +47,22 @@ pub(super) async fn handle_command( unreachable!() }; - fork.create_ref( - &Reference::Branch(format!("triagebot-ice-{}", number)), - master, - ) - .await?; + let pr_branch_name = format!( + "triagebot-ice-{}-{}", + number, + rand::thread_rng() + .sample_iter(&rand::distributions::Alphanumeric) + .take(8) + .collect::() + ); + fork.create_ref(&Reference::Branch(pr_branch_name), master) + .await?; fork.create_file( format!("ices/{}.rs", number), format!("Add ICE reproduction for issue rust-lang/rust#{}.", number), body, ) - .branch(format!("triagebot-ice-{}", number)) + .branch(pr_branch_name) .send() .await?; @@ -64,7 +70,7 @@ pub(super) async fn handle_command( .pulls("rust-lang", "glacier") .create( format!("ICE - rust-lang/rust#{}", number), - format!("rustbot:triagebot-ice-{}", number), + format!("rustbot:{}", pr_branch_name), "master", ) .body(format!(