diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:14:47 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:14:47 +0000 |
| commit | 0067804cc00e94ce2b7043e67f9ff50968525479 (patch) | |
| tree | 2accdc6d4e9b73df4f20499238ec24f24a52a1b8 /src/funcs/create_branch_and_pr.rs | |
| parent | 5c5feaa732363e32e2a980a887fa42b4394b1a5e (diff) | |
v0.0.1-alpha funcs
Diffstat (limited to 'src/funcs/create_branch_and_pr.rs')
| -rw-r--r-- | src/funcs/create_branch_and_pr.rs | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/src/funcs/create_branch_and_pr.rs b/src/funcs/create_branch_and_pr.rs new file mode 100644 index 0000000..f67dfb3 --- /dev/null +++ b/src/funcs/create_branch_and_pr.rs | |||
| @@ -0,0 +1,212 @@ | |||
| 1 | use std::{path::PathBuf}; | ||
| 2 | |||
| 3 | use dialoguer::{Input, theme::ColorfulTheme, Confirm}; | ||
| 4 | use nostr::{Keys, prelude::{Nip19Event, ToBech32}}; | ||
| 5 | use nostr_sdk::blocking::Client; | ||
| 6 | |||
| 7 | use crate::{repos::{repo::Repo, init::InitializeRepo}, branch_refs::BranchRefs, cli_helpers::multi_select_with_add, groups::{init::{InitializeGroup}, group::{Group}}, config::{MyConfig, save_conifg}, repo_config::RepoConfig, pull_request::initialize_pull_request}; | ||
| 8 | |||
| 9 | struct PullRequest { | ||
| 10 | pub title: String, | ||
| 11 | pub description: String, | ||
| 12 | pub tags: Vec<String> | ||
| 13 | } | ||
| 14 | /// returns branch_id | ||
| 15 | pub fn create_branch_and_pr( | ||
| 16 | local_branch_name: &String, | ||
| 17 | commits_to_push: usize, | ||
| 18 | repo_dir_path: &PathBuf, | ||
| 19 | repo: &Repo, | ||
| 20 | branch_refs: &mut BranchRefs, | ||
| 21 | keys: &Keys, | ||
| 22 | cfg: &mut MyConfig, | ||
| 23 | client: &Client, | ||
| 24 | ) -> String { | ||
| 25 | let new_branch_name: String = Input::with_theme(&ColorfulTheme::default()) | ||
| 26 | .with_prompt(format!( | ||
| 27 | "push {} commits to a branch named", | ||
| 28 | commits_to_push, | ||
| 29 | )) | ||
| 30 | .with_initial_text(&local_branch_name.to_string()) | ||
| 31 | .interact_text() | ||
| 32 | .unwrap(); | ||
| 33 | let pr_details = match Confirm::with_theme(&ColorfulTheme::default()) | ||
| 34 | .with_prompt("open a pull request?") | ||
| 35 | .default(true) | ||
| 36 | .interact() | ||
| 37 | .unwrap() { | ||
| 38 | false => None, | ||
| 39 | true => { | ||
| 40 | let title = Input::with_theme(&ColorfulTheme::default()) | ||
| 41 | .with_prompt("title") | ||
| 42 | .with_initial_text(&new_branch_name) | ||
| 43 | .interact_text() | ||
| 44 | .unwrap(); | ||
| 45 | let tags = multi_select_with_add( | ||
| 46 | vec![ | ||
| 47 | "bugfix".to_string(), | ||
| 48 | "feature".to_string(), | ||
| 49 | ], | ||
| 50 | vec![ | ||
| 51 | false, | ||
| 52 | false, | ||
| 53 | ], | ||
| 54 | "tags", | ||
| 55 | "new tag", | ||
| 56 | ); | ||
| 57 | let description = Input::with_theme(&ColorfulTheme::default()) | ||
| 58 | .with_prompt("description") | ||
| 59 | .interact_text() | ||
| 60 | .unwrap(); | ||
| 61 | // into main / master (lookup (yes/no) - if no select from existing branches with mapping | ||
| 62 | Some( | ||
| 63 | PullRequest { | ||
| 64 | title, | ||
| 65 | tags, | ||
| 66 | description, | ||
| 67 | } | ||
| 68 | ) | ||
| 69 | }, | ||
| 70 | }; | ||
| 71 | // create it now immediately before pushing the patches | ||
| 72 | let mut events_to_broadcast = vec![]; | ||
| 73 | |||
| 74 | // create/store admin group | ||
| 75 | let admin_group = match &cfg.default_admin_group_event_serialized { | ||
| 76 | None => { | ||
| 77 | let new_admin_group = Group::new( | ||
| 78 | &InitializeGroup::new() | ||
| 79 | .members( | ||
| 80 | vec![ | ||
| 81 | keys.public_key().to_string(), | ||
| 82 | ], | ||
| 83 | vec![], | ||
| 84 | ) | ||
| 85 | .relays(&repo.relays), | ||
| 86 | &keys, | ||
| 87 | ).unwrap(); | ||
| 88 | cfg.default_admin_group_event_serialized = Some(new_admin_group.events[0].as_json()); | ||
| 89 | save_conifg(&cfg); | ||
| 90 | new_admin_group | ||
| 91 | }, | ||
| 92 | Some(admin) => Group::new_from_json_event(admin.clone()) | ||
| 93 | .expect("admin group event in MyConfig loads into Group"), | ||
| 94 | }; | ||
| 95 | events_to_broadcast.push(admin_group.events[0].clone()); | ||
| 96 | branch_refs.update(admin_group.events[0].clone()); | ||
| 97 | |||
| 98 | |||
| 99 | // create group | ||
| 100 | let branch_group_ref = match branch_refs.is_authorized( | ||
| 101 | None, | ||
| 102 | &keys.public_key(), | ||
| 103 | ) | ||
| 104 | .expect("main repo maintainers group is cached in .ngit") | ||
| 105 | { | ||
| 106 | // use repo maintainers group | ||
| 107 | true => branch_refs.maintainers_group(None) | ||
| 108 | .expect("main repo maintainers group is cached in .ngit") | ||
| 109 | .get_ref(), | ||
| 110 | // create branch group | ||
| 111 | false => { | ||
| 112 | let new_group = Group::new( | ||
| 113 | &InitializeGroup::new() | ||
| 114 | .name( | ||
| 115 | format!( | ||
| 116 | "branch-of:{},named:{}", | ||
| 117 | match &repo.name { | ||
| 118 | None => "untitled", | ||
| 119 | Some(s) => s.as_str(), | ||
| 120 | }, | ||
| 121 | &new_branch_name, | ||
| 122 | ), | ||
| 123 | ) | ||
| 124 | .admin(admin_group.get_ref()) | ||
| 125 | .members( | ||
| 126 | vec![keys.public_key().to_string()], | ||
| 127 | vec![ | ||
| 128 | branch_refs.maintainers_group(None) | ||
| 129 | .expect("repo maintainers group to exist in .ngit directory") | ||
| 130 | .get_ref() | ||
| 131 | ] | ||
| 132 | ) | ||
| 133 | .relays(&repo.relays) | ||
| 134 | , | ||
| 135 | &keys, | ||
| 136 | ) | ||
| 137 | .expect("new branch group to be created"); | ||
| 138 | |||
| 139 | events_to_broadcast.push(new_group.events[0].clone()); | ||
| 140 | branch_refs.update(new_group.events[0].clone()); | ||
| 141 | new_group.get_ref() | ||
| 142 | } | ||
| 143 | }; | ||
| 144 | |||
| 145 | // create branch | ||
| 146 | let branch_init = InitializeRepo::new() | ||
| 147 | .name(&new_branch_name) | ||
| 148 | .relays(&repo.relays) | ||
| 149 | .root_repo(repo.id.to_string()) | ||
| 150 | .maintainers_group(branch_group_ref) | ||
| 151 | .initialize(&keys); | ||
| 152 | events_to_broadcast.push(branch_init.clone()); | ||
| 153 | branch_refs.update(branch_init.clone()); | ||
| 154 | |||
| 155 | // TODO: create PR | ||
| 156 | match pr_details { | ||
| 157 | None => (), | ||
| 158 | Some(pr_details) => { | ||
| 159 | let pull_request_init = initialize_pull_request( | ||
| 160 | &keys, | ||
| 161 | &repo.id.to_string(), | ||
| 162 | &repo.id.to_string(), | ||
| 163 | &branch_init.id.to_string(), | ||
| 164 | &pr_details.title, | ||
| 165 | &pr_details.description, | ||
| 166 | pr_details.tags | ||
| 167 | ); | ||
| 168 | events_to_broadcast.push(pull_request_init.clone()); | ||
| 169 | branch_refs.update(pull_request_init.clone()); | ||
| 170 | println!( | ||
| 171 | "pull request '{}' created with id: {}", | ||
| 172 | &pr_details.title, | ||
| 173 | Nip19Event::new( | ||
| 174 | pull_request_init.id.clone(), | ||
| 175 | vec![&repo.relays[0]], | ||
| 176 | ) | ||
| 177 | .to_bech32() | ||
| 178 | .expect("Nip19Event to convert to to_bech32") | ||
| 179 | ); | ||
| 180 | }, | ||
| 181 | } | ||
| 182 | |||
| 183 | // add mapping to conifg.json | ||
| 184 | RepoConfig::open(repo_dir_path).set_mapping( | ||
| 185 | local_branch_name, | ||
| 186 | &branch_init.id.to_string(), | ||
| 187 | ); | ||
| 188 | |||
| 189 | println!( | ||
| 190 | "branch '{}' created with id: {}", | ||
| 191 | &new_branch_name, | ||
| 192 | Nip19Event::new( | ||
| 193 | branch_init.id.clone(), | ||
| 194 | vec![&repo.relays[0]], | ||
| 195 | ) | ||
| 196 | .to_bech32() | ||
| 197 | .expect("Nip19Event to convert to to_bech32") | ||
| 198 | ); | ||
| 199 | |||
| 200 | // broadcast events | ||
| 201 | for e in &events_to_broadcast { | ||
| 202 | match client.send_event(e.clone()) { | ||
| 203 | Ok(_) => (), | ||
| 204 | // TODO: this isn't working - if a relay is specified with a type it will wait 30ish secs and then return successful | ||
| 205 | Err(e) => { println!("error broadcasting event: {}",e); }, | ||
| 206 | } | ||
| 207 | // TODO: better error handling here / reporting. potentially warn if taking a while and report on troublesome relays | ||
| 208 | } | ||
| 209 | |||
| 210 | // return branch_id | ||
| 211 | branch_init.id.to_string() | ||
| 212 | } | ||