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 | c37ceaf1c3e4371f320798bf423719a9a6fe71fd (patch) | |
| tree | e202a3eccb05f7ca0734cb4130ce69c1076452f5 | |
| parent | 0753e0bcdd3d606f8f0226a3980bcd817117abaa (diff) | |
refactor(claim) add RepoRef struct
enable wider usage of repoistory reference details
| -rw-r--r-- | src/sub_commands/claim.rs | 49 | ||||
| -rw-r--r-- | src/sub_commands/mod.rs | 1 | ||||
| -rw-r--r-- | src/sub_commands/repo_ref.rs | 137 | ||||
| -rw-r--r-- | tests/claim.rs | 4 |
4 files changed, 151 insertions, 40 deletions
diff --git a/src/sub_commands/claim.rs b/src/sub_commands/claim.rs index 5eb66bb..cb6e99d 100644 --- a/src/sub_commands/claim.rs +++ b/src/sub_commands/claim.rs | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | use anyhow::{Context, Result}; | 1 | use anyhow::{Context, Result}; |
| 2 | use nostr::{EventBuilder, Tag}; | ||
| 3 | 2 | ||
| 4 | use super::prs::create::send_events; | 3 | use super::prs::create::send_events; |
| 5 | #[cfg(not(test))] | 4 | #[cfg(not(test))] |
| @@ -10,7 +9,9 @@ use crate::{ | |||
| 10 | cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, | 9 | cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, |
| 11 | client::Connect, | 10 | client::Connect, |
| 12 | git::{Repo, RepoActions}, | 11 | git::{Repo, RepoActions}, |
| 13 | login, Cli, | 12 | login, |
| 13 | sub_commands::repo_ref::RepoRef, | ||
| 14 | Cli, | ||
| 14 | }; | 15 | }; |
| 15 | 16 | ||
| 16 | #[derive(Debug, clap::Args)] | 17 | #[derive(Debug, clap::Args)] |
| @@ -66,15 +67,16 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 66 | 67 | ||
| 67 | println!("publishing repostory reference..."); | 68 | println!("publishing repostory reference..."); |
| 68 | 69 | ||
| 70 | let repo_event = RepoRef::default() | ||
| 71 | .set_name(name) | ||
| 72 | .set_description(description) | ||
| 73 | .set_root_commit(root_commit.to_string()) | ||
| 74 | .set_relays(repo_relays.clone()) | ||
| 75 | .to_event(&keys)?; | ||
| 76 | |||
| 69 | send_events( | 77 | send_events( |
| 70 | &client, | 78 | &client, |
| 71 | vec![generate_repo_event( | 79 | vec![repo_event], |
| 72 | &name, | ||
| 73 | &description, | ||
| 74 | &repo_relays, | ||
| 75 | &root_commit.to_string(), | ||
| 76 | &keys, | ||
| 77 | )?], | ||
| 78 | user_ref.relays.write(), | 80 | user_ref.relays.write(), |
| 79 | repo_relays, | 81 | repo_relays, |
| 80 | !cli_args.disable_cli_spinners, | 82 | !cli_args.disable_cli_spinners, |
| @@ -83,32 +85,3 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 83 | 85 | ||
| 84 | Ok(()) | 86 | Ok(()) |
| 85 | } | 87 | } |
| 86 | |||
| 87 | fn generate_repo_event( | ||
| 88 | name: &str, | ||
| 89 | description: &str, | ||
| 90 | relays: &[String], | ||
| 91 | // git_server: String, | ||
| 92 | root_commit: &String, | ||
| 93 | keys: &nostr::Keys, | ||
| 94 | ) -> Result<nostr::Event> { | ||
| 95 | EventBuilder::new( | ||
| 96 | nostr::event::Kind::Custom(30017), | ||
| 97 | "", | ||
| 98 | &[ | ||
| 99 | vec![ | ||
| 100 | Tag::Identifier(root_commit.to_string()), | ||
| 101 | Tag::Reference(format!("r-{root_commit}")), | ||
| 102 | Tag::Name(name.to_owned()), | ||
| 103 | Tag::Description(description.to_owned()), | ||
| 104 | ], | ||
| 105 | relays.iter().map(|r| Tag::Relay(r.into())).collect(), | ||
| 106 | // git_servers | ||
| 107 | // other maintainers | ||
| 108 | // code languages and hashtags | ||
| 109 | ] | ||
| 110 | .concat(), | ||
| 111 | ) | ||
| 112 | .to_event(keys) | ||
| 113 | .context("failed to create pr event") | ||
| 114 | } | ||
diff --git a/src/sub_commands/mod.rs b/src/sub_commands/mod.rs index 6e99ca5..9e9f602 100644 --- a/src/sub_commands/mod.rs +++ b/src/sub_commands/mod.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | pub mod claim; | 1 | pub mod claim; |
| 2 | pub mod login; | 2 | pub mod login; |
| 3 | pub mod prs; | 3 | pub mod prs; |
| 4 | pub mod repo_ref; | ||
diff --git a/src/sub_commands/repo_ref.rs b/src/sub_commands/repo_ref.rs new file mode 100644 index 0000000..82580df --- /dev/null +++ b/src/sub_commands/repo_ref.rs | |||
| @@ -0,0 +1,137 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use nostr::Tag; | ||
| 3 | |||
| 4 | #[derive(Default)] | ||
| 5 | pub struct RepoRef { | ||
| 6 | name: String, | ||
| 7 | description: String, | ||
| 8 | root_commit: String, | ||
| 9 | relays: Vec<String>, | ||
| 10 | // git_server: String, | ||
| 11 | // other maintainers | ||
| 12 | // code languages and hashtags | ||
| 13 | } | ||
| 14 | |||
| 15 | impl RepoRef { | ||
| 16 | pub fn set_name(&mut self, name: String) -> &mut Self { | ||
| 17 | self.name = name; | ||
| 18 | self | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn set_description(&mut self, description: String) -> &mut Self { | ||
| 22 | self.description = description; | ||
| 23 | self | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn set_root_commit(&mut self, root_commit: String) -> &mut Self { | ||
| 27 | self.root_commit = root_commit; | ||
| 28 | self | ||
| 29 | } | ||
| 30 | |||
| 31 | pub fn set_relays(&mut self, relays: Vec<String>) -> &mut Self { | ||
| 32 | self.relays = relays; | ||
| 33 | self | ||
| 34 | } | ||
| 35 | |||
| 36 | pub fn to_event(&self, keys: &nostr::Keys) -> Result<nostr::Event> { | ||
| 37 | nostr_sdk::EventBuilder::new( | ||
| 38 | nostr::event::Kind::Custom(30017), | ||
| 39 | "", | ||
| 40 | &[ | ||
| 41 | vec![ | ||
| 42 | Tag::Identifier(self.root_commit.to_string()), | ||
| 43 | Tag::Reference(format!("r-{}", self.root_commit)), | ||
| 44 | Tag::Name(self.name.clone()), | ||
| 45 | Tag::Description(self.description.clone()), | ||
| 46 | ], | ||
| 47 | self.relays.iter().map(|r| Tag::Relay(r.into())).collect(), | ||
| 48 | // git_servers | ||
| 49 | // other maintainers | ||
| 50 | // code languages and hashtags | ||
| 51 | ] | ||
| 52 | .concat(), | ||
| 53 | ) | ||
| 54 | .to_event(keys) | ||
| 55 | .context("failed to create repository reference event") | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | #[cfg(test)] | ||
| 60 | mod tests { | ||
| 61 | use test_utils::*; | ||
| 62 | |||
| 63 | use super::*; | ||
| 64 | |||
| 65 | mod to_event { | ||
| 66 | use super::*; | ||
| 67 | mod tags { | ||
| 68 | use super::*; | ||
| 69 | fn create() -> nostr::Event { | ||
| 70 | RepoRef::default() | ||
| 71 | .set_name("test name".to_string()) | ||
| 72 | .set_description("test description".to_string()) | ||
| 73 | .set_root_commit("23471389461".to_string()) | ||
| 74 | .set_relays(vec![ | ||
| 75 | "ws://relay1.io".to_string(), | ||
| 76 | "ws://relay2.io".to_string(), | ||
| 77 | ]) | ||
| 78 | .to_event(&TEST_KEY_1_KEYS) | ||
| 79 | .unwrap() | ||
| 80 | } | ||
| 81 | |||
| 82 | #[test] | ||
| 83 | fn name() { | ||
| 84 | assert!( | ||
| 85 | create() | ||
| 86 | .tags | ||
| 87 | .iter() | ||
| 88 | .any(|t| t.as_vec()[0].eq("name") && t.as_vec()[1].eq("test name")) | ||
| 89 | ) | ||
| 90 | } | ||
| 91 | #[test] | ||
| 92 | fn description() { | ||
| 93 | assert!(create().tags.iter().any( | ||
| 94 | |t| t.as_vec()[0].eq("description") && t.as_vec()[1].eq("test description") | ||
| 95 | )) | ||
| 96 | } | ||
| 97 | |||
| 98 | #[test] | ||
| 99 | fn root_commit_as_d_replaceable_event_identifier() { | ||
| 100 | assert!( | ||
| 101 | create() | ||
| 102 | .tags | ||
| 103 | .iter() | ||
| 104 | .any(|t| t.as_vec()[0].eq("d") && t.as_vec()[1].eq("23471389461")) | ||
| 105 | ) | ||
| 106 | } | ||
| 107 | |||
| 108 | #[test] | ||
| 109 | fn root_commit_as_reference() { | ||
| 110 | assert!( | ||
| 111 | create() | ||
| 112 | .tags | ||
| 113 | .iter() | ||
| 114 | .any(|t| t.as_vec()[0].eq("r") && t.as_vec()[1].eq("r-23471389461")) | ||
| 115 | ) | ||
| 116 | } | ||
| 117 | |||
| 118 | #[test] | ||
| 119 | fn relays() { | ||
| 120 | let event = create(); | ||
| 121 | let relay_tags = event | ||
| 122 | .tags | ||
| 123 | .iter() | ||
| 124 | .filter(|t| t.as_vec()[0].eq("relay")) | ||
| 125 | .collect::<Vec<&nostr::Tag>>(); | ||
| 126 | assert_eq!(relay_tags[0].as_vec().len(), 2); | ||
| 127 | assert_eq!(relay_tags[0].as_vec()[1], "ws://relay1.io"); | ||
| 128 | assert_eq!(relay_tags[1].as_vec()[1], "ws://relay2.io"); | ||
| 129 | } | ||
| 130 | |||
| 131 | #[test] | ||
| 132 | fn no_other_tags() { | ||
| 133 | assert_eq!(create().tags.len(), 6) | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
diff --git a/tests/claim.rs b/tests/claim.rs index ec62c0b..914b12e 100644 --- a/tests/claim.rs +++ b/tests/claim.rs | |||
| @@ -165,7 +165,7 @@ mod sends_repoistory_to_relays { | |||
| 165 | 165 | ||
| 166 | #[test] | 166 | #[test] |
| 167 | #[serial] | 167 | #[serial] |
| 168 | fn d_replaceable_event_identifier() -> Result<()> { | 168 | fn root_commit_as_d_replaceable_event_identifier() -> Result<()> { |
| 169 | let (_, _, r53, r55, r56) = futures::executor::block_on(prep_run_claim())?; | 169 | let (_, _, r53, r55, r56) = futures::executor::block_on(prep_run_claim())?; |
| 170 | for relay in [&r53, &r55, &r56] { | 170 | for relay in [&r53, &r55, &r56] { |
| 171 | let event: &nostr::Event = relay | 171 | let event: &nostr::Event = relay |
| @@ -182,7 +182,7 @@ mod sends_repoistory_to_relays { | |||
| 182 | 182 | ||
| 183 | #[test] | 183 | #[test] |
| 184 | #[serial] | 184 | #[serial] |
| 185 | fn root_commit() -> Result<()> { | 185 | fn root_commit_as_reference() -> Result<()> { |
| 186 | let (_, _, r53, r55, r56) = futures::executor::block_on(prep_run_claim())?; | 186 | let (_, _, r53, r55, r56) = futures::executor::block_on(prep_run_claim())?; |
| 187 | for relay in [&r53, &r55, &r56] { | 187 | for relay in [&r53, &r55, &r56] { |
| 188 | let event: &nostr::Event = relay | 188 | let event: &nostr::Event = relay |