From 6b62f5bb28637c2b358ef7ca961c0ca0b5726af8 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 24 Mar 2023 11:51:32 +0100 Subject: [PATCH] httpd: Add patch head refs to patch output Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/json.rs | 30 +++++++++++++++++++++--- radicle-httpd/src/api/v1/projects.rs | 34 ++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 165dda20..d28d8450 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -9,10 +9,12 @@ use serde_json::{json, Value}; use radicle::cob::issue::{Issue, IssueId}; use radicle::cob::patch::{Patch, PatchId}; use radicle::cob::thread::{self, CommentId}; -use radicle::cob::{Author, Timestamp}; +use radicle::cob::{ActorId, Author, Timestamp}; +use radicle::git::RefString; +use radicle::storage::{git, refs, ReadRepository}; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; -use radicle_surf::{Commit, Stats}; +use radicle_surf::{Commit, Oid, Stats}; use crate::api::auth::Session; @@ -102,7 +104,7 @@ pub(crate) fn issue(id: IssueId, issue: Issue) -> Value { } /// Returns JSON for a `patch`. -pub(crate) fn patch(id: PatchId, patch: Patch) -> Value { +pub(crate) fn patch(id: PatchId, patch: Patch, repo: &git::Repository) -> Value { json!({ "id": id.to_string(), "author": patch.author(), @@ -117,6 +119,7 @@ pub(crate) fn patch(id: PatchId, patch: Patch) -> Value { "description": rev.description(), "base": rev.base, "oid": rev.oid, + "refs": get_refs(repo, patch.author().id(), &rev.oid).unwrap_or(vec![]), "merges": rev.merges().collect::>(), "discussions": rev.discussion.comments().collect::(), "timestamp": rev.timestamp, @@ -134,6 +137,27 @@ fn name_in_path(path: &str) -> &str { } } +fn get_refs( + repo: &git::Repository, + id: &ActorId, + head: &Oid, +) -> Result, refs::Error> { + let remote = repo.remote(id)?; + let refs = remote + .refs + .iter() + .filter_map(|(name, o)| { + if o == head { + Some(name.to_owned()) + } else { + None + } + }) + .collect::>(); + + Ok(refs) +} + #[derive(Serialize)] #[serde(rename_all = "camelCase")] struct Comment { diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 2fcc7ad3..798bbf72 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -690,7 +690,7 @@ async fn patches_handler( patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let patches = patches .into_iter() - .map(|(id, patch, _)| api::json::patch(id, patch)) + .map(|(id, patch, _)| api::json::patch(id, patch, &repo)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -710,7 +710,7 @@ async fn patch_handler( .get(&patch_id.into())? .ok_or(Error::NotFound)?; - Ok::<_, Error>(Json(api::json::patch(patch_id.into(), patch))) + Ok::<_, Error>(Json(api::json::patch(patch_id.into(), patch, &repo))) } #[cfg(test)] @@ -1670,6 +1670,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -1706,6 +1709,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -1781,6 +1787,9 @@ mod routes { "description": "", "base": INITIAL_COMMIT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -1841,6 +1850,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -1898,6 +1910,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -1908,6 +1923,9 @@ mod routes { "description": "This is a new revision", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -1965,6 +1983,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -2044,6 +2065,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [ { @@ -2137,6 +2161,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [], "discussions": [], "timestamp": TIMESTAMP, @@ -2217,6 +2244,9 @@ mod routes { "description": "", "base": PARENT, "oid": HEAD, + "refs": [ + "refs/heads/master", + ], "merges": [ { "node": CONTRIBUTOR_NID,