From 069a4c21c56291455fb9af09b693672889f98a03 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 13 Sep 2024 17:00:00 +0100 Subject: refactor: abstract find pr from branch name so it is done consistantly across ngit and the remote helper --- src/bin/git_remote_nostr/utils.rs | 23 +++-------------------- src/bin/ngit/sub_commands/pull.rs | 17 ++++++----------- src/bin/ngit/sub_commands/push.rs | 18 ++++++++---------- src/lib/git_events.rs | 18 ++++++++++++++++-- 4 files changed, 33 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index 15a0d76..bbe7cfa 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs @@ -16,8 +16,8 @@ use ngit::{ Repo, RepoActions, }, git_events::{ - event_is_revision_root, event_to_cover_letter, get_most_recent_patch_with_ancestors, - status_kinds, + event_is_revision_root, get_most_recent_patch_with_ancestors, + is_event_proposal_root_for_branch, status_kinds, }, repo_ref::RepoRef, }; @@ -189,24 +189,7 @@ pub fn find_proposal_and_patches_by_branch_name<'a>( current_user: &Option, ) -> Option<(&'a EventId, &'a (Event, Vec))> { open_proposals.iter().find(|(_, (proposal, _))| { - if let Ok(cl) = event_to_cover_letter(proposal) { - if let Ok(mut branch_name) = cl.get_branch_name() { - branch_name = if let Some(public_key) = current_user { - if proposal.author().eq(public_key) { - cl.branch_name.to_string() - } else { - branch_name - } - } else { - branch_name - }; - branch_name.eq(&refstr.replace("refs/heads/", "")) - } else { - false - } - } else { - false - } + is_event_proposal_root_for_branch(proposal, refstr, current_user).unwrap_or(false) }) } diff --git a/src/bin/ngit/sub_commands/pull.rs b/src/bin/ngit/sub_commands/pull.rs index eba6fc5..9a3d911 100644 --- a/src/bin/ngit/sub_commands/pull.rs +++ b/src/bin/ngit/sub_commands/pull.rs @@ -1,4 +1,5 @@ use anyhow::{bail, Context, Result}; +use ngit::git_events::is_event_proposal_root_for_branch; use nostr_sdk::PublicKey; use crate::{ @@ -7,10 +8,7 @@ use crate::{ get_proposals_and_revisions_from_cache, get_repo_ref_from_cache, Client, Connect, }, git::{str_to_sha1, Repo, RepoActions}, - git_events::{ - event_is_revision_root, event_to_cover_letter, get_commit_id_from_patch, - get_most_recent_patch_with_ancestors, tag_value, - }, + git_events::{get_commit_id_from_patch, get_most_recent_patch_with_ancestors, tag_value}, repo_ref::get_repo_coordinates, }; @@ -37,7 +35,7 @@ pub async fn launch() -> Result<()> { let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; - let public_key_if_known = + let logged_in_public_key = if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { PublicKey::parse(npub).ok() } else { @@ -49,15 +47,12 @@ pub async fn launch() -> Result<()> { .await? .iter() .find(|e| { - event_to_cover_letter(e).is_ok_and(|cl| { - (public_key_if_known.is_some_and(|public_key| e.author().eq(&public_key)) - && (branch_name.eq(&format!("pr/{}", cl.branch_name)) - || cl.branch_name.eq(&branch_name))) - || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name)) - }) && !event_is_revision_root(e) + is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key) + .unwrap_or(false) }) .context("cannot find proposal that matches the current branch name")? .clone(); + let commit_events = get_all_proposal_patch_events_from_cache( git_repo_path, &repo_ref, diff --git a/src/bin/ngit/sub_commands/push.rs b/src/bin/ngit/sub_commands/push.rs index 7cdc655..66edfb4 100644 --- a/src/bin/ngit/sub_commands/push.rs +++ b/src/bin/ngit/sub_commands/push.rs @@ -1,5 +1,8 @@ use anyhow::{bail, Context, Result}; -use ngit::{client::send_events, git_events::tag_value}; +use ngit::{ + client::send_events, + git_events::{is_event_proposal_root_for_branch, tag_value}, +}; use nostr_sdk::PublicKey; use crate::{ @@ -10,8 +13,7 @@ use crate::{ }, git::{identify_ahead_behind, str_to_sha1, Repo, RepoActions}, git_events::{ - event_is_revision_root, event_to_cover_letter, generate_patch_event, - get_commit_id_from_patch, get_most_recent_patch_with_ancestors, + generate_patch_event, get_commit_id_from_patch, get_most_recent_patch_with_ancestors, }, login, repo_ref::get_repo_coordinates, @@ -53,7 +55,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; - let public_key_if_known = + let logged_in_public_key = if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { PublicKey::parse(npub).ok() } else { @@ -65,12 +67,8 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { .await? .iter() .find(|e| { - event_to_cover_letter(e).is_ok_and(|cl| { - (public_key_if_known.is_some_and(|public_key| e.author().eq(&public_key)) - && (branch_name.eq(&format!("pr/{}", cl.branch_name)) - || cl.branch_name.eq(&branch_name))) - || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name)) - }) && !event_is_revision_root(e) + is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key) + .unwrap_or(false) }) .context("cannot find proposal that matches the current branch name")? .clone(); diff --git a/src/lib/git_events.rs b/src/lib/git_events.rs index b4ac676..d818f4c 100644 --- a/src/lib/git_events.rs +++ b/src/lib/git_events.rs @@ -3,8 +3,8 @@ use std::str::FromStr; use anyhow::{bail, Context, Result}; use nostr::nips::{nip01::Coordinate, nip10::Marker, nip19::Nip19}; use nostr_sdk::{ - hashes::sha1::Hash as Sha1Hash, Event, EventBuilder, EventId, FromBech32, Kind, Tag, TagKind, - TagStandard, UncheckedUrl, + hashes::sha1::Hash as Sha1Hash, Event, EventBuilder, EventId, FromBech32, Kind, PublicKey, Tag, + TagKind, TagStandard, UncheckedUrl, }; use nostr_signer::NostrSigner; @@ -594,6 +594,20 @@ fn get_event_parent_id(event: &nostr::Event) -> Result { .clone()) } +pub fn is_event_proposal_root_for_branch( + e: &Event, + branch_name_or_refstr: &str, + logged_in_user: &Option, +) -> Result { + let branch_name = branch_name_or_refstr.replace("refs/heads/", ""); + Ok(event_to_cover_letter(e).is_ok_and(|cl| { + (logged_in_user.is_some_and(|public_key| e.author().eq(&public_key)) + && (branch_name.eq(&format!("pr/{}", cl.branch_name)) + || cl.branch_name.eq(&branch_name))) + || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name)) + }) && !event_is_revision_root(e)) +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3