diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-14 16:44:56 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-14 16:44:56 +0000 |
| commit | 115eca3d69310a58e8357fe091183d0a8e723967 (patch) | |
| tree | 51860d62bdcde1d466d99134a41b767ac24eff52 /src/repo_ref.rs | |
| parent | a1d67c50c8ebc5395b069e30b60d66e0c7de5a5a (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/repo_ref.rs')
| -rw-r--r-- | src/repo_ref.rs | 65 |
1 files changed, 50 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 @@ | |||
| 1 | use std::{fs::File, io::BufReader, str::FromStr}; | 1 | use std::{fs::File, io::BufReader, str::FromStr}; |
| 2 | 2 | ||
| 3 | use anyhow::{bail, Context, Result}; | 3 | use anyhow::{bail, Context, Result}; |
| 4 | use nostr::{secp256k1::XOnlyPublicKey, FromBech32, Tag, ToBech32}; | 4 | use nostr::{nips::nip19::Nip19, secp256k1::XOnlyPublicKey, FromBech32, Tag, ToBech32}; |
| 5 | use serde::{Deserialize, Serialize}; | 5 | use 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)] |
| 10 | use crate::client::MockConnect; | 10 | use crate::client::MockConnect; |
| 11 | use crate::{ | 11 | use 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)] |