Skip to content

Commit

Permalink
glacier: Append random sequence to branch name to make it unique
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
camelid committed Dec 10, 2020
1 parent bcb7f65 commit 689c731
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 13 additions & 7 deletions src/handlers/glacier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -46,25 +47,30 @@ 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::<String>()
);
fork.create_ref(&Reference::Branch(pr_branch_name.clone()), 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.clone())
.send()
.await?;

octocrab
.pulls("rust-lang", "glacier")
.create(
format!("ICE - rust-lang/rust#{}", number),
format!("rustbot:triagebot-ice-{}", number),
format!("rustbot:{}", pr_branch_name),
"master",
)
.body(format!(
Expand Down

0 comments on commit 689c731

Please sign in to comment.