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>2024-02-14 16:44:56 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-02-14 16:44:56 +0000
commit115eca3d69310a58e8357fe091183d0a8e723967 (patch)
tree51860d62bdcde1d466d99134a41b767ac24eff52 /src
parenta1d67c50c8ebc5395b069e30b60d66e0c7de5a5a (diff)
feat: find repo event by nevent or naddr
if repo event cannot be found using unique commit the user can find it via a nevent or naddr also handle no PRs found
Diffstat (limited to 'src')
-rw-r--r--src/repo_ref.rs65
-rw-r--r--src/sub_commands/list.rs5
2 files changed, 55 insertions, 15 deletions
diff --git a/src/repo_ref.rs b/src/repo_ref.rs
index 87c5b75..38ea470 100644
--- a/src/repo_ref.rs
+++ b/src/repo_ref.rs
@@ -1,7 +1,7 @@
1use std::{fs::File, io::BufReader, str::FromStr}; 1use std::{fs::File, io::BufReader, str::FromStr};
2 2
3use anyhow::{bail, Context, Result}; 3use anyhow::{bail, Context, Result};
4use nostr::{secp256k1::XOnlyPublicKey, FromBech32, Tag, ToBech32}; 4use nostr::{nips::nip19::Nip19, secp256k1::XOnlyPublicKey, FromBech32, Tag, ToBech32};
5use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
6 6
7#[cfg(not(test))] 7#[cfg(not(test))]
@@ -9,6 +9,7 @@ use crate::client::Client;
9#[cfg(test)] 9#[cfg(test)]
10use crate::client::MockConnect; 10use crate::client::MockConnect;
11use crate::{ 11use crate::{
12 cli_interactor::{Interactor, InteractorPrompt, PromptInputParms},
12 client::Connect, 13 client::Connect,
13 git::{Repo, RepoActions}, 14 git::{Repo, RepoActions},
14}; 15};
@@ -168,25 +169,59 @@ pub async fn fetch(
168 relays = repo_config.relays.clone(); 169 relays = repo_config.relays.clone();
169 } 170 }
170 171
171 let events: Vec<nostr::Event> = client.get_events(relays, vec![repo_event_filter]).await?; 172 let event = loop {
173 let events: Vec<nostr::Event> = client
174 .get_events(relays.clone(), vec![repo_event_filter.clone()])
175 .await?;
172 176
173 // TODO: fallback to ask user for nevent - to capture 177 // TODO: if maintainers.yaml isn't present, as the user to select from the
178 // pubkeys they want to use. could use WoT as an indicator as well as the repo
179 // and user name.
174 180
175 // TODO: if maintainers.yaml isn't present, as the user to select from the 181 // TODO: if maintainers.yaml isn't present, save the selected repo pubkey
176 // pubkeys they want to use. could use WoT as an indicator as well as the repo 182 // somewhere within .git folder for future use and seek to get that next time
177 // and user name. 183 if let Some(event) = events
178
179 // TODO: if maintainers.yaml isn't present, save the selected repo pubkey
180 // somewhere within .git folder for future use and seek to get that next time
181
182 RepoRef::try_from(
183 events
184 .iter() 184 .iter()
185 .filter(|e| e.kind.as_u64() == REPO_REF_KIND) 185 .filter(|e| e.kind.as_u64() == REPO_REF_KIND)
186 .max_by_key(|e| e.created_at) 186 .max_by_key(|e| e.created_at)
187 .context("cannot find repository reference event")? 187 {
188 .clone(), 188 break event.clone();
189 ) 189 }
190 println!("cannot find repo event");
191 loop {
192 let bech32 = Interactor::default()
193 .input(PromptInputParms::default().with_prompt("repository naddr or nevent"))?;
194 if let Ok(nip19) = Nip19::from_bech32(bech32) {
195 repo_event_filter =
196 nostr::Filter::default().kind(nostr::Kind::Custom(REPO_REF_KIND));
197 match nip19 {
198 Nip19::Coordinate(c) => {
199 repo_event_filter =
200 repo_event_filter.identifier(c.identifier).author(c.pubkey);
201 for r in c.relays {
202 relays.push(r);
203 }
204 }
205 Nip19::Event(n) => {
206 if let Some(author) = n.author {
207 repo_event_filter = repo_event_filter.id(n.event_id).author(author);
208 }
209 for r in n.relays {
210 relays.push(r);
211 }
212 }
213 Nip19::EventId(id) => repo_event_filter = repo_event_filter.id(id),
214 _ => (),
215 }
216 } else {
217 println!("not a valid nevent or naddr");
218 continue;
219 }
220 break;
221 }
222 };
223
224 RepoRef::try_from(event.clone()).context("cannot parse event as repo reference")
190} 225}
191 226
192#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)] 227#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)]
diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs
index 49cbf6d..47a8fdc 100644
--- a/src/sub_commands/list.rs
+++ b/src/sub_commands/list.rs
@@ -51,6 +51,11 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
51 let pr_events: Vec<nostr::Event> = 51 let pr_events: Vec<nostr::Event> =
52 find_pr_events(&client, &repo_ref, &root_commit.to_string()).await?; 52 find_pr_events(&client, &repo_ref, &root_commit.to_string()).await?;
53 53
54 if pr_events.is_empty() {
55 println!("no PRs found... create one? try `ngit send`");
56 return Ok(());
57 }
58
54 let selected_index = Interactor::default().choice( 59 let selected_index = Interactor::default().choice(
55 PromptChoiceParms::default() 60 PromptChoiceParms::default()
56 .with_prompt("All PRs") 61 .with_prompt("All PRs")