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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
use std::{io::Write, ops::Add};
use anyhow::{bail, Context, Result};
use super::send::event_is_patch_set_root;
#[cfg(not(test))]
use crate::client::Client;
#[cfg(test)]
use crate::client::MockConnect;
use crate::{
cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms, PromptConfirmParms},
client::Connect,
git::{str_to_sha1, Repo, RepoActions},
repo_ref::{self, RepoRef, REPO_REF_KIND},
sub_commands::send::{
commit_msg_from_patch_oneliner, event_is_cover_letter, event_is_revision_root,
event_to_cover_letter, patch_supports_commit_ids, PATCH_KIND,
},
};
#[allow(clippy::too_many_lines)]
pub async fn launch() -> Result<()> {
let git_repo = Repo::discover().context("cannot find a git repository")?;
let root_commit = git_repo
.get_root_commit()
.context("failed to get root commit of the repository")?;
// TODO: check for empty repo
// TODO: check for existing maintaiers file
// TODO: check for other claims
#[cfg(not(test))]
let client = Client::default();
#[cfg(test)]
let client = <MockConnect as std::default::Default>::default();
let repo_ref = repo_ref::fetch(
&git_repo,
root_commit.to_string(),
&client,
client.get_fallback_relays().clone(),
true,
)
.await?;
println!("finding proposals...");
let proposal_events_and_revisions: Vec<nostr::Event> =
find_proposal_events(&client, &repo_ref, &root_commit.to_string()).await?;
if proposal_events_and_revisions.is_empty() {
println!("no proposals found... create one? try `ngit send`");
return Ok(());
}
let proposal_events: Vec<&nostr::Event> = proposal_events_and_revisions
.iter()
.filter(|e| !event_is_revision_root(e))
.collect::<Vec<&nostr::Event>>();
loop {
let selected_index = Interactor::default().choice(
PromptChoiceParms::default()
.with_prompt("all proposals")
.with_choices(
proposal_events
.iter()
.map(|e| {
if let Ok(cl) = event_to_cover_letter(e) {
cl.title
} else if let Ok(msg) = tag_value(e, "description") {
msg.split('\n').collect::<Vec<&str>>()[0].to_string()
} else {
e.id.to_string()
}
})
.collect(),
),
)?;
let cover_letter = event_to_cover_letter(proposal_events[selected_index])
.context("cannot extract proposal details from proposal root event")?;
println!("finding commits...");
let commits_events: Vec<nostr::Event> = find_commits_for_proposal_root_events(
&client,
&[
vec![proposal_events[selected_index]],
proposal_events_and_revisions
.iter()
.filter(|e| {
e.tags.iter().any(|t| {
t.as_vec().len().gt(&1)
&& t.as_vec()[1].eq(&proposal_events[selected_index].id.to_string())
})
})
.collect::<Vec<&nostr::Event>>(),
]
.concat(),
&repo_ref,
)
.await?;
// for commit in &commits_events {
// println!("cevent: {:?}", commit.as_json());
// }
let Ok(most_recent_proposal_patch_chain) =
get_most_recent_patch_with_ancestors(commits_events.clone())
else {
if Interactor::default().confirm(
PromptConfirmParms::default()
.with_default(true)
.with_prompt(
"cannot find any patches on this proposal. choose another proposal?",
),
)? {
continue;
}
return Ok(());
};
// for commit in &most_recent_proposal_patch_chain {
// println!("recent_event: {:?}", commit.as_json());
// }
let binding_patch_text_ref = format!("{} commits", most_recent_proposal_patch_chain.len());
let patch_text_ref = if most_recent_proposal_patch_chain.len().gt(&1) {
binding_patch_text_ref.as_str()
} else {
"1 commit"
};
let no_support_for_patches_as_branch = most_recent_proposal_patch_chain
.iter()
.any(|event| !patch_supports_commit_ids(event));
if no_support_for_patches_as_branch {
println!("{patch_text_ref}");
return match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec![
"learn why 'patch only' proposals can't be checked out".to_string(),
format!("apply to current branch with `git am`"),
format!("download to ./patches"),
"back".to_string(),
]),
)? {
0 => {
println!("Some proposals are posted as 'patch only'\n");
println!(
"they are not anchored against a particular state of the code base like a standard proposal or a GitHub Pull Request can be\n"
);
println!(
"they are designed to reviewed by studying the diff (in a tool like gitworkshop.dev) and if acceptable by a maintainer, applied to the latest version of master with any conflicts resolved as the do so\n"
);
println!(
"this has proven to be a smoother workflow for large scale projects with a high frequency of changes, even when patches are exchanged via email\n"
);
println!(
"by default ngit posts proposals that support both the branch and patch model so either workflow can be used"
);
Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec!["back".to_string()]),
)?;
continue;
}
1 => launch_git_am_with_patches(most_recent_proposal_patch_chain),
2 => save_patches_to_dir(most_recent_proposal_patch_chain, &git_repo),
3 => continue,
_ => {
bail!("unexpected choice")
}
};
}
let branch_exists = git_repo
.get_local_branch_names()
.context("gitlib2 will not show a list of local branch names")?
.iter()
.any(|n| n.eq(&cover_letter.branch_name));
let checked_out_proposal_branch = git_repo
.get_checked_out_branch_name()?
.eq(&cover_letter.branch_name);
let proposal_base_commit = str_to_sha1(&tag_value(
most_recent_proposal_patch_chain.last().context(
"there should be at least one patch as we have already checked for this",
)?,
"parent-commit",
)?)
.context("cannot get valid parent commit id from patch")?;
let (main_branch_name, master_tip) = git_repo.get_main_or_master_branch()?;
if !git_repo.does_commit_exist(&proposal_base_commit.to_string())? {
println!("your '{main_branch_name}' branch may not be up-to-date.");
println!("the proposal parent commit doesnt exist in your local repository.");
return match Interactor::default().choice(PromptChoiceParms::default().with_default(0).with_choices(
vec![
format!(
"manually run `git pull` on '{main_branch_name}' and select proposal again"
),
format!("apply to current branch with `git am`"),
format!("download to ./patches"),
"back".to_string(),
],
))? {
0 | 3 => continue,
1 => launch_git_am_with_patches(most_recent_proposal_patch_chain),
2 => save_patches_to_dir(most_recent_proposal_patch_chain, &git_repo),
_ => {
bail!("unexpected choice")
}
};
}
let proposal_tip = str_to_sha1(
&get_commit_id_from_patch(most_recent_proposal_patch_chain.first().context(
"there should be at least one patch as we have already checked for this",
)?)
.context("cannot get valid commit_id from patch")?,
)
.context("cannot get valid commit_id from patch")?;
let (_, proposal_behind_main) =
git_repo.get_commits_ahead_behind(&master_tip, &proposal_base_commit)?;
// branch doesnt exist
if !branch_exists {
return match Interactor::default()
.choice(PromptChoiceParms::default().with_default(0).with_choices(vec![
format!(
"create and checkout proposal branch ({} ahead {} behind '{main_branch_name}')",
most_recent_proposal_patch_chain.len(),
proposal_behind_main.len(),
),
format!("apply to current branch with `git am`"),
format!("download to ./patches"),
"back".to_string(),
]))? {
0 => {
check_clean(&git_repo)?;
let _ = git_repo
.apply_patch_chain(
&cover_letter.branch_name,
most_recent_proposal_patch_chain,
)
.context("cannot apply patch chain")?;
println!(
"checked out proposal as '{}' branch",
cover_letter.branch_name
);
Ok(())
}
1 => launch_git_am_with_patches(most_recent_proposal_patch_chain),
2 => save_patches_to_dir(most_recent_proposal_patch_chain, &git_repo),
3 => continue,
_ => {
bail!("unexpected choice")
}
};
}
let local_branch_tip = git_repo.get_tip_of_branch(&cover_letter.branch_name)?;
// up-to-date
if proposal_tip.eq(&local_branch_tip) {
if checked_out_proposal_branch {
println!("branch checked out and up-to-date");
return match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec!["exit".to_string(), "back".to_string()]),
)? {
0 => Ok(()),
1 => continue,
_ => {
bail!("unexpected choice")
}
};
}
return match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec![
format!(
"checkout proposal branch ({} ahead {} behind '{main_branch_name}')",
most_recent_proposal_patch_chain.len(),
proposal_behind_main.len(),
),
format!("apply to current branch with `git am`"),
format!("download to ./patches"),
"back".to_string(),
]),
)? {
0 => {
check_clean(&git_repo)?;
git_repo.checkout(&cover_letter.branch_name)?;
println!(
"checked out proposal as '{}' branch",
cover_letter.branch_name
);
Ok(())
}
1 => launch_git_am_with_patches(most_recent_proposal_patch_chain),
2 => save_patches_to_dir(most_recent_proposal_patch_chain, &git_repo),
3 => continue,
_ => {
bail!("unexpected choice")
}
};
}
let (local_ahead_of_main, local_beind_main) =
git_repo.get_commits_ahead_behind(&master_tip, &local_branch_tip)?;
// new appendments to proposal
if let Some(index) = most_recent_proposal_patch_chain.iter().position(|patch| {
get_commit_id_from_patch(patch)
.unwrap_or_default()
.eq(&local_branch_tip.to_string())
}) {
return match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec![
format!("checkout proposal branch and apply {} appendments", &index,),
format!("apply to current branch with `git am`"),
format!("download to ./patches"),
"back".to_string(),
]),
)? {
0 => {
check_clean(&git_repo)?;
git_repo.checkout(&cover_letter.branch_name)?;
let _ = git_repo
.apply_patch_chain(
&cover_letter.branch_name,
most_recent_proposal_patch_chain,
)
.context("cannot apply patch chain")?;
println!(
"checked out proposal branch and applied {} appendments ({} ahead {} behind '{main_branch_name}')",
&index,
local_ahead_of_main.len().add(&index),
local_beind_main.len(),
);
Ok(())
}
1 => launch_git_am_with_patches(most_recent_proposal_patch_chain),
2 => save_patches_to_dir(most_recent_proposal_patch_chain, &git_repo),
3 => continue,
_ => {
bail!("unexpected choice")
}
};
}
// new proposal revision / rebase
// tip of local in proposal history (new, amended or rebased version but no
// local changes)
if commits_events.iter().any(|patch| {
get_commit_id_from_patch(patch)
.unwrap_or_default()
.eq(&local_branch_tip.to_string())
}) {
println!(
"updated proposal available ({} ahead {} behind '{main_branch_name}'). existing version is {} ahead {} behind '{main_branch_name}'",
most_recent_proposal_patch_chain.len(),
proposal_behind_main.len(),
local_ahead_of_main.len(),
local_beind_main.len(),
);
return match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec![
format!("checkout and overwrite existing proposal branch"),
format!("checkout existing outdated proposal branch"),
format!("apply to current branch with `git am`"),
format!("download to ./patches"),
"back".to_string(),
]),
)? {
0 => {
check_clean(&git_repo)?;
git_repo.create_branch_at_commit(
&cover_letter.branch_name,
&proposal_base_commit.to_string(),
)?;
git_repo.checkout(&cover_letter.branch_name)?;
let chain_length = most_recent_proposal_patch_chain.len();
let _ = git_repo
.apply_patch_chain(
&cover_letter.branch_name,
most_recent_proposal_patch_chain,
)
.context("cannot apply patch chain")?;
println!(
"checked out new version of proposal ({} ahead {} behind '{main_branch_name}'), replacing old version ({} ahead {} behind '{main_branch_name}')",
chain_length,
proposal_behind_main.len(),
local_ahead_of_main.len(),
local_beind_main.len(),
);
Ok(())
}
1 => {
check_clean(&git_repo)?;
git_repo.checkout(&cover_letter.branch_name)?;
println!(
"checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')",
local_ahead_of_main.len(),
local_beind_main.len(),
);
Ok(())
}
2 => launch_git_am_with_patches(most_recent_proposal_patch_chain),
3 => save_patches_to_dir(most_recent_proposal_patch_chain, &git_repo),
4 => continue,
_ => {
bail!("unexpected choice")
}
};
}
// tip of proposal in branch in history (local appendments made to up-to-date
// proposal)
else if git_repo.ancestor_of(&local_branch_tip, &proposal_tip)? {
let (local_ahead_of_proposal, _) = git_repo
.get_commits_ahead_behind(&proposal_tip, &local_branch_tip)
.context("cannot get commits ahead behind for propsal_top and local_branch_tip")?;
println!(
"local proposal branch exists with {} unpublished commits on top of the most up-to-date version of the proposal ({} ahead {} behind '{main_branch_name}')",
local_ahead_of_proposal.len(),
local_ahead_of_main.len(),
proposal_behind_main.len(),
);
return match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec![
format!(
"checkout proposal branch with {} unpublished commits",
local_ahead_of_proposal.len(),
),
"back".to_string(),
]),
)? {
0 => {
git_repo.checkout(&cover_letter.branch_name)?;
println!(
"checked out proposal branch with {} unpublished commits ({} ahead {} behind '{main_branch_name}')",
local_ahead_of_proposal.len(),
local_ahead_of_main.len(),
proposal_behind_main.len(),
);
Ok(())
}
1 => continue,
_ => {
bail!("unexpected choice")
}
};
}
println!("you have an amended/rebase version the proposal that is unpublished");
// user probably has a unpublished amended or rebase version of the latest
// proposal version
// if tip of proposal commits exist (were once part of branch but have been
// amended and git clean up job hasn't removed them)
if git_repo.does_commit_exist(&proposal_tip.to_string())? {
println!(
"you have previously applied the latest version of the proposal ({} ahead {} behind '{main_branch_name}') but your local proposal branch has amended or rebased it ({} ahead {} behind '{main_branch_name}')",
most_recent_proposal_patch_chain.len(),
proposal_behind_main.len(),
local_ahead_of_main.len(),
local_beind_main.len(),
);
}
// user probably has a unpublished amended or rebase version of an older
// proposal version
else {
println!(
"your local proposal branch ({} ahead {} behind '{main_branch_name}') has conflicting changes with the latest published proposal ({} ahead {} behind '{main_branch_name}')",
local_ahead_of_main.len(),
local_beind_main.len(),
most_recent_proposal_patch_chain.len(),
proposal_behind_main.len(),
);
println!(
"its likely that you have rebased / amended an old proposal version because git has no record of the latest proposal commit."
);
println!(
"it is possible that you have been working off the latest version and git has delete this commit as part of a clean up"
);
}
println!("to view the latest proposal but retain your changes:");
println!(" 1) create a new branch off the tip commit of this one to store your changes");
println!(" 2) run `ngit list` and checkout the latest published version of this proposal");
println!("if you are confident in your changes consider running `ngit push --force`");
return match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_choices(vec![
format!("checkout local branch with unpublished changes"),
format!("discard unpublished changes and checkout new revision",),
format!("apply to current branch with `git am`"),
format!("download to ./patches"),
"back".to_string(),
]),
)? {
0 => {
check_clean(&git_repo)?;
git_repo.checkout(&cover_letter.branch_name)?;
println!(
"checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')",
local_ahead_of_main.len(),
local_beind_main.len(),
);
Ok(())
}
1 => {
check_clean(&git_repo)?;
git_repo.create_branch_at_commit(
&cover_letter.branch_name,
&proposal_base_commit.to_string(),
)?;
let chain_length = most_recent_proposal_patch_chain.len();
let _ = git_repo
.apply_patch_chain(&cover_letter.branch_name, most_recent_proposal_patch_chain)
.context("cannot apply patch chain")?;
git_repo.checkout(&cover_letter.branch_name)?;
println!(
"checked out latest version of proposal ({} ahead {} behind '{main_branch_name}'), replacing unpublished version ({} ahead {} behind '{main_branch_name}')",
chain_length,
proposal_behind_main.len(),
local_ahead_of_main.len(),
local_beind_main.len(),
);
Ok(())
}
2 => launch_git_am_with_patches(most_recent_proposal_patch_chain),
3 => save_patches_to_dir(most_recent_proposal_patch_chain, &git_repo),
4 => continue,
_ => {
bail!("unexpected choice")
}
};
}
}
fn launch_git_am_with_patches(mut patches: Vec<nostr::Event>) -> Result<()> {
println!("applying to current branch with `git am`");
// TODO: add PATCH x/n to appended patches
patches.reverse();
let mut am = std::process::Command::new("git")
.arg("am")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.spawn()
.context("failed to spawn git am")?;
let stdin = am
.stdin
.as_mut()
.context("git am process failed to take stdin")?;
for patch in patches {
stdin
.write(format!("{}\n\n", patch.content).as_bytes())
.context("failed to write patch content into git am stdin buffer")?;
}
stdin.flush()?;
let output = am
.wait_with_output()
.context("failed to read git am stdout")?;
print!("{:?}", output.stdout);
Ok(())
}
fn event_id_extra_shorthand(event: &nostr::Event) -> String {
event.id.to_string()[..5].to_string()
}
fn save_patches_to_dir(mut patches: Vec<nostr::Event>, git_repo: &Repo) -> Result<()> {
// TODO: add PATCH x/n to appended patches
patches.reverse();
let path = git_repo.get_path()?.join("patches");
std::fs::create_dir_all(&path)?;
let id = event_id_extra_shorthand(
patches
.first()
.context("there must be at least one patch to save")?,
);
for (i, patch) in patches.iter().enumerate() {
let path = path.join(format!(
"{}-{:0>4}-{}.patch",
&id,
i.add(&1),
commit_msg_from_patch_oneliner(patch)?
));
let mut file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(path)
.context("open new patch file with write and truncate options")?;
file.write_all(patch.content().as_bytes())?;
file.write_all("\n\n".as_bytes())?;
file.flush()?;
}
println!("created {} patch files in ./patches/{id}-*", patches.len());
Ok(())
}
fn check_clean(git_repo: &Repo) -> Result<()> {
if git_repo.has_outstanding_changes()? {
bail!(
"cannot pull proposal branch when repository is not clean. discard or stash (un)staged changes and try again."
);
}
Ok(())
}
pub fn tag_value(event: &nostr::Event, tag_name: &str) -> Result<String> {
Ok(event
.tags
.iter()
.find(|t| t.as_vec()[0].eq(tag_name))
.context(format!("tag '{tag_name}'not present"))?
.as_vec()[1]
.clone())
}
pub fn get_commit_id_from_patch(event: &nostr::Event) -> Result<String> {
let value = tag_value(event, "commit");
if value.is_ok() {
value
} else if event.content.starts_with("From ") && event.content.len().gt(&45) {
Ok(event.content[5..45].to_string())
} else {
bail!("event is not a patch")
}
}
fn get_event_parent_id(event: &nostr::Event) -> Result<String> {
Ok(if let Some(reply_tag) = event
.tags
.iter()
.find(|t| t.as_vec().len().gt(&3) && t.as_vec()[3].eq("reply"))
{
reply_tag
} else {
event
.tags
.iter()
.find(|t| t.as_vec().len().gt(&3) && t.as_vec()[3].eq("root"))
.context("no reply or root e tag present".to_string())?
}
.as_vec()[1]
.clone())
}
pub fn get_most_recent_patch_with_ancestors(
mut patches: Vec<nostr::Event>,
) -> Result<Vec<nostr::Event>> {
patches.sort_by_key(|e| e.created_at);
let youngest_patch = patches.last().context("no patches found")?;
let patches_with_youngest_created_at: Vec<&nostr::Event> = patches
.iter()
.filter(|p| p.created_at.eq(&youngest_patch.created_at))
.collect();
let mut res = vec![];
let mut event_id_to_search = patches_with_youngest_created_at
.clone()
.iter()
.find(|p| {
!patches_with_youngest_created_at.iter().any(|p2| {
if let Ok(reply_to) = get_event_parent_id(p2) {
reply_to.eq(&p.id.to_string())
} else {
false
}
})
})
.context("cannot find patches_with_youngest_created_at")?
.id
.to_string();
while let Some(event) = patches
.iter()
.find(|e| e.id.to_string().eq(&event_id_to_search))
{
res.push(event.clone());
if event_is_patch_set_root(event) {
break;
}
event_id_to_search = get_event_parent_id(event).unwrap_or_default();
}
Ok(res)
}
pub async fn find_proposal_events(
#[cfg(test)] client: &crate::client::MockConnect,
#[cfg(not(test))] client: &Client,
repo_ref: &RepoRef,
root_commit: &str,
) -> Result<Vec<nostr::Event>> {
let mut proposals = client
.get_events(
repo_ref.relays.clone(),
vec![
nostr::Filter::default()
.kind(nostr::Kind::Custom(PATCH_KIND))
.custom_tag(nostr::Alphabet::T, vec!["root"])
.custom_tag(
nostr::Alphabet::A,
repo_ref
.maintainers
.iter()
.map(|m| format!("{REPO_REF_KIND}:{m}:{}", repo_ref.identifier)),
),
// also pick up proposals from the same repo but no target at our maintainers repo
// events
nostr::Filter::default()
.kind(nostr::Kind::Custom(PATCH_KIND))
.custom_tag(nostr::Alphabet::T, vec!["root"])
.reference(root_commit),
],
)
.await
.context("cannot get proposal events")?
.iter()
.filter(|e| {
event_is_patch_set_root(e)
&& (e
.tags
.iter()
.any(|t| t.as_vec().len() > 1 && t.as_vec()[1].eq(root_commit))
|| e.tags.iter().any(|t| {
t.as_vec().len() > 1
&& repo_ref
.maintainers
.iter()
.map(|m| format!("{REPO_REF_KIND}:{m}:{}", repo_ref.identifier))
.any(|d| t.as_vec()[1].eq(&d))
}))
})
.map(std::borrow::ToOwned::to_owned)
.collect::<Vec<nostr::Event>>();
proposals.sort_by_key(|e| e.created_at);
proposals.reverse();
Ok(proposals)
}
pub async fn find_commits_for_proposal_root_events(
#[cfg(test)] client: &crate::client::MockConnect,
#[cfg(not(test))] client: &Client,
proposal_root_events: &Vec<&nostr::Event>,
repo_ref: &RepoRef,
) -> Result<Vec<nostr::Event>> {
let mut patch_events: Vec<nostr::Event> = client
.get_events(
repo_ref.relays.clone(),
vec![
nostr::Filter::default()
.kind(nostr::Kind::Custom(PATCH_KIND))
.events(
proposal_root_events
.iter()
.map(|e| e.id)
.collect::<Vec<nostr::EventId>>(),
),
],
)
.await
.context("cannot fetch patch events")?
.iter()
.filter(|e| {
e.kind.as_u64() == PATCH_KIND
&& e.tags.iter().any(|t| {
t.as_vec().len() > 2
&& proposal_root_events
.iter()
.any(|e| t.as_vec()[1].eq(&e.id.to_string()))
})
})
.map(std::borrow::ToOwned::to_owned)
.collect();
for e in proposal_root_events {
if !event_is_cover_letter(e) && !patch_events.iter().any(|e2| e2.id.eq(&e.id)) {
patch_events.push(e.to_owned().clone());
}
}
Ok(patch_events)
}
|