From 3b4f0b0eee124133b641d6770704c368712f3dff Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 23 May 2024 14:44:39 +0100 Subject: feat: add NIP-31 alt tags to repo announcements, patches and cover letters --- src/repo_ref.rs | 15 ++++++++++++++- src/sub_commands/send.rs | 10 ++++++++++ tests/init.rs | 17 +++++++++++++++++ tests/send.rs | 39 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/repo_ref.rs b/src/repo_ref.rs index 3e8db64..d314e6d 100644 --- a/src/repo_ref.rs +++ b/src/repo_ref.rs @@ -136,6 +136,10 @@ impl RepoRef { .map(std::string::ToString::to_string) .collect(), ), + Tag::Generic( + nostr::TagKind::Custom("alt".to_string()), + vec![format!("git repository: {}", self.name.clone())], + ), ], // code languages and hashtags ] @@ -451,6 +455,15 @@ mod tests { .any(|t| t.as_vec()[0].eq("name") && t.as_vec()[1].eq("test name")) ) } + + #[test] + fn alt() { + assert!( + create().tags.iter().any(|t| t.as_vec()[0].eq("alt") + && t.as_vec()[1].eq("git repository: test name")) + ) + } + #[test] fn description() { assert!(create().tags.iter().any( @@ -515,7 +528,7 @@ mod tests { #[test] fn no_other_tags() { - assert_eq!(create().tags.len(), 8) + assert_eq!(create().tags.len(), 9) } } } diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs index 4639b01..189dc53 100644 --- a/src/sub_commands/send.rs +++ b/src/sub_commands/send.rs @@ -649,6 +649,10 @@ pub fn generate_cover_letter_and_patch_events( }, Tag::Reference(format!("{root_commit}")), Tag::Hashtag("cover-letter".to_string()), + Tag::Generic( + nostr::TagKind::Custom("alt".to_string()), + vec![format!("git patch cover letter: {}", title.clone())], + ), ], if let Some(event_ref) = root_proposal_id.clone() { vec![ @@ -895,6 +899,7 @@ pub fn patch_supports_commit_ids(event: &nostr::Event) -> bool { } #[allow(clippy::too_many_arguments)] +#[allow(clippy::too_many_lines)] pub fn generate_patch_event( git_repo: &Repo, root_commit: &Sha1Hash, @@ -912,6 +917,7 @@ pub fn generate_patch_event( .get_commit_parent(commit) .context("failed to get parent commit")?; let relay_hint = repo_ref.relays.first().map(nostr::UncheckedUrl::from); + EventBuilder::new( nostr::event::Kind::Custom(PATCH_KIND), git_repo @@ -937,6 +943,10 @@ pub fn generate_patch_event( // code that makes it into the main branch, assuming // the commit id is correct Tag::Reference(commit.to_string()), + Tag::Generic( + nostr::TagKind::Custom("alt".to_string()), + vec![format!("git patch: {}", git_repo.get_commit_message_summary(commit).unwrap_or_default())], + ), ], if let Some(thread_event_id) = thread_event_id { diff --git a/tests/init.rs b/tests/init.rs index 2083c82..1beeaa5 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -336,6 +336,23 @@ mod when_repo_not_previously_claimed { Ok(()) } + #[tokio::test] + #[serial] + async fn alt() -> Result<()> { + let (_, _, r53, r55, r56, r57) = prep_run_init().await?; + for relay in [&r53, &r55, &r56, &r57] { + let event: &nostr::Event = relay + .events + .iter() + .find(|e| e.kind.as_u64().eq(&REPOSITORY_KIND)) + .unwrap(); + + assert!(event.tags.iter().any(|t| t.as_vec()[0].eq("alt") + && t.as_vec()[1].eq("git repository: example-name"))); + } + Ok(()) + } + #[tokio::test] #[serial] async fn description() -> Result<()> { diff --git a/tests/send.rs b/tests/send.rs index 8474c19..aa16949 100644 --- a/tests/send.rs +++ b/tests/send.rs @@ -448,7 +448,7 @@ mod when_cover_letter_details_specified_with_range_of_head_2_sends_cover_letter_ #[tokio::test] #[serial] - async fn pr_tags_branch_name() -> Result<()> { + async fn cover_letter_tags_branch_name() -> Result<()> { let (_, _, r53, r55, r56) = prep_run_create_proposal(true).await?; for relay in [&r53, &r55, &r56] { let cover_letter_event: &nostr::Event = @@ -466,6 +466,27 @@ mod when_cover_letter_details_specified_with_range_of_head_2_sends_cover_letter_ } Ok(()) } + + #[tokio::test] + #[serial] + async fn cover_letter_tags_alt() -> Result<()> { + let (_, _, r53, r55, r56) = prep_run_create_proposal(true).await?; + for relay in [&r53, &r55, &r56] { + let cover_letter_event: &nostr::Event = + relay.events.iter().find(|e| is_cover_letter(e)).unwrap(); + + // branch-name tag + assert_eq!( + cover_letter_event + .iter_tags() + .find(|t| t.as_vec()[0].eq("alt")) + .unwrap() + .as_vec()[1], + "git patch cover letter: exampletitle" + ); + } + Ok(()) + } } mod patch_tags { @@ -603,6 +624,22 @@ mod when_cover_letter_details_specified_with_range_of_head_2_sends_cover_letter_ Ok(()) } + #[tokio::test] + #[serial] + async fn alt() -> Result<()> { + assert_eq!( + prep() + .await? + .tags + .iter() + .find(|t| t.as_vec()[0].eq("alt")) + .unwrap() + .as_vec(), + vec!["alt", "git patch: add t3.md"], + ); + Ok(()) + } + #[tokio::test] #[serial] async fn patch_tags_cover_letter_event_as_root() -> Result<()> { -- cgit v1.2.3