From 61b130e61e3ba88d61f75dd2f6a201e700e7dc01 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 13 Sep 2024 17:09:10 +0100 Subject: fix(ngit): pull and push find pr from branch as since cl.get_branch_name has been introduced branch names could be prefixed with a pr and sometimes postfixed with an event id with the fix your branch could be called `article-editor` or `pr/article-editor` if you are logged in as the author of the PR (git config nostr.npub matches the PR author) or `article-editor(3fbba0ec)` which is what it would be named if you weren't logged in as the author and checked out the branch on a remote with the nostr url. --- src/sub_commands/pull.rs | 29 ++++++++++++++++++++++++----- src/sub_commands/push.rs | 20 +++++++++++--------- 2 files changed, 35 insertions(+), 14 deletions(-) (limited to 'src/sub_commands') diff --git a/src/sub_commands/pull.rs b/src/sub_commands/pull.rs index e33a744..2547be5 100644 --- a/src/sub_commands/pull.rs +++ b/src/sub_commands/pull.rs @@ -1,4 +1,5 @@ use anyhow::{bail, Context, Result}; +use nostr_sdk::{Event, PublicKey}; use super::{ list::{ @@ -45,17 +46,21 @@ pub async fn launch() -> Result<()> { let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; + let logged_in_public_key = + if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { + PublicKey::parse(npub).ok() + } else { + None + }; + let proposal_root_event = get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) .await? .iter() - .find(|e| { - event_to_cover_letter(e) - .is_ok_and(|cl| cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name))) - && !event_is_revision_root(e) - }) + .find(|e| is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key)) .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, @@ -207,3 +212,17 @@ fn check_clean(git_repo: &Repo) -> Result<()> { } Ok(()) } + +pub fn is_event_proposal_root_for_branch( + e: &Event, + branch_name_or_refstr: &str, + logged_in_user: &Option, +) -> bool { + let branch_name = branch_name_or_refstr.replace("refs/heads/", ""); + 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) +} diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index 7a82c7a..15baad9 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs @@ -1,4 +1,5 @@ use anyhow::{bail, Context, Result}; +use nostr_sdk::PublicKey; #[cfg(not(test))] use crate::client::Client; @@ -17,10 +18,8 @@ use crate::{ get_most_recent_patch_with_ancestors, get_proposals_and_revisions_from_cache, tag_value, }, - send::{ - event_is_revision_root, event_to_cover_letter, generate_patch_event, - identify_ahead_behind, send_events, - }, + pull::is_event_proposal_root_for_branch, + send::{generate_patch_event, identify_ahead_behind, send_events}, }, }; @@ -62,15 +61,18 @@ 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 logged_in_public_key = + if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { + PublicKey::parse(npub).ok() + } else { + None + }; + let proposal_root_event = get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) .await? .iter() - .find(|e| { - event_to_cover_letter(e) - .is_ok_and(|cl| cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name))) - && !event_is_revision_root(e) - }) + .find(|e| is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key)) .context("cannot find proposal that matches the current branch name")? .clone(); -- cgit v1.2.3