upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/funcs/create_branch_and_pr.rs
blob: f67dfb39f3822db0151991d710e3745c14d98276 (plain)
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
use std::{path::PathBuf};

use dialoguer::{Input, theme::ColorfulTheme, Confirm};
use nostr::{Keys, prelude::{Nip19Event, ToBech32}};
use nostr_sdk::blocking::Client;

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};

struct PullRequest {
    pub title: String,
    pub description: String,
    pub tags: Vec<String>
}
/// returns branch_id
pub fn create_branch_and_pr(
    local_branch_name: &String,
    commits_to_push: usize,
    repo_dir_path: &PathBuf,
    repo: &Repo,
    branch_refs: &mut BranchRefs,
    keys: &Keys,
    cfg: &mut MyConfig,
    client: &Client,
) -> String {
    let new_branch_name: String = Input::with_theme(&ColorfulTheme::default())
        .with_prompt(format!(
            "push {} commits to a branch named",
            commits_to_push,
        ))
        .with_initial_text(&local_branch_name.to_string())
        .interact_text()
        .unwrap();
    let pr_details = match Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("open a pull request?")
        .default(true)
        .interact()
        .unwrap() {
            false => None,
            true => {
                let title = Input::with_theme(&ColorfulTheme::default())
                    .with_prompt("title")
                    .with_initial_text(&new_branch_name)
                    .interact_text()
                    .unwrap();
                let tags = multi_select_with_add(
                    vec![
                        "bugfix".to_string(),
                        "feature".to_string(),
                    ],
                    vec![
                        false,
                        false,
                    ],
                    "tags",
                    "new tag",
                );
                let description = Input::with_theme(&ColorfulTheme::default())
                    .with_prompt("description")
                    .interact_text()
                    .unwrap();
                // into main / master (lookup (yes/no) - if no select from existing branches with mapping
                Some(
                    PullRequest {
                        title,
                        tags,
                        description,
                    }
                )
            },
    };
    // create it now immediately before pushing the patches
    let mut events_to_broadcast = vec![];
    
    // create/store admin group
    let admin_group = match &cfg.default_admin_group_event_serialized {
        None => {
            let new_admin_group = Group::new(
                &InitializeGroup::new()
                    .members(
                        vec![
                            keys.public_key().to_string(),
                        ],
                        vec![],
                    )
                    .relays(&repo.relays),
                &keys,
            ).unwrap();
            cfg.default_admin_group_event_serialized = Some(new_admin_group.events[0].as_json());
            save_conifg(&cfg);
            new_admin_group
        },
        Some(admin) => Group::new_from_json_event(admin.clone())
            .expect("admin group event in MyConfig loads into Group"),
    };
    events_to_broadcast.push(admin_group.events[0].clone());
    branch_refs.update(admin_group.events[0].clone());


    // create group
    let branch_group_ref = match branch_refs.is_authorized(
        None,
        &keys.public_key(),
    )
        .expect("main repo maintainers group is cached in .ngit")
    {
        // use repo maintainers group
        true => branch_refs.maintainers_group(None)
            .expect("main repo maintainers group is cached in .ngit")
            .get_ref(),
        // create branch group
        false => {
            let new_group = Group::new(
                &InitializeGroup::new()
                    .name(
                        format!(
                            "branch-of:{},named:{}",
                            match &repo.name {
                                None => "untitled",
                                Some(s) => s.as_str(),
                            },
                            &new_branch_name,
                        ),
                    )
                    .admin(admin_group.get_ref())
                    .members(
                        vec![keys.public_key().to_string()],
                        vec![
                            branch_refs.maintainers_group(None)
                                .expect("repo maintainers group to exist in .ngit directory")
                                .get_ref()
                        ]
                    )
                    .relays(&repo.relays)
                    ,
                &keys,
            )
                .expect("new branch group to be created");

            events_to_broadcast.push(new_group.events[0].clone());
            branch_refs.update(new_group.events[0].clone());
            new_group.get_ref()
        }
     };

    // create branch
    let branch_init = InitializeRepo::new()
        .name(&new_branch_name)
        .relays(&repo.relays)
        .root_repo(repo.id.to_string())
        .maintainers_group(branch_group_ref)
        .initialize(&keys);
    events_to_broadcast.push(branch_init.clone());
    branch_refs.update(branch_init.clone());

     // TODO: create PR
     match pr_details {
        None => (),
        Some(pr_details) => {
            let pull_request_init = initialize_pull_request(
                &keys, 
                &repo.id.to_string(),
                &repo.id.to_string(),
                &branch_init.id.to_string(),
                &pr_details.title,
                &pr_details.description,
                pr_details.tags
            );
            events_to_broadcast.push(pull_request_init.clone());
            branch_refs.update(pull_request_init.clone());
            println!(
                "pull request '{}' created with id: {}",
                &pr_details.title,
                Nip19Event::new(
                    pull_request_init.id.clone(),
                    vec![&repo.relays[0]],
                )
                    .to_bech32()
                    .expect("Nip19Event to convert to to_bech32")
            );
        },
     }

    // add mapping to conifg.json
    RepoConfig::open(repo_dir_path).set_mapping(
        local_branch_name,
        &branch_init.id.to_string(),
    );

    println!(
        "branch '{}' created with id: {}",
        &new_branch_name,
        Nip19Event::new(
            branch_init.id.clone(),
            vec![&repo.relays[0]],
        )
            .to_bech32()
            .expect("Nip19Event to convert to to_bech32")         
    );

    // broadcast events
    for e in &events_to_broadcast { 
        match client.send_event(e.clone()) {
            Ok(_) => (),
            // TODO: this isn't working - if a relay is specified with a type it will wait 30ish secs and then return successful
            Err(e) => { println!("error broadcasting event: {}",e); },
        }
        // TODO: better error handling here / reporting. potentially warn if taking a while and report on troublesome relays
    }

    // return branch_id
    branch_init.id.to_string()
}