Skip to content

Commit 04fa114

Browse files
EliSchleifertrunk bot
andauthored
Eli/fix logical rate (#14384)
logical rate will be every X pull requests instead of using a math random function. This will guarantee better distribution --------- Co-authored-by: trunk bot <[email protected]>
1 parent a804b5d commit 04fa114

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

.config/mq.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ max_deps = 7
2323
max_impacted_deps = 2
2424

2525
# rate at which PRs should simulate a logical merge conflict and need to be rejected
26-
logical_conflict_rate = 0.002
26+
logical_conflict_every = 500
2727
logical_conflict_file = "logical-conflict.txt"
2828

2929
[merge]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ mode = "singlequeue"
4848
# Default value: 1
4949
#max_impacted_deps = 1
5050

51-
# Default value: 0.01
52-
#logical_conflict_rate = 0.01
51+
# Default value: 100 (create logical merge conflict every 100 PRs)
52+
#logical_conflict_every = 100
5353

5454
# Default value: "logical-conflict.txt"
5555
#logical_conflict_file = "logical-conflict.txt"

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ pub struct PullRequestConf {
6767
#[config(default = 1)]
6868
pub max_impacted_deps: usize,
6969

70-
#[config(default = 0.01)]
71-
pub logical_conflict_rate: f32,
70+
#[config(default = 100)]
71+
pub logical_conflict_every: u32,
7272

7373
#[config(default = "logical-conflict.txt")]
7474
pub logical_conflict_file: String,

src/main.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,35 +157,49 @@ fn simulate_test(config: &Conf) -> bool {
157157
random_float > config.test.flake_rate
158158
}
159159

160-
fn maybe_add_logical_merge_conflict(config: &Conf) -> bool {
160+
fn maybe_add_logical_merge_conflict(last_pr: u32, config: &Conf) -> bool {
161161
if config.pullrequest.logical_conflict_file.is_empty()
162-
|| config.pullrequest.logical_conflict_rate == 0.0
162+
|| config.pullrequest.logical_conflict_every == 0
163163
{
164164
return false;
165165
}
166166

167167
// check if we should simulate a logical merge conflict with this pull request
168-
let mut rng = rand::thread_rng();
169-
let random_float = rng.gen_range(0.0..1.0);
168+
if last_pr + 1 % config.pullrequest.logical_conflict_every != 0 {
169+
return false;
170+
}
170171

171172
println!(
172-
"logical conflict rate: {}",
173-
config.pullrequest.logical_conflict_rate
173+
"logical conflict every {} prs",
174+
config.pullrequest.logical_conflict_every
174175
);
175-
if random_float < config.pullrequest.logical_conflict_rate {
176-
// create logical conflict
177-
let filename = &config.pullrequest.logical_conflict_file;
178-
std::fs::write(filename, "simulate logical merge conflict")
179-
.expect("Unable to write logical merge conflict file");
180176

181-
git(&["add", &config.pullrequest.logical_conflict_file]);
182-
return true;
177+
// create logical conflict
178+
let filename = &config.pullrequest.logical_conflict_file;
179+
std::fs::write(filename, "simulate logical merge conflict")
180+
.expect("Unable to write logical merge conflict file");
181+
182+
git(&["add", &config.pullrequest.logical_conflict_file]);
183+
true
184+
}
185+
186+
fn get_last_pr() -> u32 {
187+
let result = try_gh(&["pr", "list", "--limit=1", "--json", "number"]);
188+
if result.is_err() {
189+
return 0;
183190
}
184-
false
191+
let json_str = result.unwrap();
192+
193+
let v: Value = serde_json::from_str(&json_str).expect("Failed to parse JSON");
194+
let last_pr = v
195+
.as_array()
196+
.and_then(|arr| arr.first().cloned())
197+
.expect("Failed to get first item");
198+
last_pr["number"].as_u64().unwrap_or(0) as u32
185199
}
186200

187-
fn create_pull_request(words: &[String], config: &Conf) -> Result<String, String> {
188-
let lc = maybe_add_logical_merge_conflict(config);
201+
fn create_pull_request(words: &[String], last_pr: u32, config: &Conf) -> Result<String, String> {
202+
let lc = maybe_add_logical_merge_conflict(last_pr, config);
189203

190204
let branch_name = format!("change/{}", words.join("-"));
191205
git(&["checkout", "-t", "-b", &branch_name]);
@@ -281,6 +295,9 @@ fn run() -> anyhow::Result<()> {
281295
// divide by 6 since we run once every 10 minutes
282296
let pull_requests_to_make = (config.pullrequest.requests_per_hour as f32 / 6.0).ceil() as usize;
283297

298+
// get the most recent PR to be created (used for creating logical merge conflicts)
299+
let mut last_pr = get_last_pr();
300+
284301
let mut prs: Vec<String> = Vec::new();
285302

286303
for _ in 0..pull_requests_to_make {
@@ -300,7 +317,7 @@ fn run() -> anyhow::Result<()> {
300317
let max_impacted_deps = config.pullrequest.max_impacted_deps as u32; // Convert usize to u32
301318
let words = change_file(&filenames, max_impacted_deps); // Use the converted value
302319

303-
let pr_result = create_pull_request(&words, &config);
320+
let pr_result = create_pull_request(&words, last_pr, &config);
304321
if pr_result.is_err() {
305322
println!("problem created pr for {:?}", words);
306323
continue;
@@ -309,6 +326,7 @@ fn run() -> anyhow::Result<()> {
309326
let pr = pr_result.unwrap();
310327
println!("created pr: {} in {:?}", pr, duration);
311328
prs.push(pr);
329+
last_pr += 1;
312330
}
313331

314332
for pr in &prs {

0 commit comments

Comments
 (0)