Skip to content

Commit

Permalink
http: Rename project(s) to repo(s)
Browse files Browse the repository at this point in the history
The responses of the current `projects` endpoints are really
`repositories` so we should pull the bandaid off and start calling them
for what they are.

I kept the term `project` in places where we return or handle the
`project` payload.
  • Loading branch information
sebastinez committed Aug 20, 2024
1 parent 06cc559 commit 7f30919
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 248 deletions.
50 changes: 27 additions & 23 deletions radicle-httpd/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use radicle::patch::cache::Patches as _;
use radicle::storage::git::Repository;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tower_http::cors::{self, CorsLayer};
use tower_http::cors::{Any, CorsLayer};

use radicle::cob::{issue, patch, Author};
use radicle::identity::{DocAt, RepoId};
Expand Down Expand Up @@ -47,16 +47,15 @@ impl Context {
}
}

pub fn project_info<R: ReadRepository + radicle::cob::Store>(
pub fn repo_info<R: ReadRepository + radicle::cob::Store>(
&self,
repo: &R,
doc: DocAt,
) -> Result<project::Info, error::Error> {
) -> Result<repo::Info, error::Error> {
let (_, head) = repo.head()?;
let DocAt { doc, .. } = doc;
let id = repo.id();
let rid = repo.id();

let payload = doc.project()?;
let aliases = self.profile.aliases();
let delegates = doc
.delegates
Expand All @@ -66,17 +65,17 @@ impl Context {
let issues = self.profile.issues(repo)?.counts()?;
let patches = self.profile.patches(repo)?.counts()?;
let db = &self.profile.database()?;
let seeding = db.count(&id).unwrap_or_default();
let seeding = db.count(&rid).unwrap_or_default();

Ok(project::Info {
payload,
Ok(repo::Info {
payload: doc.payload,
delegates,
threshold: doc.threshold,
visibility: doc.visibility,
head,
issues,
patches,
id,
rid,
seeding,
})
}
Expand Down Expand Up @@ -105,7 +104,7 @@ pub fn router(ctx: Context) -> Router {
.layer(
CorsLayer::new()
.max_age(Duration::from_secs(86400))
.allow_origin(cors::Any)
.allow_origin(Any)
.allow_methods([Method::GET])
.allow_headers([CONTENT_TYPE]),
)
Expand All @@ -130,14 +129,14 @@ async fn root_handler() -> impl IntoResponse {
#[serde(rename_all = "camelCase")]
pub struct PaginationQuery {
#[serde(default)]
pub show: ProjectQuery,
pub show: RepoQuery,
pub page: Option<usize>,
pub per_page: Option<usize>,
}

#[derive(Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub enum ProjectQuery {
pub enum RepoQuery {
All,
#[default]
Pinned,
Expand Down Expand Up @@ -209,13 +208,15 @@ impl PatchStatus {

mod search {
use std::cmp::Ordering;
use std::collections::BTreeMap;

use nonempty::NonEmpty;
use radicle::identity::doc::{Payload, PayloadId};
use serde::{Deserialize, Serialize};
use serde_json::json;

use radicle::crypto::Verified;
use radicle::identity::{Project, RepoId};
use radicle::identity::RepoId;
use radicle::node::routing::Store;
use radicle::node::AliasStore;
use radicle::node::Database;
Expand All @@ -234,7 +235,7 @@ mod search {
pub struct SearchResult {
pub rid: RepoId,
#[serde(flatten)]
pub payload: Project,
pub payload: BTreeMap<PayloadId, Payload>,
pub delegates: NonEmpty<serde_json::Value>,
pub seeds: usize,
#[serde(skip)]
Expand All @@ -251,8 +252,9 @@ mod search {
if info.doc.visibility.is_private() {
return None;
}
let payload = info.doc.project().ok()?;
let index = payload.name().find(q)?;
let Ok(Some(index)) = info.doc.project().map(|p| p.name().find(q)) else {
return None;
};
let seeds = db.count(&info.rid).unwrap_or_default();
let delegates = info.doc.delegates.map(|did| match aliases.alias(&did) {
Some(alias) => json!({
Expand All @@ -266,7 +268,7 @@ mod search {

Some(SearchResult {
rid: info.rid,
payload,
payload: info.doc.payload,
delegates,
seeds,
index,
Expand Down Expand Up @@ -299,29 +301,31 @@ mod search {
}
}

mod project {
mod repo {
use std::collections::BTreeMap;

use serde::Serialize;
use serde_json::Value;

use radicle::cob;
use radicle::git::Oid;
use radicle::identity::project::Project;
use radicle::identity::doc::{Payload, PayloadId};
use radicle::identity::{RepoId, Visibility};

/// Project info.
/// Repos info.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Info {
/// Project metadata.
/// Repos metadata.
#[serde(flatten)]
pub payload: Project,
pub payload: BTreeMap<PayloadId, Payload>,
pub delegates: Vec<Value>,
pub threshold: usize,
pub visibility: Visibility,
pub head: Oid,
pub patches: cob::patch::PatchCounts,
pub issues: cob::issue::IssueCounts,
pub id: RepoId,
pub rid: RepoId,
pub seeding: usize,
}
}
12 changes: 6 additions & 6 deletions radicle-httpd/src/api/v1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod delegates;
mod node;
mod projects;
mod repos;
mod stats;

use axum::extract::State;
Expand All @@ -20,7 +20,7 @@ pub fn router(ctx: Context) -> Router {
.merge(root_router)
.merge(node::router(ctx.clone()))
.merge(delegates::router(ctx.clone()))
.merge(projects::router(ctx.clone()))
.merge(repos::router(ctx.clone()))
.merge(stats::router(ctx));

Router::new().nest("/v1", routes)
Expand All @@ -36,8 +36,8 @@ async fn root_handler(State(ctx): State<Context>) -> impl IntoResponse {
"path": "/api/v1",
"links": [
{
"href": "/projects",
"rel": "projects",
"href": "/repos",
"rel": "repos",
"type": "GET"
},
{
Expand All @@ -51,8 +51,8 @@ async fn root_handler(State(ctx): State<Context>) -> impl IntoResponse {
"type": "GET"
},
{
"href": "/delegates/:did/projects",
"rel": "projects",
"href": "/delegates/:did/repos",
"rel": "repos",
"type": "GET"
},
{
Expand Down
92 changes: 42 additions & 50 deletions radicle-httpd/src/api/v1/delegates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,36 @@ use radicle::patch::cache::Patches as _;
use radicle::storage::{ReadRepository, ReadStorage};

use crate::api::error::Error;
use crate::api::json;
use crate::api::project::Info;
use crate::api::Context;
use crate::api::{PaginationQuery, ProjectQuery};
use crate::api::repo::Info;
use crate::api::{json, Context, PaginationQuery};
use crate::axum_extra::{Path, Query};

pub fn router(ctx: Context) -> Router {
Router::new()
.route(
"/delegates/:delegate/projects",
get(delegates_projects_handler),
)
.route("/delegates/:delegate/repos", get(delegates_repos_handler))
.with_state(ctx)
}

/// List all projects which delegate is a part of.
/// `GET /delegates/:delegate/projects`
async fn delegates_projects_handler(
/// List all repos which delegate is a part of.
/// `GET /delegates/:delegate/repos`
async fn delegates_repos_handler(
State(ctx): State<Context>,
Path(delegate): Path<Did>,
Query(qs): Query<PaginationQuery>,
) -> impl IntoResponse {
let PaginationQuery {
show,
page,
per_page,
} = qs;
let PaginationQuery { page, per_page, .. } = qs;
let page = page.unwrap_or(0);
let per_page = per_page.unwrap_or(10);
let storage = &ctx.profile.storage;
let db = &ctx.profile.database()?;
let pinned = &ctx.profile.config.web.pinned;
let mut projects = match show {
ProjectQuery::All => storage
.repositories()?
.into_iter()
.filter(|repo| repo.doc.visibility.is_public())
.collect::<Vec<_>>(),
ProjectQuery::Pinned => storage.repositories_by_id(pinned.repositories.iter())?,
};
projects.sort_by_key(|p| p.rid);
let mut repos = storage
.repositories()?
.into_iter()
.filter(|repo| repo.doc.visibility.is_public())
.collect::<Vec<_>>();
repos.sort_by_key(|p| p.rid);

let infos = projects
let infos = repos
.into_iter()
.filter_map(|id| {
if !id.doc.delegates.iter().any(|d| *d == delegate) {
Expand All @@ -66,9 +53,6 @@ async fn delegates_projects_handler(
let Ok((_, head)) = repo.head() else {
return None;
};
let Ok(payload) = id.doc.project() else {
return None;
};
let Ok(issues) = ctx.profile.issues(&repo) else {
return None;
};
Expand All @@ -92,14 +76,14 @@ async fn delegates_projects_handler(
let seeding = db.count(&id.rid).unwrap_or_default();

Some(Info {
payload,
payload: id.doc.payload,
delegates,
threshold: id.doc.threshold,
visibility: id.doc.visibility,
head,
issues,
patches,
id: id.rid,
rid: id.rid,
seeding,
})
})
Expand Down Expand Up @@ -128,7 +112,7 @@ mod routes {
.layer(MockConnectInfo(SocketAddr::from(([127, 0, 0, 1], 8080))));
let response = get(
&app,
"/delegates/did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/projects?show=all",
"/delegates/did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/repos?show=all",
)
.await;

Expand All @@ -142,9 +126,11 @@ mod routes {
response.json().await,
json!([
{
"name": "hello-world",
"description": "Rad repository for tests",
"defaultBranch": "master",
"xyz.radicle.project": {
"defaultBranch": "master",
"description": "Rad repository for tests",
"name": "hello-world",
},
"delegates": [
{
"id": DID,
Expand All @@ -166,13 +152,15 @@ mod routes {
"open": 1,
"closed": 0,
},
"id": RID,
"rid": RID,
"seeding": 1,
},
{
"name": "again-hello-world",
"description": "Rad repository for sorting",
"defaultBranch": "master",
"xyz.radicle.project": {
"defaultBranch": "master",
"description": "Rad repository for sorting",
"name": "again-hello-world",
},
"delegates": [
{
"id": DID,
Expand All @@ -194,7 +182,7 @@ mod routes {
"open": 0,
"closed": 0,
},
"id": "rad:z4GypKmh1gkEfmkXtarcYnkvtFUfE",
"rid": "rad:z4GypKmh1gkEfmkXtarcYnkvtFUfE",
"seeding": 1,
}
])
Expand All @@ -206,7 +194,7 @@ mod routes {
))));
let response = get(
&app,
"/delegates/did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/projects?show=all",
"/delegates/did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/repos?show=all",
)
.await;

Expand All @@ -220,9 +208,11 @@ mod routes {
response.json().await,
json!([
{
"name": "hello-world",
"description": "Rad repository for tests",
"defaultBranch": "master",
"xyz.radicle.project": {
"defaultBranch": "master",
"description": "Rad repository for tests",
"name": "hello-world",
},
"delegates": [
{
"id": DID,
Expand All @@ -244,13 +234,15 @@ mod routes {
"open": 1,
"closed": 0,
},
"id": RID,
"rid": RID,
"seeding": 1,
},
{
"name": "again-hello-world",
"description": "Rad repository for sorting",
"defaultBranch": "master",
"xyz.radicle.project": {
"defaultBranch": "master",
"description": "Rad repository for sorting",
"name": "again-hello-world",
},
"delegates": [
{
"id": DID,
Expand All @@ -272,7 +264,7 @@ mod routes {
"open": 0,
"closed": 0,
},
"id": "rad:z4GypKmh1gkEfmkXtarcYnkvtFUfE",
"rid": "rad:z4GypKmh1gkEfmkXtarcYnkvtFUfE",
"seeding": 1,
}
])
Expand Down
Loading

0 comments on commit 7f30919

Please sign in to comment.