-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
681 lines (595 loc) · 22.8 KB
/
main.rs
File metadata and controls
681 lines (595 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use std::{env, thread};
use chrono::{DateTime, Utc};
use clap::Parser;
use confique::Config;
use gen::cli::{Cli, Subcommands};
use gen::config::{Conf, EnqueueTrigger};
use gen::config_error::handle_config_load_error;
use gen::edit::edit_files_for_pr;
use gen::github::GitHub;
use gen::process::{git, run_cmd, try_gh, try_git, try_git_quiet};
use gen::trunk::{submit_pull_request, upload_targets};
use rand::Rng;
use regex::Regex;
use serde_json::{to_string_pretty, Value};
use walkdir::WalkDir;
/// Get the first GitHub token or exit with an error
fn get_first_github_token(cli: &Cli) -> String {
let github_tokens = cli.get_github_tokens();
if github_tokens.is_empty() {
eprintln!("No GitHub tokens provided. Use --gh-token to specify at least one token.");
std::process::exit(1);
}
github_tokens[0].clone()
}
fn get_txt_files(config: &Conf) -> std::io::Result<Vec<PathBuf>> {
let mut path = std::env::current_dir()?;
path.push(&config.pullrequest.change_code_path);
let mut paths = Vec::new();
for entry in WalkDir::new(&path) {
let entry = entry?;
if entry.file_type().is_file()
&& entry.path().extension().and_then(std::ffi::OsStr::to_str) == Some("txt")
{
paths.push(entry.path().to_path_buf());
}
}
Ok(paths)
}
fn housekeeping(config: &Conf, gh_token: &str) {
for _ in 0..3 {
let json_str = try_gh(
&[
"pr",
"list",
"--limit=1000",
"--json",
"number,mergeable,comments",
],
gh_token,
)
.expect("Failed to list PRs");
let v: Value = serde_json::from_str(&json_str).expect("Failed to parse JSON");
let mut has_unknown = false;
let mut requeued: HashSet<String> = HashSet::new();
if let Some(array) = v.as_array() {
for item in array {
let mergeable = item["mergeable"].as_str().unwrap_or("");
let pr = item["number"].as_i64().unwrap_or(0).to_string();
match mergeable {
"UNKNOWN" => {
has_unknown = true;
}
"CONFLICTING" => {
GitHub::close(&pr, gh_token);
println!("closed pr: {} (had merge conflicts)", &pr);
}
"MERGEABLE" => {
if requeued.contains(&pr) {
continue;
}
let comments = item["comments"].as_array().unwrap();
'comment: for comment in comments.iter() {
let body = comment["body"].as_str().unwrap_or("");
let created_at_str = comment["createdAt"].as_str().unwrap_or("");
if created_at_str.is_empty() {
continue 'comment;
}
let stale_age = Utc::now() - config.close_stale_after_duration();
let created_at = DateTime::parse_from_rfc3339(created_at_str)
.expect("Unable to parse datetime")
.with_timezone(&Utc);
if created_at > stale_age {
continue 'comment; // The datetime was less than stale age
}
if config
.pullrequest
.detect_stale_pr_comments
.iter()
.any(|s| body.contains(s))
{
//enqueue(&pr, config, cli, gh_token);
GitHub::close(&pr, gh_token);
println!("closed stale pr: {}", &pr);
requeued.insert(pr.to_owned());
}
}
}
_ => {
// handle other states
}
}
}
if has_unknown {
thread::sleep(Duration::from_secs(10));
} else {
return;
}
} else {
return;
}
}
}
fn configure_git(config: &Conf) {
git(&["config", "user.email", &config.git.email]);
git(&["config", "user.name", &config.git.name]);
}
fn get_repo_info() -> Result<(String, String), String> {
let remote_url = git(&["config", "--get", "remote.origin.url"]);
let re = Regex::new(r"[:/]([^/]+)/([^/]+?)(?:\.git)?$").unwrap();
if let Some(caps) = re.captures(&remote_url) {
let owner = caps.get(1).map_or("", |m| m.as_str()).to_string();
let name = caps.get(2).map_or("", |m| m.as_str()).to_string();
Ok((owner, name))
} else {
Err("Could not parse repository owner and name from remote URL".to_string())
}
}
fn enqueue(pr: &str, config: &Conf, cli: &Cli, gh_token: &str) {
match config.merge.trigger {
EnqueueTrigger::Comment => {
if config.merge.comment.is_empty() {
eprintln!("Cannot enqueue PR because merge 'trigger' is set to comment but no comment was provided");
return;
}
GitHub::comment(pr, &config.merge.comment, gh_token);
}
EnqueueTrigger::Label => {
if config.merge.labels.is_empty() {
eprintln!("Cannot enqueue PR because merge 'trigger' is set to label but no labels were provided");
return;
}
let labels: Vec<&str> = config.merge.labels.split(',').map(|s| s.trim()).collect();
for lbl in &labels {
GitHub::add_label(pr, lbl, gh_token);
}
}
EnqueueTrigger::Run => {
if config.merge.run.is_empty() {
eprintln!("Cannot enqueue PR because merge 'trigger' is set to run but no run command was provided");
return;
}
// perform token replacement for pr
let cmd = config.merge.run.replace("{{PR_NUMBER}}", pr);
println!("run commd {}", cmd);
let result = run_cmd(&cmd);
println!("merge run results: {}", result);
}
EnqueueTrigger::Api => {
// TRUNK_TOKEN will be checked at runtime in submit_pull_request
match get_repo_info() {
Ok((owner, name)) => {
let pr_number: u32 = match pr.parse() {
Ok(num) => num,
Err(_) => {
eprintln!("Invalid PR number: {}", pr);
return;
}
};
// Get the PR's base branch
let target_branch = GitHub::get_pr_base_branch(pr, gh_token);
println!("Enqueuing PR {} targeting branch: {}", pr, target_branch);
match submit_pull_request(
&owner,
&name,
pr_number,
&target_branch,
None, // Default priority, could be made configurable
&config.trunk.api,
cli,
) {
Ok(_) => println!(
"Successfully submitted PR {} to Trunk merge queue (target: {})",
pr, target_branch
),
Err(e) => {
eprintln!("Failed to submit PR {} to Trunk merge queue: {}", pr, e);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("Failed to get repository information: {}", e);
std::process::exit(1);
}
}
}
}
}
fn simulate_test(config: &Conf) -> bool {
let is_merge_str = env::var("IS_MERGE").unwrap_or_else(|_| String::from("false"));
let is_merge = is_merge_str.to_lowercase() == "true";
if !is_merge {
println!("no flake or sleep when running on pr branch");
return true;
}
println!("sleeping for {} seconds", config.sleep_duration().as_secs());
thread::sleep(config.sleep_duration());
if !config.pullrequest.logical_conflict_file.is_empty()
&& Path::new(&config.pullrequest.logical_conflict_file).exists()
{
// this pull request is simulating a "logical merge conflict" and should always fail
println!("Simulating logical merge conflict - failing test");
return false;
}
let mut rng = rand::thread_rng();
let random_float = rng.gen_range(0.0..1.0);
println!("Random float: {}", random_float);
println!("Flake rate: {}", config.test.flake_rate);
random_float > config.test.flake_rate
}
fn maybe_add_logical_merge_conflict(last_pr: u32, config: &Conf) -> bool {
if config.pullrequest.logical_conflict_file.is_empty()
|| config.pullrequest.logical_conflict_every == 0
{
return false;
}
// check if we should simulate a logical merge conflict with this pull request
if (last_pr + 1) % config.pullrequest.logical_conflict_every != 0 {
return false;
}
println!(
"logical conflict every {} prs",
config.pullrequest.logical_conflict_every
);
// create logical conflict
let filename = &config.pullrequest.logical_conflict_file;
std::fs::write(filename, "simulate logical merge conflict")
.expect("Unable to write logical merge conflict file");
git(&["add", &config.pullrequest.logical_conflict_file]);
true
}
fn checkout_branch(branch: &str) -> Result<(), String> {
// Check if branch exists locally (quietly - we expect this to fail if branch doesn't exist)
let branch_exists_locally =
try_git_quiet(&["rev-parse", "--verify", &format!("refs/heads/{}", branch)]).is_ok();
// Switch to the branch
if branch_exists_locally {
// Branch exists locally, just checkout
if let Err(e) = try_git(&["checkout", branch]) {
return Err(format!(
"Failed to checkout existing branch '{}': {}",
branch, e
));
}
} else {
// Branch doesn't exist locally, fetch from origin to check if it exists there
let _ = try_git(&["fetch", "origin", branch]);
// Check if it exists on origin (quietly - we expect this to fail if branch doesn't exist)
let remote_branch = format!("origin/{}", branch);
let remote_exists =
gen::process::try_git_quiet(&["rev-parse", "--verify", &remote_branch]).is_ok();
if remote_exists {
// Branch exists on origin, create local tracking branch
if let Err(e) = try_git(&["checkout", "-b", branch, &remote_branch]) {
return Err(format!(
"Failed to create local branch '{}' tracking {}: {}",
branch, remote_branch, e
));
}
} else {
return Err(format!(
"Base branch '{}' does not exist locally or on origin.",
branch
));
}
}
Ok(())
}
fn get_last_pr(gh_token: &str) -> u32 {
let result = try_gh(&["pr", "list", "--limit=1", "--json", "number"], gh_token);
if result.is_err() {
return 0;
}
let json_str = result.unwrap();
let v: Value = serde_json::from_str(&json_str).expect("Failed to parse JSON");
let array = v.as_array();
match array {
Some(pr_array) => {
if pr_array.is_empty() {
// No PRs in the system. return 0
return 0;
}
let first = pr_array.first().cloned();
match first {
Some(pr) => pr["number"].as_u64().unwrap_or(0) as u32,
None => 0,
}
}
None => 0,
}
}
fn create_pull_request(
filenames: &[String],
last_pr: u32,
config: &Conf,
dry_run: bool,
gh_token: &str,
base_branch: &str,
) -> Result<(String, usize), String> {
let current_branch = git(&["branch", "--show-current"]);
// Checkout the base branch (will fetch from origin if needed)
checkout_branch(base_branch)?;
// Pull latest changes
let _ = try_git(&["pull"]);
// Now edit the files to create changes (after we're on the correct base branch)
let next_pr_number = last_pr + 1;
let words = edit_files_for_pr(filenames, next_pr_number, config);
let deps_count = words.len();
let branch_name = format!("change/{}", words.join("-"));
git(&["checkout", "-b", &branch_name]);
// Create logical conflict file if needed (after we're on the new branch)
let lc = maybe_add_logical_merge_conflict(last_pr, config);
// Stage only the files that were modified (not all changes)
for filename in filenames {
let _ = try_git(&["add", filename]);
}
let commit_msg = format!("Moving words {}", words.join(", "));
if let Err(e) = try_git(&["commit", "--no-verify", "-m", &commit_msg]) {
return Err(format!("Failed to commit changes: {}", e));
}
if !dry_run {
let result = try_git(&["push", "--set-upstream", "origin", "HEAD"]);
if result.is_err() {
git(&["checkout", ¤t_branch]);
git(&["pull"]);
return Err("could not push to origin".to_owned());
}
}
let mut title = format!("[{}] {}", base_branch, words.join(", "));
if lc {
title = format!("{} (logical-conflict)", title);
}
let mut body = config.pullrequest.body.to_string();
body.push_str("\n\n[test]\n");
body.push_str(&format!("flake rate: {}\n", config.test.flake_rate));
body.push_str(&format!(
"logical conflict every: {}\n",
config.pullrequest.logical_conflict_every
));
body.push_str(&format!(
"sleep for: {}s\n",
config.sleep_duration().as_secs()
));
body.push_str(&format!(
"close stale after: {}\n",
config.pullrequest.close_stale_after
));
body.push_str(&format!(
"\n\n[pullrequest]\nrequests per hour: {}\ntarget branch: {}\n",
config.pullrequest.requests_per_hour, base_branch
));
let mut first_letters: Vec<_> = words
.iter()
.map(|word| word.chars().next().unwrap().to_string())
.collect();
first_letters.sort();
first_letters.dedup();
body.push_str(&format!(
"\n\ndeps=[{}]\n",
first_letters.into_iter().collect::<Vec<_>>().join(",")
));
let mut args: Vec<&str> = vec![
"pr",
"create",
"--title",
&title,
"--body",
&body,
"--base",
base_branch,
];
for lbl in config.pullrequest.labels.split(',') {
args.push("--label");
args.push(lbl.trim());
}
if dry_run {
git(&["checkout", ¤t_branch]);
git(&["pull"]);
return Ok(((last_pr + 1).to_string(), deps_count));
}
let result = try_gh(args.as_slice(), gh_token);
// no matter what is result - need to reset checkout and clean up
git(&["checkout", ¤t_branch]);
// Clean up any uncommitted changes and untracked files
let _ = try_git(&["reset", "--hard", "HEAD"]);
let _ = try_git(&["clean", "-fd"]);
git(&["pull"]);
if result.is_err() {
return Err("could not create pull request".to_owned());
}
let pr_url = result.unwrap();
let re = Regex::new(r"(.*)/pull/(\d+)$").unwrap();
let caps = re.captures(pr_url.trim()).unwrap();
let pr_number = caps.get(2).map_or("", |m| m.as_str());
Ok((pr_number.to_string(), deps_count))
}
fn generate(config: &Conf, cli: &Cli) -> anyhow::Result<()> {
if config.is_generator_disabled() {
println!("generator is disabled pull requests per hour is set to 0");
return Ok(());
}
configure_git(config);
let pull_requests_to_make: usize;
let pull_request_every: u64;
if config.pullrequest.requests_per_run > 0 {
pull_requests_to_make = config.pullrequest.requests_per_run as usize;
pull_request_every = 1;
println!(
"will generate {} requests in burst mode",
pull_requests_to_make
);
} else {
let dur = config.run_generate_for_duration();
let hours = dur.as_secs() as f32 / 3600.0;
pull_requests_to_make =
(config.pullrequest.requests_per_hour as f32 * hours).ceil() as usize;
// assuming that generating a pr doesn't take any time we will project to sleep every
pull_request_every = (dur.as_secs() as f32 / pull_requests_to_make as f32).ceil() as u64;
println!(
"will generate pull request every {} seconds",
pull_request_every
);
}
// get the most recent PR to be created (used for creating logical merge conflicts)
let github_tokens = cli.get_github_tokens();
if github_tokens.is_empty() {
eprintln!("No GitHub tokens provided. Use --gh-token to specify at least one token.");
std::process::exit(1);
}
let mut last_pr = get_last_pr(&github_tokens[0]);
let mut prs: Vec<String> = Vec::new();
if cli.dry_run {
println!("dry-run set - no actual pull requests will be generated");
}
if !github_tokens.is_empty() {
println!(
"Using {} GitHub token(s) in round-robin fashion",
github_tokens.len()
);
}
for token_index in 0..pull_requests_to_make {
let start = Instant::now();
let files = get_txt_files(config)?;
let mut filenames: Vec<String> = files
.into_iter()
.map(|path| path.to_string_lossy().into_owned())
.collect();
filenames.sort();
// Select token for this PR (round-robin)
let current_token = &github_tokens[token_index % github_tokens.len()];
// Select base branch for this PR (round-robin from protected_branches)
let protected_branches = &config.pullrequest.protected_branches;
let base_branch = &protected_branches[token_index % protected_branches.len()];
let pr_result = create_pull_request(
&filenames,
last_pr,
config,
cli.dry_run,
current_token,
base_branch,
);
if pr_result.is_err() {
println!("problem created pr for files: {:?}", filenames);
continue;
}
let duration = start.elapsed();
let (pr, deps_count) = pr_result.unwrap();
println!(
"created pr: {} (target: {}, deps: {}) in {}s // waiting: {} mins",
pr,
base_branch,
deps_count,
duration.as_secs(),
(pull_request_every as f32 / 60.0)
);
thread::sleep(Duration::from_secs(pull_request_every) / 2);
enqueue(&pr, config, cli, current_token); // Change the argument type to accept a String
thread::sleep(Duration::from_secs(pull_request_every) / 2);
prs.push(pr);
last_pr += 1;
}
Ok(())
}
fn run() -> anyhow::Result<()> {
let cli: Cli = Cli::parse();
if cli.subcommand.is_none() {
println!("Subcommand required. run 'mq help'");
return Ok(());
}
if let Some(Subcommands::Defaultconfig {}) = &cli.subcommand {
Conf::print_default();
return Ok(());
}
let config = Conf::builder()
.env()
.file("mq.toml")
.file(".config/mq.toml")
.load()
.unwrap_or_else(|err| handle_config_load_error(err));
config.is_valid(Some(&cli)).unwrap_or_else(|err| {
eprintln!("Invalid config:\n {}", err);
std::process::exit(1);
});
match &cli.subcommand {
Some(Subcommands::Housekeeping {}) => {
let token: String = get_first_github_token(&cli);
housekeeping(&config, &token);
Ok(())
}
Some(Subcommands::TestSim {}) => {
if !simulate_test(&config) {
std::process::exit(1);
}
Ok(())
}
Some(Subcommands::Config { path }) => {
if let Some(path) = path {
let path = path.trim();
if path.is_empty() {
// If path is empty, print full config
let config_json =
to_string_pretty(&config).expect("Failed to serialize config to JSON");
println!("{}", config_json);
return Ok(());
}
// Extract a single value from the config
let config_json: Value =
serde_json::to_value(&config).expect("Failed to serialize config to JSON");
let parts: Vec<&str> = path.split('.').collect();
let mut current = &config_json;
for part in parts {
match current.get(part) {
Some(value) => {
current = value;
}
None => {
eprintln!("Config path '{}' not found", path);
std::process::exit(1);
}
}
}
// Output the value - if it's a string, output without quotes, otherwise output as JSON
match current {
Value::String(s) => println!("{}", s),
_ => println!("{}", serde_json::to_string(current).unwrap()),
}
} else {
// Print full config as JSON
let config_json =
to_string_pretty(&config).expect("Failed to serialize config to JSON");
println!("{}", config_json);
}
Ok(())
}
Some(Subcommands::Generate {}) => generate(&config, &cli),
Some(Subcommands::UploadTargets(ut)) => {
// upload_targets(&cli, &gen::pullrequest::get_json()); // &ut.github_json);
upload_targets(&config, &cli, &ut.github_json);
Ok(())
}
Some(Subcommands::Enqueue(enqueue_args)) => {
println!("Enqueuing PR: {}", enqueue_args.pr);
let token = get_first_github_token(&cli);
enqueue(&enqueue_args.pr, &config, &cli, &token);
Ok(())
}
_ => {
// Handle other cases here
Err(anyhow::anyhow!("Subcommand not supported"))
}
}
}
fn main() {
env_logger::init();
match run() {
Ok(_) => (),
Err(err) => {
log::error!("{}", err);
std::process::exit(1);
}
}
}