upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/sub_commands/list.rs
blob: 4764adcbba25d85379fc2778d027d4ed972c5ee7 (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
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
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::{Repo, RepoActions},
    repo_ref::{self, RepoRef, REPO_REF_KIND},
    sub_commands::send::{event_is_cover_letter, event_to_cover_letter, PATCH_KIND},
    Cli,
};

#[derive(Debug, clap::Args)]
pub struct SubCommandArgs {
    /// TODO ignore merged, and closed
    #[arg(long, action)]
    open_only: bool,
}

#[allow(clippy::too_many_lines)]
pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> 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(),
    )
    .await?;

    println!("finding PRs...");

    let pr_events: Vec<nostr::Event> =
        find_pr_events(&client, &repo_ref, &root_commit.to_string()).await?;

    if pr_events.is_empty() {
        println!("no PRs found... create one? try `ngit send`");
        return Ok(());
    }

    let selected_index = Interactor::default().choice(
        PromptChoiceParms::default()
            .with_prompt("All PRs")
            .with_choices(
                pr_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(),
            ),
    )?;

    println!("finding commits...");

    let commits_events: Vec<nostr::Event> =
        find_commits_for_pr_event(&client, &pr_events[selected_index], &repo_ref).await?;

    confirm_checkout(&git_repo)?;

    let most_recent_pr_patch_chain = get_most_recent_patch_with_ancestors(commits_events)
        .context("cannot get most recent patch for PR")?;

    let branch_name: String = event_to_cover_letter(&pr_events[selected_index])
        .context("cannot assign a branch name as event is not a patch set root")?
        .branch_name;

    let applied = git_repo
        .apply_patch_chain(&branch_name, most_recent_pr_patch_chain)
        .context("cannot apply patch chain")?;

    if applied.is_empty() {
        println!("checked out PR branch. no new commits to pull");
    } else {
        println!(
            "checked out PR branch. pulled {} new commits",
            applied.len(),
        );
    }
    Ok(())
}

fn confirm_checkout(git_repo: &Repo) -> Result<()> {
    if !Interactor::default().confirm(
        PromptConfirmParms::default()
            .with_prompt("check out branch?")
            .with_default(true),
    )? {
        bail!("Exiting...");
    }

    if git_repo.has_outstanding_changes()? {
        bail!(
            "cannot pull PR 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 first_patch = patches.first().context("no patches found")?;

    let patches_with_youngest_created_at: Vec<&nostr::Event> = patches
        .iter()
        .filter(|p| p.created_at.eq(&first_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());
        event_id_to_search = get_event_parent_id(event).unwrap_or_default();
    }
    Ok(res)
}

pub async fn find_pr_events(
    #[cfg(test)] client: &crate::client::MockConnect,
    #[cfg(not(test))] client: &Client,
    repo_ref: &RepoRef,
    root_commit: &str,
) -> Result<Vec<nostr::Event>> {
    Ok(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 prs 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 pr 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>>())
}

pub async fn find_commits_for_pr_event(
    #[cfg(test)] client: &crate::client::MockConnect,
    #[cfg(not(test))] client: &Client,
    pr_event: &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))
                    // this requires every patch to reference the root event
                    // this will not pick up v2,v3 patch sets
                    // TODO: fetch commits for v2.. patch sets
                    .event(pr_event.id),
            ],
        )
        .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 && t.as_vec()[1].eq(&pr_event.id.to_string()))
        })
        .map(std::borrow::ToOwned::to_owned)
        .collect();

    if !event_is_cover_letter(pr_event) {
        patch_events.push(pr_event.clone());
    }
    Ok(patch_events)
}