diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
| commit | 57321aa9136293b24757a6695a5c92087af539ab (patch) | |
| tree | 2867d02430e346b13f7abcbb1022113d4fac31fa /src | |
| parent | 82f1ba360b60c8339c7caea0dec7903ee5e764cd (diff) | |
feat(prs-create) send to repo relays
fetch repository reference events to identify repository relays
send pr events to repository relays alongside user relays
Diffstat (limited to 'src')
| -rw-r--r-- | src/repo_ref.rs | 105 | ||||
| -rw-r--r-- | src/sub_commands/prs/create.rs | 23 |
2 files changed, 116 insertions, 12 deletions
diff --git a/src/repo_ref.rs b/src/repo_ref.rs index 3b0b1b4..a92b5b3 100644 --- a/src/repo_ref.rs +++ b/src/repo_ref.rs | |||
| @@ -1,6 +1,12 @@ | |||
| 1 | use anyhow::{Context, Result}; | 1 | use anyhow::{bail, Context, Result}; |
| 2 | use nostr::Tag; | 2 | use nostr::Tag; |
| 3 | 3 | ||
| 4 | #[cfg(not(test))] | ||
| 5 | use crate::client::Client; | ||
| 6 | use crate::client::Connect; | ||
| 7 | #[cfg(test)] | ||
| 8 | use crate::client::MockConnect; | ||
| 9 | |||
| 4 | #[derive(Default)] | 10 | #[derive(Default)] |
| 5 | pub struct RepoRef { | 11 | pub struct RepoRef { |
| 6 | pub name: String, | 12 | pub name: String, |
| @@ -12,10 +18,43 @@ pub struct RepoRef { | |||
| 12 | // code languages and hashtags | 18 | // code languages and hashtags |
| 13 | } | 19 | } |
| 14 | 20 | ||
| 21 | impl TryFrom<nostr::Event> for RepoRef { | ||
| 22 | type Error = anyhow::Error; | ||
| 23 | |||
| 24 | fn try_from(event: nostr::Event) -> Result<Self> { | ||
| 25 | if !event.kind.as_u64().eq(&REPO_REF_KIND) { | ||
| 26 | bail!("incorrect kind"); | ||
| 27 | } | ||
| 28 | let mut r = Self::default(); | ||
| 29 | |||
| 30 | if let Some(t) = event.tags.iter().find(|t| t.as_vec()[0].eq("name")) { | ||
| 31 | r.name = t.as_vec()[1].clone(); | ||
| 32 | } | ||
| 33 | |||
| 34 | if let Some(t) = event.tags.iter().find(|t| t.as_vec()[0].eq("description")) { | ||
| 35 | r.description = t.as_vec()[1].clone(); | ||
| 36 | } | ||
| 37 | |||
| 38 | if let Some(t) = event.tags.iter().find(|t| t.as_vec()[0].eq("d")) { | ||
| 39 | r.root_commit = t.as_vec()[1].clone(); | ||
| 40 | } | ||
| 41 | |||
| 42 | r.relays = event | ||
| 43 | .tags | ||
| 44 | .iter() | ||
| 45 | .filter(|t| t.as_vec()[0].eq("relay")) | ||
| 46 | .map(|t| t.as_vec()[1].clone()) | ||
| 47 | .collect(); | ||
| 48 | |||
| 49 | Ok(r) | ||
| 50 | } | ||
| 51 | } | ||
| 52 | static REPO_REF_KIND: u64 = 300_317; | ||
| 53 | |||
| 15 | impl RepoRef { | 54 | impl RepoRef { |
| 16 | pub fn to_event(&self, keys: &nostr::Keys) -> Result<nostr::Event> { | 55 | pub fn to_event(&self, keys: &nostr::Keys) -> Result<nostr::Event> { |
| 17 | nostr_sdk::EventBuilder::new( | 56 | nostr_sdk::EventBuilder::new( |
| 18 | nostr::event::Kind::Custom(30017), | 57 | nostr::event::Kind::Custom(REPO_REF_KIND), |
| 19 | "", | 58 | "", |
| 20 | &[ | 59 | &[ |
| 21 | vec![ | 60 | vec![ |
| @@ -36,6 +75,36 @@ impl RepoRef { | |||
| 36 | } | 75 | } |
| 37 | } | 76 | } |
| 38 | 77 | ||
| 78 | pub async fn fetch( | ||
| 79 | root_commit: String, | ||
| 80 | #[cfg(test)] client: &MockConnect, | ||
| 81 | #[cfg(not(test))] client: &Client, | ||
| 82 | // TODO: more rubust way of finding repo events | ||
| 83 | relays: Vec<String>, | ||
| 84 | ) -> Result<RepoRef> { | ||
| 85 | // TODO: fetch relay information from file | ||
| 86 | |||
| 87 | let events: Vec<nostr::Event> = client | ||
| 88 | .get_events( | ||
| 89 | relays, | ||
| 90 | vec![ | ||
| 91 | nostr::Filter::default() | ||
| 92 | .kind(nostr::Kind::Custom(REPO_REF_KIND)) | ||
| 93 | .identifier(root_commit), | ||
| 94 | ], | ||
| 95 | ) | ||
| 96 | .await?; | ||
| 97 | |||
| 98 | RepoRef::try_from( | ||
| 99 | events | ||
| 100 | .iter() | ||
| 101 | .filter(|e| e.kind.as_u64() == REPO_REF_KIND) | ||
| 102 | .max_by_key(|e| e.created_at) | ||
| 103 | .context("cannot find repository reference event")? | ||
| 104 | .clone(), | ||
| 105 | ) | ||
| 106 | } | ||
| 107 | |||
| 39 | #[cfg(test)] | 108 | #[cfg(test)] |
| 40 | mod tests { | 109 | mod tests { |
| 41 | use test_utils::*; | 110 | use test_utils::*; |
| @@ -52,6 +121,38 @@ mod tests { | |||
| 52 | .to_event(&TEST_KEY_1_KEYS) | 121 | .to_event(&TEST_KEY_1_KEYS) |
| 53 | .unwrap() | 122 | .unwrap() |
| 54 | } | 123 | } |
| 124 | mod try_from { | ||
| 125 | use super::*; | ||
| 126 | |||
| 127 | #[test] | ||
| 128 | fn name() { | ||
| 129 | assert_eq!(RepoRef::try_from(create()).unwrap().name, "test name",) | ||
| 130 | } | ||
| 131 | |||
| 132 | #[test] | ||
| 133 | fn description() { | ||
| 134 | assert_eq!( | ||
| 135 | RepoRef::try_from(create()).unwrap().description, | ||
| 136 | "test description", | ||
| 137 | ) | ||
| 138 | } | ||
| 139 | |||
| 140 | #[test] | ||
| 141 | fn root_commit() { | ||
| 142 | assert_eq!( | ||
| 143 | RepoRef::try_from(create()).unwrap().root_commit, | ||
| 144 | "23471389461", | ||
| 145 | ) | ||
| 146 | } | ||
| 147 | |||
| 148 | #[test] | ||
| 149 | fn relays() { | ||
| 150 | assert_eq!( | ||
| 151 | RepoRef::try_from(create()).unwrap().relays, | ||
| 152 | vec!["ws://relay1.io".to_string(), "ws://relay2.io".to_string()], | ||
| 153 | ) | ||
| 154 | } | ||
| 155 | } | ||
| 55 | 156 | ||
| 56 | mod to_event { | 157 | mod to_event { |
| 57 | use super::*; | 158 | use super::*; |
diff --git a/src/sub_commands/prs/create.rs b/src/sub_commands/prs/create.rs index d82f53e..e85611f 100644 --- a/src/sub_commands/prs/create.rs +++ b/src/sub_commands/prs/create.rs | |||
| @@ -13,7 +13,7 @@ use crate::{ | |||
| 13 | cli_interactor::{Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms}, | 13 | cli_interactor::{Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms}, |
| 14 | client::Connect, | 14 | client::Connect, |
| 15 | git::{Repo, RepoActions}, | 15 | git::{Repo, RepoActions}, |
| 16 | login, Cli, | 16 | login, repo_ref, Cli, |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | #[derive(Debug, clap::Args)] | 19 | #[derive(Debug, clap::Args)] |
| @@ -99,11 +99,16 @@ pub async fn launch( | |||
| 99 | let events = | 99 | let events = |
| 100 | generate_pr_and_patch_events(&title, &description, &to_branch, &git_repo, &ahead, &keys)?; | 100 | generate_pr_and_patch_events(&title, &description, &to_branch, &git_repo, &ahead, &keys)?; |
| 101 | 101 | ||
| 102 | // TODO: get relays from repo event | 102 | let repo_ref = repo_ref::fetch( |
| 103 | let repo_read_relays: Vec<String> = vec![ | 103 | git_repo |
| 104 | "ws://localhost:8055".to_string(), | 104 | .get_root_commit(&to_branch) |
| 105 | "ws://localhost:8056".to_string(), | 105 | .context("failed to get root commit of the repository")? |
| 106 | ]; | 106 | .to_string(), |
| 107 | &client, | ||
| 108 | // TODO: get relay list from local yaml file | ||
| 109 | user_ref.relays.write(), | ||
| 110 | ) | ||
| 111 | .await?; | ||
| 107 | 112 | ||
| 108 | println!( | 113 | println!( |
| 109 | "posting 1 pull request with {} commits...", | 114 | "posting 1 pull request with {} commits...", |
| @@ -114,12 +119,11 @@ pub async fn launch( | |||
| 114 | &client, | 119 | &client, |
| 115 | events, | 120 | events, |
| 116 | user_ref.relays.write(), | 121 | user_ref.relays.write(), |
| 117 | repo_read_relays, | 122 | repo_ref.relays.clone(), |
| 118 | !cli_args.disable_cli_spinners, | 123 | !cli_args.disable_cli_spinners, |
| 119 | ) | 124 | ) |
| 120 | .await?; | 125 | .await?; |
| 121 | // TODO check if there is already a similarly named PR | 126 | // TODO check if there is already a similarly named |
| 122 | |||
| 123 | Ok(()) | 127 | Ok(()) |
| 124 | } | 128 | } |
| 125 | 129 | ||
| @@ -212,7 +216,6 @@ pub async fn send_events( | |||
| 212 | } | 216 | } |
| 213 | })) | 217 | })) |
| 214 | .await; | 218 | .await; |
| 215 | client.disconnect().await?; | ||
| 216 | Ok(()) | 219 | Ok(()) |
| 217 | } | 220 | } |
| 218 | 221 | ||