diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-13 17:09:10 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-13 17:23:09 +0100 |
| commit | 61b130e61e3ba88d61f75dd2f6a201e700e7dc01 (patch) | |
| tree | 3e9a211a81e14a5daa1beb8899275768b9c97d4b /src/sub_commands/pull.rs | |
| parent | 36763dc0d821425e088adefd16dc7c4bf3b30f3b (diff) | |
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.
Diffstat (limited to 'src/sub_commands/pull.rs')
| -rw-r--r-- | src/sub_commands/pull.rs | 29 |
1 files changed, 24 insertions, 5 deletions
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 @@ | |||
| 1 | use anyhow::{bail, Context, Result}; | 1 | use anyhow::{bail, Context, Result}; |
| 2 | use nostr_sdk::{Event, PublicKey}; | ||
| 2 | 3 | ||
| 3 | use super::{ | 4 | use super::{ |
| 4 | list::{ | 5 | list::{ |
| @@ -45,17 +46,21 @@ pub async fn launch() -> Result<()> { | |||
| 45 | 46 | ||
| 46 | let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; | 47 | let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; |
| 47 | 48 | ||
| 49 | let logged_in_public_key = | ||
| 50 | if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { | ||
| 51 | PublicKey::parse(npub).ok() | ||
| 52 | } else { | ||
| 53 | None | ||
| 54 | }; | ||
| 55 | |||
| 48 | let proposal_root_event = | 56 | let proposal_root_event = |
| 49 | get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) | 57 | get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) |
| 50 | .await? | 58 | .await? |
| 51 | .iter() | 59 | .iter() |
| 52 | .find(|e| { | 60 | .find(|e| is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key)) |
| 53 | event_to_cover_letter(e) | ||
| 54 | .is_ok_and(|cl| cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name))) | ||
| 55 | && !event_is_revision_root(e) | ||
| 56 | }) | ||
| 57 | .context("cannot find proposal that matches the current branch name")? | 61 | .context("cannot find proposal that matches the current branch name")? |
| 58 | .clone(); | 62 | .clone(); |
| 63 | |||
| 59 | let commit_events = get_all_proposal_patch_events_from_cache( | 64 | let commit_events = get_all_proposal_patch_events_from_cache( |
| 60 | git_repo_path, | 65 | git_repo_path, |
| 61 | &repo_ref, | 66 | &repo_ref, |
| @@ -207,3 +212,17 @@ fn check_clean(git_repo: &Repo) -> Result<()> { | |||
| 207 | } | 212 | } |
| 208 | Ok(()) | 213 | Ok(()) |
| 209 | } | 214 | } |
| 215 | |||
| 216 | pub fn is_event_proposal_root_for_branch( | ||
| 217 | e: &Event, | ||
| 218 | branch_name_or_refstr: &str, | ||
| 219 | logged_in_user: &Option<PublicKey>, | ||
| 220 | ) -> bool { | ||
| 221 | let branch_name = branch_name_or_refstr.replace("refs/heads/", ""); | ||
| 222 | event_to_cover_letter(e).is_ok_and(|cl| { | ||
| 223 | (logged_in_user.is_some_and(|public_key| e.author().eq(&public_key)) | ||
| 224 | && (branch_name.eq(&format!("pr/{}", cl.branch_name)) | ||
| 225 | || cl.branch_name.eq(&branch_name))) | ||
| 226 | || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name)) | ||
| 227 | }) && !event_is_revision_root(e) | ||
| 228 | } | ||