upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2023-05-21 11:24:58 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2023-05-21 11:24:58 +0000
commit1555833279efac993be5a0897b5552a8c0e9c77d (patch)
tree464c408acefdf40395e87561129d5eb9c1407f95 /src
parentd8dbd5fe94219b9df7f0aa5f6f810009335f0f46 (diff)
prs
Diffstat (limited to 'src')
-rw-r--r--src/sub_commands/prs.rs113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/sub_commands/prs.rs b/src/sub_commands/prs.rs
new file mode 100644
index 0000000..beafea4
--- /dev/null
+++ b/src/sub_commands/prs.rs
@@ -0,0 +1,113 @@
1
2
3use clap::Args;
4use dialoguer::{Select, Confirm, theme::ColorfulTheme};
5use nostr::{Event};
6
7use crate::{fetch_pull_push::fetch_pull_push, ngit_tag::{tag_extract_value, tag_is_branch_merged_from, tag_is_branch}};
8
9#[derive(Args)]
10pub struct PrsSubCommand {
11}
12
13pub fn prs(
14 _sub_command_args: &PrsSubCommand,
15) {
16
17 let branch_refs = fetch_pull_push(
18 None,
19 false,
20 false,
21 None,
22 false,
23 None,
24 None,
25 );
26 fn pull_request_merged(pull_request: &Event, merges: &Vec<Event>) -> bool {
27 let branch_from = tag_extract_value(
28 pull_request.tags.iter().find(|tag|tag_is_branch_merged_from(tag))
29 .expect("pull_request will always have branch merge from tag")
30 );
31 let branch_to = tag_extract_value(
32 pull_request.tags.iter().find(|tag|tag_is_branch(tag))
33 .expect("pull_request will always have branch tag")
34 );
35
36 merges.iter().any(|m|
37 tag_extract_value(
38 m.tags.iter().find(|tag|tag_is_branch_merged_from(tag))
39 .expect("merge will always have branch merge from tag")
40 ) == branch_from.clone()
41 // && tag_extract_value(
42 // m.tags.iter().find(|tag|tag_is_branch(tag))
43 // .expect("merge will always have branch merge from tag")
44 // ) == branch_to.clone()
45 )
46 }
47 // list PRs against branches that have not been merged
48 let outstanding_prs: Vec<&Event> = branch_refs.pull_requests.iter().filter(|pr|
49 !pull_request_merged(pr, &branch_refs.merges)
50 ).collect();
51
52 if outstanding_prs.is_empty() {
53 return println!("There are no open pull requests for this repository on selected relays");
54 }
55
56 fn extract_summary(pr:&Event) -> String {
57 let split_string:Vec<String> = pr.content.split("\n").map(|s| s.to_string()).collect();
58 split_string.get(1)
59 .expect("PR content will always have a second line which is the title")
60 .clone()
61 }
62 let summaries:Vec<String> = outstanding_prs.iter().map(|pr|extract_summary(pr)).collect();
63
64 loop {
65
66 // select pr to review
67 let i = Select::new()
68 .with_prompt("Select PR to review")
69 .items(&summaries)
70 .report(false)
71 .interact()
72 .unwrap();
73
74 // display summary
75 println!(
76 "{}\n raised: {}",
77 outstanding_prs[i].content,
78 outstanding_prs[i].created_at, //.to_human_datetime(), in v0.22
79 );
80
81 let branch_id = tag_extract_value(
82 outstanding_prs[i].tags.iter()
83 .find(|tag|tag_is_branch_merged_from(tag))
84 .expect("pr will always have a branch merged from tag")
85 );
86
87 let _branch_title = branch_refs
88 .branch_as_repo(Some(&branch_id)).name;
89 // pull branch
90 if Confirm::with_theme(&ColorfulTheme::default())
91 .with_prompt(
92 format!(
93 "pull branch for '{}'?",
94 extract_summary(outstanding_prs[i])
95 )
96 )
97 .default(true)
98 .interact()
99 .unwrap()
100 {
101 fetch_pull_push(
102 None,
103 true,
104 false,
105 Some(branch_id),
106 false,
107 None,
108 None,
109 );
110 break
111 }
112 }
113}