httpd: Add patch head refs to patch output

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez 2023-03-24 11:51:32 +01:00 committed by Alexis Sellier
parent da9d63b284
commit 6b62f5bb28
No known key found for this signature in database
2 changed files with 59 additions and 5 deletions

View File

@ -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::<Vec<_>>(),
"discussions": rev.discussion.comments().collect::<Comments>(),
"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<Vec<RefString>, 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::<Vec<_>>();
Ok(refs)
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Comment {

View File

@ -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::<Vec<_>>();
@ -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,