upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-07-25 11:48:22 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2025-07-25 16:10:57 +0100
commit9357b62b9824299be6fc85b09f57d93d3902f79a (patch)
tree04de089315d58bc6b4c5fc4df786d9604a1abb61 /src
parent802b115ca648011fd311a22ef3650aaa5a1a0acf (diff)
refactor: abstract `get_status`
for use by `ngit list`
Diffstat (limited to 'src')
-rw-r--r--src/bin/git_remote_nostr/utils.rs58
-rw-r--r--src/bin/ngit/sub_commands/list.rs5
-rw-r--r--src/lib/git_events.rs56
3 files changed, 71 insertions, 48 deletions
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs
index 8433967..2cb85bf 100644
--- a/src/bin/git_remote_nostr/utils.rs
+++ b/src/bin/git_remote_nostr/utils.rs
@@ -18,9 +18,8 @@ use ngit::{
18 nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol}, 18 nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol},
19 }, 19 },
20 git_events::{ 20 git_events::{
21 KIND_PULL_REQUEST, event_is_revision_root, 21 event_is_revision_root, get_pr_tip_event_or_most_recent_patch_with_ancestors, get_status,
22 get_pr_tip_event_or_most_recent_patch_with_ancestors, is_event_proposal_root_for_branch, 22 is_event_proposal_root_for_branch, status_kinds,
23 status_kinds,
24 }, 23 },
25 repo_ref::RepoRef, 24 repo_ref::RepoRef,
26}; 25};
@@ -104,7 +103,10 @@ pub async fn get_open_or_draft_proposals(
104 get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) 103 get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates())
105 .await? 104 .await?
106 .iter() 105 .iter()
107 .filter(|e| !event_is_revision_root(e)) 106 .filter(|e|
107 // If we wanted to treat to list Pull Requests that revise a Patch we would do this:
108 // e.kind.eq(&KIND_PULL_REQUEST) ||
109 !event_is_revision_root(e))
108 .cloned() 110 .cloned()
109 .collect(); 111 .collect();
110 112
@@ -124,48 +126,9 @@ pub async fn get_open_or_draft_proposals(
124 }; 126 };
125 let mut open_or_draft_proposals = HashMap::new(); 127 let mut open_or_draft_proposals = HashMap::new();
126 128
127 let get_status = |proposal: &Event| {
128 if let Some(e) = statuses
129 .iter()
130 .filter(|e| {
131 status_kinds().contains(&e.kind)
132 && e.tags.iter().any(|t| {
133 t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string())
134 })
135 && (proposal.pubkey.eq(&e.pubkey) || repo_ref.maintainers.contains(&e.pubkey))
136 })
137 .collect::<Vec<&nostr::Event>>()
138 .first()
139 {
140 e.kind
141 } else {
142 Kind::GitStatusOpen
143 }
144 };
145
146 let is_proposal_pr_revision_of_patch = |proposal: &Event, patch: &Event| {
147 proposal.kind.eq(&KIND_PULL_REQUEST)
148 && proposal.tags.clone().into_iter().any(|t| {
149 t.as_slice().len() > 1
150 && t.as_slice()[0].eq("e")
151 && t.as_slice()[1].eq(&patch.id.to_string())
152 && [Kind::GitStatusOpen, Kind::GitStatusDraft].contains(&get_status(proposal))
153 })
154 };
155
156 for proposal in &proposals { 129 for proposal in &proposals {
157 let status = get_status(proposal); 130 let status = get_status(proposal, repo_ref, &statuses, &proposals);
158 if [Kind::GitStatusOpen, Kind::GitStatusDraft].contains(&status) 131 if [Kind::GitStatusOpen, Kind::GitStatusDraft].contains(&status) {
159 || // or patch has been revised as a PR which is open or draft
160 (
161 status.eq(&Kind::GitStatusClosed) &&
162 proposal.kind.eq(&Kind::GitPatch) &&
163 proposals.iter()
164 .any(|p| {
165 is_proposal_pr_revision_of_patch(p, proposal)
166 })
167 )
168 {
169 if let Ok(commits_events) = get_all_proposal_patch_pr_pr_update_events_from_cache( 132 if let Ok(commits_events) = get_all_proposal_patch_pr_pr_update_events_from_cache(
170 git_repo_path, 133 git_repo_path,
171 repo_ref, 134 repo_ref,
@@ -196,7 +159,10 @@ pub async fn get_all_proposals(
196 get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) 159 get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates())
197 .await? 160 .await?
198 .iter() 161 .iter()
199 .filter(|e| !event_is_revision_root(e)) 162 .filter(|e|
163 // If we wanted to treat to list Pull Requests that revise a Patch we would do this:
164 // e.kind.eq(&KIND_PULL_REQUEST) ||
165 !event_is_revision_root(e))
200 .cloned() 166 .cloned()
201 .collect(); 167 .collect();
202 168
diff --git a/src/bin/ngit/sub_commands/list.rs b/src/bin/ngit/sub_commands/list.rs
index 9e35b33..95e17f3 100644
--- a/src/bin/ngit/sub_commands/list.rs
+++ b/src/bin/ngit/sub_commands/list.rs
@@ -72,7 +72,10 @@ pub async fn launch() -> Result<()> {
72 72
73 let proposals: Vec<nostr::Event> = proposals_and_revisions 73 let proposals: Vec<nostr::Event> = proposals_and_revisions
74 .iter() 74 .iter()
75 .filter(|e| !event_is_revision_root(e)) 75 .filter(|e|
76 // If we wanted to treat to list Pull Requests that revise a Patch we would do this:
77 // e.kind.eq(&KIND_PULL_REQUEST) ||
78 !event_is_revision_root(e))
76 .cloned() 79 .cloned()
77 .collect(); 80 .collect();
78 81
diff --git a/src/lib/git_events.rs b/src/lib/git_events.rs
index 7cd64ab..2e1f215 100644
--- a/src/lib/git_events.rs
+++ b/src/lib/git_events.rs
@@ -835,7 +835,61 @@ pub fn is_event_proposal_root_for_branch(
835 || cl 835 || cl
836 .get_branch_name_with_pr_prefix_and_shorthand_id() 836 .get_branch_name_with_pr_prefix_and_shorthand_id()
837 .is_ok_and(|s| s.eq(&branch_name)) 837 .is_ok_and(|s| s.eq(&branch_name))
838 }) && !event_is_revision_root(e)) 838 }) && (
839 // If we wanted to treat to list Pull Requests that revise a Patch we would do this:
840 // Note: whilst this the the case elsewhere event_is_revision_root is used, there is more to
841 // think about here?
842 // e.kind.eq(&KIND_PULL_REQUEST) ||
843 !event_is_revision_root(e)
844 ))
845}
846
847pub fn get_status(
848 proposal: &Event,
849 repo_ref: &RepoRef,
850 all_status_in_repo: &[Event],
851 all_pr_roots_in_repo: &[Event],
852) -> Kind {
853 let get_direct_status = |proposal: &Event| {
854 if let Some(e) = all_status_in_repo
855 .iter()
856 .filter(|e| {
857 status_kinds().contains(&e.kind)
858 && e.tags.iter().any(|t| {
859 t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string())
860 })
861 && (proposal.pubkey.eq(&e.pubkey) || repo_ref.maintainers.contains(&e.pubkey))
862 })
863 .collect::<Vec<&nostr::Event>>()
864 .first()
865 {
866 e.kind
867 } else {
868 Kind::GitStatusOpen
869 }
870 };
871 let is_proposal_pr_revision_of_patch = |proposal: &Event, patch: &Event| {
872 proposal.kind.eq(&KIND_PULL_REQUEST)
873 && proposal.tags.clone().into_iter().any(|t| {
874 t.as_slice().len() > 1
875 && t.as_slice()[0].eq("e")
876 && t.as_slice()[1].eq(&patch.id.to_string())
877 })
878 };
879
880 let direct_status = get_direct_status(proposal);
881 if direct_status.eq(&Kind::GitStatusClosed) && proposal.kind.eq(&Kind::GitPatch) {
882 if let Some(pr_revision) = all_pr_roots_in_repo
883 .iter()
884 .find(|p| is_proposal_pr_revision_of_patch(p, proposal))
885 {
886 get_direct_status(pr_revision)
887 } else {
888 direct_status
889 }
890 } else {
891 direct_status
892 }
839} 893}
840 894
841#[cfg(test)] 895#[cfg(test)]