httpd: Return `alias` field on `merges` of a patch

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2023-05-23 11:11:15 +00:00 committed by Alexis Sellier
parent 3af53626f0
commit c25450d310
No known key found for this signature in database
2 changed files with 78 additions and 32 deletions

View File

@ -7,12 +7,15 @@ use serde::Serialize;
use serde_json::{json, Value}; use serde_json::{json, Value};
use radicle::cob::issue::{Issue, IssueId}; use radicle::cob::issue::{Issue, IssueId};
use radicle::cob::patch::Merge;
use radicle::cob::patch::Review;
use radicle::cob::patch::{Patch, PatchId}; use radicle::cob::patch::{Patch, PatchId};
use radicle::cob::thread; use radicle::cob::thread;
use radicle::cob::thread::{CommentId, Thread}; use radicle::cob::thread::{CommentId, Thread};
use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::cob::{ActorId, Author, Reaction, Timestamp};
use radicle::git::RefString; use radicle::git::RefString;
use radicle::node::tracking::store as TrackingStore; use radicle::node::tracking::store as TrackingStore;
use radicle::prelude::NodeId;
use radicle::storage::{git, refs, ReadRepository}; use radicle::storage::{git, refs, ReadRepository};
use radicle_surf::blob::Blob; use radicle_surf::blob::Blob;
use radicle_surf::tree::Tree; use radicle_surf::tree::Tree;
@ -122,14 +125,7 @@ pub(crate) fn patch(
"state": patch.state(), "state": patch.state(),
"target": patch.target(), "target": patch.target(),
"tags": patch.tags().collect::<Vec<_>>(), "tags": patch.tags().collect::<Vec<_>>(),
"merges": patch.merges().map(|(a, m)| { "merges": patch.merges().map(|(nid, m)| merge(m, nid, aliases.alias(nid))).collect::<Vec<_>>(),
json!({
"author": a,
"revision": m.revision,
"commit": m.commit,
"timestamp": m.timestamp
})
}).collect::<Vec<_>>(),
"reviewers": patch.reviewers().collect::<Vec<_>>(), "reviewers": patch.reviewers().collect::<Vec<_>>(),
"revisions": patch.revisions().map(|(id, rev)| { "revisions": patch.revisions().map(|(id, rev)| {
json!({ json!({
@ -143,7 +139,7 @@ pub(crate) fn patch(
.map(|(id, comment)| Comment::new(id, comment, rev.discussion(), aliases)) .map(|(id, comment)| Comment::new(id, comment, rev.discussion(), aliases))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
"timestamp": rev.timestamp(), "timestamp": rev.timestamp(),
"reviews": rev.reviews().collect::<Vec<_>>(), "reviews": rev.reviews().map(|(nid, _review)| review(nid, aliases.alias(nid), _review)).collect::<Vec<_>>(),
}) })
}).collect::<Vec<_>>(), }).collect::<Vec<_>>(),
}) })
@ -160,6 +156,54 @@ fn author(author: &Author, alias: Option<String>) -> Value {
} }
} }
/// Returns JSON for a patch `Merge` and fills in `alias` when present.
fn merge(merge: &Merge, nid: &NodeId, alias: Option<String>) -> Value {
match alias {
Some(alias) => json!({
"author": {
"id": nid,
"alias": alias,
},
"commit": merge.commit,
"timestamp": merge.timestamp,
"revision": merge.revision,
}),
None => json!({
"author": {
"id": nid,
},
"commit": merge.commit,
"timestamp": merge.timestamp,
"revision": merge.revision,
}),
}
}
/// Returns JSON for a patch `Review` and fills in `alias` when present.
fn review(nid: &NodeId, alias: Option<String>, review: &Review) -> Value {
match alias {
Some(alias) => json!({
"author": {
"id": nid,
"alias": alias,
},
"verdict": review.verdict(),
"comment": review.comment(),
"inline": review.inline().collect::<Vec<_>>(),
"timestamp": review.timestamp(),
}),
None => json!({
"author": {
"id": nid,
},
"verdict": review.verdict(),
"comment": review.comment(),
"inline": review.inline().collect::<Vec<_>>(),
"timestamp": review.timestamp(),
}),
}
}
/// Returns the name part of a path string. /// Returns the name part of a path string.
fn name_in_path(path: &str) -> &str { fn name_in_path(path: &str) -> &str {
match path.rsplit('/').next() { match path.rsplit('/').next() {

View File

@ -2271,9 +2271,10 @@ mod routes {
"discussions": [], "discussions": [],
"timestamp": TIMESTAMP, "timestamp": TIMESTAMP,
"reviews": [ "reviews": [
[
CONTRIBUTOR_NID,
{ {
"author": {
"id": CONTRIBUTOR_NID,
},
"verdict": "accept", "verdict": "accept",
"comment": "A small review", "comment": "A small review",
"inline": [ "inline": [
@ -2294,7 +2295,6 @@ mod routes {
"timestamp": TIMESTAMP, "timestamp": TIMESTAMP,
}, },
], ],
],
}, },
], ],
}) })
@ -2345,7 +2345,9 @@ mod routes {
"target": "delegates", "target": "delegates",
"tags": [], "tags": [],
"merges": [{ "merges": [{
"author": CONTRIBUTOR_NID, "author": {
"id": CONTRIBUTOR_NID,
},
"revision": CONTRIBUTOR_PATCH_ID, "revision": CONTRIBUTOR_PATCH_ID,
"commit": PARENT, "commit": PARENT,
"timestamp": TIMESTAMP, "timestamp": TIMESTAMP,