From ba2a36b983265a134d47880cb19aecfd10238f91 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 19 Oct 2022 08:47:40 +0200 Subject: [PATCH 001/264] httpd: implement `git` fetch-only server Introduces a new `radicle-httpd` crate which will serve as the basis for heartwood's HTTP backend. The first part of this is a git server which allows projects to be fetched from storage using plain `git`. Signed-off-by: Alexis Sellier --- radicle-httpd/Cargo.toml | 33 +++++ radicle-httpd/src/error.rs | 52 ++++++++ radicle-httpd/src/lib.rs | 251 +++++++++++++++++++++++++++++++++++++ radicle-httpd/src/main.rs | 82 ++++++++++++ 4 files changed, 418 insertions(+) create mode 100644 radicle-httpd/Cargo.toml create mode 100644 radicle-httpd/src/error.rs create mode 100644 radicle-httpd/src/lib.rs create mode 100644 radicle-httpd/src/main.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml new file mode 100644 index 000000000..36f41aafa --- /dev/null +++ b/radicle-httpd/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "radicle-httpd" +license = "MIT OR Apache-2.0" +version = "0.1.0" +authors = ["Alexis Sellier "] +edition = "2021" +default-run = "radicle-httpd" +build = "../build.rs" + +[features] +default = [] +logfmt = [ + "tracing-logfmt", + "tracing-subscriber/env-filter" +] + +[dependencies] +anyhow = { version = "1" } +flate2 = { version = "1" } +lexopt = { version = "0.2.1" } +thiserror = { version = "1" } +tokio = { version = "1.21", default-features = false, features = ["macros", "rt-multi-thread"] } +tracing = { version = "0.1.37", default-features = false, features = ["std", "log"] } +tracing-subscriber = { version = "0.3", default-features = false, features = ["std", "ansi", "fmt"] } +tracing-logfmt = { version = "0.2", optional = true } +axum = { version = "0.5.16", default-features = false } +axum-server = { version = "0.4.2", default-features = false } +hyper = { version = "0.14.17", default-features = false } +tower-http = { version = "0.3.4", default-features = false, features = ["trace"] } + +[dependencies.radicle] +path = "../radicle" +version = "0.2.0" diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs new file mode 100644 index 000000000..01f11f6a4 --- /dev/null +++ b/radicle-httpd/src/error.rs @@ -0,0 +1,52 @@ +use axum::http; +use axum::response::{IntoResponse, Response}; + +/// Errors relating to the HTTP backend. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// I/O error. + #[error("i/o error: {0}")] + Io(#[from] std::io::Error), + + /// The service is not available. + #[error("service '{0}' not available")] + ServiceUnavailable(&'static str), + + /// Invalid identifier. + #[error("invalid radicle identifier: {0}")] + Id(#[from] radicle::identity::IdError), + + /// Git backend error. + #[error("backend error")] + Backend, + + /// Id is not valid. + #[error("id is not valid")] + InvalidId, + + /// HeaderName error. + #[error(transparent)] + InvalidHeaderName(#[from] axum::http::header::InvalidHeaderName), + + /// HeaderValue error. + #[error(transparent)] + InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue), +} + +impl Error { + pub fn status(&self) -> http::StatusCode { + match self { + Error::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE, + Error::InvalidId => http::StatusCode::NOT_FOUND, + _ => http::StatusCode::INTERNAL_SERVER_ERROR, + } + } +} + +impl IntoResponse for Error { + fn into_response(self) -> Response { + tracing::error!("{}", self); + + self.status().into_response() + } +} diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs new file mode 100644 index 000000000..554cbf15b --- /dev/null +++ b/radicle-httpd/src/lib.rs @@ -0,0 +1,251 @@ +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] +pub mod error; + +use std::collections::HashMap; +use std::io::prelude::*; +use std::net::SocketAddr; +use std::path::Path; +use std::process::{Command, Stdio}; +use std::sync::Arc; +use std::time::Duration; +use std::{io, net, str}; + +use anyhow::Context as _; +use axum::body::Body; +use axum::body::{BoxBody, Bytes}; +use axum::extract::{ConnectInfo, Path as AxumPath, RawQuery}; +use axum::http::header::HeaderName; +use axum::http::HeaderMap; +use axum::http::{Method, StatusCode}; +use axum::http::{Request, Response}; +use axum::response::IntoResponse; +use axum::routing::any; +use axum::{Extension, Router}; +use flate2::write::GzDecoder; +use hyper::body::Buf as _; +use tower_http::trace::TraceLayer; +use tracing::Span; + +use radicle::identity::Id; +use radicle::profile::Profile; + +use error::Error; + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + +#[derive(Debug, Clone)] +pub struct Options { + pub listen: net::SocketAddr, +} + +/// Run the Git Server. +pub async fn run(options: Options) -> anyhow::Result<()> { + let git_version = Command::new("git") + .arg("version") + .output() + .context("'git' command must be available")? + .stdout; + tracing::info!("{}", str::from_utf8(&git_version)?.trim()); + + let profile = radicle::Profile::load()?; + tracing::info!("using radicle home at {}", profile.home.display()); + + let app = Router::new() + .route("/:project/*request", any(git_handler)) + .layer(Extension(Arc::new(profile))) + .layer( + TraceLayer::new_for_http() + .make_span_with(|request: &Request| { + tracing::info_span!( + "request", + method = %request.method(), + uri = %request.uri(), + status = tracing::field::Empty, + latency = tracing::field::Empty, + ) + }) + .on_response( + |response: &Response, latency: Duration, span: &Span| { + span.record("status", &tracing::field::debug(response.status())); + span.record("latency", &tracing::field::debug(latency)); + + tracing::info!("Processed"); + }, + ), + ) + .into_make_service_with_connect_info::(); + + tracing::info!("listening on http://{}", options.listen); + + axum::Server::bind(&options.listen) + .serve(app) + .await + .map_err(anyhow::Error::from) +} + +async fn git_handler( + Extension(profile): Extension>, + AxumPath((project, request)): AxumPath<(String, String)>, + method: Method, + headers: HeaderMap, + body: Bytes, + ConnectInfo(remote): ConnectInfo, + query: RawQuery, +) -> impl IntoResponse { + let query = query.0.unwrap_or_default(); + let id: Id = project.strip_suffix(".git").unwrap_or(&project).parse()?; + + let (status, headers, body) = + git_http_backend(&profile, method, headers, body, remote, id, &request, query).await?; + + let mut response_headers = HeaderMap::new(); + for (name, vec) in headers.iter() { + for value in vec { + let header: HeaderName = name.try_into()?; + response_headers.insert(header, value.parse()?); + } + } + + Ok::<_, Error>((status, response_headers, body)) +} + +async fn git_http_backend( + profile: &Profile, + method: Method, + headers: HeaderMap, + mut body: Bytes, + remote: net::SocketAddr, + id: Id, + path: &str, + query: String, +) -> Result<(StatusCode, HashMap>, Vec), Error> { + let git_dir = radicle::storage::git::paths::repository(&profile.storage, &id); + let content_type = + if let Some(Ok(content_type)) = headers.get("Content-Type").map(|h| h.to_str()) { + content_type + } else { + "" + }; + + // Reject push requests. + match (path, query.as_str()) { + ("git-receive-pack", _) | (_, "service=git-receive-pack") => { + return Err(Error::ServiceUnavailable("git-receive-pack")); + } + _ => {} + }; + + tracing::debug!("id: {:?}", id); + tracing::debug!("headers: {:?}", headers); + tracing::debug!("path: {:?}", path); + tracing::debug!("method: {:?}", method.as_str()); + tracing::debug!("remote: {:?}", remote.to_string()); + + let mut cmd = Command::new("git"); + let mut child = cmd + .arg("http-backend") + .env("REQUEST_METHOD", method.as_str()) + .env("GIT_PROJECT_ROOT", git_dir) + // "The GIT_HTTP_EXPORT_ALL environmental variable may be passed to git-http-backend to bypass + // the check for the "git-daemon-export-ok" file in each repository before allowing export of + // that repository." + .env("GIT_HTTP_EXPORT_ALL", String::default()) + .env("PATH_INFO", Path::new("/").join(path)) + .env("CONTENT_TYPE", content_type) + .env("QUERY_STRING", query) + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .stdin(Stdio::piped()) + .spawn()?; + + // Whether the request body is compressed. + let gzip = matches!( + headers.get("Content-Encoding").map(|h| h.to_str()), + Some(Ok("gzip")) + ); + + { + // This is safe because we captured the child's stdin. + let mut stdin = child.stdin.take().unwrap(); + + // Copy the request body to git-http-backend's stdin. + if gzip { + let mut decoder = GzDecoder::new(&mut stdin); + let mut reader = body.reader(); + + io::copy(&mut reader, &mut decoder)?; + decoder.finish()?; + } else { + while body.has_remaining() { + let mut chunk = body.chunk(); + let count = chunk.len(); + + io::copy(&mut chunk, &mut stdin)?; + body.advance(count); + } + } + } + + match child.wait_with_output() { + Ok(output) if output.status.success() => { + tracing::info!("git-http-backend: exited successfully for {}", id); + + let mut reader = std::io::Cursor::new(output.stdout); + let mut headers = HashMap::new(); + + // Parse headers returned by git so that we can use them in the client response. + for line in io::Read::by_ref(&mut reader).lines() { + let line = line?; + + if line.is_empty() || line == "\r" { + break; + } + + let mut parts = line.splitn(2, ':'); + let key = parts.next(); + let value = parts.next(); + + if let (Some(key), Some(value)) = (key, value) { + let value = &value[1..]; + + headers + .entry(key.to_string()) + .or_insert_with(Vec::new) + .push(value.to_string()); + } else { + return Err(Error::Backend); + } + } + + let status = { + tracing::debug!("git-http-backend: {:?}", &headers); + + let line = headers.remove("Status").unwrap_or_default(); + let line = line.into_iter().next().unwrap_or_default(); + let mut parts = line.split(' '); + + parts + .next() + .and_then(|p| p.parse().ok()) + .unwrap_or(StatusCode::OK) + }; + + let position = reader.position() as usize; + let body = reader.into_inner().split_off(position); + + Ok((status, headers, body)) + } + Ok(output) => { + tracing::error!("git-http-backend: exited with code {}", output.status); + + if let Ok(output) = std::str::from_utf8(&output.stderr) { + tracing::error!("git-http-backend: stderr: {}", output.trim_end()); + } + Err(Error::Backend) + } + Err(err) => { + panic!("failed to wait for git-http-backend: {}", err); + } + } +} diff --git a/radicle-httpd/src/main.rs b/radicle-httpd/src/main.rs new file mode 100644 index 000000000..ab6704750 --- /dev/null +++ b/radicle-httpd/src/main.rs @@ -0,0 +1,82 @@ +use std::{net, process}; + +use tracing::dispatcher::Dispatch; + +use radicle_httpd as httpd; + +#[derive(Debug)] +pub struct Options { + pub listen: net::SocketAddr, +} + +impl Options { + fn from_env() -> Result { + use lexopt::prelude::*; + + let mut parser = lexopt::Parser::from_env(); + let mut listen = None; + + while let Some(arg) = parser.next()? { + match arg { + Long("listen") => { + let addr = parser.value()?.parse()?; + listen = Some(addr); + } + Long("help") => { + println!("usage: radicle-httpd [--listen ]"); + process::exit(0); + } + _ => return Err(arg.unexpected()), + } + } + Ok(Self { + listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8080).into()), + }) + } +} + +impl From for httpd::Options { + fn from(other: Options) -> Self { + Self { + listen: other.listen, + } + } +} + +#[cfg(feature = "logfmt")] +mod logger { + use tracing_subscriber::layer::SubscriberExt as _; + use tracing_subscriber::EnvFilter; + + pub fn subscriber() -> impl tracing::Subscriber { + tracing_subscriber::Registry::default() + .with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"))) + .with(tracing_logfmt::layer()) + } +} + +#[cfg(not(feature = "logfmt"))] +mod logger { + pub fn subscriber() -> impl tracing::Subscriber { + tracing_subscriber::FmtSubscriber::new() + } +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let options = Options::from_env()?; + + tracing::dispatcher::set_global_default(Dispatch::new(logger::subscriber())) + .expect("Global logger hasn't already been set"); + + tracing::info!("version {}-{}", env!("CARGO_PKG_VERSION"), env!("GIT_HEAD")); + + match httpd::run(options.into()).await { + Ok(()) => {} + Err(err) => { + tracing::error!("Fatal: {:#}", err); + process::exit(1); + } + } + Ok(()) +} From ef1c331cb6d16f088e1bed724106716cb480b55f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 19 Oct 2022 11:47:29 +0200 Subject: [PATCH 002/264] Remove redundant `Options` type Signed-off-by: Alexis Sellier --- radicle-httpd/src/main.rs | 73 ++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/radicle-httpd/src/main.rs b/radicle-httpd/src/main.rs index ab6704750..4e13ae3b2 100644 --- a/radicle-httpd/src/main.rs +++ b/radicle-httpd/src/main.rs @@ -1,47 +1,7 @@ -use std::{net, process}; - -use tracing::dispatcher::Dispatch; +use std::process; use radicle_httpd as httpd; - -#[derive(Debug)] -pub struct Options { - pub listen: net::SocketAddr, -} - -impl Options { - fn from_env() -> Result { - use lexopt::prelude::*; - - let mut parser = lexopt::Parser::from_env(); - let mut listen = None; - - while let Some(arg) = parser.next()? { - match arg { - Long("listen") => { - let addr = parser.value()?.parse()?; - listen = Some(addr); - } - Long("help") => { - println!("usage: radicle-httpd [--listen ]"); - process::exit(0); - } - _ => return Err(arg.unexpected()), - } - } - Ok(Self { - listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8080).into()), - }) - } -} - -impl From for httpd::Options { - fn from(other: Options) -> Self { - Self { - listen: other.listen, - } - } -} +use tracing::dispatcher::Dispatch; #[cfg(feature = "logfmt")] mod logger { @@ -64,14 +24,14 @@ mod logger { #[tokio::main] async fn main() -> anyhow::Result<()> { - let options = Options::from_env()?; + let options = parse_options()?; tracing::dispatcher::set_global_default(Dispatch::new(logger::subscriber())) .expect("Global logger hasn't already been set"); tracing::info!("version {}-{}", env!("CARGO_PKG_VERSION"), env!("GIT_HEAD")); - match httpd::run(options.into()).await { + match httpd::run(options).await { Ok(()) => {} Err(err) => { tracing::error!("Fatal: {:#}", err); @@ -80,3 +40,28 @@ async fn main() -> anyhow::Result<()> { } Ok(()) } + +/// Parse command-line arguments into HTTP options. +fn parse_options() -> Result { + use lexopt::prelude::*; + + let mut parser = lexopt::Parser::from_env(); + let mut listen = None; + + while let Some(arg) = parser.next()? { + match arg { + Long("listen") => { + let addr = parser.value()?.parse()?; + listen = Some(addr); + } + Long("help") => { + println!("usage: radicle-httpd [--listen ]"); + process::exit(0); + } + _ => return Err(arg.unexpected()), + } + } + Ok(httpd::Options { + listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8080).into()), + }) +} From ae2a35abb91585e1e989ea5e53298f4e00db8137 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 22 Nov 2022 10:54:57 +0000 Subject: [PATCH 003/264] Migrate `/v1/`, `/v1/node` routes to heartwood Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 23 +++--- radicle-httpd/src/api.rs | 119 +++++++++++++++++++++++++++++++ radicle-httpd/src/api/auth.rs | 90 +++++++++++++++++++++++ radicle-httpd/src/api/v1.rs | 11 +++ radicle-httpd/src/api/v1/node.rs | 26 +++++++ radicle-httpd/src/lib.rs | 21 ++++-- 6 files changed, 275 insertions(+), 15 deletions(-) create mode 100644 radicle-httpd/src/api.rs create mode 100644 radicle-httpd/src/api/auth.rs create mode 100644 radicle-httpd/src/api/v1.rs create mode 100644 radicle-httpd/src/api/v1/node.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 36f41aafa..b0f93c736 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -16,17 +16,22 @@ logfmt = [ [dependencies] anyhow = { version = "1" } -flate2 = { version = "1" } -lexopt = { version = "0.2.1" } -thiserror = { version = "1" } -tokio = { version = "1.21", default-features = false, features = ["macros", "rt-multi-thread"] } -tracing = { version = "0.1.37", default-features = false, features = ["std", "log"] } -tracing-subscriber = { version = "0.3", default-features = false, features = ["std", "ansi", "fmt"] } -tracing-logfmt = { version = "0.2", optional = true } -axum = { version = "0.5.16", default-features = false } +axum = { version = "0.5.16", default-features = false, features = ["json", "headers", "query"] } axum-server = { version = "0.4.2", default-features = false } +ethers-core = { version = "1.0" } +flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } -tower-http = { version = "0.3.4", default-features = false, features = ["trace"] } +lexopt = { version = "0.2.1" } +serde = { version = "1", features = ["derive"] } +serde_json = { version = "1", features = ["preserve_order"] } +siwe = { version = "0.5" } +thiserror = { version = "1" } +time = { version = "0.3.17" } +tokio = { version = "1.21", default-features = false, features = ["macros", "rt-multi-thread"] } +tower-http = { version = "0.3.4", default-features = false, features = ["trace", "cors"] } +tracing = { version = "0.1.37", default-features = false, features = ["std", "log"] } +tracing-logfmt = { version = "0.2", optional = true } +tracing-subscriber = { version = "0.3", default-features = false, features = ["std", "ansi", "fmt"] } [dependencies.radicle] path = "../radicle" diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs new file mode 100644 index 000000000..feda09c92 --- /dev/null +++ b/radicle-httpd/src/api.rs @@ -0,0 +1,119 @@ +use std::collections::HashMap; +use std::sync::Arc; +use std::time::Duration; + +use axum::body::{Body, BoxBody}; +use axum::http::header::{AUTHORIZATION, CONTENT_TYPE}; +use axum::http::Method; +use axum::response::{IntoResponse, Json}; +use axum::routing::get; +use axum::{Extension, Router}; +use hyper::http::{Request, Response}; +use serde::{Deserialize, Serialize}; +use serde_json::json; +use tokio::sync::RwLock; +use tower_http::cors::{self, CorsLayer}; +use tower_http::trace::TraceLayer; +use tracing::Span; + +use radicle::{Profile, Storage}; + +mod auth; +mod v1; + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + +/// Identifier for sessions +type SessionId = String; + +#[derive(Clone)] +pub struct Context { + profile: Arc, + sessions: Arc>>, +} + +impl Context { + pub fn new(profile: Arc) -> Self { + Self { + profile, + sessions: Default::default(), + } + } + + fn storage(&self) -> &Storage { + &self.profile.storage + } +} + +pub fn router(ctx: Context) -> Router { + let root_router = Router::new() + .route("/", get(root_handler)) + .layer(Extension(ctx.clone())); + + Router::new() + .merge(root_router) + .merge(v1::router(ctx)) + .layer( + CorsLayer::new() + .max_age(Duration::from_secs(86400)) + .allow_origin(cors::Any) + .allow_methods([Method::GET, Method::POST, Method::PUT]) + .allow_headers([CONTENT_TYPE, AUTHORIZATION]), + ) + .layer( + TraceLayer::new_for_http() + .make_span_with(|request: &Request| { + tracing::info_span!( + "request", + method = %request.method(), + uri = %request.uri(), + status = tracing::field::Empty, + latency = tracing::field::Empty, + ) + }) + .on_response( + |response: &Response, latency: Duration, span: &Span| { + span.record("status", &tracing::field::debug(response.status())); + span.record("latency", &tracing::field::debug(latency)); + + tracing::info!("Processed"); + }, + ), + ) +} + +async fn root_handler(Extension(ctx): Extension) -> impl IntoResponse { + let response = json!({ + "message": "Welcome!", + "service": "radicle-httpd", + "version": format!("{}-{}", VERSION, env!("GIT_HEAD")), + "node": { "id": ctx.profile.public_key }, + "path": "/", + "links": [ + { + "href": "/v1/projects", + "rel": "projects", + "type": "GET" + }, + { + "href": "/v1/node", + "rel": "node", + "type": "GET" + }, + { + "href": "/v1/delegates/:urn/projects", + "rel": "projects", + "type": "GET" + } + ] + }); + + Json(response) +} + +#[derive(Serialize, Deserialize, Clone)] +#[serde(rename_all = "kebab-case")] +pub struct PaginationQuery { + pub page: Option, + pub per_page: Option, +} diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs new file mode 100644 index 000000000..bdb9b95d7 --- /dev/null +++ b/radicle-httpd/src/api/auth.rs @@ -0,0 +1,90 @@ +use std::convert::TryFrom; +use std::str::FromStr; + +use ethers_core::types::{Signature, H160}; +use serde::{Deserialize, Serialize, Serializer}; +use time::OffsetDateTime; + +use crate::error::Error; + +#[derive(Clone)] +pub struct DateTime(OffsetDateTime); + +impl Serialize for DateTime { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(&format!("{}", self.0)) + } +} + +#[derive(Deserialize, Serialize)] +pub struct AuthRequest { + pub message: String, + #[serde(deserialize_with = "deserialize_signature")] + pub signature: Signature, +} + +fn deserialize_signature<'de, D>(deserializer: D) -> Result +where + D: serde::de::Deserializer<'de>, +{ + let buf = String::deserialize(deserializer)?; + Signature::from_str(&buf).map_err(serde::de::Error::custom) +} + +pub enum AuthState { + Authorized(Session), + Unauthorized { + nonce: String, + expiration_time: DateTime, + }, +} + +// We copy the implementation of siwe::Message here to derive Serialization and Debug +#[derive(Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Session { + pub domain: String, + pub address: H160, + pub statement: Option, + pub uri: String, + pub version: u64, + pub chain_id: u64, + pub nonce: String, + pub issued_at: DateTime, + pub expiration_time: Option, + pub resources: Vec, +} + +impl TryFrom for Session { + type Error = Error; + + fn try_from(message: siwe::Message) -> Result { + Ok(Session { + domain: message.domain.host().to_string(), + address: H160(message.address), + statement: None, + uri: message.uri.to_string(), + version: message.version as u64, + chain_id: message.chain_id, + nonce: message.nonce, + issued_at: DateTime(message.issued_at.as_ref().to_owned()), + expiration_time: message + .expiration_time + .map(|x| DateTime(x.as_ref().to_owned())), + resources: message.resources.iter().map(|r| r.to_string()).collect(), + }) + } +} + +#[cfg(test)] +mod test { + #[test] + fn test_auth_request_de() { + let json = serde_json::json!({ + "message": "Hello World!", + "signature": "20096c6ed2bcccb88c9cafbbbbda7a5a3cff6d0ca318c07faa58464083ca40a92f899fbeb26a4c763a7004b13fd0f1ba6c321d4e3a023e30f63c40d4154b99a41c" + }); + + let _req: super::AuthRequest = serde_json::from_value(json).unwrap(); + } +} diff --git a/radicle-httpd/src/api/v1.rs b/radicle-httpd/src/api/v1.rs new file mode 100644 index 000000000..66e2c6c09 --- /dev/null +++ b/radicle-httpd/src/api/v1.rs @@ -0,0 +1,11 @@ +mod node; + +use axum::Router; + +use crate::api::Context; + +pub fn router(ctx: Context) -> Router { + let routes = Router::new().merge(node::router(ctx)); + + Router::new().nest("/v1", routes) +} diff --git a/radicle-httpd/src/api/v1/node.rs b/radicle-httpd/src/api/v1/node.rs new file mode 100644 index 000000000..529c6598d --- /dev/null +++ b/radicle-httpd/src/api/v1/node.rs @@ -0,0 +1,26 @@ +use axum::response::IntoResponse; +use axum::routing::get; +use axum::{Extension, Json, Router}; +use serde_json::json; + +use radicle::node::NodeId; + +use crate::api::Context; + +pub fn router(ctx: Context) -> Router { + let node_id = ctx.profile.public_key; + + Router::new() + .route("/node", get(node_handler)) + .layer(Extension(node_id)) +} + +/// Return the node id for the node identity. +/// `GET /node` +async fn node_handler(Extension(node_id): Extension) -> impl IntoResponse { + let response = json!({ + "id": node_id.to_string(), + }); + + Json(response) +} diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 554cbf15b..065e7f459 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -32,6 +32,8 @@ use radicle::profile::Profile; use error::Error; +mod api; + pub const VERSION: &str = env!("CARGO_PKG_VERSION"); #[derive(Debug, Clone)] @@ -39,7 +41,7 @@ pub struct Options { pub listen: net::SocketAddr, } -/// Run the Git Server. +/// Run the Server. pub async fn run(options: Options) -> anyhow::Result<()> { let git_version = Command::new("git") .arg("version") @@ -48,12 +50,12 @@ pub async fn run(options: Options) -> anyhow::Result<()> { .stdout; tracing::info!("{}", str::from_utf8(&git_version)?.trim()); - let profile = radicle::Profile::load()?; + let profile = Arc::new(radicle::Profile::load()?); tracing::info!("using radicle home at {}", profile.home.display()); - let app = Router::new() + let git_router = Router::new() .route("/:project/*request", any(git_handler)) - .layer(Extension(Arc::new(profile))) + .layer(Extension(profile.clone())) .layer( TraceLayer::new_for_http() .make_span_with(|request: &Request| { @@ -73,11 +75,18 @@ pub async fn run(options: Options) -> anyhow::Result<()> { tracing::info!("Processed"); }, ), - ) - .into_make_service_with_connect_info::(); + ); + + let ctx = api::Context::new(profile); + let api_router = api::router(ctx); tracing::info!("listening on http://{}", options.listen); + let app = Router::new() + .merge(git_router) + .nest("/api", api_router) + .into_make_service_with_connect_info::(); + axum::Server::bind(&options.listen) .serve(app) .await From b26562f2c5763737a712c3a600b4174dccf52d40 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 22 Nov 2022 10:55:08 +0000 Subject: [PATCH 004/264] Migrate `/v1/sessions` route Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 1 + radicle-httpd/src/api.rs | 8 +- radicle-httpd/src/api/auth.rs | 4 +- radicle-httpd/src/api/axum_extra.rs | 88 ++++++++++++++++++ radicle-httpd/src/api/error.rs | 56 ++++++++++++ radicle-httpd/src/api/v1.rs | 5 +- radicle-httpd/src/api/v1/sessions.rs | 130 +++++++++++++++++++++++++++ 7 files changed, 284 insertions(+), 8 deletions(-) create mode 100644 radicle-httpd/src/api/axum_extra.rs create mode 100644 radicle-httpd/src/api/error.rs create mode 100644 radicle-httpd/src/api/v1/sessions.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index b0f93c736..69b6b7f6b 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -19,6 +19,7 @@ anyhow = { version = "1" } axum = { version = "0.5.16", default-features = false, features = ["json", "headers", "query"] } axum-server = { version = "0.4.2", default-features = false } ethers-core = { version = "1.0" } +fastrand = { version = "1.7.0" } flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index feda09c92..264491e1c 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -16,9 +16,11 @@ use tower_http::cors::{self, CorsLayer}; use tower_http::trace::TraceLayer; use tracing::Span; -use radicle::{Profile, Storage}; +use radicle::Profile; mod auth; +mod axum_extra; +mod error; mod v1; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -39,10 +41,6 @@ impl Context { sessions: Default::default(), } } - - fn storage(&self) -> &Storage { - &self.profile.storage - } } pub fn router(ctx: Context) -> Router { diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index bdb9b95d7..3c29f8b1c 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -5,10 +5,10 @@ use ethers_core::types::{Signature, H160}; use serde::{Deserialize, Serialize, Serializer}; use time::OffsetDateTime; -use crate::error::Error; +use crate::api::error::Error; #[derive(Clone)] -pub struct DateTime(OffsetDateTime); +pub struct DateTime(pub OffsetDateTime); impl Serialize for DateTime { fn serialize(&self, serializer: S) -> Result { diff --git a/radicle-httpd/src/api/axum_extra.rs b/radicle-httpd/src/api/axum_extra.rs new file mode 100644 index 000000000..8c23f9658 --- /dev/null +++ b/radicle-httpd/src/api/axum_extra.rs @@ -0,0 +1,88 @@ +use axum::extract::path::ErrorKind; +use axum::extract::rejection::{PathRejection, QueryRejection}; +use axum::extract::{FromRequest, RequestParts}; +use axum::http::StatusCode; +use axum::{async_trait, Json}; + +use serde::de::DeserializeOwned; +use serde::Serialize; + +pub struct Path(pub T); + +#[async_trait] +impl FromRequest for Path +where + T: DeserializeOwned + Send, + B: Send, +{ + type Rejection = (StatusCode, axum::Json); + + async fn from_request(req: &mut RequestParts) -> Result { + match axum::extract::Path::::from_request(req).await { + Ok(value) => Ok(Self(value.0)), + Err(rejection) => { + let status = StatusCode::BAD_REQUEST; + let body = match rejection { + PathRejection::FailedToDeserializePathParams(inner) => { + let kind = inner.into_kind(); + match &kind { + ErrorKind::Message(msg) => Json(Error { + success: false, + error: msg.to_string(), + }), + _ => Json(Error { + success: false, + error: kind.to_string(), + }), + } + } + _ => Json(Error { + success: false, + error: format!("{}", rejection), + }), + }; + + Err((status, body)) + } + } + } +} + +#[derive(Default)] +pub struct Query(pub T); + +#[async_trait] +impl FromRequest for Query +where + T: DeserializeOwned + Send, + B: Send, +{ + type Rejection = (StatusCode, axum::Json); + + async fn from_request(req: &mut RequestParts) -> Result { + match axum::extract::Query::::from_request(req).await { + Ok(value) => Ok(Self(value.0)), + Err(rejection) => { + let status = StatusCode::BAD_REQUEST; + let body = match rejection { + QueryRejection::FailedToDeserializeQueryString(inner) => Json(Error { + success: false, + error: inner.to_string(), + }), + _ => Json(Error { + success: false, + error: format!("{}", rejection), + }), + }; + + Err((status, body)) + } + } + } +} + +#[derive(Serialize)] +pub struct Error { + success: bool, + error: String, +} diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs new file mode 100644 index 000000000..78ff62df7 --- /dev/null +++ b/radicle-httpd/src/api/error.rs @@ -0,0 +1,56 @@ +use axum::http; +use axum::response::{IntoResponse, Response}; + +/// Errors relating to the HTTP backend. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// The entity was not found. + #[error("entity not found")] + NotFound, + + /// An error occurred during an authentication process. + #[error("could not authenticate: {0}")] + Auth(&'static str), + + /// An error occurred with env variables. + #[error(transparent)] + Env(#[from] std::env::VarError), + + /// I/O error. + #[error("i/o error: {0}")] + Io(#[from] std::io::Error), + + /// Invalid identifier. + #[error("invalid radicle identifier: {0}")] + Id(#[from] radicle::identity::IdError), + + /// HeaderName error. + #[error(transparent)] + InvalidHeaderName(#[from] axum::http::header::InvalidHeaderName), + + /// HeaderValue error. + #[error(transparent)] + InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue), + + /// An error occurred while verifying the siwe message. + #[error(transparent)] + SiweVerification(#[from] siwe::VerificationError), + + /// An error occurred while parsing the siwe message. + #[error(transparent)] + SiweParse(#[from] siwe::ParseError), +} + +impl Error { + pub fn status(&self) -> http::StatusCode { + http::StatusCode::INTERNAL_SERVER_ERROR + } +} + +impl IntoResponse for Error { + fn into_response(self) -> Response { + tracing::error!("{}", self); + + self.status().into_response() + } +} diff --git a/radicle-httpd/src/api/v1.rs b/radicle-httpd/src/api/v1.rs index 66e2c6c09..f09751f1a 100644 --- a/radicle-httpd/src/api/v1.rs +++ b/radicle-httpd/src/api/v1.rs @@ -1,11 +1,14 @@ mod node; +mod sessions; use axum::Router; use crate::api::Context; pub fn router(ctx: Context) -> Router { - let routes = Router::new().merge(node::router(ctx)); + let routes = Router::new() + .merge(node::router(ctx.clone())) + .merge(sessions::router(ctx)); Router::new().nest("/v1", routes) } diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs new file mode 100644 index 000000000..1b03d7379 --- /dev/null +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -0,0 +1,130 @@ +use std::collections::HashMap; +use std::convert::TryInto; +use std::env; +use std::iter::repeat_with; +use std::str::FromStr; + +use axum::response::IntoResponse; +use axum::routing::{get, post}; +use axum::{Extension, Json, Router}; +use ethers_core::utils::hex; +use hyper::http::uri::Authority; +use serde_json::json; +use siwe::Message; +use time::{Duration, OffsetDateTime}; + +use crate::api::auth::{AuthRequest, AuthState, DateTime, Session}; +use crate::api::axum_extra::Path; +use crate::api::error::Error; +use crate::api::Context; + +pub const UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60); + +pub fn router(ctx: Context) -> Router { + Router::new() + .route("/sessions", post(session_create_handler)) + .route( + "/sessions/:id", + get(session_get_handler).put(session_signin_handler), + ) + .layer(Extension(ctx)) +} + +/// Create session. +/// `POST /sessions` +async fn session_create_handler(Extension(ctx): Extension) -> impl IntoResponse { + let expiration_time = OffsetDateTime::now_utc() + .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) + .unwrap(); + let mut sessions = ctx.sessions.write().await; + let (session_id, nonce) = create_session(&mut sessions, DateTime(expiration_time)); + + Json(json!({ "id": session_id, "nonce": nonce })) +} + +/// Get session. +/// `GET /sessions/:id` +async fn session_get_handler( + Extension(ctx): Extension, + Path(id): Path, +) -> impl IntoResponse { + let sessions = ctx.sessions.read().await; + let session = sessions.get(&id).ok_or(Error::NotFound)?; + + match session { + AuthState::Authorized(session) => { + Ok::<_, Error>(Json(json!({ "id": id, "session": session }))) + } + AuthState::Unauthorized { + nonce, + expiration_time, + } => Ok::<_, Error>(Json( + json!({ "id": id, "nonce": nonce, "expirationTime": expiration_time }), + )), + } +} + +/// Update session. +/// `PUT /sessions/:id` +async fn session_signin_handler( + Extension(ctx): Extension, + Path(id): Path, + Json(request): Json, +) -> impl IntoResponse { + // Get unauthenticated session data, return early if not found + let mut sessions = ctx.sessions.write().await; + let session = sessions.get(&id).ok_or(Error::NotFound)?; + + if let AuthState::Unauthorized { nonce, .. } = session { + let message = Message::from_str(request.message.as_str()).map_err(Error::from)?; + + let host = env::var("RADICLE_DOMAIN").map_err(Error::from)?; + + // Validate nonce + if *nonce != message.nonce { + return Err(Error::Auth("Invalid nonce")); + } + + // Verify that domain is the correct one + let authority = Authority::from_str(&host).map_err(|_| Error::Auth("Invalid host"))?; + if authority != message.domain { + return Err(Error::Auth("Invalid domain")); + } + + // Verifies the following: + // - AuthRequest sig matches the address passed in the AuthRequest message. + // - expirationTime is not in the past. + // - notBefore time is in the future. + message + .verify(&request.signature.to_vec(), &Default::default()) + .await + .map_err(Error::from)?; + + let session: Session = message.try_into()?; + sessions.insert(id.clone(), AuthState::Authorized(session.clone())); + + return Ok::<_, Error>(Json(json!({ "id": id, "session": session }))); + } + + Err(Error::Auth("Session already authorized")) +} + +fn create_session( + map: &mut HashMap, + expiration_time: DateTime, +) -> (String, String) { + let nonce = siwe::generate_nonce(); + + // We generate a value from the RNG for the session id + let rng = fastrand::Rng::new(); + let id = hex::encode(repeat_with(|| rng.u8(..)).take(32).collect::>()); + + let auth_state = AuthState::Unauthorized { + nonce: nonce.clone(), + expiration_time, + }; + + map.insert(id.clone(), auth_state); + + (id, nonce) +} From 2d49f8bbff7a498e9305f7efd634451a62a49074 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 22 Nov 2022 10:55:13 +0000 Subject: [PATCH 005/264] Migrate `/v1/delegates` route Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 2 +- radicle-httpd/src/api/error.rs | 4 ++ radicle-httpd/src/api/v1.rs | 4 +- radicle-httpd/src/api/v1/delegates.rs | 78 +++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 radicle-httpd/src/api/v1/delegates.rs diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 264491e1c..215650f36 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -99,7 +99,7 @@ async fn root_handler(Extension(ctx): Extension) -> impl IntoResponse { "type": "GET" }, { - "href": "/v1/delegates/:urn/projects", + "href": "/v1/delegates/:did/projects", "rel": "projects", "type": "GET" } diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 78ff62df7..b8c5e1d17 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -39,6 +39,10 @@ pub enum Error { /// An error occurred while parsing the siwe message. #[error(transparent)] SiweParse(#[from] siwe::ParseError), + + /// Storage error. + #[error(transparent)] + StorageError(#[from] radicle::storage::Error), } impl Error { diff --git a/radicle-httpd/src/api/v1.rs b/radicle-httpd/src/api/v1.rs index f09751f1a..b39bc609f 100644 --- a/radicle-httpd/src/api/v1.rs +++ b/radicle-httpd/src/api/v1.rs @@ -1,3 +1,4 @@ +mod delegates; mod node; mod sessions; @@ -8,7 +9,8 @@ use crate::api::Context; pub fn router(ctx: Context) -> Router { let routes = Router::new() .merge(node::router(ctx.clone())) - .merge(sessions::router(ctx)); + .merge(sessions::router(ctx.clone())) + .merge(delegates::router(ctx)); Router::new().nest("/v1", routes) } diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs new file mode 100644 index 000000000..05abb9d52 --- /dev/null +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -0,0 +1,78 @@ +use axum::response::IntoResponse; +use axum::routing::get; +use axum::{Extension, Json, Router}; +use serde::Serialize; + +use radicle::cob::store::Store; +use radicle::git::Oid; +use radicle::identity::project::Payload; +use radicle::identity::{Did, Doc}; +use radicle::storage::{ReadRepository, WriteStorage}; + +use crate::api::axum_extra::{Path, Query}; +use crate::api::error::Error; +use crate::api::Context; +use crate::api::PaginationQuery; + +/// Project info. +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Info { + /// Project metadata. + #[serde(flatten)] + pub payload: Payload, + pub head: Oid, + pub patches: usize, + pub issues: usize, +} + +pub fn router(ctx: Context) -> Router { + Router::new() + .route( + "/delegates/:delegate/projects", + get(delegates_projects_handler), + ) + .layer(Extension(ctx)) +} + +/// List all projects which delegate is a part of. +/// `GET /delegates/:delegate/projects` +async fn delegates_projects_handler( + Extension(ctx): Extension, + Path(delegate): Path, + Query(qs): Query, +) -> impl IntoResponse { + 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 projects = storage + .projects()? + .into_iter() + .filter_map(|id| { + let Ok(repo) = storage.repository(id) else { return None }; + let Ok((_, head)) = repo.head() else { return None }; + let Ok(Doc { payload, delegates, .. }) = repo.project_of(ctx.profile.id()) else { return None }; + + if !delegates.iter().any(|d| d.id == delegate) { + return None; + } + + let Ok(cobs) = Store::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(issues) = cobs.issues().count() else { return None }; + let Ok(patches) = cobs.patches().count() else { return None }; + + Some(Info { + payload, + head, + issues, + patches, + id, + }) + }) + .skip(page * per_page) + .take(per_page) + .collect::>(); + + Ok::<_, Error>(Json(projects)) +} From de1f84b7927eafd653c1cb8439bae7d4c7089ee5 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 22 Nov 2022 13:16:34 +0000 Subject: [PATCH 006/264] Migrate `/projects`: `root`, `project`, `commit`, `activity` handlers Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 8 +- radicle-httpd/src/api.rs | 39 ++++++++ radicle-httpd/src/api/error.rs | 14 ++- radicle-httpd/src/api/v1.rs | 4 +- radicle-httpd/src/api/v1/delegates.rs | 25 ++---- radicle-httpd/src/api/v1/projects.rs | 123 ++++++++++++++++++++++++++ 6 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 radicle-httpd/src/api/v1/projects.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 69b6b7f6b..bfd912590 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -18,6 +18,7 @@ logfmt = [ anyhow = { version = "1" } axum = { version = "0.5.16", default-features = false, features = ["json", "headers", "query"] } axum-server = { version = "0.4.2", default-features = false } +chrono = { version = "0.4.22" } ethers-core = { version = "1.0" } fastrand = { version = "1.7.0" } flate2 = { version = "1" } @@ -29,7 +30,7 @@ siwe = { version = "0.5" } thiserror = { version = "1" } time = { version = "0.3.17" } tokio = { version = "1.21", default-features = false, features = ["macros", "rt-multi-thread"] } -tower-http = { version = "0.3.4", default-features = false, features = ["trace", "cors"] } +tower-http = { version = "0.3.4", default-features = false, features = ["trace", "cors", "set-header"] } tracing = { version = "0.1.37", default-features = false, features = ["std", "log"] } tracing-logfmt = { version = "0.2", optional = true } tracing-subscriber = { version = "0.3", default-features = false, features = ["std", "ansi", "fmt"] } @@ -37,3 +38,8 @@ tracing-subscriber = { version = "0.3", default-features = false, features = ["s [dependencies.radicle] path = "../radicle" version = "0.2.0" + +[dependencies.radicle-surf] +git = "https://github.com/radicle-dev/radicle-git" +features = ["serialize"] +rev = "d3115a22158c8395705babefdc89049f7510d32d" diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 215650f36..8650460e3 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -16,6 +16,9 @@ use tower_http::cors::{self, CorsLayer}; use tower_http::trace::TraceLayer; use tracing::Span; +use radicle::cob::issue::Issues; +use radicle::identity::{Doc, Id}; +use radicle::storage::{ReadRepository, WriteStorage}; use radicle::Profile; mod auth; @@ -41,6 +44,22 @@ impl Context { sessions: Default::default(), } } + + pub fn project_info(&self, id: Id) -> Result { + let storage = &self.profile.storage; + let repo = storage.repository(id)?; + let (_, head) = repo.head()?; + let Doc { payload, .. } = repo.project_of(self.profile.id())?; + let issues = (Issues::open(self.profile.public_key, &repo)?).count()?; + + Ok(project::Info { + payload, + head, + issues, + patches: 0, + id, + }) + } } pub fn router(ctx: Context) -> Router { @@ -115,3 +134,23 @@ pub struct PaginationQuery { pub page: Option, pub per_page: Option, } + +mod project { + use radicle::git::Oid; + use radicle::identity::project::Payload; + use radicle::identity::Id; + use serde::Serialize; + + /// Project info. + #[derive(Serialize)] + #[serde(rename_all = "camelCase")] + pub struct Info { + /// Project metadata. + #[serde(flatten)] + pub payload: Payload, + pub head: Oid, + pub patches: usize, + pub issues: usize, + pub id: Id, + } +} diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index b8c5e1d17..7100b1a5d 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -42,7 +42,19 @@ pub enum Error { /// Storage error. #[error(transparent)] - StorageError(#[from] radicle::storage::Error), + Storage(#[from] radicle::storage::Error), + + /// Cob store error. + #[error(transparent)] + CobStore(#[from] radicle::cob::store::Error), + + /// Git project error. + #[error(transparent)] + GitProject(#[from] radicle::storage::git::ProjectError), + + /// Surf error. + #[error(transparent)] + SurfCommit(#[from] radicle_surf::commit::Error), } impl Error { diff --git a/radicle-httpd/src/api/v1.rs b/radicle-httpd/src/api/v1.rs index b39bc609f..543d4dc4f 100644 --- a/radicle-httpd/src/api/v1.rs +++ b/radicle-httpd/src/api/v1.rs @@ -1,5 +1,6 @@ mod delegates; mod node; +mod projects; mod sessions; use axum::Router; @@ -10,7 +11,8 @@ pub fn router(ctx: Context) -> Router { let routes = Router::new() .merge(node::router(ctx.clone())) .merge(sessions::router(ctx.clone())) - .merge(delegates::router(ctx)); + .merge(delegates::router(ctx.clone())) + .merge(projects::router(ctx)); Router::new().nest("/v1", routes) } diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 05abb9d52..c28385b0b 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -1,31 +1,17 @@ use axum::response::IntoResponse; use axum::routing::get; use axum::{Extension, Json, Router}; -use serde::Serialize; -use radicle::cob::store::Store; -use radicle::git::Oid; -use radicle::identity::project::Payload; +use radicle::cob::issue::Issues; use radicle::identity::{Did, Doc}; use radicle::storage::{ReadRepository, WriteStorage}; use crate::api::axum_extra::{Path, Query}; use crate::api::error::Error; +use crate::api::project::Info; use crate::api::Context; use crate::api::PaginationQuery; -/// Project info. -#[derive(Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Info { - /// Project metadata. - #[serde(flatten)] - pub payload: Payload, - pub head: Oid, - pub patches: usize, - pub issues: usize, -} - pub fn router(ctx: Context) -> Router { Router::new() .route( @@ -58,15 +44,14 @@ async fn delegates_projects_handler( return None; } - let Ok(cobs) = Store::open(ctx.profile.public_key, &repo) else { return None }; - let Ok(issues) = cobs.issues().count() else { return None }; - let Ok(patches) = cobs.patches().count() else { return None }; + let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(issues) = (*issues).count() else { return None }; Some(Info { payload, head, issues, - patches, + patches: 0, id, }) }) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs new file mode 100644 index 000000000..1ab121253 --- /dev/null +++ b/radicle-httpd/src/api/v1/projects.rs @@ -0,0 +1,123 @@ +use axum::handler::Handler; +use axum::http::{header, HeaderValue}; +use axum::response::IntoResponse; +use axum::routing::get; +use axum::{Extension, Json, Router}; +use hyper::StatusCode; +use serde_json::json; +use tower_http::set_header::SetResponseHeaderLayer; + +use radicle::cob::issue::Issues; +use radicle::identity::{Doc, Id}; +use radicle::storage::{Oid, ReadRepository, WriteRepository, WriteStorage}; +use radicle_surf::git::History; + +use crate::api::axum_extra::{Path, Query}; +use crate::api::error::Error; +use crate::api::project::Info; +use crate::api::{Context, PaginationQuery}; + +const CACHE_1_HOUR: &str = "public, max-age=3600, must-revalidate"; + +pub fn router(ctx: Context) -> Router { + Router::new() + .route("/projects", get(project_root_handler)) + .route("/projects/:project", get(project_handler)) + .route("/projects/:project/commits/:sha", get(commit_handler)) + .route( + "/projects/:project/activity", + get( + activity_handler.layer(SetResponseHeaderLayer::if_not_present( + header::CACHE_CONTROL, + HeaderValue::from_static(CACHE_1_HOUR), + )), + ), + ) + .layer(Extension(ctx)) +} + +/// List all projects. +/// `GET /projects` +async fn project_root_handler( + Extension(ctx): Extension, + Query(qs): Query, +) -> impl IntoResponse { + 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 projects = storage + .projects()? + .into_iter() + .filter_map(|id| { + let Ok(repo) = storage.repository(id) else { return None }; + let Ok((_, head)) = repo.head() else { return None }; + let Ok(Doc { payload, .. }) = repo.project_of(ctx.profile.id()) else { return None }; + let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(issues) = (*issues).count() else { return None }; + + Some(Info { + payload, + head, + issues, + patches: 0, + id, + }) + }) + .skip(page * per_page) + .take(per_page) + .collect::>(); + + Ok::<_, Error>(Json(projects)) +} + +/// Get project metadata. +/// `GET /projects/:project` +async fn project_handler( + Extension(ctx): Extension, + Path(id): Path, +) -> impl IntoResponse { + let info = ctx.project_info(id)?; + + Ok::<_, Error>(Json(info)) +} + +/// Get project commit. +/// `GET /projects/:project/commits/:sha` +async fn commit_handler( + Extension(ctx): Extension, + Path((project, sha)): Path<(Id, Oid)>, +) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let commit = radicle_surf::commit(&repo.raw().into(), sha)?; + + Ok::<_, Error>(Json(json!(commit))) +} + +/// Get project activity for the past year. +/// `GET /projects/:project/activity` +async fn activity_handler( + Extension(ctx): Extension, + Path(project): Path, +) -> impl IntoResponse { + let current_date = chrono::Utc::now().timestamp(); + let one_year_ago = chrono::Duration::weeks(52); + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let (_, head) = repo.head()?; + let timestamps = History::new(repo.raw().into(), head) + .unwrap() + .filter_map(|a| { + if let Ok(a) = a { + let seconds = a.committer.time.seconds(); + if seconds > current_date - one_year_ago.num_seconds() { + return Some(seconds); + } + } + None + }) + .collect::>(); + + Ok::<_, Error>((StatusCode::OK, Json(json!({ "activity": timestamps })))) +} From 45751c56c6d94230802f32a73cf753a25aa79c11 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 22 Nov 2022 13:16:43 +0000 Subject: [PATCH 007/264] Migrate `/projects`: `history` and `tree` handlers Signed-off-by: xphoniex --- radicle-httpd/src/api/error.rs | 14 ++- radicle-httpd/src/api/v1/projects.rs | 147 ++++++++++++++++++++++++++- 2 files changed, 158 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 7100b1a5d..f3a96e705 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -52,9 +52,21 @@ pub enum Error { #[error(transparent)] GitProject(#[from] radicle::storage::git::ProjectError), - /// Surf error. + /// Surf commit error. #[error(transparent)] SurfCommit(#[from] radicle_surf::commit::Error), + + /// Surf object error. + #[error(transparent)] + SurfObject(#[from] radicle_surf::object::Error), + + /// Surf git error. + #[error(transparent)] + SurfGit(#[from] radicle_surf::git::Error), + + /// Git2 error. + #[error(transparent)] + Git2(#[from] radicle::git::raw::Error), } impl Error { diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 1ab121253..62f22a065 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1,13 +1,17 @@ +use std::collections::BTreeSet; + use axum::handler::Handler; use axum::http::{header, HeaderValue}; use axum::response::IntoResponse; use axum::routing::get; use axum::{Extension, Json, Router}; use hyper::StatusCode; +use serde::{Deserialize, Serialize}; use serde_json::json; use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::Issues; +use radicle::git::raw::BranchType; use radicle::identity::{Doc, Id}; use radicle::storage::{Oid, ReadRepository, WriteRepository, WriteStorage}; use radicle_surf::git::History; @@ -23,6 +27,7 @@ pub fn router(ctx: Context) -> Router { Router::new() .route("/projects", get(project_root_handler)) .route("/projects/:project", get(project_handler)) + .route("/projects/:project/commits", get(history_handler)) .route("/projects/:project/commits/:sha", get(commit_handler)) .route( "/projects/:project/activity", @@ -33,6 +38,7 @@ pub fn router(ctx: Context) -> Router { )), ), ) + .route("/projects/:project/tree/:sha/*path", get(tree_handler)) .layer(Extension(ctx)) } @@ -82,6 +88,91 @@ async fn project_handler( Ok::<_, Error>(Json(info)) } +#[derive(Serialize, Deserialize, Clone)] +#[serde(rename_all = "kebab-case")] +pub struct CommitsQueryString { + pub parent: Option, + pub since: Option, + pub until: Option, + pub page: Option, + pub per_page: Option, +} + +/// Get project commit range. +/// `GET /projects/:project/commits?since=` +async fn history_handler( + Extension(ctx): Extension, + Path(project): Path, + Query(qs): Query, +) -> impl IntoResponse { + let CommitsQueryString { + since, + until, + parent, + page, + per_page, + } = qs; + + let (sha, fallback_to_head) = match parent { + Some(commit) => (commit, false), + None => { + let info = ctx.project_info(project)?; + + (info.head.to_string(), true) + } + }; + + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + + // If a pagination is defined, we do not want to paginate the commits, and we return all of them on the first page. + let page = page.unwrap_or(0); + let per_page = if per_page.is_none() && (since.is_some() || until.is_some()) { + usize::MAX + } else { + per_page.unwrap_or(30) + }; + + let headers = History::new(repo.raw().into(), sha.as_str())? + .filter(|q| { + if let Ok(q) = q { + if let (Some(since), Some(until)) = (since, until) { + q.committer.time.seconds() >= since && q.committer.time.seconds() < until + } else if let Some(since) = since { + q.committer.time.seconds() >= since + } else if let Some(until) = until { + q.committer.time.seconds() < until + } else { + // If neither `since` nor `until` are specified, we include the commit. + true + } + } else { + false + } + }) + .skip(page * per_page) + .take(per_page) + .filter_map(|commit| { + if let Ok(commit) = commit { + radicle_surf::commit(&repo.raw().into(), commit.id).ok() + } else { + None + } + }) + .collect::>(); + + let response = json!({ + "headers": headers, + "stats": stats(&repo)?, + }); + + if fallback_to_head { + return Ok::<_, Error>((StatusCode::FOUND, Json(response))); + } + + Ok::<_, Error>((StatusCode::OK, Json(response))) +} + /// Get project commit. /// `GET /projects/:project/commits/:sha` async fn commit_handler( @@ -106,8 +197,7 @@ async fn activity_handler( let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let (_, head) = repo.head()?; - let timestamps = History::new(repo.raw().into(), head) - .unwrap() + let timestamps = History::new(repo.raw().into(), head)? .filter_map(|a| { if let Ok(a) = a { let seconds = a.committer.time.seconds(); @@ -121,3 +211,56 @@ async fn activity_handler( Ok::<_, Error>((StatusCode::OK, Json(json!({ "activity": timestamps })))) } + +/// Get project source tree. +/// `GET /projects/:project/tree/:sha/*path` +async fn tree_handler( + Extension(ctx): Extension, + Path((project, sha, path)): Path<(Id, Oid, String)>, +) -> impl IntoResponse { + let path = path.strip_prefix('/').ok_or(Error::NotFound)?.to_string(); + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let tree = radicle_surf::object::tree( + &repo.raw().into(), + Some(radicle_surf::Revision::Sha { sha }), + Some(path), + )?; + let response = json!({ + "path": &tree.path, + "entries": &tree.entries, + "info": &tree.info, + "stats": stats(&repo)?, + }); + + Ok::<_, Error>(Json(response)) +} + +#[derive(Serialize)] +struct Stats { + branches: usize, + commits: usize, + contributors: usize, +} + +fn stats(repo: &R) -> Result { + let branches = repo.raw().branches(Some(BranchType::Local))?.count(); + let (_, head) = repo.head()?; + let mut commits = 0; + let contributors = History::new(repo.raw().into(), head)? + .filter_map(|commit| { + commits += 1; + if let Ok(commit) = commit { + Some((commit.author.name, commit.author.email)) + } else { + None + } + }) + .collect::>(); + + Ok(Stats { + branches, + commits, + contributors: contributors.len(), + }) +} From 02ab60ac5542ebee13a14cc0faa29366d1616e07 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 22 Nov 2022 15:17:56 +0000 Subject: [PATCH 008/264] Migrate `/projects`: `remotes`, `remote`, `blob`, `readme` handlers Signed-off-by: xphoniex --- radicle-httpd/src/api/error.rs | 4 ++ radicle-httpd/src/api/v1/projects.rs | 85 ++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index f3a96e705..7c405af72 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -67,6 +67,10 @@ pub enum Error { /// Git2 error. #[error(transparent)] Git2(#[from] radicle::git::raw::Error), + + /// Storage refs error. + #[error(transparent)] + StorageRef(#[from] radicle::storage::refs::Error), } impl Error { diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 62f22a065..11665f4a7 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -13,8 +13,10 @@ use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::Issues; use radicle::git::raw::BranchType; use radicle::identity::{Doc, Id}; +use radicle::node::NodeId; use radicle::storage::{Oid, ReadRepository, WriteRepository, WriteStorage}; use radicle_surf::git::History; +use radicle_surf::Revision::Sha; use crate::api::axum_extra::{Path, Query}; use crate::api::error::Error; @@ -39,6 +41,10 @@ pub fn router(ctx: Context) -> Router { ), ) .route("/projects/:project/tree/:sha/*path", get(tree_handler)) + .route("/projects/:project/remotes", get(remotes_handler)) + .route("/projects/:project/remotes/:peer", get(remote_handler)) + .route("/projects/:project/blob/:sha/*path", get(blob_handler)) + .route("/projects/:project/readme/:sha", get(readme_handler)) .layer(Extension(ctx)) } @@ -183,7 +189,7 @@ async fn commit_handler( let repo = storage.repository(project)?; let commit = radicle_surf::commit(&repo.raw().into(), sha)?; - Ok::<_, Error>(Json(json!(commit))) + Ok::<_, Error>(Json(commit)) } /// Get project activity for the past year. @@ -221,11 +227,7 @@ async fn tree_handler( let path = path.strip_prefix('/').ok_or(Error::NotFound)?.to_string(); let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let tree = radicle_surf::object::tree( - &repo.raw().into(), - Some(radicle_surf::Revision::Sha { sha }), - Some(path), - )?; + let tree = radicle_surf::object::tree(&repo.raw().into(), Some(Sha { sha }), Some(path))?; let response = json!({ "path": &tree.path, "entries": &tree.entries, @@ -236,6 +238,77 @@ async fn tree_handler( Ok::<_, Error>(Json(response)) } +/// Get all project remotes. +/// `GET /projects/:project/remotes` +async fn remotes_handler( + Extension(ctx): Extension, + Path(project): Path, +) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let remotes = repo + .remotes()? + .filter_map(|r| r.map(|r| r.1).ok()) + .collect::>(); + + Ok::<_, Error>(Json(remotes)) +} + +/// Get project remote. +/// `GET /projects/:project/remotes/:peer` +async fn remote_handler( + Extension(ctx): Extension, + Path((project, node_id)): Path<(Id, NodeId)>, +) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let remote = repo.remote(&node_id)?; + + Ok::<_, Error>(Json(remote)) +} + +/// Get project source file. +/// `GET /projects/:project/blob/:sha/*path` +async fn blob_handler( + Extension(ctx): Extension, + Path((project, sha, path)): Path<(Id, Oid, String)>, +) -> impl IntoResponse { + let path = path.strip_prefix('/').ok_or(Error::NotFound)?; + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let blob = radicle_surf::blob::blob(&repo.raw().into(), Some(Sha { sha }), path)?; + + Ok::<_, Error>(Json(blob)) +} + +/// Get project readme. +/// `GET /projects/:project/readme/:sha` +async fn readme_handler( + Extension(ctx): Extension, + Path((project, sha)): Path<(Id, Oid)>, +) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let paths = &[ + "README", + "README.md", + "README.markdown", + "README.txt", + "README.rst", + "Readme.md", + ]; + + for path in paths { + if let Ok(blob) = radicle_surf::blob::blob(&repo.raw().into(), Some(Sha { sha }), path) { + return Ok::<_, Error>(Json(blob)); + } + } + + Err(radicle_surf::object::Error::PathNotFound( + radicle_surf::file_system::Path::try_from("README").unwrap(), + ))? +} + #[derive(Serialize)] struct Stats { branches: usize, From 3b50828f41a19bbbd688d28ca485d3ebd8208ec7 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Wed, 23 Nov 2022 05:57:12 +0000 Subject: [PATCH 009/264] Migrate `/projects`: `issues`, `issue` handlers Signed-off-by: xphoniex --- radicle-httpd/src/api/v1/projects.rs | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 11665f4a7..04b4cd210 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -45,6 +45,8 @@ pub fn router(ctx: Context) -> Router { .route("/projects/:project/remotes/:peer", get(remote_handler)) .route("/projects/:project/blob/:sha/*path", get(blob_handler)) .route("/projects/:project/readme/:sha", get(readme_handler)) + .route("/projects/:project/issues", get(issues_handler)) + .route("/projects/:project/issues/:id", get(issue_handler)) .layer(Extension(ctx)) } @@ -309,6 +311,63 @@ async fn readme_handler( ))? } +/// Get project issues list. +/// `GET /projects/:project/issues` +async fn issues_handler( + Extension(ctx): Extension, + Path(project): Path, + Query(qs): Query, +) -> impl IntoResponse { + 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 repo = storage.repository(project)?; + let issues = Issues::open(ctx.profile.public_key, &repo)?; + let issues = issues + .all()? + .into_iter() + .filter_map(|r| r.ok()) + .map(|(id, issue)| { + json!({ + "id": id, + "author": issue.author(), + "title": issue.title(), + "status": issue.status(), + "discussion": issue.comments().collect::>(), + "tags": issue.tags().collect::>(), + }) + }) + .skip(page * per_page) + .take(per_page) + .collect::>(); + + Ok::<_, Error>(Json(issues)) +} + +/// Get project issue. +/// `GET /projects/:project/issues/:id` +async fn issue_handler( + Extension(ctx): Extension, + Path((project, issue_id)): Path<(Id, Oid)>, +) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let issue = Issues::open(ctx.profile.public_key, &repo)? + .get(&issue_id.into())? + .ok_or(Error::NotFound)?; + let issue = json!({ + "id": issue_id, + "author": issue.author(), + "title": issue.title(), + "status": issue.status(), + "discussion": issue.comments().collect::>(), + "tags": issue.tags().collect::>(), + }); + + Ok::<_, Error>(Json(issue)) +} + #[derive(Serialize)] struct Stats { branches: usize, From 591b86aee5c1ab4c3d481f0d717ce389297bc1bd Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 24 Nov 2022 12:42:21 +0000 Subject: [PATCH 010/264] Migrate `/v1/stats` route Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 5 +++++ radicle-httpd/src/api/v1.rs | 4 +++- radicle-httpd/src/api/v1/stats.rs | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 radicle-httpd/src/api/v1/stats.rs diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 8650460e3..b06dd8cd7 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -121,6 +121,11 @@ async fn root_handler(Extension(ctx): Extension) -> impl IntoResponse { "href": "/v1/delegates/:did/projects", "rel": "projects", "type": "GET" + }, + { + "href": "/v1/stats", + "rel": "stats", + "type": "GET" } ] }); diff --git a/radicle-httpd/src/api/v1.rs b/radicle-httpd/src/api/v1.rs index 543d4dc4f..97829cdf3 100644 --- a/radicle-httpd/src/api/v1.rs +++ b/radicle-httpd/src/api/v1.rs @@ -2,6 +2,7 @@ mod delegates; mod node; mod projects; mod sessions; +mod stats; use axum::Router; @@ -12,7 +13,8 @@ pub fn router(ctx: Context) -> Router { .merge(node::router(ctx.clone())) .merge(sessions::router(ctx.clone())) .merge(delegates::router(ctx.clone())) - .merge(projects::router(ctx)); + .merge(projects::router(ctx.clone())) + .merge(stats::router(ctx)); Router::new().nest("/v1", routes) } diff --git a/radicle-httpd/src/api/v1/stats.rs b/radicle-httpd/src/api/v1/stats.rs new file mode 100644 index 000000000..fdb512780 --- /dev/null +++ b/radicle-httpd/src/api/v1/stats.rs @@ -0,0 +1,24 @@ +use axum::response::IntoResponse; +use axum::routing::get; +use axum::{Extension, Json, Router}; +use serde_json::json; + +use crate::api::error::Error; +use crate::api::Context; + +pub fn router(ctx: Context) -> Router { + Router::new() + .route("/stats", get(stats_handler)) + .layer(Extension(ctx)) +} + +/// Return the stats for the node. +/// `GET /stats` +async fn stats_handler(Extension(ctx): Extension) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let projects = storage.projects()?.len(); + + Ok::<_, Error>(Json( + json!({ "projects": { "count": projects }, "users": { "count": 0 } }), + )) +} From da4287258b132b17a476622ab5efbbdab0b6005d Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 29 Nov 2022 16:49:18 +0000 Subject: [PATCH 011/264] httpd: implement `IntoResponse` for api error variants Signed-off-by: xphoniex --- radicle-httpd/src/api/error.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 7c405af72..84cb12459 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -1,5 +1,7 @@ -use axum::http; +use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; +use axum::Json; +use serde_json::json; /// Errors relating to the HTTP backend. #[derive(Debug, thiserror::Error)] @@ -73,16 +75,29 @@ pub enum Error { StorageRef(#[from] radicle::storage::refs::Error), } -impl Error { - pub fn status(&self) -> http::StatusCode { - http::StatusCode::INTERNAL_SERVER_ERROR - } -} - impl IntoResponse for Error { fn into_response(self) -> Response { - tracing::error!("{}", self); + let (status, msg) = match &self { + Error::NotFound => (StatusCode::NOT_FOUND, None), + Error::Auth(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), + Error::SiweParse(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), + Error::SiweVerification(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), + Error::Git2(e) => ( + StatusCode::INTERNAL_SERVER_ERROR, + Some(e.message().to_owned()), + ), + _ => { + tracing::error!("Error: {:?}", &self); - self.status().into_response() + (StatusCode::INTERNAL_SERVER_ERROR, None) + } + }; + + let body = Json(json!({ + "error": msg.or_else(|| status.canonical_reason().map(|r| r.to_string())), + "code": status.as_u16() + })); + + (status, body).into_response() } } From b22c152ed98b99f54442f5085ac0a0d0ef2ff87f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Sun, 27 Nov 2022 12:54:08 +0100 Subject: [PATCH 012/264] cob: Move clock out of `Issue` The clock shouldn't be part of the issue state, but rather the local "actor" state. Signed-off-by: Alexis Sellier --- radicle-httpd/src/api/v1/projects.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 04b4cd210..6edcb001f 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -328,7 +328,7 @@ async fn issues_handler( .all()? .into_iter() .filter_map(|r| r.ok()) - .map(|(id, issue)| { + .map(|(id, issue, _)| { json!({ "id": id, "author": issue.author(), From 864f0022e24d03fa1d716aef4fe40a9efe9e4e76 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 7 Dec 2022 12:16:00 +0100 Subject: [PATCH 013/264] Remove delegate names from identity doc We are working on other mechanisms to help identify keys, and this is just redundant. Also update some of the CLI examples to create a canonical example for documentation. Signed-off-by: Alexis Sellier --- radicle-httpd/src/api/v1/delegates.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index c28385b0b..89364523e 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -40,7 +40,7 @@ async fn delegates_projects_handler( let Ok((_, head)) = repo.head() else { return None }; let Ok(Doc { payload, delegates, .. }) = repo.project_of(ctx.profile.id()) else { return None }; - if !delegates.iter().any(|d| d.id == delegate) { + if !delegates.iter().any(|d| *d == delegate) { return None; } From 3f4aecfadf254c92fcfcd3ef31c1fcca9acb529b Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 9 Dec 2022 10:44:59 +0100 Subject: [PATCH 014/264] Rename patch/issue "status" This reverts to the previous naming, since after serialization we would otherwise end up with `{ status: { status: closed } }`. Signed-off-by: Alexis Sellier --- radicle-httpd/src/api/v1/projects.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 6edcb001f..69fdc0bd2 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -333,7 +333,7 @@ async fn issues_handler( "id": id, "author": issue.author(), "title": issue.title(), - "status": issue.status(), + "status": issue.state(), "discussion": issue.comments().collect::>(), "tags": issue.tags().collect::>(), }) @@ -360,7 +360,7 @@ async fn issue_handler( "id": issue_id, "author": issue.author(), "title": issue.title(), - "status": issue.status(), + "status": issue.state(), "discussion": issue.comments().collect::>(), "tags": issue.tags().collect::>(), }); From a40d53743dab8133d4ede6a5573e3f6e6f388c17 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 8 Dec 2022 19:13:07 +0000 Subject: [PATCH 015/264] httpd: Conform response from issue handlers to previous model Signed-off-by: xphoniex --- radicle-httpd/src/api/v1/projects.rs | 50 ++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 69fdc0bd2..5f351946d 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -11,8 +11,10 @@ use serde_json::json; use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::Issues; +use radicle::cob::thread::{self, CommentId}; +use radicle::cob::Timestamp; use radicle::git::raw::BranchType; -use radicle::identity::{Doc, Id}; +use radicle::identity::{Doc, Id, PublicKey}; use radicle::node::NodeId; use radicle::storage::{Oid, ReadRepository, WriteRepository, WriteStorage}; use radicle_surf::git::History; @@ -330,11 +332,11 @@ async fn issues_handler( .filter_map(|r| r.ok()) .map(|(id, issue, _)| { json!({ - "id": id, + "id": id.to_string(), "author": issue.author(), "title": issue.title(), - "status": issue.state(), - "discussion": issue.comments().collect::>(), + "state": issue.state(), + "discussion": issue.comments().collect::(), "tags": issue.tags().collect::>(), }) }) @@ -360,14 +362,50 @@ async fn issue_handler( "id": issue_id, "author": issue.author(), "title": issue.title(), - "status": issue.state(), - "discussion": issue.comments().collect::>(), + "state": issue.state(), + "discussion": issue.comments().collect::(), "tags": issue.tags().collect::>(), }); Ok::<_, Error>(Json(issue)) } +#[derive(Serialize)] +struct Author { + id: PublicKey, +} + +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct Comment { + author: Author, + body: String, + reactions: [String; 0], + timestamp: Timestamp, + reply_to: Option, +} + +#[derive(Serialize)] +struct Comments(Vec); + +impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { + fn from_iter>(iter: I) -> Self { + let mut comments = Vec::new(); + + for (comment_id, comment) in iter { + comments.push(Comment { + author: Author { id: comment_id.1 }, + body: comment.body.to_owned(), + reactions: [], + timestamp: comment.timestamp, + reply_to: comment.reply_to, + }); + } + + Comments(comments) + } +} + #[derive(Serialize)] struct Stats { branches: usize, From 0b138f2c0c0720557b469b83dd594b0a72cc51b2 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 8 Dec 2022 19:33:25 +0000 Subject: [PATCH 016/264] httpd: Use `HashSet` instead of `BTreeSet` Signed-off-by: xphoniex --- radicle-httpd/src/api/v1/projects.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 5f351946d..c8b24f9dc 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeSet; +use std::collections::HashSet; use axum::handler::Handler; use axum::http::{header, HeaderValue}; @@ -426,7 +426,7 @@ fn stats(repo: &R) -> Result { None } }) - .collect::>(); + .collect::>(); Ok(Stats { branches, From cc942024ce6a7fa4436e0e37cebf328908ccf400 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 8 Dec 2022 15:38:09 +0100 Subject: [PATCH 017/264] Make `Doc` type payload generic --- radicle-httpd/src/api.rs | 8 ++++---- radicle-httpd/src/api/v1/delegates.rs | 7 ++++--- radicle-httpd/src/api/v1/projects.rs | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index b06dd8cd7..f87142d85 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -17,7 +17,7 @@ use tower_http::trace::TraceLayer; use tracing::Span; use radicle::cob::issue::Issues; -use radicle::identity::{Doc, Id}; +use radicle::identity::Id; use radicle::storage::{ReadRepository, WriteStorage}; use radicle::Profile; @@ -49,7 +49,7 @@ impl Context { let storage = &self.profile.storage; let repo = storage.repository(id)?; let (_, head) = repo.head()?; - let Doc { payload, .. } = repo.project_of(self.profile.id())?; + let payload = repo.project_of(self.profile.id())?; let issues = (Issues::open(self.profile.public_key, &repo)?).count()?; Ok(project::Info { @@ -142,7 +142,7 @@ pub struct PaginationQuery { mod project { use radicle::git::Oid; - use radicle::identity::project::Payload; + use radicle::identity::project::Project; use radicle::identity::Id; use serde::Serialize; @@ -152,7 +152,7 @@ mod project { pub struct Info { /// Project metadata. #[serde(flatten)] - pub payload: Payload, + pub payload: Project, pub head: Oid, pub patches: usize, pub issues: usize, diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 89364523e..d578504d2 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -3,7 +3,7 @@ use axum::routing::get; use axum::{Extension, Json, Router}; use radicle::cob::issue::Issues; -use radicle::identity::{Did, Doc}; +use radicle::identity::Did; use radicle::storage::{ReadRepository, WriteStorage}; use crate::api::axum_extra::{Path, Query}; @@ -38,9 +38,10 @@ async fn delegates_projects_handler( .filter_map(|id| { let Ok(repo) = storage.repository(id) else { return None }; let Ok((_, head)) = repo.head() else { return None }; - let Ok(Doc { payload, delegates, .. }) = repo.project_of(ctx.profile.id()) else { return None }; + let Ok(doc) = repo.identity_of(ctx.profile.id()) else { return None }; + let Ok(payload) = doc.project() else { return None }; - if !delegates.iter().any(|d| *d == delegate) { + if !doc.delegates.iter().any(|d| *d == delegate) { return None; } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index c8b24f9dc..f81410176 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -14,7 +14,7 @@ use radicle::cob::issue::Issues; use radicle::cob::thread::{self, CommentId}; use radicle::cob::Timestamp; use radicle::git::raw::BranchType; -use radicle::identity::{Doc, Id, PublicKey}; +use radicle::identity::{Id, PublicKey}; use radicle::node::NodeId; use radicle::storage::{Oid, ReadRepository, WriteRepository, WriteStorage}; use radicle_surf::git::History; @@ -68,7 +68,7 @@ async fn project_root_handler( .filter_map(|id| { let Ok(repo) = storage.repository(id) else { return None }; let Ok((_, head)) = repo.head() else { return None }; - let Ok(Doc { payload, .. }) = repo.project_of(ctx.profile.id()) else { return None }; + let Ok(payload) = repo.project_of(ctx.profile.id()) else { return None }; let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; let Ok(issues) = (*issues).count() else { return None }; From 2bd0a6f68de39913656a8ae673c1a8d495c7944a Mon Sep 17 00:00:00 2001 From: Adam Szkoda Date: Thu, 8 Dec 2022 14:54:51 +0100 Subject: [PATCH 018/264] Dockerize services Signed-off-by: Adam Szkoda --- radicle-httpd/Dockerfile | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 radicle-httpd/Dockerfile diff --git a/radicle-httpd/Dockerfile b/radicle-httpd/Dockerfile new file mode 100644 index 000000000..2a3c1fb62 --- /dev/null +++ b/radicle-httpd/Dockerfile @@ -0,0 +1,22 @@ +# Build +FROM rust:1.65.0-slim@sha256:27349440d16ac15f52e8cae6181999a1b2fc05272d612a358d0e53078fcef88e as build + +RUN apt-get update && apt-get install -y pkg-config libssl-dev git cmake + +COPY . /app +WORKDIR /app/radicle-httpd + +RUN set -eux; \ + cargo install --locked --path .; \ + objcopy --compress-debug-sections /usr/local/cargo/bin/radicle-httpd /usr/local/cargo/bin/radicle-httpd.compressed + +# Run +FROM debian:bullseye-slim@sha256:25f10b4f1ded5341a3ca0a30290ff3cd5639415f0c5a2222d5e7d5dd72952aa1 + +RUN echo deb http://deb.debian.org/debian bullseye-backports main contrib non-free >/etc/apt/sources.list.d/backports.list +RUN apt-get update && apt-get install -y libssl1.1 && apt -t bullseye-backports install --yes git && rm -rf /var/lib/apt/lists/* +COPY --from=build /usr/local/cargo/bin/radicle-httpd.compressed /usr/local/bin/radicle-httpd + +WORKDIR /app + +ENTRYPOINT ["/usr/local/bin/radicle-httpd"] From 821bcf1ce89b7066fbeb4caaf2243d1486c63224 Mon Sep 17 00:00:00 2001 From: Adam Szkoda Date: Fri, 9 Dec 2022 14:23:01 +0100 Subject: [PATCH 019/264] Put fully static binaries into containers --- radicle-httpd/Dockerfile | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/radicle-httpd/Dockerfile b/radicle-httpd/Dockerfile index 2a3c1fb62..ce9fea022 100644 --- a/radicle-httpd/Dockerfile +++ b/radicle-httpd/Dockerfile @@ -1,21 +1,20 @@ # Build -FROM rust:1.65.0-slim@sha256:27349440d16ac15f52e8cae6181999a1b2fc05272d612a358d0e53078fcef88e as build +FROM registry.gitlab.com/rust_musl_docker/image:stable-1.65.0@sha256:f95635740757e0e9a29b51d5060a7564e900cb5192defdd3a5b2ba5b47ce17b3 as build -RUN apt-get update && apt-get install -y pkg-config libssl-dev git cmake +COPY . /workdir -COPY . /app -WORKDIR /app/radicle-httpd +RUN rustup target add x86_64-unknown-linux-musl -RUN set -eux; \ - cargo install --locked --path .; \ - objcopy --compress-debug-sections /usr/local/cargo/bin/radicle-httpd /usr/local/cargo/bin/radicle-httpd.compressed +WORKDIR /workdir/radicle-httpd +RUN RUSTFLAGS=-Cforce-frame-pointers=yes cargo build --profile=container --target=x86_64-unknown-linux-musl +RUN objcopy --compress-debug-sections /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd.compressed # Run FROM debian:bullseye-slim@sha256:25f10b4f1ded5341a3ca0a30290ff3cd5639415f0c5a2222d5e7d5dd72952aa1 RUN echo deb http://deb.debian.org/debian bullseye-backports main contrib non-free >/etc/apt/sources.list.d/backports.list RUN apt-get update && apt-get install -y libssl1.1 && apt -t bullseye-backports install --yes git && rm -rf /var/lib/apt/lists/* -COPY --from=build /usr/local/cargo/bin/radicle-httpd.compressed /usr/local/bin/radicle-httpd +COPY --from=build /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd.compressed /usr/local/bin/radicle-httpd WORKDIR /app From ec4a766bee8121d1149f96d5e34cf340ff3e083e Mon Sep 17 00:00:00 2001 From: Adam Szkoda Date: Mon, 12 Dec 2022 11:25:17 +0100 Subject: [PATCH 020/264] Do not install OpenSSL as it's statically linked --- radicle-httpd/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/Dockerfile b/radicle-httpd/Dockerfile index ce9fea022..73abe07c6 100644 --- a/radicle-httpd/Dockerfile +++ b/radicle-httpd/Dockerfile @@ -13,7 +13,7 @@ RUN objcopy --compress-debug-sections /workdir/target/x86_64-unknown-linux-musl/ FROM debian:bullseye-slim@sha256:25f10b4f1ded5341a3ca0a30290ff3cd5639415f0c5a2222d5e7d5dd72952aa1 RUN echo deb http://deb.debian.org/debian bullseye-backports main contrib non-free >/etc/apt/sources.list.d/backports.list -RUN apt-get update && apt-get install -y libssl1.1 && apt -t bullseye-backports install --yes git && rm -rf /var/lib/apt/lists/* +RUN apt-get update && apt -t bullseye-backports install --yes git && rm -rf /var/lib/apt/lists/* COPY --from=build /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd.compressed /usr/local/bin/radicle-httpd WORKDIR /app From 3d9489373701dd5977e5a5a6cf701e11820d6678 Mon Sep 17 00:00:00 2001 From: Adam Szkoda Date: Mon, 12 Dec 2022 12:58:24 +0100 Subject: [PATCH 021/264] Build container images for deployment Signed-off-by: Adam Szkoda --- radicle-httpd/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/Dockerfile b/radicle-httpd/Dockerfile index 73abe07c6..e87728cbd 100644 --- a/radicle-httpd/Dockerfile +++ b/radicle-httpd/Dockerfile @@ -18,4 +18,4 @@ COPY --from=build /workdir/target/x86_64-unknown-linux-musl/container/radicle-ht WORKDIR /app -ENTRYPOINT ["/usr/local/bin/radicle-httpd"] +ENTRYPOINT ["/usr/local/bin/radicle-httpd", "--listen", "0.0.0.0:8080"] From 10d450c751d64d2c1b89ff5ab0bdf778a1aa6a55 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 15 Dec 2022 09:43:48 +0100 Subject: [PATCH 022/264] cob: Implement comment edit action This implementation intentionally preserves all edits such that they may be displayed in an application as an edit history. Signed-off-by: Alexis Sellier --- radicle-httpd/src/api/v1/projects.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index f81410176..7de08df18 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -395,10 +395,10 @@ impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { for (comment_id, comment) in iter { comments.push(Comment { author: Author { id: comment_id.1 }, - body: comment.body.to_owned(), + body: comment.body().to_owned(), reactions: [], - timestamp: comment.timestamp, - reply_to: comment.reply_to, + timestamp: comment.timestamp(), + reply_to: comment.reply_to(), }); } From 8e688fef341d3126c10a52b46f236753562e921c Mon Sep 17 00:00:00 2001 From: xphoniex Date: Wed, 21 Dec 2022 15:24:22 +0000 Subject: [PATCH 023/264] Update `radicle-git` to `79a94721366490053e2d8ac1c1afa14fb0c25f09` Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 4 +- radicle-httpd/src/api/error.rs | 20 +----- radicle-httpd/src/api/v1/projects.rs | 97 ++++++++++++++++++---------- 3 files changed, 68 insertions(+), 53 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index bfd912590..32a9a9f51 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -41,5 +41,5 @@ version = "0.2.0" [dependencies.radicle-surf] git = "https://github.com/radicle-dev/radicle-git" -features = ["serialize"] -rev = "d3115a22158c8395705babefdc89049f7510d32d" +features = ["serde"] +rev = "79a94721366490053e2d8ac1c1afa14fb0c25f09" diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 84cb12459..d969fc5b2 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -18,22 +18,6 @@ pub enum Error { #[error(transparent)] Env(#[from] std::env::VarError), - /// I/O error. - #[error("i/o error: {0}")] - Io(#[from] std::io::Error), - - /// Invalid identifier. - #[error("invalid radicle identifier: {0}")] - Id(#[from] radicle::identity::IdError), - - /// HeaderName error. - #[error(transparent)] - InvalidHeaderName(#[from] axum::http::header::InvalidHeaderName), - - /// HeaderValue error. - #[error(transparent)] - InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue), - /// An error occurred while verifying the siwe message. #[error(transparent)] SiweVerification(#[from] siwe::VerificationError), @@ -56,11 +40,11 @@ pub enum Error { /// Surf commit error. #[error(transparent)] - SurfCommit(#[from] radicle_surf::commit::Error), + SurfCommit(#[from] radicle_surf::source::commit::Error), /// Surf object error. #[error(transparent)] - SurfObject(#[from] radicle_surf::object::Error), + SurfObject(#[from] radicle_surf::source::object::Error), /// Surf git error. #[error(transparent)] diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 7de08df18..0518e62a2 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::{BTreeMap, HashSet}; use axum::handler::Handler; use axum::http::{header, HeaderValue}; @@ -13,12 +13,15 @@ use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::Issues; use radicle::cob::thread::{self, CommentId}; use radicle::cob::Timestamp; -use radicle::git::raw::BranchType; +use radicle::git::Oid; use radicle::identity::{Id, PublicKey}; use radicle::node::NodeId; -use radicle::storage::{Oid, ReadRepository, WriteRepository, WriteStorage}; -use radicle_surf::git::History; -use radicle_surf::Revision::Sha; +use radicle::storage::git::paths; +use radicle::storage::{ReadRepository, WriteStorage}; +use radicle_surf::git::{Glob, History, Repository}; +use radicle_surf::source; +use radicle_surf::source::object::blob::Blob; +use radicle_surf::source::object::tree::Tree; use crate::api::axum_extra::{Path, Query}; use crate::api::error::Error; @@ -133,7 +136,7 @@ async fn history_handler( }; let storage = &ctx.profile.storage; - let repo = storage.repository(project)?; + let repo = Repository::open(paths::repository(storage, &project))?; // If a pagination is defined, we do not want to paginate the commits, and we return all of them on the first page. let page = page.unwrap_or(0); @@ -143,7 +146,8 @@ async fn history_handler( per_page.unwrap_or(30) }; - let headers = History::new(repo.raw().into(), sha.as_str())? + let headers = repo + .history(sha)? .filter(|q| { if let Ok(q) = q { if let (Some(since), Some(until)) = (since, until) { @@ -164,7 +168,7 @@ async fn history_handler( .take(per_page) .filter_map(|commit| { if let Ok(commit) = commit { - radicle_surf::commit(&repo.raw().into(), commit.id).ok() + source::commit(&repo, commit.id).ok() } else { None } @@ -173,7 +177,7 @@ async fn history_handler( let response = json!({ "headers": headers, - "stats": stats(&repo)?, + "stats": repo.stats()?, }); if fallback_to_head { @@ -190,8 +194,8 @@ async fn commit_handler( Path((project, sha)): Path<(Id, Oid)>, ) -> impl IntoResponse { let storage = &ctx.profile.storage; - let repo = storage.repository(project)?; - let commit = radicle_surf::commit(&repo.raw().into(), sha)?; + let repo = Repository::open(paths::repository(storage, &project))?; + let commit = source::commit(&repo, sha)?; Ok::<_, Error>(Json(commit)) } @@ -205,9 +209,10 @@ async fn activity_handler( let current_date = chrono::Utc::now().timestamp(); let one_year_ago = chrono::Duration::weeks(52); let storage = &ctx.profile.storage; - let repo = storage.repository(project)?; - let (_, head) = repo.head()?; - let timestamps = History::new(repo.raw().into(), head)? + let repo = Repository::open(paths::repository(storage, &project))?; + let head = repo.head()?; + let timestamps = repo + .history(head)? .filter_map(|a| { if let Ok(a) = a { let seconds = a.committer.time.seconds(); @@ -230,14 +235,11 @@ async fn tree_handler( ) -> impl IntoResponse { let path = path.strip_prefix('/').ok_or(Error::NotFound)?.to_string(); let storage = &ctx.profile.storage; - let repo = storage.repository(project)?; - let tree = radicle_surf::object::tree(&repo.raw().into(), Some(Sha { sha }), Some(path))?; - let response = json!({ - "path": &tree.path, - "entries": &tree.entries, - "info": &tree.info, - "stats": stats(&repo)?, - }); + let repo = Repository::open(paths::repository(storage, &project))?; + let path = if path.is_empty() { None } else { Some(&path) }; + let tree = Tree::new(&repo, &sha, path)?; + let mut response = json!(tree); + response["stats"] = json!(stats(&repo, sha)?); Ok::<_, Error>(Json(response)) } @@ -253,6 +255,23 @@ async fn remotes_handler( let remotes = repo .remotes()? .filter_map(|r| r.map(|r| r.1).ok()) + .map(|remote| { + let refs = remote + .refs + .iter() + .filter_map(|(r, oid)| { + r.as_str() + .strip_prefix("refs/heads/") + .map(|head| (head.to_string(), oid)) + }) + .collect::>(); + + json!({ + "id": remote.id, + "heads": refs, + "delegate": remote.delegate, + }) + }) .collect::>(); Ok::<_, Error>(Json(remotes)) @@ -267,6 +286,20 @@ async fn remote_handler( let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let remote = repo.remote(&node_id)?; + let refs = remote + .refs + .iter() + .filter_map(|(r, oid)| { + r.as_str() + .strip_prefix("refs/heads/") + .map(|head| (head.to_string(), oid)) + }) + .collect::>(); + let remote = json!({ + "id": remote.id, + "heads": refs, + "delegate": remote.delegate, + }); Ok::<_, Error>(Json(remote)) } @@ -279,8 +312,8 @@ async fn blob_handler( ) -> impl IntoResponse { let path = path.strip_prefix('/').ok_or(Error::NotFound)?; let storage = &ctx.profile.storage; - let repo = storage.repository(project)?; - let blob = radicle_surf::blob::blob(&repo.raw().into(), Some(Sha { sha }), path)?; + let repo = Repository::open(paths::repository(storage, &project))?; + let blob = Blob::new(&repo, &sha, &path)?; Ok::<_, Error>(Json(blob)) } @@ -292,7 +325,7 @@ async fn readme_handler( Path((project, sha)): Path<(Id, Oid)>, ) -> impl IntoResponse { let storage = &ctx.profile.storage; - let repo = storage.repository(project)?; + let repo = Repository::open(paths::repository(storage, &project))?; let paths = &[ "README", "README.md", @@ -303,14 +336,12 @@ async fn readme_handler( ]; for path in paths { - if let Ok(blob) = radicle_surf::blob::blob(&repo.raw().into(), Some(Sha { sha }), path) { + if let Ok(blob) = Blob::new(&repo, &sha, &path) { return Ok::<_, Error>(Json(blob)); } } - Err(radicle_surf::object::Error::PathNotFound( - radicle_surf::file_system::Path::try_from("README").unwrap(), - ))? + Err(Error::NotFound) } /// Get project issues list. @@ -413,11 +444,11 @@ struct Stats { contributors: usize, } -fn stats(repo: &R) -> Result { - let branches = repo.raw().branches(Some(BranchType::Local))?.count(); - let (_, head) = repo.head()?; +fn stats(repo: &Repository, head: Oid) -> Result { + let branches = repo.branches(Glob::all_heads())?.count(); + let mut commits = 0; - let contributors = History::new(repo.raw().into(), head)? + let contributors = History::new(repo, head)? .filter_map(|commit| { commits += 1; if let Ok(commit) = commit { From a93803c8ca169cbc986ffa9eac8c83aa07df51db Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 26 Dec 2022 12:02:12 +0000 Subject: [PATCH 024/264] httpd: Use `TraceLayer` on uppermost router This allows prefix of each route e.g. `/api/*` to be visible in logs. Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 24 ------------------------ radicle-httpd/src/lib.rs | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index f87142d85..93ff3004a 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -2,19 +2,15 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use axum::body::{Body, BoxBody}; use axum::http::header::{AUTHORIZATION, CONTENT_TYPE}; use axum::http::Method; use axum::response::{IntoResponse, Json}; use axum::routing::get; use axum::{Extension, Router}; -use hyper::http::{Request, Response}; use serde::{Deserialize, Serialize}; use serde_json::json; use tokio::sync::RwLock; use tower_http::cors::{self, CorsLayer}; -use tower_http::trace::TraceLayer; -use tracing::Span; use radicle::cob::issue::Issues; use radicle::identity::Id; @@ -77,26 +73,6 @@ pub fn router(ctx: Context) -> Router { .allow_methods([Method::GET, Method::POST, Method::PUT]) .allow_headers([CONTENT_TYPE, AUTHORIZATION]), ) - .layer( - TraceLayer::new_for_http() - .make_span_with(|request: &Request| { - tracing::info_span!( - "request", - method = %request.method(), - uri = %request.uri(), - status = tracing::field::Empty, - latency = tracing::field::Empty, - ) - }) - .on_response( - |response: &Response, latency: Duration, span: &Span| { - span.record("status", &tracing::field::debug(response.status())); - span.record("latency", &tracing::field::debug(latency)); - - tracing::info!("Processed"); - }, - ), - ) } async fn root_handler(Extension(ctx): Extension) -> impl IntoResponse { diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 065e7f459..b89c4d910 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -55,7 +55,16 @@ pub async fn run(options: Options) -> anyhow::Result<()> { let git_router = Router::new() .route("/:project/*request", any(git_handler)) - .layer(Extension(profile.clone())) + .layer(Extension(profile.clone())); + + let ctx = api::Context::new(profile); + let api_router = api::router(ctx); + + tracing::info!("listening on http://{}", options.listen); + + let app = Router::new() + .merge(git_router) + .nest("/api", api_router) .layer( TraceLayer::new_for_http() .make_span_with(|request: &Request| { @@ -75,16 +84,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> { tracing::info!("Processed"); }, ), - ); - - let ctx = api::Context::new(profile); - let api_router = api::router(ctx); - - tracing::info!("listening on http://{}", options.listen); - - let app = Router::new() - .merge(git_router) - .nest("/api", api_router) + ) .into_make_service_with_connect_info::(); axum::Server::bind(&options.listen) From 134bb7a847e3afcadf339bad521346c657d57c85 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 30 Dec 2022 13:30:35 +0100 Subject: [PATCH 025/264] cob: Create proper `OpId` type To still access a comment's author, we add a new `author` field. --- radicle-httpd/src/api/v1/projects.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 0518e62a2..1e7de595d 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -423,9 +423,11 @@ impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { fn from_iter>(iter: I) -> Self { let mut comments = Vec::new(); - for (comment_id, comment) in iter { + for (_, comment) in iter { comments.push(Comment { - author: Author { id: comment_id.1 }, + author: Author { + id: comment.author(), + }, body: comment.body().to_owned(), reactions: [], timestamp: comment.timestamp(), From d98073a55d222c141421ad0c9247eee4f8943728 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 12 Jan 2023 19:18:02 +0000 Subject: [PATCH 026/264] httpd: Add api tests Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 7 + radicle-httpd/src/api.rs | 2 + radicle-httpd/src/api/test.rs | 141 +++++++++ radicle-httpd/src/api/v1/delegates.rs | 35 +++ radicle-httpd/src/api/v1/projects.rs | 435 ++++++++++++++++++++++++++ radicle-httpd/src/api/v1/stats.rs | 28 ++ 6 files changed, 648 insertions(+) create mode 100644 radicle-httpd/src/api/test.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 32a9a9f51..e0b0d58b2 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -43,3 +43,10 @@ version = "0.2.0" git = "https://github.com/radicle-dev/radicle-git" features = ["serde"] rev = "79a94721366490053e2d8ac1c1afa14fb0c25f09" + +[dev-dependencies] +hyper = { version = "0.14.17", default-features = false, features = ["client"] } +radicle-cli = { path = "../radicle-cli" } +radicle-crypto = { path = "../radicle-crypto" } +tempfile = { version = "3.3.0" } +tower = { version = "0.4", features = ["util"] } diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 93ff3004a..573d2776e 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -20,6 +20,8 @@ use radicle::Profile; mod auth; mod axum_extra; mod error; +#[cfg(test)] +mod test; mod v1; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs new file mode 100644 index 000000000..d04e8911f --- /dev/null +++ b/radicle-httpd/src/api/test.rs @@ -0,0 +1,141 @@ +use std::path::Path; +use std::sync::Arc; +use std::{env, fs}; + +use axum::body::Body; +use axum::http::Request; +use axum::Router; +use serde_json::Value; +use tower::ServiceExt; + +use radicle::cob::issue::Issues; +use radicle::git::raw as git2; +use radicle::storage::WriteStorage; +use radicle_cli::commands::rad_init; +use radicle_crypto::ssh::keystore::MemorySigner; +use radicle_crypto::Signer; + +use crate::api::Context; + +pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; +pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; + +const PASSWORD: &str = "radicle"; + +pub fn seed(dir: &Path) -> Context { + let workdir = dir.join("hello-world"); + let rad_home = dir.join("radicle"); + + env::set_var("RAD_PASSPHRASE", PASSWORD); + env::set_var("RAD_DEBUG", "1"); + + fs::create_dir_all(&workdir).unwrap(); + fs::create_dir_all(&rad_home).unwrap(); + + // add commits to workdir (repo) + let repo = git2::Repository::init(&workdir).unwrap(); + let tree = + radicle::git::write_tree(Path::new("README"), "Hello World!\n".as_bytes(), &repo).unwrap(); + + let sig_time = git2::Time::new(1673001014, 0); + let sig = git2::Signature::new("Alice Liddell", "alice@radicle.xyz", &sig_time).unwrap(); + + let oid = repo + .commit(Some("HEAD"), &sig, &sig, "Initial commit\n", &tree, &[]) + .unwrap(); + let commit = repo.find_commit(oid).unwrap(); + + repo.checkout_tree(tree.as_object(), None).unwrap(); + + fs::create_dir(workdir.join("dir1")).unwrap(); + fs::write( + workdir.join("dir1").join("README"), + "Hello World from dir1!\n", + ) + .unwrap(); + let mut index = repo.index().unwrap(); + index + .add_all(["."], git2::IndexAddOption::DEFAULT, None) + .unwrap(); + index.write().unwrap(); + + let oid = index.write_tree().unwrap(); + let tree = repo.find_tree(oid).unwrap(); + + repo.commit( + Some("HEAD"), + &sig, + &sig, + "Add another folder\n", + &tree, + &[&commit], + ) + .unwrap(); + + // eq. rad auth + let profile = radicle::Profile::init(rad_home, PASSWORD.to_owned()).unwrap(); + + // rad init + rad_init::init( + rad_init::Options { + path: Some(workdir.clone()), + name: Some("hello-world".to_string()), + description: Some("Rad repository for tests".to_string()), + branch: None, + interactive: false.into(), + setup_signing: false, + set_upstream: false, + }, + &profile, + ) + .unwrap(); + + // eq. rad issue new + env::set_var("RAD_COMMIT_TIME", "1673001014"); + + let signer = MemorySigner::load(&profile.keystore, PASSWORD.to_owned().into()).unwrap(); + let storage = &profile.storage; + let (_, id) = radicle::rad::repo(&workdir).unwrap(); + let repo = storage.repository(id).unwrap(); + let mut issues = Issues::open(*signer.public_key(), &repo).unwrap(); + issues + .create( + "Issue #1".to_string(), + "Change 'hello world' to 'hello everyone'".to_string(), + &[], + &signer, + ) + .unwrap(); + + Context { + profile: Arc::new(profile), + sessions: Default::default(), + } +} + +pub async fn request(app: &Router, path: impl ToString) -> Response { + Response( + app.clone() + .oneshot( + Request::builder() + .uri(path.to_string()) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(), + ) +} + +pub struct Response(axum::response::Response); + +impl Response { + pub async fn json(self) -> Value { + let body = hyper::body::to_bytes(self.0.into_body()).await.unwrap(); + serde_json::from_slice(&body).unwrap() + } + + pub fn status(&self) -> axum::http::StatusCode { + self.0.status() + } +} diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index d578504d2..8e8c2a0ec 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -62,3 +62,38 @@ async fn delegates_projects_handler( Ok::<_, Error>(Json(projects)) } + +#[cfg(test)] +mod routes { + use axum::http::StatusCode; + use serde_json::json; + + use crate::api::test::{self, request, HEAD}; + + #[tokio::test] + async fn test_delegates_projects() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request( + &app, + "/delegates/did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/projects", + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!([ + { + "name": "hello-world", + "description": "Rad repository for tests", + "defaultBranch": "master", + "head": HEAD, + "patches": 0, + "issues": 1, + "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" + } + ]) + ); + } +} diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 1e7de595d..176acf102 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -467,3 +467,438 @@ fn stats(repo: &Repository, head: Oid) -> Result { contributors: contributors.len(), }) } + +#[cfg(test)] +mod routes { + use axum::http::StatusCode; + use serde_json::json; + + use crate::api::test::{self, request, HEAD, HEAD_1}; + + #[tokio::test] + async fn test_projects_root() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request(&app, "/projects").await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!([ + { + "name": "hello-world", + "description": "Rad repository for tests", + "defaultBranch": "master", + "head": HEAD, + "patches": 0, + "issues": 1, + "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" + } + ]) + ); + } + + #[tokio::test] + async fn test_projects() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp").await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "name": "hello-world", + "description": "Rad repository for tests", + "defaultBranch": "master", + "head": HEAD, + "patches": 0, + "issues": 1, + "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" + }) + ); + } + + #[tokio::test] + async fn test_projects_commits_root() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/commits").await; + + assert_eq!(response.status(), StatusCode::FOUND); + assert_eq!( + response.json().await, + json!({ + "headers": [ + { + "header": { + "sha1": HEAD, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Add another folder", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "committerTime": 1673001014 + }, + "diff": { + "added": [ + { + "path": "dir1/README", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Hello World from dir1!\n", + "lineNo": 1, + "type": "addition" + } + ] + } + ] + } + } + ], + "deleted": [], + "moved": [], + "copied": [], + "modified": [], + "stats": { + "filesChanged": 1, + "insertions": 1, + "deletions": 0 + } + }, + "branches": [ + "refs/heads/master" + ] + }, + { + "header": { + "sha1": HEAD_1, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Initial commit", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "committerTime": 1673001014 + }, + "diff": { + "added": [ + { + "path": "README", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Hello World!\n", + "lineNo": 1, + "type": "addition" + } + ] + } + ] + } + } + ], + "deleted": [], + "moved": [], + "copied": [], + "modified": [], + "stats": { + "filesChanged": 1, + "insertions": 1, + "deletions": 0 + } + }, + "branches": [ + "refs/heads/master" + ] + } + ], + "stats": { + "commits": 2, + "branches": 1, + "contributors": 1 + } + + }) + ); + } + + #[tokio::test] + async fn test_projects_commits() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request( + &app, + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/commits/{HEAD}"), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "header": { + "sha1": HEAD, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Add another folder", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "committerTime": 1673001014 + }, + "diff": { + "added": [ + { + "path": "dir1/README", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Hello World from dir1!\n", + "lineNo": 1, + "type": "addition" + } + ] + } + ] + } + } + ], + "deleted": [], + "moved": [], + "copied": [], + "modified": [], + "stats": { + "filesChanged": 1, + "insertions": 1, + "deletions": 0 + } + }, + "branches": [ + "refs/heads/master" + ] + }) + ); + } + + #[tokio::test] + async fn test_projects_tree() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request( + &app, + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/tree/{HEAD}/"), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "entries": [ + { + "path": "README", + "name": "README", + "lastCommit": null, + "kind": "blob" + }, + { + "path": "dir1", + "name": "dir1", + "lastCommit": null, + "kind": "tree" + } + ], + "lastCommit": { + "sha1": HEAD, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Add another folder", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "committerTime": 1673001014 + }, + "name": "", + "path": "", + "stats": { + "branches": 1, + "commits": 2, + "contributors": 1 + } + } + ) + ); + + let response = request( + &app, + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/tree/{HEAD}/dir1"), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "entries": [ + { + "path": "dir1/README", + "name": "README", + "lastCommit": null, + "kind": "blob" + } + ], + "lastCommit": null, + "name": "dir1", + "path": "dir1", + "stats": { + "branches": 1, + "commits": 2, + "contributors": 1 + } + }) + ); + } + + #[tokio::test] + async fn test_projects_remotes_root() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes").await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!([ + { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "heads": { + "master": HEAD + }, + "delegate": false + } + ]) + ); + } + + #[tokio::test] + async fn test_projects_remotes() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi").await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "heads": { + "master": HEAD + }, + "delegate": false + }) + ); + } + + #[tokio::test] + async fn test_projects_blob() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request( + &app, + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/blob/{HEAD}/README"), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "binary": false, + "content": "Hello World!\n", + "lastCommit": { + "sha1": HEAD_1, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Initial commit", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "committerTime": 1673001014 + }, + "name": "README", + "path": "README" + }) + ); + } + + #[tokio::test] + async fn test_projects_issues_root() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues").await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!([ + { + "id": "458bbd9f6d47eed3d60cd905141687ad1f99251e", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + }, + "title": "Issue #1", + "state": { + "status": "open" + }, + "discussion": [ + { + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + }, + "body": "Change 'hello world' to 'hello everyone'", + "reactions": [], + "timestamp": 1673001014, + "replyTo": null + } + ], + "tags": [] + } + ]) + ); + } +} diff --git a/radicle-httpd/src/api/v1/stats.rs b/radicle-httpd/src/api/v1/stats.rs index fdb512780..a1bb588ad 100644 --- a/radicle-httpd/src/api/v1/stats.rs +++ b/radicle-httpd/src/api/v1/stats.rs @@ -22,3 +22,31 @@ async fn stats_handler(Extension(ctx): Extension) -> impl IntoResponse json!({ "projects": { "count": projects }, "users": { "count": 0 } }), )) } + +#[cfg(test)] +mod routes { + use axum::http::StatusCode; + use serde_json::json; + + use crate::api::test::{self, request}; + + #[tokio::test] + async fn test_stats() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request(&app, "/stats").await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "projects": { + "count": 1 + }, + "users": { + "count": 0 + } + }) + ); + } +} From 1072fd1efcf3a568d179e43ec9a901b2de0db358 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 16 Jan 2023 17:58:06 +0100 Subject: [PATCH 027/264] Speed up node E2E tests By allowing the node to run using a `MockSigner`, we speed up tests significantly, since PBKDF2 doesn't need to run. Signed-off-by: Alexis Sellier --- radicle-httpd/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index b89c4d910..baa77cd80 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -51,7 +51,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> { tracing::info!("{}", str::from_utf8(&git_version)?.trim()); let profile = Arc::new(radicle::Profile::load()?); - tracing::info!("using radicle home at {}", profile.home.display()); + tracing::info!("using radicle home at {}", profile.home().display()); let git_router = Router::new() .route("/:project/*request", any(git_handler)) From c5841cefb6a152f0b1edcefd7f5238fa57b323c0 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 16 Jan 2023 18:06:14 +0100 Subject: [PATCH 028/264] Rename `profile::Paths` to `profile::Home` This makes the intent clearer. Signed-off-by: Alexis Sellier --- radicle-httpd/src/api/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index d04e8911f..7b4abb546 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -73,7 +73,7 @@ pub fn seed(dir: &Path) -> Context { .unwrap(); // eq. rad auth - let profile = radicle::Profile::init(rad_home, PASSWORD.to_owned()).unwrap(); + let profile = radicle::Profile::init(rad_home.into(), PASSWORD.to_owned()).unwrap(); // rad init rad_init::init( From 64a933dccdda6b2c79c77a32be9accd5978214b5 Mon Sep 17 00:00:00 2001 From: Han Xu Date: Tue, 6 Dec 2022 08:23:39 -0800 Subject: [PATCH 029/264] httpd: Dogfood the new radicle-surf API Signed-off-by: Han Xu --- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/api.rs | 1 + radicle-httpd/src/api/error.rs | 12 +- radicle-httpd/src/api/json.rs | 68 +++++++++++ radicle-httpd/src/api/v1/projects.rs | 164 +++++++++++++++++---------- 5 files changed, 175 insertions(+), 72 deletions(-) create mode 100644 radicle-httpd/src/api/json.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index e0b0d58b2..4171aacef 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -42,7 +42,7 @@ version = "0.2.0" [dependencies.radicle-surf] git = "https://github.com/radicle-dev/radicle-git" features = ["serde"] -rev = "79a94721366490053e2d8ac1c1afa14fb0c25f09" +rev = "016ece66474edf45d2d304141515152e6f7b37a3" [dev-dependencies] hyper = { version = "0.14.17", default-features = false, features = ["client"] } diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 573d2776e..3bb9a6681 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -20,6 +20,7 @@ use radicle::Profile; mod auth; mod axum_extra; mod error; +mod json; #[cfg(test)] mod test; mod v1; diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index d969fc5b2..d2b1dd089 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -38,17 +38,13 @@ pub enum Error { #[error(transparent)] GitProject(#[from] radicle::storage::git::ProjectError), - /// Surf commit error. + /// Surf directory error. #[error(transparent)] - SurfCommit(#[from] radicle_surf::source::commit::Error), + SurfDir(#[from] radicle_surf::fs::error::Directory), - /// Surf object error. + /// Surf error. #[error(transparent)] - SurfObject(#[from] radicle_surf::source::object::Error), - - /// Surf git error. - #[error(transparent)] - SurfGit(#[from] radicle_surf::git::Error), + Surf(#[from] radicle_surf::Error), /// Git2 error. #[error(transparent)] diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs new file mode 100644 index 000000000..d503d3e41 --- /dev/null +++ b/radicle-httpd/src/api/json.rs @@ -0,0 +1,68 @@ +//! Utilities for building JSON responses of our API. + +use radicle_surf::{ + object::{Blob, Tree}, + Commit, Stats, +}; +use serde_json::json; + +/// Returns JSON of a commit. +pub(crate) fn commit(commit: &Commit) -> serde_json::Value { + json!({ + "sha1": commit.id, + "author": { + "name": commit.author.name, + "email": commit.author.email + }, + "summary": commit.summary, + "description": commit.description(), + "committer": { + "name": commit.committer.name, + "email": commit.committer.email + }, + "committerTime": commit.committer.time.seconds() + }) +} + +/// Returns JSON for a blob with a given `path`. +pub(crate) fn blob(blob: &Blob, path: &str) -> serde_json::Value { + json!({ + "binary": blob.is_binary(), + "content": blob.content(), + "name": name_in_path(path), + "path": path, + "lastCommit": commit(blob.commit()) + }) +} + +/// Returns JSON for a tree with a given `path` and `stats`. +pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> serde_json::Value { + let prefix = std::path::Path::new(path); + let entries = tree + .entries() + .iter() + .map(|entry| { + json!({ + "path": prefix.join(entry.name()), + "name": entry.name(), + "lastCommit": serde_json::Value::Null, + "kind": if entry.is_tree() { "tree" } else { "blob" }, + }) + }) + .collect::>(); + json!({ + "entries": &entries, + "lastCommit": commit(tree.commit()), + "name": name_in_path(path), + "path": path, + "stats": stats, + }) +} + +/// Returns the name part of a path string. +fn name_in_path(path: &str) -> &str { + match path.rsplit('/').next() { + Some(name) => name, + None => path, + } +} diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 176acf102..36cc7f917 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1,4 +1,4 @@ -use std::collections::{BTreeMap, HashSet}; +use std::collections::BTreeMap; use axum::handler::Handler; use axum::http::{header, HeaderValue}; @@ -13,20 +13,15 @@ use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::Issues; use radicle::cob::thread::{self, CommentId}; use radicle::cob::Timestamp; -use radicle::git::Oid; use radicle::identity::{Id, PublicKey}; use radicle::node::NodeId; -use radicle::storage::git::paths; -use radicle::storage::{ReadRepository, WriteStorage}; -use radicle_surf::git::{Glob, History, Repository}; -use radicle_surf::source; -use radicle_surf::source::object::blob::Blob; -use radicle_surf::source::object::tree::Tree; +use radicle::storage::{git::paths, ReadRepository, WriteStorage}; +use radicle_surf::{Glob, Oid, Repository}; use crate::api::axum_extra::{Path, Query}; use crate::api::error::Error; use crate::api::project::Info; -use crate::api::{Context, PaginationQuery}; +use crate::api::{self, Context, PaginationQuery}; const CACHE_1_HOUR: &str = "public, max-age=3600, must-revalidate"; @@ -147,7 +142,7 @@ async fn history_handler( }; let headers = repo - .history(sha)? + .history(&sha)? .filter(|q| { if let Ok(q) = q { if let (Some(since), Some(until)) = (since, until) { @@ -166,14 +161,23 @@ async fn history_handler( }) .skip(page * per_page) .take(per_page) - .filter_map(|commit| { - if let Ok(commit) = commit { - source::commit(&repo, commit.id).ok() - } else { - None - } + .map(|r| { + r.and_then(|c| { + let glob = Glob::all_heads().branches().and(Glob::all_remotes()); + let branches: Vec = repo + .revision_branches(c.id, glob)? + .iter() + .map(|b| b.refname().to_string()) + .collect(); + let diff = repo.diff_commit(c.id)?; + Ok(json!({ + "header": api::json::commit(&c), + "diff": diff, + "branches": branches + })) + }) }) - .collect::>(); + .collect::, _>>()?; let response = json!({ "headers": headers, @@ -195,9 +199,22 @@ async fn commit_handler( ) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = Repository::open(paths::repository(storage, &project))?; - let commit = source::commit(&repo, sha)?; + let commit = repo.commit(sha)?; - Ok::<_, Error>(Json(commit)) + let diff = repo.diff_commit(commit.id)?; + let glob = Glob::all_heads().branches().and(Glob::all_remotes()); + let branches: Vec = repo + .revision_branches(commit.id, glob)? + .iter() + .map(|b| b.refname().to_string()) + .collect(); + + let response = json!({ + "header": api::json::commit(&commit), + "diff": diff, + "branches": branches + }); + Ok::<_, Error>(Json(response)) } /// Get project activity for the past year. @@ -236,10 +253,9 @@ async fn tree_handler( let path = path.strip_prefix('/').ok_or(Error::NotFound)?.to_string(); let storage = &ctx.profile.storage; let repo = Repository::open(paths::repository(storage, &project))?; - let path = if path.is_empty() { None } else { Some(&path) }; - let tree = Tree::new(&repo, &sha, path)?; - let mut response = json!(tree); - response["stats"] = json!(stats(&repo, sha)?); + let tree = repo.tree(sha, &path)?; + let stats = repo.stats_from(&sha)?; + let response = api::json::tree(&tree, &path, &stats); Ok::<_, Error>(Json(response)) } @@ -313,9 +329,10 @@ async fn blob_handler( let path = path.strip_prefix('/').ok_or(Error::NotFound)?; let storage = &ctx.profile.storage; let repo = Repository::open(paths::repository(storage, &project))?; - let blob = Blob::new(&repo, &sha, &path)?; + let blob = repo.blob(sha, &path)?; + let response = api::json::blob(&blob, path); - Ok::<_, Error>(Json(blob)) + Ok::<_, Error>(Json(response)) } /// Get project readme. @@ -336,8 +353,9 @@ async fn readme_handler( ]; for path in paths { - if let Ok(blob) = Blob::new(&repo, &sha, &path) { - return Ok::<_, Error>(Json(blob)); + if let Ok(blob) = repo.blob(sha, path) { + let response = api::json::blob(&blob, path); + return Ok::<_, Error>(Json(response)); } } @@ -439,35 +457,6 @@ impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { } } -#[derive(Serialize)] -struct Stats { - branches: usize, - commits: usize, - contributors: usize, -} - -fn stats(repo: &Repository, head: Oid) -> Result { - let branches = repo.branches(Glob::all_heads())?.count(); - - let mut commits = 0; - let contributors = History::new(repo, head)? - .filter_map(|commit| { - commits += 1; - if let Ok(commit) = commit { - Some((commit.author.name, commit.author.email)) - } else { - None - } - }) - .collect::>(); - - Ok(Stats { - branches, - commits, - contributors: contributors.len(), - }) -} - #[cfg(test)] mod routes { use axum::http::StatusCode; @@ -722,17 +711,17 @@ mod routes { response.json().await, json!({ "entries": [ - { - "path": "README", - "name": "README", - "lastCommit": null, - "kind": "blob" - }, { "path": "dir1", "name": "dir1", "lastCommit": null, "kind": "tree" + }, + { + "path": "README", + "name": "README", + "lastCommit": null, + "kind": "blob" } ], "lastCommit": { @@ -778,7 +767,20 @@ mod routes { "kind": "blob" } ], - "lastCommit": null, + "lastCommit": { + "sha1": HEAD, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Add another folder", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "committerTime": 1673001014 + }, "name": "dir1", "path": "dir1", "stats": { @@ -866,6 +868,42 @@ mod routes { ); } + #[tokio::test] + async fn test_projects_readme() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = request( + &app, + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/readme/{HEAD}"), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "binary": false, + "content": "Hello World!\n", + "lastCommit": { + "sha1": HEAD_1, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Initial commit", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "committerTime": 1673001014 + }, + "name": "README", + "path": "README" + }) + ); + } + #[tokio::test] async fn test_projects_issues_root() { let tmp = tempfile::tempdir().unwrap(); From 59e3afac6750ef5b1fc410100890e631e96e34a5 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Wed, 18 Jan 2023 14:09:48 +0000 Subject: [PATCH 030/264] httpd: Update axum from 0.5.x to 0.6.x Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 4 +-- radicle-httpd/src/api/axum_extra.rs | 19 ++++++----- radicle-httpd/src/api/v1/delegates.rs | 7 ++-- radicle-httpd/src/api/v1/node.rs | 7 ++-- radicle-httpd/src/api/v1/projects.rs | 49 ++++++++++++++------------- radicle-httpd/src/api/v1/sessions.rs | 11 +++--- radicle-httpd/src/api/v1/stats.rs | 7 ++-- radicle-httpd/src/lib.rs | 2 +- 8 files changed, 57 insertions(+), 49 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 4171aacef..821416dc4 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -16,8 +16,8 @@ logfmt = [ [dependencies] anyhow = { version = "1" } -axum = { version = "0.5.16", default-features = false, features = ["json", "headers", "query"] } -axum-server = { version = "0.4.2", default-features = false } +axum = { version = "0.6.2", default-features = false, features = ["headers", "json", "query", "tokio"] } +axum-server = { version = "0.4.4", default-features = false } chrono = { version = "0.4.22" } ethers-core = { version = "1.0" } fastrand = { version = "1.7.0" } diff --git a/radicle-httpd/src/api/axum_extra.rs b/radicle-httpd/src/api/axum_extra.rs index 8c23f9658..0e1b13869 100644 --- a/radicle-httpd/src/api/axum_extra.rs +++ b/radicle-httpd/src/api/axum_extra.rs @@ -1,6 +1,7 @@ use axum::extract::path::ErrorKind; use axum::extract::rejection::{PathRejection, QueryRejection}; -use axum::extract::{FromRequest, RequestParts}; +use axum::extract::FromRequestParts; +use axum::http::request::Parts; use axum::http::StatusCode; use axum::{async_trait, Json}; @@ -10,15 +11,15 @@ use serde::Serialize; pub struct Path(pub T); #[async_trait] -impl FromRequest for Path +impl FromRequestParts for Path where T: DeserializeOwned + Send, - B: Send, + S: Send + Sync, { type Rejection = (StatusCode, axum::Json); - async fn from_request(req: &mut RequestParts) -> Result { - match axum::extract::Path::::from_request(req).await { + async fn from_request_parts(req: &mut Parts, state: &S) -> Result { + match axum::extract::Path::::from_request_parts(req, state).await { Ok(value) => Ok(Self(value.0)), Err(rejection) => { let status = StatusCode::BAD_REQUEST; @@ -52,15 +53,15 @@ where pub struct Query(pub T); #[async_trait] -impl FromRequest for Query +impl FromRequestParts for Query where T: DeserializeOwned + Send, - B: Send, + S: Send + Sync, { type Rejection = (StatusCode, axum::Json); - async fn from_request(req: &mut RequestParts) -> Result { - match axum::extract::Query::::from_request(req).await { + async fn from_request_parts(req: &mut Parts, state: &S) -> Result { + match axum::extract::Query::::from_request_parts(req, state).await { Ok(value) => Ok(Self(value.0)), Err(rejection) => { let status = StatusCode::BAD_REQUEST; diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 8e8c2a0ec..499ca9092 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -1,6 +1,7 @@ +use axum::extract::State; use axum::response::IntoResponse; use axum::routing::get; -use axum::{Extension, Json, Router}; +use axum::{Json, Router}; use radicle::cob::issue::Issues; use radicle::identity::Did; @@ -18,13 +19,13 @@ pub fn router(ctx: Context) -> Router { "/delegates/:delegate/projects", get(delegates_projects_handler), ) - .layer(Extension(ctx)) + .with_state(ctx) } /// List all projects which delegate is a part of. /// `GET /delegates/:delegate/projects` async fn delegates_projects_handler( - Extension(ctx): Extension, + State(ctx): State, Path(delegate): Path, Query(qs): Query, ) -> impl IntoResponse { diff --git a/radicle-httpd/src/api/v1/node.rs b/radicle-httpd/src/api/v1/node.rs index 529c6598d..1dfea6132 100644 --- a/radicle-httpd/src/api/v1/node.rs +++ b/radicle-httpd/src/api/v1/node.rs @@ -1,6 +1,7 @@ +use axum::extract::State; use axum::response::IntoResponse; use axum::routing::get; -use axum::{Extension, Json, Router}; +use axum::{Json, Router}; use serde_json::json; use radicle::node::NodeId; @@ -12,12 +13,12 @@ pub fn router(ctx: Context) -> Router { Router::new() .route("/node", get(node_handler)) - .layer(Extension(node_id)) + .with_state(node_id) } /// Return the node id for the node identity. /// `GET /node` -async fn node_handler(Extension(node_id): Extension) -> impl IntoResponse { +async fn node_handler(State(node_id): State) -> impl IntoResponse { let response = json!({ "id": node_id.to_string(), }); diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 36cc7f917..68c9e11f9 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1,10 +1,11 @@ use std::collections::BTreeMap; +use axum::extract::State; use axum::handler::Handler; use axum::http::{header, HeaderValue}; use axum::response::IntoResponse; use axum::routing::get; -use axum::{Extension, Json, Router}; +use axum::{Json, Router}; use hyper::StatusCode; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -40,6 +41,7 @@ pub fn router(ctx: Context) -> Router { )), ), ) + .route("/projects/:project/tree/:sha/", get(tree_handler_root)) .route("/projects/:project/tree/:sha/*path", get(tree_handler)) .route("/projects/:project/remotes", get(remotes_handler)) .route("/projects/:project/remotes/:peer", get(remote_handler)) @@ -47,13 +49,13 @@ pub fn router(ctx: Context) -> Router { .route("/projects/:project/readme/:sha", get(readme_handler)) .route("/projects/:project/issues", get(issues_handler)) .route("/projects/:project/issues/:id", get(issue_handler)) - .layer(Extension(ctx)) + .with_state(ctx) } /// List all projects. /// `GET /projects` async fn project_root_handler( - Extension(ctx): Extension, + State(ctx): State, Query(qs): Query, ) -> impl IntoResponse { let PaginationQuery { page, per_page } = qs; @@ -87,10 +89,7 @@ async fn project_root_handler( /// Get project metadata. /// `GET /projects/:project` -async fn project_handler( - Extension(ctx): Extension, - Path(id): Path, -) -> impl IntoResponse { +async fn project_handler(State(ctx): State, Path(id): Path) -> impl IntoResponse { let info = ctx.project_info(id)?; Ok::<_, Error>(Json(info)) @@ -109,7 +108,7 @@ pub struct CommitsQueryString { /// Get project commit range. /// `GET /projects/:project/commits?since=` async fn history_handler( - Extension(ctx): Extension, + State(ctx): State, Path(project): Path, Query(qs): Query, ) -> impl IntoResponse { @@ -194,7 +193,7 @@ async fn history_handler( /// Get project commit. /// `GET /projects/:project/commits/:sha` async fn commit_handler( - Extension(ctx): Extension, + State(ctx): State, Path((project, sha)): Path<(Id, Oid)>, ) -> impl IntoResponse { let storage = &ctx.profile.storage; @@ -220,7 +219,7 @@ async fn commit_handler( /// Get project activity for the past year. /// `GET /projects/:project/activity` async fn activity_handler( - Extension(ctx): Extension, + State(ctx): State, Path(project): Path, ) -> impl IntoResponse { let current_date = chrono::Utc::now().timestamp(); @@ -244,13 +243,21 @@ async fn activity_handler( Ok::<_, Error>((StatusCode::OK, Json(json!({ "activity": timestamps })))) } +/// Get project source tree for '/' path. +/// `GET /projects/:project/tree/:sha/` +async fn tree_handler_root( + State(ctx): State, + Path((project, sha)): Path<(Id, Oid)>, +) -> impl IntoResponse { + tree_handler(State(ctx), Path((project, sha, String::new()))).await +} + /// Get project source tree. /// `GET /projects/:project/tree/:sha/*path` async fn tree_handler( - Extension(ctx): Extension, + State(ctx): State, Path((project, sha, path)): Path<(Id, Oid, String)>, ) -> impl IntoResponse { - let path = path.strip_prefix('/').ok_or(Error::NotFound)?.to_string(); let storage = &ctx.profile.storage; let repo = Repository::open(paths::repository(storage, &project))?; let tree = repo.tree(sha, &path)?; @@ -262,10 +269,7 @@ async fn tree_handler( /// Get all project remotes. /// `GET /projects/:project/remotes` -async fn remotes_handler( - Extension(ctx): Extension, - Path(project): Path, -) -> impl IntoResponse { +async fn remotes_handler(State(ctx): State, Path(project): Path) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let remotes = repo @@ -296,7 +300,7 @@ async fn remotes_handler( /// Get project remote. /// `GET /projects/:project/remotes/:peer` async fn remote_handler( - Extension(ctx): Extension, + State(ctx): State, Path((project, node_id)): Path<(Id, NodeId)>, ) -> impl IntoResponse { let storage = &ctx.profile.storage; @@ -323,14 +327,13 @@ async fn remote_handler( /// Get project source file. /// `GET /projects/:project/blob/:sha/*path` async fn blob_handler( - Extension(ctx): Extension, + State(ctx): State, Path((project, sha, path)): Path<(Id, Oid, String)>, ) -> impl IntoResponse { - let path = path.strip_prefix('/').ok_or(Error::NotFound)?; let storage = &ctx.profile.storage; let repo = Repository::open(paths::repository(storage, &project))?; let blob = repo.blob(sha, &path)?; - let response = api::json::blob(&blob, path); + let response = api::json::blob(&blob, &path); Ok::<_, Error>(Json(response)) } @@ -338,7 +341,7 @@ async fn blob_handler( /// Get project readme. /// `GET /projects/:project/readme/:sha` async fn readme_handler( - Extension(ctx): Extension, + State(ctx): State, Path((project, sha)): Path<(Id, Oid)>, ) -> impl IntoResponse { let storage = &ctx.profile.storage; @@ -365,7 +368,7 @@ async fn readme_handler( /// Get project issues list. /// `GET /projects/:project/issues` async fn issues_handler( - Extension(ctx): Extension, + State(ctx): State, Path(project): Path, Query(qs): Query, ) -> impl IntoResponse { @@ -399,7 +402,7 @@ async fn issues_handler( /// Get project issue. /// `GET /projects/:project/issues/:id` async fn issue_handler( - Extension(ctx): Extension, + State(ctx): State, Path((project, issue_id)): Path<(Id, Oid)>, ) -> impl IntoResponse { let storage = &ctx.profile.storage; diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 1b03d7379..6ce7ad311 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -4,9 +4,10 @@ use std::env; use std::iter::repeat_with; use std::str::FromStr; +use axum::extract::State; use axum::response::IntoResponse; use axum::routing::{get, post}; -use axum::{Extension, Json, Router}; +use axum::{Json, Router}; use ethers_core::utils::hex; use hyper::http::uri::Authority; use serde_json::json; @@ -27,12 +28,12 @@ pub fn router(ctx: Context) -> Router { "/sessions/:id", get(session_get_handler).put(session_signin_handler), ) - .layer(Extension(ctx)) + .with_state(ctx) } /// Create session. /// `POST /sessions` -async fn session_create_handler(Extension(ctx): Extension) -> impl IntoResponse { +async fn session_create_handler(State(ctx): State) -> impl IntoResponse { let expiration_time = OffsetDateTime::now_utc() .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); @@ -45,7 +46,7 @@ async fn session_create_handler(Extension(ctx): Extension) -> impl Into /// Get session. /// `GET /sessions/:id` async fn session_get_handler( - Extension(ctx): Extension, + State(ctx): State, Path(id): Path, ) -> impl IntoResponse { let sessions = ctx.sessions.read().await; @@ -67,7 +68,7 @@ async fn session_get_handler( /// Update session. /// `PUT /sessions/:id` async fn session_signin_handler( - Extension(ctx): Extension, + State(ctx): State, Path(id): Path, Json(request): Json, ) -> impl IntoResponse { diff --git a/radicle-httpd/src/api/v1/stats.rs b/radicle-httpd/src/api/v1/stats.rs index a1bb588ad..aadbf8d29 100644 --- a/radicle-httpd/src/api/v1/stats.rs +++ b/radicle-httpd/src/api/v1/stats.rs @@ -1,6 +1,7 @@ +use axum::extract::State; use axum::response::IntoResponse; use axum::routing::get; -use axum::{Extension, Json, Router}; +use axum::{Json, Router}; use serde_json::json; use crate::api::error::Error; @@ -9,12 +10,12 @@ use crate::api::Context; pub fn router(ctx: Context) -> Router { Router::new() .route("/stats", get(stats_handler)) - .layer(Extension(ctx)) + .with_state(ctx) } /// Return the stats for the node. /// `GET /stats` -async fn stats_handler(Extension(ctx): Extension) -> impl IntoResponse { +async fn stats_handler(State(ctx): State) -> impl IntoResponse { let storage = &ctx.profile.storage; let projects = storage.projects()?.len(); diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index baa77cd80..fe5bd696f 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -98,9 +98,9 @@ async fn git_handler( AxumPath((project, request)): AxumPath<(String, String)>, method: Method, headers: HeaderMap, - body: Bytes, ConnectInfo(remote): ConnectInfo, query: RawQuery, + body: Bytes, ) -> impl IntoResponse { let query = query.0.unwrap_or_default(); let id: Id = project.strip_suffix(".git").unwrap_or(&project).parse()?; From 24d19b69d23858e3e9aa85470379eac61bb6c80d Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 19 Jan 2023 14:17:48 +0000 Subject: [PATCH 031/264] httpd: change `committerTime` into `committer.time` Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 6 +++--- radicle-httpd/src/api/v1/projects.rs | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index d503d3e41..5fa27c5f5 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -18,9 +18,9 @@ pub(crate) fn commit(commit: &Commit) -> serde_json::Value { "description": commit.description(), "committer": { "name": commit.committer.name, - "email": commit.committer.email - }, - "committerTime": commit.committer.time.seconds() + "email": commit.committer.email, + "time": commit.committer.time.seconds() + } }) } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 68c9e11f9..b8ead43ad 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -533,9 +533,9 @@ mod routes { "description": "", "committer": { "name": "Alice Liddell", - "email": "alice@radicle.xyz" + "email": "alice@radicle.xyz", + "time": 1673001014 }, - "committerTime": 1673001014 }, "diff": { "added": [ @@ -583,9 +583,9 @@ mod routes { "description": "", "committer": { "name": "Alice Liddell", - "email": "alice@radicle.xyz" + "email": "alice@radicle.xyz", + "time": 1673001014 }, - "committerTime": 1673001014 }, "diff": { "added": [ @@ -657,9 +657,9 @@ mod routes { "description": "", "committer": { "name": "Alice Liddell", - "email": "alice@radicle.xyz" + "email": "alice@radicle.xyz", + "time": 1673001014 }, - "committerTime": 1673001014 }, "diff": { "added": [ @@ -737,9 +737,9 @@ mod routes { "description": "", "committer": { "name": "Alice Liddell", - "email": "alice@radicle.xyz" + "email": "alice@radicle.xyz", + "time": 1673001014 }, - "committerTime": 1673001014 }, "name": "", "path": "", @@ -780,9 +780,9 @@ mod routes { "description": "", "committer": { "name": "Alice Liddell", - "email": "alice@radicle.xyz" + "email": "alice@radicle.xyz", + "time": 1673001014 }, - "committerTime": 1673001014 }, "name": "dir1", "path": "dir1", @@ -861,9 +861,9 @@ mod routes { "description": "", "committer": { "name": "Alice Liddell", - "email": "alice@radicle.xyz" + "email": "alice@radicle.xyz", + "time": 1673001014 }, - "committerTime": 1673001014 }, "name": "README", "path": "README" @@ -897,9 +897,9 @@ mod routes { "description": "", "committer": { "name": "Alice Liddell", - "email": "alice@radicle.xyz" + "email": "alice@radicle.xyz", + "time": 1673001014 }, - "committerTime": 1673001014 }, "name": "README", "path": "README" From 05da23b707bb529271510c7529a626188cf2d0c3 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 19 Jan 2023 14:22:27 +0000 Subject: [PATCH 032/264] httpd: Use `commits`/`commit` instead of `headers`/`header` Signed-off-by: xphoniex --- radicle-httpd/src/api/v1/projects.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index b8ead43ad..53e902520 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -140,7 +140,7 @@ async fn history_handler( per_page.unwrap_or(30) }; - let headers = repo + let commits = repo .history(&sha)? .filter(|q| { if let Ok(q) = q { @@ -170,7 +170,7 @@ async fn history_handler( .collect(); let diff = repo.diff_commit(c.id)?; Ok(json!({ - "header": api::json::commit(&c), + "commit": api::json::commit(&c), "diff": diff, "branches": branches })) @@ -179,7 +179,7 @@ async fn history_handler( .collect::, _>>()?; let response = json!({ - "headers": headers, + "commits": commits, "stats": repo.stats()?, }); @@ -209,7 +209,7 @@ async fn commit_handler( .collect(); let response = json!({ - "header": api::json::commit(&commit), + "commit": api::json::commit(&commit), "diff": diff, "branches": branches }); @@ -521,9 +521,9 @@ mod routes { assert_eq!( response.json().await, json!({ - "headers": [ + "commits": [ { - "header": { + "commit": { "sha1": HEAD, "author": { "name": "Alice Liddell", @@ -573,7 +573,7 @@ mod routes { ] }, { - "header": { + "commit": { "sha1": HEAD_1, "author": { "name": "Alice Liddell", @@ -647,7 +647,7 @@ mod routes { assert_eq!( response.json().await, json!({ - "header": { + "commit": { "sha1": HEAD, "author": { "name": "Alice Liddell", From f9e519296dcb9ebf8a2fd698f3b399db1df4cfe8 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 19 Jan 2023 14:25:49 +0000 Subject: [PATCH 033/264] httpd: Change `sha1` into `id` Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 2 +- radicle-httpd/src/api/v1/projects.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 5fa27c5f5..96cf79a34 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -9,7 +9,7 @@ use serde_json::json; /// Returns JSON of a commit. pub(crate) fn commit(commit: &Commit) -> serde_json::Value { json!({ - "sha1": commit.id, + "id": commit.id, "author": { "name": commit.author.name, "email": commit.author.email diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 53e902520..e12a1f2b3 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -524,7 +524,7 @@ mod routes { "commits": [ { "commit": { - "sha1": HEAD, + "id": HEAD, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" @@ -574,7 +574,7 @@ mod routes { }, { "commit": { - "sha1": HEAD_1, + "id": HEAD_1, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" @@ -648,7 +648,7 @@ mod routes { response.json().await, json!({ "commit": { - "sha1": HEAD, + "id": HEAD, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" @@ -728,7 +728,7 @@ mod routes { } ], "lastCommit": { - "sha1": HEAD, + "id": HEAD, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" @@ -771,7 +771,7 @@ mod routes { } ], "lastCommit": { - "sha1": HEAD, + "id": HEAD, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" @@ -852,7 +852,7 @@ mod routes { "binary": false, "content": "Hello World!\n", "lastCommit": { - "sha1": HEAD_1, + "id": HEAD_1, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" @@ -888,7 +888,7 @@ mod routes { "binary": false, "content": "Hello World!\n", "lastCommit": { - "sha1": HEAD_1, + "id": HEAD_1, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" From 9d8c5f404bac289d7f6101cb856251759204e27c Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 19 Jan 2023 14:46:14 +0000 Subject: [PATCH 034/264] httpd: Clean up `json.rs` Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 96cf79a34..6759c5f80 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -1,13 +1,14 @@ //! Utilities for building JSON responses of our API. -use radicle_surf::{ - object::{Blob, Tree}, - Commit, Stats, -}; -use serde_json::json; +use std::path::Path; + +use serde_json::{json, Value}; + +use radicle_surf::object::{Blob, Tree}; +use radicle_surf::{Commit, Stats}; /// Returns JSON of a commit. -pub(crate) fn commit(commit: &Commit) -> serde_json::Value { +pub(crate) fn commit(commit: &Commit) -> Value { json!({ "id": commit.id, "author": { @@ -25,7 +26,7 @@ pub(crate) fn commit(commit: &Commit) -> serde_json::Value { } /// Returns JSON for a blob with a given `path`. -pub(crate) fn blob(blob: &Blob, path: &str) -> serde_json::Value { +pub(crate) fn blob(blob: &Blob, path: &str) -> Value { json!({ "binary": blob.is_binary(), "content": blob.content(), @@ -36,8 +37,8 @@ pub(crate) fn blob(blob: &Blob, path: &str) -> serde_json::Value { } /// Returns JSON for a tree with a given `path` and `stats`. -pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> serde_json::Value { - let prefix = std::path::Path::new(path); +pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value { + let prefix = Path::new(path); let entries = tree .entries() .iter() @@ -45,11 +46,12 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> serde_json::Value json!({ "path": prefix.join(entry.name()), "name": entry.name(), - "lastCommit": serde_json::Value::Null, + "lastCommit": Value::Null, "kind": if entry.is_tree() { "tree" } else { "blob" }, }) }) .collect::>(); + json!({ "entries": &entries, "lastCommit": commit(tree.commit()), From 2125128b5fbf473c5af943056a7e6aa9aa804326 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Sat, 21 Jan 2023 16:53:46 +0000 Subject: [PATCH 035/264] httpd: Remove `lastCommit` in `/tree` for entries Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 1 - radicle-httpd/src/api/v1/projects.rs | 3 --- 2 files changed, 4 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 6759c5f80..ee2bfb017 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -46,7 +46,6 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value { json!({ "path": prefix.join(entry.name()), "name": entry.name(), - "lastCommit": Value::Null, "kind": if entry.is_tree() { "tree" } else { "blob" }, }) }) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index e12a1f2b3..5322fadc9 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -717,13 +717,11 @@ mod routes { { "path": "dir1", "name": "dir1", - "lastCommit": null, "kind": "tree" }, { "path": "README", "name": "README", - "lastCommit": null, "kind": "blob" } ], @@ -766,7 +764,6 @@ mod routes { { "path": "dir1/README", "name": "README", - "lastCommit": null, "kind": "blob" } ], From 71edcfe7f23fce9928368c13f698643158d7757d Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 25 Jan 2023 17:09:16 +0100 Subject: [PATCH 036/264] Replace `RAD_DEBUG` with `RAD_SEED` The latter is strictly more powerful. --- radicle-httpd/src/api/test.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 7b4abb546..dd43df58d 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -27,7 +27,10 @@ pub fn seed(dir: &Path) -> Context { let rad_home = dir.join("radicle"); env::set_var("RAD_PASSPHRASE", PASSWORD); - env::set_var("RAD_DEBUG", "1"); + env::set_var( + "RAD_SEED", + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + ); fs::create_dir_all(&workdir).unwrap(); fs::create_dir_all(&rad_home).unwrap(); From 932d8d2ed8dea6a9431b3f269e001db2185784ba Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 27 Jan 2023 09:46:03 +0100 Subject: [PATCH 037/264] Add web-based authentication Signed-off-by: Sebastian Martinez --- radicle-httpd/Cargo.toml | 2 - radicle-httpd/src/api.rs | 2 +- radicle-httpd/src/api/auth.rs | 75 ++---------- radicle-httpd/src/api/error.rs | 11 +- radicle-httpd/src/api/test.rs | 38 ++++-- radicle-httpd/src/api/v1/delegates.rs | 4 +- radicle-httpd/src/api/v1/projects.rs | 24 ++-- radicle-httpd/src/api/v1/sessions.rs | 166 +++++++++++++------------- radicle-httpd/src/api/v1/stats.rs | 4 +- 9 files changed, 144 insertions(+), 182 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 821416dc4..1ecedff9e 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -19,14 +19,12 @@ anyhow = { version = "1" } axum = { version = "0.6.2", default-features = false, features = ["headers", "json", "query", "tokio"] } axum-server = { version = "0.4.4", default-features = false } chrono = { version = "0.4.22" } -ethers-core = { version = "1.0" } fastrand = { version = "1.7.0" } flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } -siwe = { version = "0.5" } thiserror = { version = "1" } time = { version = "0.3.17" } tokio = { version = "1.21", default-features = false, features = ["macros", "rt-multi-thread"] } diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 3bb9a6681..a6cf2af52 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -73,7 +73,7 @@ pub fn router(ctx: Context) -> Router { CorsLayer::new() .max_age(Duration::from_secs(86400)) .allow_origin(cors::Any) - .allow_methods([Method::GET, Method::POST, Method::PUT]) + .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::PUT]) .allow_headers([CONTENT_TYPE, AUTHORIZATION]), ) } diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index 3c29f8b1c..74c2666ef 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -1,13 +1,8 @@ -use std::convert::TryFrom; -use std::str::FromStr; - -use ethers_core::types::{Signature, H160}; -use serde::{Deserialize, Serialize, Serializer}; +use radicle::crypto::PublicKey; +use serde::{Serialize, Serializer}; use time::OffsetDateTime; -use crate::api::error::Error; - -#[derive(Clone)] +#[derive(Clone, PartialEq, PartialOrd)] pub struct DateTime(pub OffsetDateTime); impl Serialize for DateTime { @@ -16,26 +11,11 @@ impl Serialize for DateTime { } } -#[derive(Deserialize, Serialize)] -pub struct AuthRequest { - pub message: String, - #[serde(deserialize_with = "deserialize_signature")] - pub signature: Signature, -} - -fn deserialize_signature<'de, D>(deserializer: D) -> Result -where - D: serde::de::Deserializer<'de>, -{ - let buf = String::deserialize(deserializer)?; - Signature::from_str(&buf).map_err(serde::de::Error::custom) -} - pub enum AuthState { Authorized(Session), Unauthorized { - nonce: String, - expiration_time: DateTime, + public_key: PublicKey, + expires_at: DateTime, }, } @@ -43,48 +23,7 @@ pub enum AuthState { #[derive(Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct Session { - pub domain: String, - pub address: H160, - pub statement: Option, - pub uri: String, - pub version: u64, - pub chain_id: u64, - pub nonce: String, + pub public_key: PublicKey, pub issued_at: DateTime, - pub expiration_time: Option, - pub resources: Vec, -} - -impl TryFrom for Session { - type Error = Error; - - fn try_from(message: siwe::Message) -> Result { - Ok(Session { - domain: message.domain.host().to_string(), - address: H160(message.address), - statement: None, - uri: message.uri.to_string(), - version: message.version as u64, - chain_id: message.chain_id, - nonce: message.nonce, - issued_at: DateTime(message.issued_at.as_ref().to_owned()), - expiration_time: message - .expiration_time - .map(|x| DateTime(x.as_ref().to_owned())), - resources: message.resources.iter().map(|r| r.to_string()).collect(), - }) - } -} - -#[cfg(test)] -mod test { - #[test] - fn test_auth_request_de() { - let json = serde_json::json!({ - "message": "Hello World!", - "signature": "20096c6ed2bcccb88c9cafbbbbda7a5a3cff6d0ca318c07faa58464083ca40a92f899fbeb26a4c763a7004b13fd0f1ba6c321d4e3a023e30f63c40d4154b99a41c" - }); - - let _req: super::AuthRequest = serde_json::from_value(json).unwrap(); - } + pub expires_at: DateTime, } diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index d2b1dd089..7995f5a6f 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -18,13 +18,13 @@ pub enum Error { #[error(transparent)] Env(#[from] std::env::VarError), - /// An error occurred while verifying the siwe message. + /// Profile error. #[error(transparent)] - SiweVerification(#[from] siwe::VerificationError), + Profile(#[from] radicle::profile::Error), - /// An error occurred while parsing the siwe message. + /// Crypto error. #[error(transparent)] - SiweParse(#[from] siwe::ParseError), + Crypto(#[from] radicle::crypto::Error), /// Storage error. #[error(transparent)] @@ -60,8 +60,7 @@ impl IntoResponse for Error { let (status, msg) = match &self { Error::NotFound => (StatusCode::NOT_FOUND, None), Error::Auth(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), - Error::SiweParse(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), - Error::SiweVerification(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), + Error::Crypto(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), Error::Git2(e) => ( StatusCode::INTERNAL_SERVER_ERROR, Some(e.message().to_owned()), diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index dd43df58d..5331c5b72 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use std::{env, fs}; use axum::body::Body; -use axum::http::Request; +use axum::http::{Method, Request}; use axum::Router; use serde_json::Value; use tower::ServiceExt; @@ -116,20 +116,42 @@ pub fn seed(dir: &Path) -> Context { } } -pub async fn request(app: &Router, path: impl ToString) -> Response { +pub async fn get(app: &Router, path: impl ToString) -> Response { Response( app.clone() - .oneshot( - Request::builder() - .uri(path.to_string()) - .body(Body::empty()) - .unwrap(), - ) + .oneshot(request(path, Method::GET, None)) .await .unwrap(), ) } +pub async fn post(app: &Router, path: impl ToString, body: Option) -> Response { + Response( + app.clone() + .oneshot(request(path, Method::POST, body)) + .await + .unwrap(), + ) +} + +pub async fn put(app: &Router, path: impl ToString, body: Option) -> Response { + Response( + app.clone() + .oneshot(request(path, Method::PUT, body)) + .await + .unwrap(), + ) +} + +fn request(path: impl ToString, method: Method, body: Option) -> Request { + Request::builder() + .method(method) + .uri(path.to_string()) + .header("Content-Type", "application/json") + .body(body.unwrap_or_else(Body::empty)) + .unwrap() +} + pub struct Response(axum::response::Response); impl Response { diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 499ca9092..ce25f58b3 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -69,13 +69,13 @@ mod routes { use axum::http::StatusCode; use serde_json::json; - use crate::api::test::{self, request, HEAD}; + use crate::api::test::{self, get, HEAD}; #[tokio::test] async fn test_delegates_projects() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request( + let response = get( &app, "/delegates/did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/projects", ) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 5322fadc9..a785ca8d5 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -465,13 +465,13 @@ mod routes { use axum::http::StatusCode; use serde_json::json; - use crate::api::test::{self, request, HEAD, HEAD_1}; + use crate::api::test::{self, get, HEAD, HEAD_1}; #[tokio::test] async fn test_projects_root() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request(&app, "/projects").await; + let response = get(&app, "/projects").await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -494,7 +494,7 @@ mod routes { async fn test_projects() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp").await; + let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp").await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -515,7 +515,7 @@ mod routes { async fn test_projects_commits_root() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/commits").await; + let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/commits").await; assert_eq!(response.status(), StatusCode::FOUND); assert_eq!( @@ -637,7 +637,7 @@ mod routes { async fn test_projects_commits() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request( + let response = get( &app, format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/commits/{HEAD}"), ) @@ -703,7 +703,7 @@ mod routes { async fn test_projects_tree() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request( + let response = get( &app, format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/tree/{HEAD}/"), ) @@ -750,7 +750,7 @@ mod routes { ) ); - let response = request( + let response = get( &app, format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/tree/{HEAD}/dir1"), ) @@ -796,7 +796,7 @@ mod routes { async fn test_projects_remotes_root() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes").await; + let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes").await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -817,7 +817,7 @@ mod routes { async fn test_projects_remotes() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi").await; + let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi").await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -836,7 +836,7 @@ mod routes { async fn test_projects_blob() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request( + let response = get( &app, format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/blob/{HEAD}/README"), ) @@ -872,7 +872,7 @@ mod routes { async fn test_projects_readme() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request( + let response = get( &app, format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/readme/{HEAD}"), ) @@ -908,7 +908,7 @@ mod routes { async fn test_projects_issues_root() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues").await; + let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues").await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 6ce7ad311..0f2798455 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -1,131 +1,135 @@ -use std::collections::HashMap; -use std::convert::TryInto; -use std::env; use std::iter::repeat_with; -use std::str::FromStr; use axum::extract::State; use axum::response::IntoResponse; -use axum::routing::{get, post}; +use axum::routing::{post, put}; use axum::{Json, Router}; -use ethers_core::utils::hex; -use hyper::http::uri::Authority; +use radicle::crypto::{PublicKey, Signature}; +use serde::{Deserialize, Serialize}; use serde_json::json; -use siwe::Message; use time::{Duration, OffsetDateTime}; -use crate::api::auth::{AuthRequest, AuthState, DateTime, Session}; +use crate::api::auth::{AuthState, DateTime, Session}; use crate::api::axum_extra::Path; use crate::api::error::Error; use crate::api::Context; pub const UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60); +pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1); pub fn router(ctx: Context) -> Router { Router::new() .route("/sessions", post(session_create_handler)) - .route( - "/sessions/:id", - get(session_get_handler).put(session_signin_handler), - ) + .route("/sessions/:id", put(session_signin_handler)) .with_state(ctx) } +#[derive(Debug, Deserialize, Serialize)] +struct AuthChallenge { + sig: Signature, + pk: PublicKey, +} + /// Create session. /// `POST /sessions` async fn session_create_handler(State(ctx): State) -> impl IntoResponse { + let rng = fastrand::Rng::new(); + let session_id = repeat_with(|| rng.alphanumeric()) + .take(32) + .collect::(); + let signer = ctx.profile.signer().map_err(Error::from)?; let expiration_time = OffsetDateTime::now_utc() .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); + let auth_state = AuthState::Unauthorized { + public_key: *signer.public_key(), + expires_at: DateTime(expiration_time), + }; let mut sessions = ctx.sessions.write().await; - let (session_id, nonce) = create_session(&mut sessions, DateTime(expiration_time)); + sessions.insert(session_id.clone(), auth_state); - Json(json!({ "id": session_id, "nonce": nonce })) -} - -/// Get session. -/// `GET /sessions/:id` -async fn session_get_handler( - State(ctx): State, - Path(id): Path, -) -> impl IntoResponse { - let sessions = ctx.sessions.read().await; - let session = sessions.get(&id).ok_or(Error::NotFound)?; - - match session { - AuthState::Authorized(session) => { - Ok::<_, Error>(Json(json!({ "id": id, "session": session }))) - } - AuthState::Unauthorized { - nonce, - expiration_time, - } => Ok::<_, Error>(Json( - json!({ "id": id, "nonce": nonce, "expirationTime": expiration_time }), - )), - } + Ok::<_, Error>(Json( + json!({"sessionId": session_id, "publicKey": signer.public_key()}), + )) } /// Update session. /// `PUT /sessions/:id` async fn session_signin_handler( State(ctx): State, - Path(id): Path, - Json(request): Json, + Path(session_id): Path, + Json(request): Json, ) -> impl IntoResponse { - // Get unauthenticated session data, return early if not found let mut sessions = ctx.sessions.write().await; - let session = sessions.get(&id).ok_or(Error::NotFound)?; - - if let AuthState::Unauthorized { nonce, .. } = session { - let message = Message::from_str(request.message.as_str()).map_err(Error::from)?; - - let host = env::var("RADICLE_DOMAIN").map_err(Error::from)?; - - // Validate nonce - if *nonce != message.nonce { - return Err(Error::Auth("Invalid nonce")); + let session = sessions.get(&session_id).ok_or(Error::NotFound)?; + if let AuthState::Unauthorized { + public_key, + expires_at, + } = session + { + if public_key != &request.pk { + return Err(Error::Auth("Invalid public key")); } - - // Verify that domain is the correct one - let authority = Authority::from_str(&host).map_err(|_| Error::Auth("Invalid host"))?; - if authority != message.domain { - return Err(Error::Auth("Invalid domain")); + if expires_at <= &DateTime(OffsetDateTime::now_utc()) { + return Err(Error::Auth("Session expired")); } - - // Verifies the following: - // - AuthRequest sig matches the address passed in the AuthRequest message. - // - expirationTime is not in the past. - // - notBefore time is in the future. - message - .verify(&request.signature.to_vec(), &Default::default()) - .await + let payload = format!("{}:{}", session_id, request.pk); + request + .pk + .verify(payload.as_bytes(), &request.sig) .map_err(Error::from)?; + let expiration_time = OffsetDateTime::now_utc() + .checked_add(AUTHORIZED_SESSIONS_EXPIRATION) + .unwrap(); + let session = Session { + public_key: request.pk, + issued_at: DateTime(OffsetDateTime::now_utc()), + expires_at: DateTime(expiration_time), + }; + sessions.insert(session_id.clone(), AuthState::Authorized(session)); - let session: Session = message.try_into()?; - sessions.insert(id.clone(), AuthState::Authorized(session.clone())); - - return Ok::<_, Error>(Json(json!({ "id": id, "session": session }))); + return Ok::<_, Error>(()); } Err(Error::Auth("Session already authorized")) } -fn create_session( - map: &mut HashMap, - expiration_time: DateTime, -) -> (String, String) { - let nonce = siwe::generate_nonce(); +#[cfg(test)] +mod routes { + use axum::body::Body; + use axum::http::StatusCode; + use radicle_cli::commands::rad_web::{self, SessionInfo}; - // We generate a value from the RNG for the session id - let rng = fastrand::Rng::new(); - let id = hex::encode(repeat_with(|| rng.u8(..)).take(32).collect::>()); + use crate::api::test::{self, post, put}; - let auth_state = AuthState::Unauthorized { - nonce: nonce.clone(), - expiration_time, - }; + #[tokio::test] + async fn test_session() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.to_owned()); - map.insert(id.clone(), auth_state); + // Create session + let response = post(&app, "/sessions", None).await; + assert_eq!(response.status(), StatusCode::OK); + let json = response.json().await; + let session_info: SessionInfo = serde_json::from_value(json).unwrap(); - (id, nonce) + // Create request body + let signer = ctx.profile.signer().unwrap(); + let signature = rad_web::sign(signer, &session_info).unwrap(); + let body = serde_json::to_vec(&super::AuthChallenge { + sig: signature, + pk: session_info.public_key, + }) + .unwrap(); + + let response = put( + &app, + format!("/sessions/{}", session_info.session_id), + Some(Body::from(body)), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + } } diff --git a/radicle-httpd/src/api/v1/stats.rs b/radicle-httpd/src/api/v1/stats.rs index aadbf8d29..79d10cec5 100644 --- a/radicle-httpd/src/api/v1/stats.rs +++ b/radicle-httpd/src/api/v1/stats.rs @@ -29,13 +29,13 @@ mod routes { use axum::http::StatusCode; use serde_json::json; - use crate::api::test::{self, request}; + use crate::api::test::{self, get}; #[tokio::test] async fn test_stats() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = request(&app, "/stats").await; + let response = get(&app, "/stats").await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( From b28b25c00fb7d2b3262dceaffaeb4ce45933b87e Mon Sep 17 00:00:00 2001 From: xphoniex Date: Sat, 28 Jan 2023 18:51:06 +0000 Subject: [PATCH 038/264] Update `radicle-surf` Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/api/json.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 1ecedff9e..8bc87c1d3 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -40,7 +40,7 @@ version = "0.2.0" [dependencies.radicle-surf] git = "https://github.com/radicle-dev/radicle-git" features = ["serde"] -rev = "016ece66474edf45d2d304141515152e6f7b37a3" +rev = "48022590d0439e7fb196605a573a725bbbf0e4d2" [dev-dependencies] hyper = { version = "0.14.17", default-features = false, features = ["client"] } diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index ee2bfb017..9516b4e9b 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -4,7 +4,8 @@ use std::path::Path; use serde_json::{json, Value}; -use radicle_surf::object::{Blob, Tree}; +use radicle_surf::blob::Blob; +use radicle_surf::tree::Tree; use radicle_surf::{Commit, Stats}; /// Returns JSON of a commit. From a8a24ec94546aa8ff01ea82fe7f9f20e3153ed6d Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 30 Jan 2023 11:38:00 +0100 Subject: [PATCH 039/264] node: Check refs when getting local inventory If we don't do this, we're going to be returning broken repos. --- radicle-httpd/src/api/v1/delegates.rs | 2 +- radicle-httpd/src/api/v1/projects.rs | 2 +- radicle-httpd/src/api/v1/stats.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index ce25f58b3..d72c91bb1 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -34,7 +34,7 @@ async fn delegates_projects_handler( let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; let projects = storage - .projects()? + .repositories()? .into_iter() .filter_map(|id| { let Ok(repo) = storage.repository(id) else { return None }; diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index a785ca8d5..9eb2385d0 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -63,7 +63,7 @@ async fn project_root_handler( let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; let projects = storage - .projects()? + .repositories()? .into_iter() .filter_map(|id| { let Ok(repo) = storage.repository(id) else { return None }; diff --git a/radicle-httpd/src/api/v1/stats.rs b/radicle-httpd/src/api/v1/stats.rs index 79d10cec5..873c06e71 100644 --- a/radicle-httpd/src/api/v1/stats.rs +++ b/radicle-httpd/src/api/v1/stats.rs @@ -17,7 +17,7 @@ pub fn router(ctx: Context) -> Router { /// `GET /stats` async fn stats_handler(State(ctx): State) -> impl IntoResponse { let storage = &ctx.profile.storage; - let projects = storage.projects()?.len(); + let projects = storage.repositories()?.len(); Ok::<_, Error>(Json( json!({ "projects": { "count": projects }, "users": { "count": 0 } }), From 49590bc72d0a23849e01f1bb05d8931a00fe1ef5 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Sat, 4 Feb 2023 13:22:09 +0000 Subject: [PATCH 040/264] httpd: Add routes for patch ops Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 21 ++++++ radicle-httpd/src/api/test.rs | 18 +++++ radicle-httpd/src/api/v1/projects.rs | 106 +++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 9516b4e9b..9de6bf17c 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -4,6 +4,7 @@ use std::path::Path; use serde_json::{json, Value}; +use radicle::cob::patch::{Patch, PatchId}; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; use radicle_surf::{Commit, Stats}; @@ -61,6 +62,26 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value { }) } +/// Returns JSON for a `patch`. +pub(crate) fn patch(id: PatchId, patch: Patch) -> Value { + json!({ + "id": id.to_string(), + "author": patch.author(), + "title": patch.title(), + "description": patch.description(), + "state": patch.state(), + "target": patch.target(), + "tags": patch.tags().collect::>(), + "revisions": patch.revisions().map(|(id, rev)| { + json!({ + "id": id, + "description": rev.description(), + "reviews": rev.reviews().collect::>(), + }) + }).collect::>(), + }) +} + /// Returns the name part of a path string. fn name_in_path(path: &str) -> &str { match path.rsplit('/').next() { diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 5331c5b72..92ca2f925 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -1,4 +1,5 @@ use std::path::Path; +use std::str::FromStr; use std::sync::Arc; use std::{env, fs}; @@ -9,6 +10,7 @@ use serde_json::Value; use tower::ServiceExt; use radicle::cob::issue::Issues; +use radicle::cob::patch::{MergeTarget, Patches}; use radicle::git::raw as git2; use radicle::storage::WriteStorage; use radicle_cli::commands::rad_init; @@ -110,6 +112,22 @@ pub fn seed(dir: &Path) -> Context { ) .unwrap(); + // eq. rad patch open + let mut patches = Patches::open(*signer.public_key(), &repo).unwrap(); + let oid = radicle::git::Oid::from_str(HEAD).unwrap(); + let base = radicle::git::Oid::from_str(HEAD_1).unwrap(); + patches + .create( + "A new `hello word`", + "change `hello world` in README to something else", + MergeTarget::Delegates, + base, + oid, + &[], + &signer, + ) + .unwrap(); + Context { profile: Arc::new(profile), sessions: Default::default(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 9eb2385d0..dc7afc3c0 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -12,6 +12,7 @@ use serde_json::json; use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::Issues; +use radicle::cob::patch::Patches; use radicle::cob::thread::{self, CommentId}; use radicle::cob::Timestamp; use radicle::identity::{Id, PublicKey}; @@ -49,6 +50,8 @@ pub fn router(ctx: Context) -> Router { .route("/projects/:project/readme/:sha", get(readme_handler)) .route("/projects/:project/issues", get(issues_handler)) .route("/projects/:project/issues/:id", get(issue_handler)) + .route("/projects/:project/patches", get(patches_handler)) + .route("/projects/:project/patches/:id", get(patch_handler)) .with_state(ctx) } @@ -422,6 +425,46 @@ async fn issue_handler( Ok::<_, Error>(Json(issue)) } +/// Get project patches list. +/// `GET /projects/:project/patches` +async fn patches_handler( + State(ctx): State, + Path(project): Path, + Query(qs): Query, +) -> impl IntoResponse { + 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 repo = storage.repository(project)?; + let patches = Patches::open(ctx.profile.public_key, &repo)?; + let patches = patches + .all()? + .into_iter() + .filter_map(|r| r.ok()) + .map(|(id, patch, _)| api::json::patch(id, patch)) + .skip(page * per_page) + .take(per_page) + .collect::>(); + + Ok::<_, Error>(Json(patches)) +} + +/// Get project patch. +/// `GET /projects/:project/patches/:id` +async fn patch_handler( + State(ctx): State, + Path((project, patch_id)): Path<(Id, Oid)>, +) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let repo = storage.repository(project)?; + let patch = Patches::open(ctx.profile.public_key, &repo)? + .get(&patch_id.into())? + .ok_or(Error::NotFound)?; + + Ok::<_, Error>(Json(api::json::patch(patch_id.into(), patch))) +} + #[derive(Serialize)] struct Author { id: PublicKey, @@ -939,4 +982,67 @@ mod routes { ]) ); } + + #[tokio::test] + async fn test_projects_patches() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches").await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!([ + { + "id": "5de9f17ca5326258412ab02f9a5339b6482198ce", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + }, + "title": "A new `hello word`", + "description": "change `hello world` in README to something else", + "state": "proposed", + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "description": "", + "reviews": [], + } + ], + } + ]) + ); + + let response = get( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/5de9f17ca5326258412ab02f9a5339b6482198ce", + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!( + { + "id": "5de9f17ca5326258412ab02f9a5339b6482198ce", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + }, + "title": "A new `hello word`", + "description": "change `hello world` in README to something else", + "state": "proposed", + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "description": "", + "reviews": [], + } + ], + } + ) + ); + } } From 6d9154d4276f200785ee053fe00c286796e7bf01 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Sat, 4 Feb 2023 17:16:26 +0330 Subject: [PATCH 041/264] httpd: Extract `issue` json response Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 55 ++++++++++++++++++++++++ radicle-httpd/src/api/v1/projects.rs | 63 ++-------------------------- 2 files changed, 58 insertions(+), 60 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 9de6bf17c..00b8e8bf0 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -2,9 +2,14 @@ use std::path::Path; +use serde::Serialize; 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::Timestamp; +use radicle::identity::PublicKey; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; use radicle_surf::{Commit, Stats}; @@ -62,6 +67,18 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value { }) } +/// Returns JSON for an `issue`. +pub(crate) fn issue(id: IssueId, issue: Issue) -> Value { + json!({ + "id": id.to_string(), + "author": issue.author(), + "title": issue.title(), + "state": issue.state(), + "discussion": issue.comments().collect::(), + "tags": issue.tags().collect::>(), + }) +} + /// Returns JSON for a `patch`. pub(crate) fn patch(id: PatchId, patch: Patch) -> Value { json!({ @@ -89,3 +106,41 @@ fn name_in_path(path: &str) -> &str { None => path, } } + +#[derive(Serialize)] +struct Author { + id: PublicKey, +} + +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct Comment { + author: Author, + body: String, + reactions: [String; 0], + timestamp: Timestamp, + reply_to: Option, +} + +#[derive(Serialize)] +struct Comments(Vec); + +impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { + fn from_iter>(iter: I) -> Self { + let mut comments = Vec::new(); + + for (_, comment) in iter { + comments.push(Comment { + author: Author { + id: comment.author(), + }, + body: comment.body().to_owned(), + reactions: [], + timestamp: comment.timestamp(), + reply_to: comment.reply_to(), + }); + } + + Comments(comments) + } +} diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index dc7afc3c0..0a21a8da8 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -13,9 +13,7 @@ use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::Issues; use radicle::cob::patch::Patches; -use radicle::cob::thread::{self, CommentId}; -use radicle::cob::Timestamp; -use radicle::identity::{Id, PublicKey}; +use radicle::identity::Id; use radicle::node::NodeId; use radicle::storage::{git::paths, ReadRepository, WriteStorage}; use radicle_surf::{Glob, Oid, Repository}; @@ -385,16 +383,7 @@ async fn issues_handler( .all()? .into_iter() .filter_map(|r| r.ok()) - .map(|(id, issue, _)| { - json!({ - "id": id.to_string(), - "author": issue.author(), - "title": issue.title(), - "state": issue.state(), - "discussion": issue.comments().collect::(), - "tags": issue.tags().collect::>(), - }) - }) + .map(|(id, issue, _)| api::json::issue(id, issue)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -413,16 +402,8 @@ async fn issue_handler( let issue = Issues::open(ctx.profile.public_key, &repo)? .get(&issue_id.into())? .ok_or(Error::NotFound)?; - let issue = json!({ - "id": issue_id, - "author": issue.author(), - "title": issue.title(), - "state": issue.state(), - "discussion": issue.comments().collect::(), - "tags": issue.tags().collect::>(), - }); - Ok::<_, Error>(Json(issue)) + Ok::<_, Error>(Json(api::json::issue(issue_id.into(), issue))) } /// Get project patches list. @@ -465,44 +446,6 @@ async fn patch_handler( Ok::<_, Error>(Json(api::json::patch(patch_id.into(), patch))) } -#[derive(Serialize)] -struct Author { - id: PublicKey, -} - -#[derive(Serialize)] -#[serde(rename_all = "camelCase")] -struct Comment { - author: Author, - body: String, - reactions: [String; 0], - timestamp: Timestamp, - reply_to: Option, -} - -#[derive(Serialize)] -struct Comments(Vec); - -impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { - fn from_iter>(iter: I) -> Self { - let mut comments = Vec::new(); - - for (_, comment) in iter { - comments.push(Comment { - author: Author { - id: comment.author(), - }, - body: comment.body().to_owned(), - reactions: [], - timestamp: comment.timestamp(), - reply_to: comment.reply_to(), - }); - } - - Comments(comments) - } -} - #[cfg(test)] mod routes { use axum::http::StatusCode; From d8da80821b1696c6872cb4bec09ea61c1a2f4eab Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Fri, 3 Feb 2023 13:43:30 +0000 Subject: [PATCH 042/264] Rust 1.67 Rust 1.67 was announced[0]. Update the necessary files for running 1.67 and fix the clippy suggestions for string interpolation. [0]: https://blog.rust-lang.org/2023/01/26/Rust-1.67.0.html Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-httpd/src/api/axum_extra.rs | 4 ++-- radicle-httpd/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api/axum_extra.rs b/radicle-httpd/src/api/axum_extra.rs index 0e1b13869..79a23e137 100644 --- a/radicle-httpd/src/api/axum_extra.rs +++ b/radicle-httpd/src/api/axum_extra.rs @@ -39,7 +39,7 @@ where } _ => Json(Error { success: false, - error: format!("{}", rejection), + error: format!("{rejection}"), }), }; @@ -72,7 +72,7 @@ where }), _ => Json(Error { success: false, - error: format!("{}", rejection), + error: format!("{rejection}"), }), }; diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index fe5bd696f..4674b0112 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -254,7 +254,7 @@ async fn git_http_backend( Err(Error::Backend) } Err(err) => { - panic!("failed to wait for git-http-backend: {}", err); + panic!("failed to wait for git-http-backend: {err}"); } } } From 585dcf4afe72889d45ada613a644c8061278dd16 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 Feb 2023 10:06:34 -0300 Subject: [PATCH 043/264] Add `GET /api/v1/sessions/:id` route to get session information Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/auth.rs | 2 ++ radicle-httpd/src/api/v1/sessions.rs | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index 74c2666ef..2f2c990d5 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -11,6 +11,8 @@ impl Serialize for DateTime { } } +#[derive(Clone, Serialize)] +#[serde(tag = "type", rename_all = "camelCase")] pub enum AuthState { Authorized(Session), Unauthorized { diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 0f2798455..6175f30a6 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -20,7 +20,10 @@ pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1); pub fn router(ctx: Context) -> Router { Router::new() .route("/sessions", post(session_create_handler)) - .route("/sessions/:id", put(session_signin_handler)) + .route( + "/sessions/:id", + put(session_signin_handler).get(session_handler), + ) .with_state(ctx) } @@ -53,6 +56,18 @@ async fn session_create_handler(State(ctx): State) -> impl IntoResponse )) } +/// Get a session. +/// `GET /sessions/:id` +async fn session_handler( + State(ctx): State, + Path(session_id): Path, +) -> impl IntoResponse { + let sessions = ctx.sessions.read().await; + let session = sessions.get(&session_id).ok_or(Error::NotFound)?.to_owned(); + + Ok::<_, Error>(Json(session)) +} + /// Update session. /// `PUT /sessions/:id` async fn session_signin_handler( @@ -100,7 +115,7 @@ mod routes { use axum::http::StatusCode; use radicle_cli::commands::rad_web::{self, SessionInfo}; - use crate::api::test::{self, post, put}; + use crate::api::test::{self, get, post, put}; #[tokio::test] async fn test_session() { @@ -131,5 +146,8 @@ mod routes { .await; assert_eq!(response.status(), StatusCode::OK); + + let response = get(&app, format!("/sessions/{}", session_info.session_id)).await; + assert_eq!(response.status(), StatusCode::OK); } } From f689cb2c9f3f855815b2a55f515ce77b82324515 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 Feb 2023 11:37:29 -0300 Subject: [PATCH 044/264] httpd: Remove session serialize and rework Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/auth.rs | 22 +++++++++++++--------- radicle-httpd/src/api/v1/sessions.rs | 25 ++++++++++++++----------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index 2f2c990d5..a73d59e2b 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -11,21 +11,25 @@ impl Serialize for DateTime { } } -#[derive(Clone, Serialize)] -#[serde(tag = "type", rename_all = "camelCase")] +#[derive(Clone)] pub enum AuthState { Authorized(Session), - Unauthorized { - public_key: PublicKey, - expires_at: DateTime, - }, + Unauthorized(Session), } -// We copy the implementation of siwe::Message here to derive Serialization and Debug -#[derive(Clone, Serialize)] -#[serde(rename_all = "camelCase")] +#[derive(Clone)] pub struct Session { + pub status: String, pub public_key: PublicKey, pub issued_at: DateTime, pub expires_at: DateTime, } + +impl From for Session { + fn from(other: AuthState) -> Self { + match other { + AuthState::Authorized(s) => s, + AuthState::Unauthorized(s) => s, + } + } +} diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 6175f30a6..083178020 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -1,3 +1,4 @@ +use std::convert::Into; use std::iter::repeat_with; use axum::extract::State; @@ -44,12 +45,14 @@ async fn session_create_handler(State(ctx): State) -> impl IntoResponse let expiration_time = OffsetDateTime::now_utc() .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); - let auth_state = AuthState::Unauthorized { + let session = Session { + status: String::from("unauthorized"), public_key: *signer.public_key(), + issued_at: DateTime(OffsetDateTime::now_utc()), expires_at: DateTime(expiration_time), }; let mut sessions = ctx.sessions.write().await; - sessions.insert(session_id.clone(), auth_state); + sessions.insert(session_id.clone(), AuthState::Unauthorized(session)); Ok::<_, Error>(Json( json!({"sessionId": session_id, "publicKey": signer.public_key()}), @@ -63,9 +66,12 @@ async fn session_handler( Path(session_id): Path, ) -> impl IntoResponse { let sessions = ctx.sessions.read().await; - let session = sessions.get(&session_id).ok_or(Error::NotFound)?.to_owned(); + let auth_state = sessions.get(&session_id).ok_or(Error::NotFound)?; + let session = Session::from(auth_state.clone()); - Ok::<_, Error>(Json(session)) + Ok::<_, Error>(Json( + json!({"publicKey": session.public_key, "issuedAt": session.issued_at, "expiresAt": session.expires_at}), + )) } /// Update session. @@ -77,15 +83,11 @@ async fn session_signin_handler( ) -> impl IntoResponse { let mut sessions = ctx.sessions.write().await; let session = sessions.get(&session_id).ok_or(Error::NotFound)?; - if let AuthState::Unauthorized { - public_key, - expires_at, - } = session - { - if public_key != &request.pk { + if let AuthState::Unauthorized(s) = session { + if s.public_key != request.pk { return Err(Error::Auth("Invalid public key")); } - if expires_at <= &DateTime(OffsetDateTime::now_utc()) { + if s.expires_at <= DateTime(OffsetDateTime::now_utc()) { return Err(Error::Auth("Session expired")); } let payload = format!("{}:{}", session_id, request.pk); @@ -97,6 +99,7 @@ async fn session_signin_handler( .checked_add(AUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); let session = Session { + status: String::from("authorized"), public_key: request.pk, issued_at: DateTime(OffsetDateTime::now_utc()), expires_at: DateTime(expiration_time), From ffdf7c171d4e0205fc7da39d79f2b3eb1297a140 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 Feb 2023 12:23:15 -0300 Subject: [PATCH 045/264] httpd: Refactor auth and session handling Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api.rs | 2 +- radicle-httpd/src/api/auth.rs | 18 ++++--------- radicle-httpd/src/api/v1/sessions.rs | 40 +++++++++++++++------------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index a6cf2af52..216786d9e 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -33,7 +33,7 @@ type SessionId = String; #[derive(Clone)] pub struct Context { profile: Arc, - sessions: Arc>>, + sessions: Arc>>, } impl Context { diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index a73d59e2b..317ef9834 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -11,25 +11,17 @@ impl Serialize for DateTime { } } -#[derive(Clone)] +#[derive(Clone, Serialize, PartialEq)] +#[serde(rename_all = "lowercase")] pub enum AuthState { - Authorized(Session), - Unauthorized(Session), + Authorized, + Unauthorized, } #[derive(Clone)] pub struct Session { - pub status: String, + pub status: AuthState, pub public_key: PublicKey, pub issued_at: DateTime, pub expires_at: DateTime, } - -impl From for Session { - fn from(other: AuthState) -> Self { - match other { - AuthState::Authorized(s) => s, - AuthState::Unauthorized(s) => s, - } - } -} diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 083178020..7b02a424e 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -1,4 +1,3 @@ -use std::convert::Into; use std::iter::repeat_with; use axum::extract::State; @@ -46,17 +45,20 @@ async fn session_create_handler(State(ctx): State) -> impl IntoResponse .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); let session = Session { - status: String::from("unauthorized"), + status: AuthState::Unauthorized, public_key: *signer.public_key(), issued_at: DateTime(OffsetDateTime::now_utc()), expires_at: DateTime(expiration_time), }; let mut sessions = ctx.sessions.write().await; - sessions.insert(session_id.clone(), AuthState::Unauthorized(session)); + sessions.insert(session_id.clone(), session.clone()); - Ok::<_, Error>(Json( - json!({"sessionId": session_id, "publicKey": signer.public_key()}), - )) + Ok::<_, Error>(Json(json!({ + "sessionId": session_id, + "publicKey": session.public_key, + "issuedAt": session.issued_at, + "expiresAt": session.expires_at + }))) } /// Get a session. @@ -66,12 +68,14 @@ async fn session_handler( Path(session_id): Path, ) -> impl IntoResponse { let sessions = ctx.sessions.read().await; - let auth_state = sessions.get(&session_id).ok_or(Error::NotFound)?; - let session = Session::from(auth_state.clone()); + let session = sessions.get(&session_id).ok_or(Error::NotFound)?; - Ok::<_, Error>(Json( - json!({"publicKey": session.public_key, "issuedAt": session.issued_at, "expiresAt": session.expires_at}), - )) + Ok::<_, Error>(Json(json!({ + "status": session.status, + "publicKey": session.public_key, + "issuedAt": session.issued_at, + "expiresAt": session.expires_at + }))) } /// Update session. @@ -83,11 +87,11 @@ async fn session_signin_handler( ) -> impl IntoResponse { let mut sessions = ctx.sessions.write().await; let session = sessions.get(&session_id).ok_or(Error::NotFound)?; - if let AuthState::Unauthorized(s) = session { - if s.public_key != request.pk { + if session.status == AuthState::Unauthorized { + if session.public_key != request.pk { return Err(Error::Auth("Invalid public key")); } - if s.expires_at <= DateTime(OffsetDateTime::now_utc()) { + if session.expires_at <= DateTime(OffsetDateTime::now_utc()) { return Err(Error::Auth("Session expired")); } let payload = format!("{}:{}", session_id, request.pk); @@ -99,14 +103,14 @@ async fn session_signin_handler( .checked_add(AUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); let session = Session { - status: String::from("authorized"), + status: AuthState::Authorized, public_key: request.pk, - issued_at: DateTime(OffsetDateTime::now_utc()), + issued_at: session.issued_at.to_owned(), expires_at: DateTime(expiration_time), }; - sessions.insert(session_id.clone(), AuthState::Authorized(session)); + sessions.insert(session_id.clone(), session); - return Ok::<_, Error>(()); + return Ok::<_, Error>(Json(json!({ "success": true }))); } Err(Error::Auth("Session already authorized")) From d39ca8e4042e78f555d27833469a3e24dfcbffb5 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 Feb 2023 16:37:13 -0300 Subject: [PATCH 046/264] httpd: Remove session serialization, improve tests Signed-off-by: Sebastian Martinez --- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/api/auth.rs | 24 ++++------ radicle-httpd/src/api/v1/sessions.rs | 66 ++++++++++++++++------------ 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 8bc87c1d3..a2b536084 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -26,7 +26,7 @@ lexopt = { version = "0.2.1" } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } -time = { version = "0.3.17" } +time = { version = "0.3.17", features = ["parsing", "serde"] } tokio = { version = "1.21", default-features = false, features = ["macros", "rt-multi-thread"] } tower-http = { version = "0.3.4", default-features = false, features = ["trace", "cors", "set-header"] } tracing = { version = "0.1.37", default-features = false, features = ["std", "log"] } diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index 317ef9834..14c52049f 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -1,27 +1,21 @@ use radicle::crypto::PublicKey; -use serde::{Serialize, Serializer}; -use time::OffsetDateTime; +use serde::{Deserialize, Serialize}; +use time::{serde::timestamp, OffsetDateTime}; -#[derive(Clone, PartialEq, PartialOrd)] -pub struct DateTime(pub OffsetDateTime); - -impl Serialize for DateTime { - fn serialize(&self, serializer: S) -> Result { - serializer.serialize_str(&format!("{}", self.0)) - } -} - -#[derive(Clone, Serialize, PartialEq)] +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] #[serde(rename_all = "lowercase")] pub enum AuthState { Authorized, Unauthorized, } -#[derive(Clone)] +#[derive(Clone, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct Session { pub status: AuthState, pub public_key: PublicKey, - pub issued_at: DateTime, - pub expires_at: DateTime, + #[serde(with = "timestamp")] + pub issued_at: OffsetDateTime, + #[serde(with = "timestamp")] + pub expires_at: OffsetDateTime, } diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 7b02a424e..d06610244 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -4,12 +4,13 @@ use axum::extract::State; use axum::response::IntoResponse; use axum::routing::{post, put}; use axum::{Json, Router}; +use hyper::StatusCode; use radicle::crypto::{PublicKey, Signature}; use serde::{Deserialize, Serialize}; use serde_json::json; use time::{Duration, OffsetDateTime}; -use crate::api::auth::{AuthState, DateTime, Session}; +use crate::api::auth::{AuthState, Session}; use crate::api::axum_extra::Path; use crate::api::error::Error; use crate::api::Context; @@ -41,24 +42,26 @@ async fn session_create_handler(State(ctx): State) -> impl IntoResponse .take(32) .collect::(); let signer = ctx.profile.signer().map_err(Error::from)?; - let expiration_time = OffsetDateTime::now_utc() - .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) - .unwrap(); let session = Session { status: AuthState::Unauthorized, public_key: *signer.public_key(), - issued_at: DateTime(OffsetDateTime::now_utc()), - expires_at: DateTime(expiration_time), + issued_at: OffsetDateTime::now_utc(), + expires_at: OffsetDateTime::now_utc() + .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) + .unwrap(), }; let mut sessions = ctx.sessions.write().await; sessions.insert(session_id.clone(), session.clone()); - Ok::<_, Error>(Json(json!({ - "sessionId": session_id, - "publicKey": session.public_key, - "issuedAt": session.issued_at, - "expiresAt": session.expires_at - }))) + Ok::<_, Error>(( + StatusCode::CREATED, + Json(json!({ + "sessionId": session_id, + "publicKey": session.public_key, + "issuedAt": session.issued_at, + "expiresAt": session.expires_at + })), + )) } /// Get a session. @@ -73,8 +76,8 @@ async fn session_handler( Ok::<_, Error>(Json(json!({ "status": session.status, "publicKey": session.public_key, - "issuedAt": session.issued_at, - "expiresAt": session.expires_at + "issuedAt": session.issued_at.unix_timestamp(), + "expiresAt": session.expires_at.unix_timestamp() }))) } @@ -86,12 +89,12 @@ async fn session_signin_handler( Json(request): Json, ) -> impl IntoResponse { let mut sessions = ctx.sessions.write().await; - let session = sessions.get(&session_id).ok_or(Error::NotFound)?; + let session = sessions.get_mut(&session_id).ok_or(Error::NotFound)?; if session.status == AuthState::Unauthorized { if session.public_key != request.pk { return Err(Error::Auth("Invalid public key")); } - if session.expires_at <= DateTime(OffsetDateTime::now_utc()) { + if session.expires_at <= OffsetDateTime::now_utc() { return Err(Error::Auth("Session expired")); } let payload = format!("{}:{}", session_id, request.pk); @@ -99,16 +102,10 @@ async fn session_signin_handler( .pk .verify(payload.as_bytes(), &request.sig) .map_err(Error::from)?; - let expiration_time = OffsetDateTime::now_utc() + session.status = AuthState::Authorized; + session.expires_at = OffsetDateTime::now_utc() .checked_add(AUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); - let session = Session { - status: AuthState::Authorized, - public_key: request.pk, - issued_at: session.issued_at.to_owned(), - expires_at: DateTime(expiration_time), - }; - sessions.insert(session_id.clone(), session); return Ok::<_, Error>(Json(json!({ "success": true }))); } @@ -122,7 +119,10 @@ mod routes { use axum::http::StatusCode; use radicle_cli::commands::rad_web::{self, SessionInfo}; - use crate::api::test::{self, get, post, put}; + use crate::api::{ + auth::{AuthState, Session}, + test::{self, get, post, put}, + }; #[tokio::test] async fn test_session() { @@ -130,12 +130,19 @@ mod routes { let ctx = test::seed(tmp.path()); let app = super::router(ctx.to_owned()); - // Create session + // Create session. let response = post(&app, "/sessions", None).await; - assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.status(), StatusCode::CREATED); let json = response.json().await; let session_info: SessionInfo = serde_json::from_value(json).unwrap(); + // Check that an unauthorized session has been created. + let response = get(&app, format!("/sessions/{}", session_info.session_id)).await; + assert_eq!(response.status(), StatusCode::OK); + let json = response.json().await; + let body: Session = serde_json::from_value(json).unwrap(); + assert_eq!(body.status, AuthState::Unauthorized); + // Create request body let signer = ctx.profile.signer().unwrap(); let signature = rad_web::sign(signer, &session_info).unwrap(); @@ -151,10 +158,13 @@ mod routes { Some(Body::from(body)), ) .await; - assert_eq!(response.status(), StatusCode::OK); + // Check that session has been authorized. let response = get(&app, format!("/sessions/{}", session_info.session_id)).await; assert_eq!(response.status(), StatusCode::OK); + let json = response.json().await; + let body: Session = serde_json::from_value(json).unwrap(); + assert_eq!(body.status, AuthState::Authorized); } } From 811b70a5cbd9a6e220ab7e5efa8b0b182908a28d Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 Feb 2023 22:15:13 -0300 Subject: [PATCH 047/264] Extract `json!` into `api::json` Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/json.rs | 13 +++++++++++++ radicle-httpd/src/api/v1/sessions.rs | 16 +++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 00b8e8bf0..fdc6b55d4 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -14,6 +14,8 @@ use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; use radicle_surf::{Commit, Stats}; +use crate::api::auth::Session; + /// Returns JSON of a commit. pub(crate) fn commit(commit: &Commit) -> Value { json!({ @@ -32,6 +34,17 @@ pub(crate) fn commit(commit: &Commit) -> Value { }) } +/// Returns JSON of a session. +pub(crate) fn session(session_id: String, session: &Session) -> Value { + json!({ + "sessionId": session_id, + "status": session.status, + "publicKey": session.public_key, + "issuedAt": session.issued_at.unix_timestamp(), + "expiresAt": session.expires_at.unix_timestamp() + }) +} + /// Returns JSON for a blob with a given `path`. pub(crate) fn blob(blob: &Blob, path: &str) -> Value { json!({ diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index d06610244..518cd4365 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -7,12 +7,12 @@ use axum::{Json, Router}; use hyper::StatusCode; use radicle::crypto::{PublicKey, Signature}; use serde::{Deserialize, Serialize}; -use serde_json::json; use time::{Duration, OffsetDateTime}; use crate::api::auth::{AuthState, Session}; use crate::api::axum_extra::Path; use crate::api::error::Error; +use crate::api::json; use crate::api::Context; pub const UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60); @@ -55,12 +55,7 @@ async fn session_create_handler(State(ctx): State) -> impl IntoResponse Ok::<_, Error>(( StatusCode::CREATED, - Json(json!({ - "sessionId": session_id, - "publicKey": session.public_key, - "issuedAt": session.issued_at, - "expiresAt": session.expires_at - })), + Json(json::session(session_id, &session)), )) } @@ -73,12 +68,7 @@ async fn session_handler( let sessions = ctx.sessions.read().await; let session = sessions.get(&session_id).ok_or(Error::NotFound)?; - Ok::<_, Error>(Json(json!({ - "status": session.status, - "publicKey": session.public_key, - "issuedAt": session.issued_at.unix_timestamp(), - "expiresAt": session.expires_at.unix_timestamp() - }))) + Ok::<_, Error>(Json(json::session(session_id, session))) } /// Update session. From 2335bc7ecc46336883075840d1cf59c23c668f4f Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 19 Jan 2023 10:50:08 +0100 Subject: [PATCH 048/264] httpd: Add issue endpoints Signed-off-by: Sebastian Martinez --- radicle-httpd/Cargo.toml | 1 + radicle-httpd/src/api/error.rs | 4 ++ radicle-httpd/src/api/json.rs | 1 + radicle-httpd/src/api/v1/projects.rs | 103 +++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index a2b536084..1654d6bcf 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -17,6 +17,7 @@ logfmt = [ [dependencies] anyhow = { version = "1" } axum = { version = "0.6.2", default-features = false, features = ["headers", "json", "query", "tokio"] } +axum-auth = { version= "0.4.0", default-features = false, features = ["auth-bearer"] } axum-server = { version = "0.4.4", default-features = false } chrono = { version = "0.4.22" } fastrand = { version = "1.7.0" } diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 7995f5a6f..b6b48d7d2 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -30,6 +30,10 @@ pub enum Error { #[error(transparent)] Storage(#[from] radicle::storage::Error), + /// Cob issue error. + #[error(transparent)] + CobIssue(#[from] radicle::cob::issue::Error), + /// Cob store error. #[error(transparent)] CobStore(#[from] radicle::cob::store::Error), diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index fdc6b55d4..1f6530b6c 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -85,6 +85,7 @@ pub(crate) fn issue(id: IssueId, issue: Issue) -> Value { json!({ "id": id.to_string(), "author": issue.author(), + "assignees": issue.assigned().collect::>(), "title": issue.title(), "state": issue.state(), "discussion": issue.comments().collect::(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 0a21a8da8..13ee94ca6 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -4,15 +4,17 @@ use axum::extract::State; use axum::handler::Handler; use axum::http::{header, HeaderValue}; use axum::response::IntoResponse; -use axum::routing::get; +use axum::routing::{get, patch, post}; use axum::{Json, Router}; +use axum_auth::AuthBearer; use hyper::StatusCode; use serde::{Deserialize, Serialize}; use serde_json::json; use tower_http::set_header::SetResponseHeaderLayer; -use radicle::cob::issue::Issues; +use radicle::cob::issue::{Action, Issues}; use radicle::cob::patch::Patches; +use radicle::cob::{thread, Tag}; use radicle::identity::Id; use radicle::node::NodeId; use radicle::storage::{git::paths, ReadRepository, WriteStorage}; @@ -46,8 +48,14 @@ pub fn router(ctx: Context) -> Router { .route("/projects/:project/remotes/:peer", get(remote_handler)) .route("/projects/:project/blob/:sha/*path", get(blob_handler)) .route("/projects/:project/readme/:sha", get(readme_handler)) - .route("/projects/:project/issues", get(issues_handler)) - .route("/projects/:project/issues/:id", get(issue_handler)) + .route( + "/projects/:project/issues", + post(issue_create_handler).get(issues_handler), + ) + .route( + "/projects/:project/issues/:id", + patch(issue_update_handler).get(issue_handler), + ) .route("/projects/:project/patches", get(patches_handler)) .route("/projects/:project/patches/:id", get(patch_handler)) .with_state(ctx) @@ -391,6 +399,92 @@ async fn issues_handler( Ok::<_, Error>(Json(issues)) } +#[derive(Debug, Deserialize, Serialize)] +pub struct IssueCreate { + pub title: String, + pub description: String, + pub tags: Vec, +} + +/// Create a new issue. +/// `POST /projects/:project/issues` +async fn issue_create_handler( + State(ctx): State, + AuthBearer(token): AuthBearer, + Path(project): Path, + Json(issue): Json, +) -> impl IntoResponse { + let sessions = ctx.sessions.read().await; + sessions.get(&token).ok_or(Error::Auth("Unauthorized"))?; + let storage = &ctx.profile.storage; + let signer = ctx + .profile + .signer() + .map_err(|_| Error::Auth("Unauthorized"))?; + let repo = storage.repository(project)?; + let mut issues = Issues::open(ctx.profile.public_key, &repo)?; + issues + .create(issue.title, issue.description, &issue.tags, &signer) + .map_err(Error::from)?; + + Ok::<_, Error>((StatusCode::CREATED, Json(json!({ "success": true })))) +} + +/// Update an issue. +/// `PATCH /projects/:project/issues/:id` +async fn issue_update_handler( + State(ctx): State, + AuthBearer(token): AuthBearer, + Path((project, issue_id)): Path<(Id, Oid)>, + Json(action): Json, +) -> impl IntoResponse { + let sessions = ctx.sessions.write().await; + sessions.get(&token).ok_or(Error::Auth("Unauthorized"))?; + let storage = &ctx.profile.storage; + let signer = ctx.profile.signer().unwrap(); + let repo = storage.repository(project)?; + let mut issues = Issues::open(ctx.profile.public_key, &repo)?; + let mut issue = issues.get_mut(&issue_id.into())?; + match action { + Action::Assign { add, remove } => { + issue.assign(add, &signer)?; + issue.unassign(remove, &signer)?; + } + Action::Lifecycle { state } => { + issue.lifecycle(state, &signer)?; + } + Action::Tag { add, remove } => { + issue.tag(add, remove, &signer)?; + } + Action::Edit { title } => { + issue.edit(title, &signer)?; + } + Action::Thread { action } => { + let mut actor = thread::Actor::new(ctx.profile.signer().unwrap()); + match action { + thread::Action::Comment { body, reply_to } => { + if let Some(reply_to) = reply_to { + issue.comment(body, reply_to, &signer)?; + } else { + issue.thread(body, &signer)?; + } + } + thread::Action::React { to, reaction, .. } => { + issue.react(to, reaction, &signer)?; + } + thread::Action::Edit { id, body } => { + actor.edit(id, &body); + } + thread::Action::Redact { id } => { + actor.redact(id); + } + } + } + }; + + Ok::<_, Error>(Json(json!({ "success": true }))) +} + /// Get project issue. /// `GET /projects/:project/issues/:id` async fn issue_handler( @@ -909,6 +1003,7 @@ mod routes { "state": { "status": "open" }, + "assignees": [], "discussion": [ { "author": { From 83a635976d069d3a88001ea159bcc9c5a1238c78 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 6 Feb 2023 11:30:10 -0300 Subject: [PATCH 049/264] httpd: Add DELETE route to remove sessions Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/sessions.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 518cd4365..977f96c4c 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -4,6 +4,7 @@ use axum::extract::State; use axum::response::IntoResponse; use axum::routing::{post, put}; use axum::{Json, Router}; +use axum_auth::AuthBearer; use hyper::StatusCode; use radicle::crypto::{PublicKey, Signature}; use serde::{Deserialize, Serialize}; @@ -23,7 +24,9 @@ pub fn router(ctx: Context) -> Router { .route("/sessions", post(session_create_handler)) .route( "/sessions/:id", - put(session_signin_handler).get(session_handler), + put(session_signin_handler) + .get(session_handler) + .delete(session_delete_handler), ) .with_state(ctx) } @@ -103,6 +106,22 @@ async fn session_signin_handler( Err(Error::Auth("Session already authorized")) } +/// Delete session. +/// `DELETE /sessions/:id` +async fn session_delete_handler( + State(ctx): State, + AuthBearer(token): AuthBearer, + Path(session_id): Path, +) -> impl IntoResponse { + if token != session_id { + return Err(Error::Auth("Not authorized to delete this session")); + } + let mut sessions = ctx.sessions.write().await; + sessions.remove_entry(&token).ok_or(Error::NotFound)?; + + Ok::<_, Error>(Json(json!({ "success": true }))) +} + #[cfg(test)] mod routes { use axum::body::Body; From 61902ca47b74672e586ed0d8bb86b9ea47834156 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 8 Feb 2023 16:29:42 +0100 Subject: [PATCH 050/264] cli: Have `init` trigger routing table updates Before this change, nodes had to be restarted to announce newly initialized projects. --- radicle-httpd/src/api/test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 92ca2f925..96278d179 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -90,6 +90,7 @@ pub fn seed(dir: &Path) -> Context { interactive: false.into(), setup_signing: false, set_upstream: false, + sync: false, }, &profile, ) From d88858e58beecab83373cb81f76b2b25bfac023f Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 9 Feb 2023 12:39:34 -0300 Subject: [PATCH 051/264] radicle-httpd: Add DELETE to allowed methods Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 216786d9e..cd819bc92 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -73,7 +73,13 @@ pub fn router(ctx: Context) -> Router { CorsLayer::new() .max_age(Duration::from_secs(86400)) .allow_origin(cors::Any) - .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::PUT]) + .allow_methods([ + Method::GET, + Method::POST, + Method::PATCH, + Method::PUT, + Method::DELETE, + ]) .allow_headers([CONTENT_TYPE, AUTHORIZATION]), ) } From a6d120fa28d4e5845cc178bf601d9e798d927798 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 7 Feb 2023 12:46:58 -0300 Subject: [PATCH 052/264] httpd: Add `api::test::patch` request method Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/auth.rs | 6 +- radicle-httpd/src/api/json.rs | 6 +- radicle-httpd/src/api/test.rs | 72 ++++++++-- radicle-httpd/src/api/v1/projects.rs | 205 ++++++++++++++++++++++++++- radicle-httpd/src/api/v1/sessions.rs | 27 ++-- 5 files changed, 289 insertions(+), 27 deletions(-) diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index 14c52049f..f4c0daba3 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -1,6 +1,10 @@ use radicle::crypto::PublicKey; use serde::{Deserialize, Serialize}; -use time::{serde::timestamp, OffsetDateTime}; +use time::serde::timestamp; +use time::{Duration, OffsetDateTime}; + +pub const UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60); +pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1); #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] #[serde(rename_all = "lowercase")] diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 1f6530b6c..ae5e6980e 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -8,7 +8,7 @@ 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::Timestamp; +use radicle::cob::{OpId, Timestamp}; use radicle::identity::PublicKey; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; @@ -129,6 +129,7 @@ struct Author { #[derive(Serialize)] #[serde(rename_all = "camelCase")] struct Comment { + id: OpId, author: Author, body: String, reactions: [String; 0], @@ -143,8 +144,9 @@ impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { fn from_iter>(iter: I) -> Self { let mut comments = Vec::new(); - for (_, comment) in iter { + for (id, comment) in iter { comments.push(Comment { + id: id.to_owned(), author: Author { id: comment.author(), }, diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 96278d179..fd6900c07 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -7,6 +7,7 @@ use axum::body::Body; use axum::http::{Method, Request}; use axum::Router; use serde_json::Value; +use time::OffsetDateTime; use tower::ServiceExt; use radicle::cob::issue::Issues; @@ -17,7 +18,7 @@ use radicle_cli::commands::rad_init; use radicle_crypto::ssh::keystore::MemorySigner; use radicle_crypto::Signer; -use crate::api::Context; +use crate::api::{auth, Context}; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; @@ -135,40 +136,89 @@ pub fn seed(dir: &Path) -> Context { } } +/// Adds an authorized session to the Context::sessions HashMap. +pub async fn create_session(ctx: Context) { + let issued_at = OffsetDateTime::now_utc(); + let mut sessions = ctx.sessions.write().await; + sessions.insert( + String::from("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"), + auth::Session { + status: auth::AuthState::Authorized, + public_key: ctx.profile.public_key, + issued_at, + expires_at: issued_at + .checked_add(auth::AUTHORIZED_SESSIONS_EXPIRATION) + .unwrap(), + }, + ); +} + pub async fn get(app: &Router, path: impl ToString) -> Response { Response( app.clone() - .oneshot(request(path, Method::GET, None)) + .oneshot(request(path, Method::GET, None, None)) .await .unwrap(), ) } -pub async fn post(app: &Router, path: impl ToString, body: Option) -> Response { +pub async fn post( + app: &Router, + path: impl ToString, + body: Option, + auth: Option, +) -> Response { Response( app.clone() - .oneshot(request(path, Method::POST, body)) + .oneshot(request(path, Method::POST, body, auth)) .await .unwrap(), ) } -pub async fn put(app: &Router, path: impl ToString, body: Option) -> Response { +pub async fn patch( + app: &Router, + path: impl ToString, + body: Option, + auth: Option, +) -> Response { Response( app.clone() - .oneshot(request(path, Method::PUT, body)) + .oneshot(request(path, Method::PATCH, body, auth)) .await .unwrap(), ) } -fn request(path: impl ToString, method: Method, body: Option) -> Request { - Request::builder() +pub async fn put( + app: &Router, + path: impl ToString, + body: Option, + auth: Option, +) -> Response { + Response( + app.clone() + .oneshot(request(path, Method::PUT, body, auth)) + .await + .unwrap(), + ) +} + +fn request( + path: impl ToString, + method: Method, + body: Option, + auth: Option, +) -> Request { + let mut request = Request::builder() .method(method) .uri(path.to_string()) - .header("Content-Type", "application/json") - .body(body.unwrap_or_else(Body::empty)) - .unwrap() + .header("Content-Type", "application/json"); + if let Some(token) = auth { + request = request.header("Authorization", format!("Bearer {token}")); + } + + request.body(body.unwrap_or_else(Body::empty)).unwrap() } pub struct Response(axum::response::Response); diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 13ee94ca6..56b3419b3 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -542,10 +542,11 @@ async fn patch_handler( #[cfg(test)] mod routes { + use axum::body::Body; use axum::http::StatusCode; use serde_json::json; - use crate::api::test::{self, get, HEAD, HEAD_1}; + use crate::api::test::{self, get, patch, post, HEAD, HEAD_1}; #[tokio::test] async fn test_projects_root() { @@ -995,7 +996,7 @@ mod routes { response.json().await, json!([ { - "id": "458bbd9f6d47eed3d60cd905141687ad1f99251e", + "id": "90c8f0bab59d9efe35e234acf3abce4168bba6b4", "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, @@ -1006,6 +1007,7 @@ mod routes { "assignees": [], "discussion": [ { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, @@ -1021,6 +1023,205 @@ mod routes { ); } + #[tokio::test] + async fn test_projects_issues_create() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ + "title": "Issue #2", + "description": "Change 'hello world' to 'hello everyone'", + "tags": ["bug"], + })) + .unwrap(); + let response = post( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues", + Some(Body::from(body)), + Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::CREATED); + assert_eq!(response.json().await, json!({ "success": true })); + + let issue_id = "bd2bde30b52db0fc2dae35f4e97ff9fdcc93dead"; + let response = get( + &app, + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{issue_id}"), + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": issue_id, + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "assignees": [], + "title": "Issue #2", + "state": { + "status": "open", + }, + "discussion": [{ + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "body": "Change 'hello world' to 'hello everyone'", + "reactions": [], + "timestamp": 1673001014, + "replyTo": null, + }], + "tags": [ + "bug", + ], + }) + ); + } + + #[tokio::test] + async fn test_projects_issues_comment() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ + "type": "thread", + "action": { + "type": "comment", + "body": "This is first-level comment", + } + })) + .unwrap(); + let response = patch( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + Some(Body::from(body)), + Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.json().await, json!({ "success": true })); + + let response = get( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": "90c8f0bab59d9efe35e234acf3abce4168bba6b4", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "assignees": [], + "title": "Issue #1", + "state": { + "status": "open", + }, + "discussion": [ + { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "body": "Change 'hello world' to 'hello everyone'", + "reactions": [], + "timestamp": 1673001014, + "replyTo": null, + }, + { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/4", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "body": "This is first-level comment", + "reactions": [], + "timestamp": 1673001014, + "replyTo": null, + }, + ], + "tags": [], + }) + ); + } + + #[tokio::test] + async fn test_projects_issues_reply() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ + "type":"thread", + "action": { + "type": "comment", + "body": "This is a reply to the first comment", + "replyTo": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + }})) + .unwrap(); + let response = patch( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + Some(Body::from(body)), + Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.json().await, json!({ "success": true })); + + let response = get( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": "90c8f0bab59d9efe35e234acf3abce4168bba6b4", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "assignees": [], + "title": "Issue #1", + "state": { + "status": "open", + }, + "discussion": [ + { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "body": "Change 'hello world' to 'hello everyone'", + "reactions": [], + "timestamp": 1673001014, + "replyTo": null, + }, + { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/4", + "author": { + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + }, + "body": "This is a reply to the first comment", + "reactions": [], + "timestamp": 1673001014, + "replyTo": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + }, + ], + "tags": [], + }) + ); + } + #[tokio::test] async fn test_projects_patches() { let tmp = tempfile::tempdir().unwrap(); diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index 977f96c4c..f2cdcc535 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -8,17 +8,14 @@ use axum_auth::AuthBearer; use hyper::StatusCode; use radicle::crypto::{PublicKey, Signature}; use serde::{Deserialize, Serialize}; -use time::{Duration, OffsetDateTime}; +use time::OffsetDateTime; -use crate::api::auth::{AuthState, Session}; +use crate::api::auth::{self, AuthState, Session}; use crate::api::axum_extra::Path; use crate::api::error::Error; use crate::api::json; use crate::api::Context; -pub const UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60); -pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1); - pub fn router(ctx: Context) -> Router { Router::new() .route("/sessions", post(session_create_handler)) @@ -50,7 +47,7 @@ async fn session_create_handler(State(ctx): State) -> impl IntoResponse public_key: *signer.public_key(), issued_at: OffsetDateTime::now_utc(), expires_at: OffsetDateTime::now_utc() - .checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION) + .checked_add(auth::UNAUTHORIZED_SESSIONS_EXPIRATION) .unwrap(), }; let mut sessions = ctx.sessions.write().await; @@ -97,7 +94,7 @@ async fn session_signin_handler( .map_err(Error::from)?; session.status = AuthState::Authorized; session.expires_at = OffsetDateTime::now_utc() - .checked_add(AUTHORIZED_SESSIONS_EXPIRATION) + .checked_add(auth::AUTHORIZED_SESSIONS_EXPIRATION) .unwrap(); return Ok::<_, Error>(Json(json!({ "success": true }))); @@ -140,16 +137,20 @@ mod routes { let app = super::router(ctx.to_owned()); // Create session. - let response = post(&app, "/sessions", None).await; - assert_eq!(response.status(), StatusCode::CREATED); + let response = post(&app, "/sessions", None, None).await; + let status = response.status(); let json = response.json().await; let session_info: SessionInfo = serde_json::from_value(json).unwrap(); + assert_eq!(status, StatusCode::CREATED); + // Check that an unauthorized session has been created. let response = get(&app, format!("/sessions/{}", session_info.session_id)).await; - assert_eq!(response.status(), StatusCode::OK); + let status = response.status(); let json = response.json().await; let body: Session = serde_json::from_value(json).unwrap(); + + assert_eq!(status, StatusCode::OK); assert_eq!(body.status, AuthState::Unauthorized); // Create request body @@ -165,15 +166,19 @@ mod routes { &app, format!("/sessions/{}", session_info.session_id), Some(Body::from(body)), + None, ) .await; + assert_eq!(response.status(), StatusCode::OK); // Check that session has been authorized. let response = get(&app, format!("/sessions/{}", session_info.session_id)).await; - assert_eq!(response.status(), StatusCode::OK); + let status = response.status(); let json = response.json().await; let body: Session = serde_json::from_value(json).unwrap(); + + assert_eq!(status, StatusCode::OK); assert_eq!(body.status, AuthState::Authorized); } } From 4a27cc3f6c5c84dab5428149691ea82a8ad2f7cb Mon Sep 17 00:00:00 2001 From: xphoniex Date: Fri, 10 Feb 2023 12:52:50 +0000 Subject: [PATCH 053/264] Update `radicle-git` and `radicle-surf` Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 6 +--- radicle-httpd/src/api/json.rs | 19 ++++++++--- radicle-httpd/src/api/test.rs | 2 ++ radicle-httpd/src/api/v1/projects.rs | 50 +++++++++++++++++----------- 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 1654d6bcf..a6b769336 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -24,6 +24,7 @@ fastrand = { version = "1.7.0" } flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } +radicle-surf = { version = "0.9.0", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } @@ -38,11 +39,6 @@ tracing-subscriber = { version = "0.3", default-features = false, features = ["s path = "../radicle" version = "0.2.0" -[dependencies.radicle-surf] -git = "https://github.com/radicle-dev/radicle-git" -features = ["serde"] -rev = "48022590d0439e7fb196605a573a725bbbf0e4d2" - [dev-dependencies] hyper = { version = "0.14.17", default-features = false, features = ["client"] } radicle-cli = { path = "../radicle-cli" } diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index ae5e6980e..e6aeff71b 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -1,6 +1,7 @@ //! Utilities for building JSON responses of our API. use std::path::Path; +use std::str; use serde::Serialize; use serde_json::{json, Value}; @@ -46,14 +47,22 @@ pub(crate) fn session(session_id: String, session: &Session) -> Value { } /// Returns JSON for a blob with a given `path`. -pub(crate) fn blob(blob: &Blob, path: &str) -> Value { - json!({ +pub(crate) fn blob>(blob: &Blob, path: &str) -> Value { + let mut response = json!({ "binary": blob.is_binary(), - "content": blob.content(), "name": name_in_path(path), "path": path, "lastCommit": commit(blob.commit()) - }) + }); + + if !blob.is_binary() { + match str::from_utf8(blob.content()) { + Ok(content) => response["content"] = content.into(), + Err(err) => return json!({ "error": err.to_string() }), + } + } + + response } /// Returns JSON for a tree with a given `path` and `stats`. @@ -85,9 +94,9 @@ pub(crate) fn issue(id: IssueId, issue: Issue) -> Value { json!({ "id": id.to_string(), "author": issue.author(), - "assignees": issue.assigned().collect::>(), "title": issue.title(), "state": issue.state(), + "assignees": issue.assigned().collect::>(), "discussion": issue.comments().collect::(), "tags": issue.tags().collect::>(), }) diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index fd6900c07..83e6c9635 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -22,6 +22,8 @@ use crate::api::{auth, Context}; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; +pub const PATCH_ID: &str = "4250f0117659ee4de9af99e699a63395cd6ffa1c"; +pub const ISSUE_ID: &str = "d8131af9738258fac139c4c96b71c02411f94892"; const PASSWORD: &str = "radicle"; diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 56b3419b3..06522960d 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -423,11 +423,14 @@ async fn issue_create_handler( .map_err(|_| Error::Auth("Unauthorized"))?; let repo = storage.repository(project)?; let mut issues = Issues::open(ctx.profile.public_key, &repo)?; - issues + let issue = issues .create(issue.title, issue.description, &issue.tags, &signer) .map_err(Error::from)?; - Ok::<_, Error>((StatusCode::CREATED, Json(json!({ "success": true })))) + Ok::<_, Error>(( + StatusCode::CREATED, + Json(json!({ "success": true, "id": issue.id().to_string() })), + )) } /// Update an issue. @@ -546,7 +549,9 @@ mod routes { use axum::http::StatusCode; use serde_json::json; - use crate::api::test::{self, get, patch, post, HEAD, HEAD_1}; + use crate::api::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; + + const CREATED_ISSUE_ID: &str = "768c6735912a34856552ae6a9ca77d728962bf31"; #[tokio::test] async fn test_projects_root() { @@ -635,7 +640,8 @@ mod routes { } ] } - ] + ], + "eof": "noneMissing", } } ], @@ -685,7 +691,8 @@ mod routes { } ] } - ] + ], + "eof": "noneMissing", } } ], @@ -759,7 +766,8 @@ mod routes { } ] } - ] + ], + "eof": "noneMissing", } } ], @@ -996,7 +1004,7 @@ mod routes { response.json().await, json!([ { - "id": "90c8f0bab59d9efe35e234acf3abce4168bba6b4", + "id": ISSUE_ID, "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, @@ -1044,19 +1052,21 @@ mod routes { .await; assert_eq!(response.status(), StatusCode::CREATED); - assert_eq!(response.json().await, json!({ "success": true })); + assert_eq!( + response.json().await, + json!({ "success": true, "id": CREATED_ISSUE_ID }) + ); - let issue_id = "bd2bde30b52db0fc2dae35f4e97ff9fdcc93dead"; let response = get( &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{issue_id}"), + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{CREATED_ISSUE_ID}"), ) .await; assert_eq!( response.json().await, json!({ - "id": issue_id, + "id": CREATED_ISSUE_ID, "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1098,7 +1108,7 @@ mod routes { .unwrap(); let response = patch( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), Some(Body::from(body)), Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), ) @@ -1109,14 +1119,14 @@ mod routes { let response = get( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), ) .await; assert_eq!( response.json().await, json!({ - "id": "90c8f0bab59d9efe35e234acf3abce4168bba6b4", + "id": ISSUE_ID, "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1168,7 +1178,7 @@ mod routes { .unwrap(); let response = patch( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), Some(Body::from(body)), Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), ) @@ -1179,14 +1189,14 @@ mod routes { let response = get( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/90c8f0bab59d9efe35e234acf3abce4168bba6b4", + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), ) .await; assert_eq!( response.json().await, json!({ - "id": "90c8f0bab59d9efe35e234acf3abce4168bba6b4", + "id": ISSUE_ID, "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1233,7 +1243,7 @@ mod routes { response.json().await, json!([ { - "id": "5de9f17ca5326258412ab02f9a5339b6482198ce", + "id": PATCH_ID, "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, @@ -1255,7 +1265,7 @@ mod routes { let response = get( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/5de9f17ca5326258412ab02f9a5339b6482198ce", + format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/{PATCH_ID}"), ) .await; @@ -1264,7 +1274,7 @@ mod routes { response.json().await, json!( { - "id": "5de9f17ca5326258412ab02f9a5339b6482198ce", + "id": PATCH_ID, "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, From 2395d5c32822cdfc1308f16248a4de9622034d6b Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Sun, 12 Feb 2023 16:15:51 +0100 Subject: [PATCH 054/264] cli: Track projects we initialize --- radicle-httpd/src/api/test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 83e6c9635..39eb7d610 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -94,6 +94,7 @@ pub fn seed(dir: &Path) -> Context { setup_signing: false, set_upstream: false, sync: false, + track: false, }, &profile, ) From 43f32c8360c4461f6990980509eedc74b5513ed4 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 9 Feb 2023 15:31:36 -0300 Subject: [PATCH 055/264] httpd: Add `assignees` to `Issues::create` Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/test.rs | 3 ++- radicle-httpd/src/api/v1/projects.rs | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 39eb7d610..06e29df1f 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -23,7 +23,7 @@ use crate::api::{auth, Context}; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; pub const PATCH_ID: &str = "4250f0117659ee4de9af99e699a63395cd6ffa1c"; -pub const ISSUE_ID: &str = "d8131af9738258fac139c4c96b71c02411f94892"; +pub const ISSUE_ID: &str = "8adca8aad2a2cb99b9847d20193930cde2042a57"; const PASSWORD: &str = "radicle"; @@ -113,6 +113,7 @@ pub fn seed(dir: &Path) -> Context { "Issue #1".to_string(), "Change 'hello world' to 'hello everyone'".to_string(), &[], + &[], &signer, ) .unwrap(); diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 06522960d..3e7cbd6b4 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -14,7 +14,7 @@ use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::issue::{Action, Issues}; use radicle::cob::patch::Patches; -use radicle::cob::{thread, Tag}; +use radicle::cob::{thread, ActorId, Tag}; use radicle::identity::Id; use radicle::node::NodeId; use radicle::storage::{git::paths, ReadRepository, WriteStorage}; @@ -404,6 +404,7 @@ pub struct IssueCreate { pub title: String, pub description: String, pub tags: Vec, + pub assignees: Vec, } /// Create a new issue. @@ -424,7 +425,13 @@ async fn issue_create_handler( let repo = storage.repository(project)?; let mut issues = Issues::open(ctx.profile.public_key, &repo)?; let issue = issues - .create(issue.title, issue.description, &issue.tags, &signer) + .create( + issue.title, + issue.description, + &issue.tags, + &issue.assignees, + &signer, + ) .map_err(Error::from)?; Ok::<_, Error>(( @@ -551,7 +558,7 @@ mod routes { use crate::api::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; - const CREATED_ISSUE_ID: &str = "768c6735912a34856552ae6a9ca77d728962bf31"; + const CREATED_ISSUE_ID: &str = "b56febfba1e7dd20f4aea43ca2fe9dcf1fd448ba"; #[tokio::test] async fn test_projects_root() { @@ -1041,6 +1048,7 @@ mod routes { "title": "Issue #2", "description": "Change 'hello world' to 'hello everyone'", "tags": ["bug"], + "assignees": [], })) .unwrap(); let response = post( @@ -1147,7 +1155,7 @@ mod routes { "replyTo": null, }, { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/4", + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/5", "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1217,7 +1225,7 @@ mod routes { "replyTo": null, }, { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/4", + "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/5", "author": { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, From 95dca98a4306168a53a4bc766615702f6a5a799e Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 10 Feb 2023 15:29:03 -0300 Subject: [PATCH 056/264] httpd: Add delegates to project routes Signed-off-by: Sebastian Martinez --- radicle-httpd/Cargo.toml | 1 + radicle-httpd/src/api.rs | 11 +++++++++-- radicle-httpd/src/api/error.rs | 4 ++++ radicle-httpd/src/api/v1/delegates.rs | 5 ++++- radicle-httpd/src/api/v1/projects.rs | 5 +++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index a6b769336..c727b661c 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -25,6 +25,7 @@ flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } radicle-surf = { version = "0.9.0", default-features = false, features = ["serde"] } +nonempty = { version = "0.8.1", features = ["serialize"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index cd819bc92..91027b31a 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -48,11 +48,14 @@ impl Context { let storage = &self.profile.storage; let repo = storage.repository(id)?; let (_, head) = repo.head()?; - let payload = repo.project_of(self.profile.id())?; + let doc = repo.identity_of(self.profile.id())?; + let payload = doc.project()?; + let delegates = doc.delegates; let issues = (Issues::open(self.profile.public_key, &repo)?).count()?; Ok(project::Info { payload, + delegates, head, issues, patches: 0, @@ -126,10 +129,13 @@ pub struct PaginationQuery { } mod project { + use nonempty::NonEmpty; + use serde::Serialize; + use radicle::git::Oid; use radicle::identity::project::Project; use radicle::identity::Id; - use serde::Serialize; + use radicle::prelude::Did; /// Project info. #[derive(Serialize)] @@ -138,6 +144,7 @@ mod project { /// Project metadata. #[serde(flatten)] pub payload: Project, + pub delegates: NonEmpty, pub head: Oid, pub patches: usize, pub issues: usize, diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index b6b48d7d2..e8bdef7cb 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -42,6 +42,10 @@ pub enum Error { #[error(transparent)] GitProject(#[from] radicle::storage::git::ProjectError), + /// Project doc error. + #[error(transparent)] + ProjectDoc(#[from] radicle::identity::doc::PayloadError), + /// Surf directory error. #[error(transparent)] SurfDir(#[from] radicle_surf::fs::error::Directory), diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index d72c91bb1..e8b3d09da 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -42,7 +42,8 @@ async fn delegates_projects_handler( let Ok(doc) = repo.identity_of(ctx.profile.id()) else { return None }; let Ok(payload) = doc.project() else { return None }; - if !doc.delegates.iter().any(|d| *d == delegate) { + let delegates = doc.delegates; + if !delegates.iter().any(|d| *d == delegate) { return None; } @@ -51,6 +52,7 @@ async fn delegates_projects_handler( Some(Info { payload, + delegates, head, issues, patches: 0, @@ -89,6 +91,7 @@ mod routes { "name": "hello-world", "description": "Rad repository for tests", "defaultBranch": "master", + "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, "patches": 0, "issues": 1, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 3e7cbd6b4..a012c3114 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -78,11 +78,14 @@ async fn project_root_handler( let Ok(repo) = storage.repository(id) else { return None }; let Ok((_, head)) = repo.head() else { return None }; let Ok(payload) = repo.project_of(ctx.profile.id()) else { return None }; + let Ok(doc) = repo.identity_of(ctx.profile.id()) else { return None }; let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; let Ok(issues) = (*issues).count() else { return None }; + let delegates = doc.delegates; Some(Info { payload, + delegates, head, issues, patches: 0, @@ -574,6 +577,7 @@ mod routes { "name": "hello-world", "description": "Rad repository for tests", "defaultBranch": "master", + "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, "patches": 0, "issues": 1, @@ -596,6 +600,7 @@ mod routes { "name": "hello-world", "description": "Rad repository for tests", "defaultBranch": "master", + "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, "patches": 0, "issues": 1, From 8b90dd674f1f8b1c00a9fa789e4bacb199053d87 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 14 Feb 2023 18:31:18 +0100 Subject: [PATCH 057/264] Make `ReadStorage` more usable Previously, we were not able to access a `ReadRepository` instance through `ReadStorage`. With this change, we are able to. --- radicle-httpd/src/api.rs | 2 +- radicle-httpd/src/api/test.rs | 2 +- radicle-httpd/src/api/v1/delegates.rs | 2 +- radicle-httpd/src/api/v1/projects.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 91027b31a..e0b6433b1 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -14,7 +14,7 @@ use tower_http::cors::{self, CorsLayer}; use radicle::cob::issue::Issues; use radicle::identity::Id; -use radicle::storage::{ReadRepository, WriteStorage}; +use radicle::storage::{ReadRepository, ReadStorage}; use radicle::Profile; mod auth; diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 06e29df1f..191794251 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -13,7 +13,7 @@ use tower::ServiceExt; use radicle::cob::issue::Issues; use radicle::cob::patch::{MergeTarget, Patches}; use radicle::git::raw as git2; -use radicle::storage::WriteStorage; +use radicle::storage::ReadStorage; use radicle_cli::commands::rad_init; use radicle_crypto::ssh::keystore::MemorySigner; use radicle_crypto::Signer; diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index e8b3d09da..8ae6a2569 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -5,7 +5,7 @@ use axum::{Json, Router}; use radicle::cob::issue::Issues; use radicle::identity::Did; -use radicle::storage::{ReadRepository, WriteStorage}; +use radicle::storage::{ReadRepository, ReadStorage}; use crate::api::axum_extra::{Path, Query}; use crate::api::error::Error; diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index a012c3114..9bd91f8d5 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -17,7 +17,7 @@ use radicle::cob::patch::Patches; use radicle::cob::{thread, ActorId, Tag}; use radicle::identity::Id; use radicle::node::NodeId; -use radicle::storage::{git::paths, ReadRepository, WriteStorage}; +use radicle::storage::{git::paths, ReadRepository, ReadStorage}; use radicle_surf::{Glob, Oid, Repository}; use crate::api::axum_extra::{Path, Query}; From 7e9b9c944bf915c5b9cf0e4007164cb6a34a7322 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 16 Feb 2023 13:44:43 +0000 Subject: [PATCH 058/264] httpd: Return correct count for `patches` Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 4 +++- radicle-httpd/src/api/v1/delegates.rs | 7 +++++-- radicle-httpd/src/api/v1/projects.rs | 8 +++++--- radicle-httpd/src/lib.rs | 4 +--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index e0b6433b1..d0b2dd6d3 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -13,6 +13,7 @@ use tokio::sync::RwLock; use tower_http::cors::{self, CorsLayer}; use radicle::cob::issue::Issues; +use radicle::cob::patch::Patches; use radicle::identity::Id; use radicle::storage::{ReadRepository, ReadStorage}; use radicle::Profile; @@ -52,13 +53,14 @@ impl Context { let payload = doc.project()?; let delegates = doc.delegates; let issues = (Issues::open(self.profile.public_key, &repo)?).count()?; + let patches = (Patches::open(self.profile.public_key, &repo)?).count()?; Ok(project::Info { payload, delegates, head, issues, - patches: 0, + patches, id, }) } diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 8ae6a2569..01138768b 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -4,6 +4,7 @@ use axum::routing::get; use axum::{Json, Router}; use radicle::cob::issue::Issues; +use radicle::cob::patch::Patches; use radicle::identity::Did; use radicle::storage::{ReadRepository, ReadStorage}; @@ -49,13 +50,15 @@ async fn delegates_projects_handler( let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; let Ok(issues) = (*issues).count() else { return None }; + let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(patches) = (*patches).count() else { return None }; Some(Info { payload, delegates, head, issues, - patches: 0, + patches, id, }) }) @@ -93,7 +96,7 @@ mod routes { "defaultBranch": "master", "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, - "patches": 0, + "patches": 1, "issues": 1, "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 9bd91f8d5..0655006ab 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -81,6 +81,8 @@ async fn project_root_handler( let Ok(doc) = repo.identity_of(ctx.profile.id()) else { return None }; let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; let Ok(issues) = (*issues).count() else { return None }; + let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(patches) = (*patches).count() else { return None }; let delegates = doc.delegates; Some(Info { @@ -88,7 +90,7 @@ async fn project_root_handler( delegates, head, issues, - patches: 0, + patches, id, }) }) @@ -579,7 +581,7 @@ mod routes { "defaultBranch": "master", "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, - "patches": 0, + "patches": 1, "issues": 1, "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" } @@ -602,7 +604,7 @@ mod routes { "defaultBranch": "master", "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, - "patches": 0, + "patches": 1, "issues": 1, "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" }) diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 4674b0112..be643f562 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -16,9 +16,7 @@ use axum::body::Body; use axum::body::{BoxBody, Bytes}; use axum::extract::{ConnectInfo, Path as AxumPath, RawQuery}; use axum::http::header::HeaderName; -use axum::http::HeaderMap; -use axum::http::{Method, StatusCode}; -use axum::http::{Request, Response}; +use axum::http::{HeaderMap, Method, Request, Response, StatusCode}; use axum::response::IntoResponse; use axum::routing::any; use axum::{Extension, Router}; From 3cf1accdddb2a183b447d81dcd4c4e98c3694dad Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 16 Feb 2023 09:39:42 -0300 Subject: [PATCH 059/264] Use `Did` in a bunch more places Instead of showing just public keys we need to show the fully qualified spec for a NID which is prefixed by `did:key:` While we want to maintain `Author` and `ActorId` for internal use. Signed-off-by: Sebastian Martinez --- radicle-httpd/Cargo.toml | 1 + radicle-httpd/src/api/json.rs | 12 ++---------- radicle-httpd/src/api/v1/projects.rs | 25 +++++++++++++------------ 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index c727b661c..fff785815 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -42,6 +42,7 @@ version = "0.2.0" [dev-dependencies] hyper = { version = "0.14.17", default-features = false, features = ["client"] } +pretty_assertions = { version = "1.3.0" } radicle-cli = { path = "../radicle-cli" } radicle-crypto = { path = "../radicle-crypto" } tempfile = { version = "3.3.0" } diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index e6aeff71b..dd4bed70b 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -9,8 +9,7 @@ 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::{OpId, Timestamp}; -use radicle::identity::PublicKey; +use radicle::cob::{Author, OpId, Timestamp}; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; use radicle_surf::{Commit, Stats}; @@ -130,11 +129,6 @@ fn name_in_path(path: &str) -> &str { } } -#[derive(Serialize)] -struct Author { - id: PublicKey, -} - #[derive(Serialize)] #[serde(rename_all = "camelCase")] struct Comment { @@ -156,9 +150,7 @@ impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { for (id, comment) in iter { comments.push(Comment { id: id.to_owned(), - author: Author { - id: comment.author(), - }, + author: comment.author().into(), body: comment.body().to_owned(), reactions: [], timestamp: comment.timestamp(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 0655006ab..68fcaffb6 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -559,6 +559,7 @@ async fn patch_handler( mod routes { use axum::body::Body; use axum::http::StatusCode; + use pretty_assertions::assert_eq; use serde_json::json; use crate::api::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; @@ -1020,7 +1021,7 @@ mod routes { { "id": ISSUE_ID, "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "title": "Issue #1", "state": { @@ -1031,7 +1032,7 @@ mod routes { { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1083,7 +1084,7 @@ mod routes { json!({ "id": CREATED_ISSUE_ID, "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "assignees": [], "title": "Issue #2", @@ -1093,7 +1094,7 @@ mod routes { "discussion": [{ "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1143,7 +1144,7 @@ mod routes { json!({ "id": ISSUE_ID, "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "assignees": [], "title": "Issue #1", @@ -1154,7 +1155,7 @@ mod routes { { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1164,7 +1165,7 @@ mod routes { { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/5", "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "body": "This is first-level comment", "reactions": [], @@ -1213,7 +1214,7 @@ mod routes { json!({ "id": ISSUE_ID, "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "assignees": [], "title": "Issue #1", @@ -1224,7 +1225,7 @@ mod routes { { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1234,7 +1235,7 @@ mod routes { { "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/5", "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "body": "This is a reply to the first comment", "reactions": [], @@ -1260,7 +1261,7 @@ mod routes { { "id": PATCH_ID, "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "title": "A new `hello word`", "description": "change `hello world` in README to something else", @@ -1291,7 +1292,7 @@ mod routes { { "id": PATCH_ID, "author": { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "title": "A new `hello word`", "description": "change `hello world` in README to something else", From a3f8a0b524013b7c1eadd344ebe842985e79e79d Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Thu, 16 Feb 2023 15:40:32 +0000 Subject: [PATCH 060/264] Exchange From for TryFrom for PathBuf and Home The `From` implementation would allow someone to create a `Home` in an incorrect fashion, for example the path is not canonical. Exchange `From` for a `TryFrom` implementation. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-httpd/src/api/test.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index 191794251..6feae6978 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -1,3 +1,4 @@ +use std::convert::TryInto as _; use std::path::Path; use std::str::FromStr; use std::sync::Arc; @@ -81,7 +82,8 @@ pub fn seed(dir: &Path) -> Context { .unwrap(); // eq. rad auth - let profile = radicle::Profile::init(rad_home.into(), PASSWORD.to_owned()).unwrap(); + let profile = + radicle::Profile::init(rad_home.try_into().unwrap(), PASSWORD.to_owned()).unwrap(); // rad init rad_init::init( From 1a2ee8da58a4d1684461cba060bc53543cf33a48 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 16 Feb 2023 14:15:53 +0000 Subject: [PATCH 061/264] httpd: Move `git` router & handler into its own file Signed-off-by: xphoniex --- radicle-httpd/src/git.rs | 194 +++++++++++++++++++++++++++++++++++++ radicle-httpd/src/lib.rs | 203 ++------------------------------------- 2 files changed, 203 insertions(+), 194 deletions(-) create mode 100644 radicle-httpd/src/git.rs diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs new file mode 100644 index 000000000..cf4c3784b --- /dev/null +++ b/radicle-httpd/src/git.rs @@ -0,0 +1,194 @@ +use std::collections::HashMap; +use std::io::prelude::*; +use std::net::SocketAddr; +use std::path::Path; +use std::process::{Command, Stdio}; +use std::sync::Arc; +use std::{io, net, str}; + +use axum::body::Bytes; +use axum::extract::{ConnectInfo, Path as AxumPath, RawQuery, State}; +use axum::http::header::HeaderName; +use axum::http::{HeaderMap, Method, StatusCode}; +use axum::response::IntoResponse; +use axum::routing::any; +use axum::Router; +use flate2::write::GzDecoder; +use hyper::body::Buf as _; + +use radicle::identity::Id; +use radicle::profile::Profile; + +use crate::error::Error; + +pub fn router(profile: Arc) -> Router { + Router::new() + .route("/:project/*request", any(git_handler)) + .with_state(profile) +} + +async fn git_handler( + State(profile): State>, + AxumPath((project, request)): AxumPath<(String, String)>, + method: Method, + headers: HeaderMap, + ConnectInfo(remote): ConnectInfo, + query: RawQuery, + body: Bytes, +) -> impl IntoResponse { + let query = query.0.unwrap_or_default(); + let id: Id = project.strip_suffix(".git").unwrap_or(&project).parse()?; + + let (status, headers, body) = + git_http_backend(&profile, method, headers, body, remote, id, &request, query).await?; + + let mut response_headers = HeaderMap::new(); + for (name, vec) in headers.iter() { + for value in vec { + let header: HeaderName = name.try_into()?; + response_headers.insert(header, value.parse()?); + } + } + + Ok::<_, Error>((status, response_headers, body)) +} + +async fn git_http_backend( + profile: &Profile, + method: Method, + headers: HeaderMap, + mut body: Bytes, + remote: net::SocketAddr, + id: Id, + path: &str, + query: String, +) -> Result<(StatusCode, HashMap>, Vec), Error> { + let git_dir = radicle::storage::git::paths::repository(&profile.storage, &id); + let content_type = + if let Some(Ok(content_type)) = headers.get("Content-Type").map(|h| h.to_str()) { + content_type + } else { + "" + }; + + // Reject push requests. + match (path, query.as_str()) { + ("git-receive-pack", _) | (_, "service=git-receive-pack") => { + return Err(Error::ServiceUnavailable("git-receive-pack")); + } + _ => {} + }; + + tracing::debug!("id: {:?}", id); + tracing::debug!("headers: {:?}", headers); + tracing::debug!("path: {:?}", path); + tracing::debug!("method: {:?}", method.as_str()); + tracing::debug!("remote: {:?}", remote.to_string()); + + let mut cmd = Command::new("git"); + let mut child = cmd + .arg("http-backend") + .env("REQUEST_METHOD", method.as_str()) + .env("GIT_PROJECT_ROOT", git_dir) + // "The GIT_HTTP_EXPORT_ALL environmental variable may be passed to git-http-backend to bypass + // the check for the "git-daemon-export-ok" file in each repository before allowing export of + // that repository." + .env("GIT_HTTP_EXPORT_ALL", String::default()) + .env("PATH_INFO", Path::new("/").join(path)) + .env("CONTENT_TYPE", content_type) + .env("QUERY_STRING", query) + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .stdin(Stdio::piped()) + .spawn()?; + + // Whether the request body is compressed. + let gzip = matches!( + headers.get("Content-Encoding").map(|h| h.to_str()), + Some(Ok("gzip")) + ); + + { + // This is safe because we captured the child's stdin. + let mut stdin = child.stdin.take().unwrap(); + + // Copy the request body to git-http-backend's stdin. + if gzip { + let mut decoder = GzDecoder::new(&mut stdin); + let mut reader = body.reader(); + + io::copy(&mut reader, &mut decoder)?; + decoder.finish()?; + } else { + while body.has_remaining() { + let mut chunk = body.chunk(); + let count = chunk.len(); + + io::copy(&mut chunk, &mut stdin)?; + body.advance(count); + } + } + } + + match child.wait_with_output() { + Ok(output) if output.status.success() => { + tracing::info!("git-http-backend: exited successfully for {}", id); + + let mut reader = std::io::Cursor::new(output.stdout); + let mut headers = HashMap::new(); + + // Parse headers returned by git so that we can use them in the client response. + for line in io::Read::by_ref(&mut reader).lines() { + let line = line?; + + if line.is_empty() || line == "\r" { + break; + } + + let mut parts = line.splitn(2, ':'); + let key = parts.next(); + let value = parts.next(); + + if let (Some(key), Some(value)) = (key, value) { + let value = &value[1..]; + + headers + .entry(key.to_string()) + .or_insert_with(Vec::new) + .push(value.to_string()); + } else { + return Err(Error::Backend); + } + } + + let status = { + tracing::debug!("git-http-backend: {:?}", &headers); + + let line = headers.remove("Status").unwrap_or_default(); + let line = line.into_iter().next().unwrap_or_default(); + let mut parts = line.split(' '); + + parts + .next() + .and_then(|p| p.parse().ok()) + .unwrap_or(StatusCode::OK) + }; + + let position = reader.position() as usize; + let body = reader.into_inner().split_off(position); + + Ok((status, headers, body)) + } + Ok(output) => { + tracing::error!("git-http-backend: exited with code {}", output.status); + + if let Ok(output) = std::str::from_utf8(&output.stderr) { + tracing::error!("git-http-backend: stderr: {}", output.trim_end()); + } + Err(Error::Backend) + } + Err(err) => { + panic!("failed to wait for git-http-backend: {err}"); + } + } +} diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index be643f562..6110d14d4 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -2,41 +2,25 @@ #![allow(clippy::too_many_arguments)] pub mod error; -use std::collections::HashMap; -use std::io::prelude::*; use std::net::SocketAddr; -use std::path::Path; -use std::process::{Command, Stdio}; +use std::process::Command; +use std::str; use std::sync::Arc; use std::time::Duration; -use std::{io, net, str}; use anyhow::Context as _; -use axum::body::Body; -use axum::body::{BoxBody, Bytes}; -use axum::extract::{ConnectInfo, Path as AxumPath, RawQuery}; -use axum::http::header::HeaderName; -use axum::http::{HeaderMap, Method, Request, Response, StatusCode}; -use axum::response::IntoResponse; -use axum::routing::any; -use axum::{Extension, Router}; -use flate2::write::GzDecoder; -use hyper::body::Buf as _; +use axum::body::{Body, BoxBody}; +use axum::http::{Request, Response}; +use axum::Router; use tower_http::trace::TraceLayer; use tracing::Span; -use radicle::identity::Id; -use radicle::profile::Profile; - -use error::Error; - mod api; - -pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +mod git; #[derive(Debug, Clone)] pub struct Options { - pub listen: net::SocketAddr, + pub listen: SocketAddr, } /// Run the Server. @@ -51,12 +35,9 @@ pub async fn run(options: Options) -> anyhow::Result<()> { let profile = Arc::new(radicle::Profile::load()?); tracing::info!("using radicle home at {}", profile.home().display()); - let git_router = Router::new() - .route("/:project/*request", any(git_handler)) - .layer(Extension(profile.clone())); - - let ctx = api::Context::new(profile); + let ctx = api::Context::new(profile.clone()); let api_router = api::router(ctx); + let git_router = git::router(profile); tracing::info!("listening on http://{}", options.listen); @@ -90,169 +71,3 @@ pub async fn run(options: Options) -> anyhow::Result<()> { .await .map_err(anyhow::Error::from) } - -async fn git_handler( - Extension(profile): Extension>, - AxumPath((project, request)): AxumPath<(String, String)>, - method: Method, - headers: HeaderMap, - ConnectInfo(remote): ConnectInfo, - query: RawQuery, - body: Bytes, -) -> impl IntoResponse { - let query = query.0.unwrap_or_default(); - let id: Id = project.strip_suffix(".git").unwrap_or(&project).parse()?; - - let (status, headers, body) = - git_http_backend(&profile, method, headers, body, remote, id, &request, query).await?; - - let mut response_headers = HeaderMap::new(); - for (name, vec) in headers.iter() { - for value in vec { - let header: HeaderName = name.try_into()?; - response_headers.insert(header, value.parse()?); - } - } - - Ok::<_, Error>((status, response_headers, body)) -} - -async fn git_http_backend( - profile: &Profile, - method: Method, - headers: HeaderMap, - mut body: Bytes, - remote: net::SocketAddr, - id: Id, - path: &str, - query: String, -) -> Result<(StatusCode, HashMap>, Vec), Error> { - let git_dir = radicle::storage::git::paths::repository(&profile.storage, &id); - let content_type = - if let Some(Ok(content_type)) = headers.get("Content-Type").map(|h| h.to_str()) { - content_type - } else { - "" - }; - - // Reject push requests. - match (path, query.as_str()) { - ("git-receive-pack", _) | (_, "service=git-receive-pack") => { - return Err(Error::ServiceUnavailable("git-receive-pack")); - } - _ => {} - }; - - tracing::debug!("id: {:?}", id); - tracing::debug!("headers: {:?}", headers); - tracing::debug!("path: {:?}", path); - tracing::debug!("method: {:?}", method.as_str()); - tracing::debug!("remote: {:?}", remote.to_string()); - - let mut cmd = Command::new("git"); - let mut child = cmd - .arg("http-backend") - .env("REQUEST_METHOD", method.as_str()) - .env("GIT_PROJECT_ROOT", git_dir) - // "The GIT_HTTP_EXPORT_ALL environmental variable may be passed to git-http-backend to bypass - // the check for the "git-daemon-export-ok" file in each repository before allowing export of - // that repository." - .env("GIT_HTTP_EXPORT_ALL", String::default()) - .env("PATH_INFO", Path::new("/").join(path)) - .env("CONTENT_TYPE", content_type) - .env("QUERY_STRING", query) - .stderr(Stdio::piped()) - .stdout(Stdio::piped()) - .stdin(Stdio::piped()) - .spawn()?; - - // Whether the request body is compressed. - let gzip = matches!( - headers.get("Content-Encoding").map(|h| h.to_str()), - Some(Ok("gzip")) - ); - - { - // This is safe because we captured the child's stdin. - let mut stdin = child.stdin.take().unwrap(); - - // Copy the request body to git-http-backend's stdin. - if gzip { - let mut decoder = GzDecoder::new(&mut stdin); - let mut reader = body.reader(); - - io::copy(&mut reader, &mut decoder)?; - decoder.finish()?; - } else { - while body.has_remaining() { - let mut chunk = body.chunk(); - let count = chunk.len(); - - io::copy(&mut chunk, &mut stdin)?; - body.advance(count); - } - } - } - - match child.wait_with_output() { - Ok(output) if output.status.success() => { - tracing::info!("git-http-backend: exited successfully for {}", id); - - let mut reader = std::io::Cursor::new(output.stdout); - let mut headers = HashMap::new(); - - // Parse headers returned by git so that we can use them in the client response. - for line in io::Read::by_ref(&mut reader).lines() { - let line = line?; - - if line.is_empty() || line == "\r" { - break; - } - - let mut parts = line.splitn(2, ':'); - let key = parts.next(); - let value = parts.next(); - - if let (Some(key), Some(value)) = (key, value) { - let value = &value[1..]; - - headers - .entry(key.to_string()) - .or_insert_with(Vec::new) - .push(value.to_string()); - } else { - return Err(Error::Backend); - } - } - - let status = { - tracing::debug!("git-http-backend: {:?}", &headers); - - let line = headers.remove("Status").unwrap_or_default(); - let line = line.into_iter().next().unwrap_or_default(); - let mut parts = line.split(' '); - - parts - .next() - .and_then(|p| p.parse().ok()) - .unwrap_or(StatusCode::OK) - }; - - let position = reader.position() as usize; - let body = reader.into_inner().split_off(position); - - Ok((status, headers, body)) - } - Ok(output) => { - tracing::error!("git-http-backend: exited with code {}", output.status); - - if let Ok(output) = std::str::from_utf8(&output.stderr) { - tracing::error!("git-http-backend: stderr: {}", output.trim_end()); - } - Err(Error::Backend) - } - Err(err) => { - panic!("failed to wait for git-http-backend: {err}"); - } - } -} From 1185a1fc6bf36cf5417949fc2e61ec38836cb459 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Fri, 17 Feb 2023 12:00:28 +0000 Subject: [PATCH 062/264] httpd: Change `500` into `404` when project doesn't exist Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 10 ++++++++-- radicle-httpd/src/error.rs | 1 + radicle-httpd/src/git.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index d0b2dd6d3..98ec964b6 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +pub mod test; + use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; @@ -22,8 +25,6 @@ mod auth; mod axum_extra; mod error; mod json; -#[cfg(test)] -mod test; mod v1; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -64,6 +65,11 @@ impl Context { id, }) } + + #[cfg(test)] + pub fn profile(&self) -> &Arc { + &self.profile + } } pub fn router(ctx: Context) -> Router { diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs index 01f11f6a4..1bee5d9e4 100644 --- a/radicle-httpd/src/error.rs +++ b/radicle-httpd/src/error.rs @@ -38,6 +38,7 @@ impl Error { match self { Error::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE, Error::InvalidId => http::StatusCode::NOT_FOUND, + Error::Id(_) => http::StatusCode::NOT_FOUND, _ => http::StatusCode::INTERNAL_SERVER_ERROR, } } diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index cf4c3784b..ddc49dd2e 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -192,3 +192,35 @@ async fn git_http_backend( } } } + +#[cfg(test)] +mod routes { + use std::net::SocketAddr; + + use axum::body::Body; + use axum::extract::ConnectInfo; + use axum::http::Request; + use axum::http::{Method, StatusCode}; + use tower::ServiceExt; + + use crate::api::test; + + #[tokio::test] + async fn test_invalid_route_returns_404() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.profile().to_owned()); + + let request = Request::builder() + .method(Method::GET) + .uri("/aa/a".to_string()); + + let mut request = request.body(Body::empty()).unwrap(); + let socket_addr: SocketAddr = "127.0.0.1:8080".parse().unwrap(); + request.extensions_mut().insert(ConnectInfo(socket_addr)); + + let response = app.oneshot(request).await.unwrap(); + + assert_eq!(response.status(), StatusCode::NOT_FOUND); + } +} From b0b94fea1893de5f15bb668a5ec3a802bc10d167 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Fri, 17 Feb 2023 12:41:33 +0000 Subject: [PATCH 063/264] httpd: Move `test` utility mod from inside `api` to root Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 9 ++++++--- radicle-httpd/src/api/v1/delegates.rs | 2 +- radicle-httpd/src/api/v1/projects.rs | 2 +- radicle-httpd/src/api/v1/sessions.rs | 6 ++---- radicle-httpd/src/api/v1/stats.rs | 2 +- radicle-httpd/src/git.rs | 2 +- radicle-httpd/src/lib.rs | 4 ++++ radicle-httpd/src/{api => }/test.rs | 9 +++------ 8 files changed, 19 insertions(+), 17 deletions(-) rename radicle-httpd/src/{api => }/test.rs (97%) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 98ec964b6..462624a55 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -1,5 +1,4 @@ -#[cfg(test)] -pub mod test; +pub mod auth; use std::collections::HashMap; use std::sync::Arc; @@ -21,7 +20,6 @@ use radicle::identity::Id; use radicle::storage::{ReadRepository, ReadStorage}; use radicle::Profile; -mod auth; mod axum_extra; mod error; mod json; @@ -70,6 +68,11 @@ impl Context { pub fn profile(&self) -> &Arc { &self.profile } + + #[cfg(test)] + pub fn sessions(&self) -> &Arc>> { + &self.sessions + } } pub fn router(ctx: Context) -> Router { diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 01138768b..52fa5ca52 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -74,7 +74,7 @@ mod routes { use axum::http::StatusCode; use serde_json::json; - use crate::api::test::{self, get, HEAD}; + use crate::test::{self, get, HEAD}; #[tokio::test] async fn test_delegates_projects() { diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 68fcaffb6..d768a3e4b 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -562,7 +562,7 @@ mod routes { use pretty_assertions::assert_eq; use serde_json::json; - use crate::api::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; + use crate::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; const CREATED_ISSUE_ID: &str = "b56febfba1e7dd20f4aea43ca2fe9dcf1fd448ba"; diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index f2cdcc535..c1e2146b7 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -125,10 +125,8 @@ mod routes { use axum::http::StatusCode; use radicle_cli::commands::rad_web::{self, SessionInfo}; - use crate::api::{ - auth::{AuthState, Session}, - test::{self, get, post, put}, - }; + use crate::api::auth::{AuthState, Session}; + use crate::test::{self, get, post, put}; #[tokio::test] async fn test_session() { diff --git a/radicle-httpd/src/api/v1/stats.rs b/radicle-httpd/src/api/v1/stats.rs index 873c06e71..c8b1f633d 100644 --- a/radicle-httpd/src/api/v1/stats.rs +++ b/radicle-httpd/src/api/v1/stats.rs @@ -29,7 +29,7 @@ mod routes { use axum::http::StatusCode; use serde_json::json; - use crate::api::test::{self, get}; + use crate::test::{self, get}; #[tokio::test] async fn test_stats() { diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index ddc49dd2e..e7aab600e 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -203,7 +203,7 @@ mod routes { use axum::http::{Method, StatusCode}; use tower::ServiceExt; - use crate::api::test; + use crate::test; #[tokio::test] async fn test_invalid_route_returns_404() { diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 6110d14d4..29dd01ad2 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -17,6 +17,8 @@ use tracing::Span; mod api; mod git; +#[cfg(test)] +mod test; #[derive(Debug, Clone)] pub struct Options { @@ -30,9 +32,11 @@ pub async fn run(options: Options) -> anyhow::Result<()> { .output() .context("'git' command must be available")? .stdout; + tracing::info!("{}", str::from_utf8(&git_version)?.trim()); let profile = Arc::new(radicle::Profile::load()?); + tracing::info!("using radicle home at {}", profile.home().display()); let ctx = api::Context::new(profile.clone()); diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/test.rs similarity index 97% rename from radicle-httpd/src/api/test.rs rename to radicle-httpd/src/test.rs index 6feae6978..283ac8180 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/test.rs @@ -136,21 +136,18 @@ pub fn seed(dir: &Path) -> Context { ) .unwrap(); - Context { - profile: Arc::new(profile), - sessions: Default::default(), - } + Context::new(Arc::new(profile)) } /// Adds an authorized session to the Context::sessions HashMap. pub async fn create_session(ctx: Context) { let issued_at = OffsetDateTime::now_utc(); - let mut sessions = ctx.sessions.write().await; + let mut sessions = ctx.sessions().write().await; sessions.insert( String::from("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"), auth::Session { status: auth::AuthState::Authorized, - public_key: ctx.profile.public_key, + public_key: ctx.profile().public_key, issued_at, expires_at: issued_at .checked_add(auth::AUTHORIZED_SESSIONS_EXPIRATION) From 8d5d84f31c17af10238b9567d00cdf9eeadd4ec7 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Fri, 17 Feb 2023 14:29:48 +0000 Subject: [PATCH 064/264] httpd: Update `/git` test to use `test` helper fns Update `axum` to 0.6.7 to be able to use fix in: https://github.com/tokio-rs/axum/pull/1767 Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/git.rs | 22 ++++++---------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index fff785815..fa7b5c2ae 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -16,7 +16,7 @@ logfmt = [ [dependencies] anyhow = { version = "1" } -axum = { version = "0.6.2", default-features = false, features = ["headers", "json", "query", "tokio"] } +axum = { version = "0.6.7", default-features = false, features = ["headers", "json", "query", "tokio"] } axum-auth = { version= "0.4.0", default-features = false, features = ["auth-bearer"] } axum-server = { version = "0.4.4", default-features = false } chrono = { version = "0.4.22" } diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index e7aab600e..0785f096f 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -197,29 +197,19 @@ async fn git_http_backend( mod routes { use std::net::SocketAddr; - use axum::body::Body; - use axum::extract::ConnectInfo; - use axum::http::Request; - use axum::http::{Method, StatusCode}; - use tower::ServiceExt; + use axum::extract::connect_info::MockConnectInfo; + use axum::http::StatusCode; - use crate::test; + use crate::test::{self, get}; #[tokio::test] async fn test_invalid_route_returns_404() { let tmp = tempfile::tempdir().unwrap(); let ctx = test::seed(tmp.path()); - let app = super::router(ctx.profile().to_owned()); + let app = super::router(ctx.profile().to_owned()) + .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); - let request = Request::builder() - .method(Method::GET) - .uri("/aa/a".to_string()); - - let mut request = request.body(Body::empty()).unwrap(); - let socket_addr: SocketAddr = "127.0.0.1:8080".parse().unwrap(); - request.extensions_mut().insert(ConnectInfo(socket_addr)); - - let response = app.oneshot(request).await.unwrap(); + let response = get(&app, "/aa/a").await; assert_eq!(response.status(), StatusCode::NOT_FOUND); } From 9a03136516aa52af0d629dd6ab8ad47e390738d4 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 16 Feb 2023 15:51:31 +0000 Subject: [PATCH 065/264] httpd: Use `State` instead of `Extension` for `root_handler` Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 462624a55..4f89da50f 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -4,11 +4,12 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; +use axum::extract::State; use axum::http::header::{AUTHORIZATION, CONTENT_TYPE}; use axum::http::Method; use axum::response::{IntoResponse, Json}; use axum::routing::get; -use axum::{Extension, Router}; +use axum::Router; use serde::{Deserialize, Serialize}; use serde_json::json; use tokio::sync::RwLock; @@ -78,7 +79,7 @@ impl Context { pub fn router(ctx: Context) -> Router { let root_router = Router::new() .route("/", get(root_handler)) - .layer(Extension(ctx.clone())); + .with_state(ctx.clone()); Router::new() .merge(root_router) @@ -98,7 +99,7 @@ pub fn router(ctx: Context) -> Router { ) } -async fn root_handler(Extension(ctx): Extension) -> impl IntoResponse { +async fn root_handler(State(ctx): State) -> impl IntoResponse { let response = json!({ "message": "Welcome!", "service": "radicle-httpd", From e7de186e0eb1a1504597c7e79cd3422c1ea33139 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 23 Feb 2023 06:16:37 +0000 Subject: [PATCH 066/264] httpd: Add `/raw` route Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 1 - radicle-httpd/src/api/v1/delegates.rs | 2 +- radicle-httpd/src/api/v1/projects.rs | 5 +- radicle-httpd/src/api/v1/sessions.rs | 2 +- radicle-httpd/src/{api => }/axum_extra.rs | 0 radicle-httpd/src/error.rs | 4 ++ radicle-httpd/src/lib.rs | 6 ++- radicle-httpd/src/raw.rs | 59 +++++++++++++++++++++++ radicle-httpd/src/test.rs | 6 ++- 9 files changed, 78 insertions(+), 7 deletions(-) rename radicle-httpd/src/{api => }/axum_extra.rs (100%) create mode 100644 radicle-httpd/src/raw.rs diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 4f89da50f..848fdef54 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -21,7 +21,6 @@ use radicle::identity::Id; use radicle::storage::{ReadRepository, ReadStorage}; use radicle::Profile; -mod axum_extra; mod error; mod json; mod v1; diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 52fa5ca52..530dc76db 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -8,11 +8,11 @@ use radicle::cob::patch::Patches; use radicle::identity::Did; use radicle::storage::{ReadRepository, ReadStorage}; -use crate::api::axum_extra::{Path, Query}; use crate::api::error::Error; use crate::api::project::Info; use crate::api::Context; use crate::api::PaginationQuery; +use crate::axum_extra::{Path, Query}; pub fn router(ctx: Context) -> Router { Router::new() diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index d768a3e4b..1bde6fee4 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -17,13 +17,14 @@ use radicle::cob::patch::Patches; use radicle::cob::{thread, ActorId, Tag}; use radicle::identity::Id; use radicle::node::NodeId; -use radicle::storage::{git::paths, ReadRepository, ReadStorage}; +use radicle::storage::git::paths; +use radicle::storage::{ReadRepository, ReadStorage}; use radicle_surf::{Glob, Oid, Repository}; -use crate::api::axum_extra::{Path, Query}; use crate::api::error::Error; use crate::api::project::Info; use crate::api::{self, Context, PaginationQuery}; +use crate::axum_extra::{Path, Query}; const CACHE_1_HOUR: &str = "public, max-age=3600, must-revalidate"; diff --git a/radicle-httpd/src/api/v1/sessions.rs b/radicle-httpd/src/api/v1/sessions.rs index c1e2146b7..340951fbf 100644 --- a/radicle-httpd/src/api/v1/sessions.rs +++ b/radicle-httpd/src/api/v1/sessions.rs @@ -11,10 +11,10 @@ use serde::{Deserialize, Serialize}; use time::OffsetDateTime; use crate::api::auth::{self, AuthState, Session}; -use crate::api::axum_extra::Path; use crate::api::error::Error; use crate::api::json; use crate::api::Context; +use crate::axum_extra::Path; pub fn router(ctx: Context) -> Router { Router::new() diff --git a/radicle-httpd/src/api/axum_extra.rs b/radicle-httpd/src/axum_extra.rs similarity index 100% rename from radicle-httpd/src/api/axum_extra.rs rename to radicle-httpd/src/axum_extra.rs diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs index 1bee5d9e4..a722aa8ef 100644 --- a/radicle-httpd/src/error.rs +++ b/radicle-httpd/src/error.rs @@ -31,6 +31,10 @@ pub enum Error { /// HeaderValue error. #[error(transparent)] InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue), + + /// Surf error. + #[error(transparent)] + Surf(#[from] radicle_surf::Error), } impl Error { diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 29dd01ad2..f69bf0977 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -16,7 +16,9 @@ use tower_http::trace::TraceLayer; use tracing::Span; mod api; +mod axum_extra; mod git; +mod raw; #[cfg(test)] mod test; @@ -41,13 +43,15 @@ pub async fn run(options: Options) -> anyhow::Result<()> { let ctx = api::Context::new(profile.clone()); let api_router = api::router(ctx); - let git_router = git::router(profile); + let git_router = git::router(profile.clone()); + let raw_router = raw::router(profile); tracing::info!("listening on http://{}", options.listen); let app = Router::new() .merge(git_router) .nest("/api", api_router) + .nest("/raw", raw_router) .layer( TraceLayer::new_for_http() .make_span_with(|request: &Request| { diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs new file mode 100644 index 000000000..648defc91 --- /dev/null +++ b/radicle-httpd/src/raw.rs @@ -0,0 +1,59 @@ +use std::sync::Arc; + +use axum::extract::State; +use axum::http::header; +use axum::response::IntoResponse; +use axum::routing::get; +use axum::Router; +use hyper::HeaderMap; + +use radicle::prelude::Id; +use radicle::profile::Profile; +use radicle::storage::git::paths; +use radicle_surf::{Oid, Repository}; + +use crate::axum_extra::Path; +use crate::error::Error; + +pub fn router(profile: Arc) -> Router { + Router::new() + .route("/:project/:sha/*path", get(file_handler)) + .with_state(profile) +} + +async fn file_handler( + Path((project, sha, path)): Path<(Id, Oid, String)>, + State(profile): State>, +) -> impl IntoResponse { + let storage = &profile.storage; + let repo = Repository::open(paths::repository(storage, &project))?; + let blob = repo.blob(sha, &path)?; + + let mut response_headers = HeaderMap::new(); + response_headers.insert(header::CONTENT_TYPE, "text; charset=utf-8".parse().unwrap()); + + Ok::<_, Error>((response_headers, blob.content().to_owned())) +} + +#[cfg(test)] +mod routes { + use axum::http::StatusCode; + + use crate::test::{self, get, HEAD}; + + #[tokio::test] + async fn test_file_handler() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.profile().to_owned()); + + let response = get( + &app, + format!("/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/{HEAD}/dir1/README"), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.body().await, "Hello World from dir1!\n"); + } +} diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 283ac8180..d3f9c9482 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -4,7 +4,7 @@ use std::str::FromStr; use std::sync::Arc; use std::{env, fs}; -use axum::body::Body; +use axum::body::{Body, Bytes}; use axum::http::{Method, Request}; use axum::Router; use serde_json::Value; @@ -235,4 +235,8 @@ impl Response { pub fn status(&self) -> axum::http::StatusCode { self.0.status() } + + pub async fn body(self) -> Bytes { + hyper::body::to_bytes(self.0.into_body()).await.unwrap() + } } From 6793b8e7d3e036f41717dcee8ff5934c639a2c62 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 24 Feb 2023 11:43:00 -0300 Subject: [PATCH 067/264] httpd: Add issue and patch counts Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api.rs | 9 +++++---- radicle-httpd/src/api/v1/delegates.rs | 15 +++++++++++---- radicle-httpd/src/api/v1/projects.rs | 26 ++++++++++++++++++++------ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 848fdef54..ca7e15b89 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -51,8 +51,8 @@ impl Context { let doc = repo.identity_of(self.profile.id())?; let payload = doc.project()?; let delegates = doc.delegates; - let issues = (Issues::open(self.profile.public_key, &repo)?).count()?; - let patches = (Patches::open(self.profile.public_key, &repo)?).count()?; + let issues = Issues::open(self.profile.public_key, &repo)?.counts()?; + let patches = Patches::open(self.profile.public_key, &repo)?.counts()?; Ok(project::Info { payload, @@ -143,6 +143,7 @@ mod project { use nonempty::NonEmpty; use serde::Serialize; + use radicle::cob; use radicle::git::Oid; use radicle::identity::project::Project; use radicle::identity::Id; @@ -157,8 +158,8 @@ mod project { pub payload: Project, pub delegates: NonEmpty, pub head: Oid, - pub patches: usize, - pub issues: usize, + pub patches: cob::patch::PatchCounts, + pub issues: cob::issue::IssueCounts, pub id: Id, } } diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 530dc76db..31f722955 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -49,9 +49,9 @@ async fn delegates_projects_handler( } let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; - let Ok(issues) = (*issues).count() else { return None }; + let Ok(issues) = issues.counts() else { return None }; let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; - let Ok(patches) = (*patches).count() else { return None }; + let Ok(patches) = patches.counts() else { return None }; Some(Info { payload, @@ -96,8 +96,15 @@ mod routes { "defaultBranch": "master", "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, - "patches": 1, - "issues": 1, + "patches": { + "proposed": 1, + "draft": 0, + "archived": 0, + }, + "issues": { + "open": 1, + "closed": 0, + }, "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" } ]) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 1bde6fee4..4940015be 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -81,9 +81,9 @@ async fn project_root_handler( let Ok(payload) = repo.project_of(ctx.profile.id()) else { return None }; let Ok(doc) = repo.identity_of(ctx.profile.id()) else { return None }; let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; - let Ok(issues) = (*issues).count() else { return None }; + let Ok(issues) = issues.counts() else { return None }; let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; - let Ok(patches) = (*patches).count() else { return None }; + let Ok(patches) = patches.counts() else { return None }; let delegates = doc.delegates; Some(Info { @@ -583,8 +583,15 @@ mod routes { "defaultBranch": "master", "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, - "patches": 1, - "issues": 1, + "patches": { + "proposed": 1, + "draft": 0, + "archived": 0, + }, + "issues": { + "open": 1, + "closed": 0, + }, "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" } ]) @@ -606,8 +613,15 @@ mod routes { "defaultBranch": "master", "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, - "patches": 1, - "issues": 1, + "patches": { + "proposed": 1, + "draft": 0, + "archived": 0, + }, + "issues": { + "open": 1, + "closed": 0, + }, "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" }) ); From 7e8030b82f2b90ec9466c981d1d56589a1224b93 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 27 Feb 2023 11:48:05 +0100 Subject: [PATCH 068/264] cob: Use hashes for operation ids It's fairly easy for a user to (by mistake or intentionally) create two operations with the same OpId. This patch makes it much less likely, and ensures that if the OpId is equal, it's because the operations are identical. --- radicle-httpd/src/api/v1/projects.rs | 66 ++++++++++++++-------------- radicle-httpd/src/test.rs | 4 +- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 4940015be..740a36efa 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -454,13 +454,18 @@ async fn issue_update_handler( Path((project, issue_id)): Path<(Id, Oid)>, Json(action): Json, ) -> impl IntoResponse { - let sessions = ctx.sessions.write().await; - sessions.get(&token).ok_or(Error::Auth("Unauthorized"))?; + ctx.sessions + .write() + .await + .get(&token) + .ok_or(Error::Auth("Unauthorized"))?; + let storage = &ctx.profile.storage; let signer = ctx.profile.signer().unwrap(); let repo = storage.repository(project)?; let mut issues = Issues::open(ctx.profile.public_key, &repo)?; let mut issue = issues.get_mut(&issue_id.into())?; + match action { Action::Assign { add, remove } => { issue.assign(add, &signer)?; @@ -475,27 +480,24 @@ async fn issue_update_handler( Action::Edit { title } => { issue.edit(title, &signer)?; } - Action::Thread { action } => { - let mut actor = thread::Actor::new(ctx.profile.signer().unwrap()); - match action { - thread::Action::Comment { body, reply_to } => { - if let Some(reply_to) = reply_to { - issue.comment(body, reply_to, &signer)?; - } else { - issue.thread(body, &signer)?; - } - } - thread::Action::React { to, reaction, .. } => { - issue.react(to, reaction, &signer)?; - } - thread::Action::Edit { id, body } => { - actor.edit(id, &body); - } - thread::Action::Redact { id } => { - actor.redact(id); + Action::Thread { action } => match action { + thread::Action::Comment { body, reply_to } => { + if let Some(reply_to) = reply_to { + issue.comment(body, reply_to, &signer)?; + } else { + issue.thread(body, &signer)?; } } - } + thread::Action::React { to, reaction, .. } => { + issue.react(to, reaction, &signer)?; + } + thread::Action::Edit { .. } => { + todo!(); + } + thread::Action::Redact { .. } => { + todo!(); + } + }, }; Ok::<_, Error>(Json(json!({ "success": true }))) @@ -565,7 +567,7 @@ mod routes { use crate::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; - const CREATED_ISSUE_ID: &str = "b56febfba1e7dd20f4aea43ca2fe9dcf1fd448ba"; + const CREATED_ISSUE_ID: &str = "745052a1603000b9566445753d7e2fee1ff5041f"; #[tokio::test] async fn test_projects_root() { @@ -1045,7 +1047,7 @@ mod routes { "assignees": [], "discussion": [ { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, @@ -1107,7 +1109,7 @@ mod routes { "status": "open", }, "discussion": [{ - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1168,7 +1170,7 @@ mod routes { }, "discussion": [ { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1178,7 +1180,7 @@ mod routes { "replyTo": null, }, { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/5", + "id": "f7da49e705f60c39265dbdd748d786a620bc8030", "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1204,7 +1206,7 @@ mod routes { "action": { "type": "comment", "body": "This is a reply to the first comment", - "replyTo": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "replyTo": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", }})) .unwrap(); let response = patch( @@ -1238,7 +1240,7 @@ mod routes { }, "discussion": [ { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, @@ -1248,14 +1250,14 @@ mod routes { "replyTo": null, }, { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/5", + "id": "ab98b5b794d02c23d29769a39fe8e0b74624f3d8", "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", }, "body": "This is a reply to the first comment", "reactions": [], "timestamp": 1673001014, - "replyTo": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "replyTo": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", }, ], "tags": [], @@ -1285,7 +1287,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "id": "d6ba305f78e2fa1ebcc55d8c3be8806bbce25fb4", "description": "", "reviews": [], } @@ -1316,7 +1318,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/1", + "id": "d6ba305f78e2fa1ebcc55d8c3be8806bbce25fb4", "description": "", "reviews": [], } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index d3f9c9482..ce12a261b 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -23,8 +23,8 @@ use crate::api::{auth, Context}; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; -pub const PATCH_ID: &str = "4250f0117659ee4de9af99e699a63395cd6ffa1c"; -pub const ISSUE_ID: &str = "8adca8aad2a2cb99b9847d20193930cde2042a57"; +pub const PATCH_ID: &str = "afb3063f8f0343fa31d2a0d55bac2a6f4a77125e"; +pub const ISSUE_ID: &str = "331569cd5e4dcc55104363ebce92c78b0e5d67d4"; const PASSWORD: &str = "radicle"; From da594fb52173fcd79347f5dab48a5f1e013c5c3b Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 28 Feb 2023 14:56:56 +0000 Subject: [PATCH 069/264] httpd: Use `repo.identity_doc()` instead of `repo.identity_of(..)` Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 2 +- radicle-httpd/src/api/error.rs | 4 ++++ radicle-httpd/src/api/v1/delegates.rs | 3 ++- radicle-httpd/src/api/v1/projects.rs | 3 ++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index ca7e15b89..49409d671 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -48,7 +48,7 @@ impl Context { let storage = &self.profile.storage; let repo = storage.repository(id)?; let (_, head) = repo.head()?; - let doc = repo.identity_of(self.profile.id())?; + let doc = repo.identity_doc()?.1.verified()?; let payload = doc.project()?; let delegates = doc.delegates; let issues = Issues::open(self.profile.public_key, &repo)?.counts()?; diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index e8bdef7cb..8f87a62e1 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -61,6 +61,10 @@ pub enum Error { /// Storage refs error. #[error(transparent)] StorageRef(#[from] radicle::storage::refs::Error), + + /// Identity doc error. + #[error(transparent)] + IdentityDoc(#[from] radicle::identity::doc::DocError), } impl IntoResponse for Error { diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 31f722955..c025f74f4 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -40,7 +40,8 @@ async fn delegates_projects_handler( .filter_map(|id| { let Ok(repo) = storage.repository(id) else { return None }; let Ok((_, head)) = repo.head() else { return None }; - let Ok(doc) = repo.identity_of(ctx.profile.id()) else { return None }; + let Ok((_, doc)) = repo.identity_doc() else { return None }; + let Ok(doc) = doc.verified() else { return None }; let Ok(payload) = doc.project() else { return None }; let delegates = doc.delegates; diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 740a36efa..9714d2bd3 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -79,7 +79,8 @@ async fn project_root_handler( let Ok(repo) = storage.repository(id) else { return None }; let Ok((_, head)) = repo.head() else { return None }; let Ok(payload) = repo.project_of(ctx.profile.id()) else { return None }; - let Ok(doc) = repo.identity_of(ctx.profile.id()) else { return None }; + let Ok((_, doc)) = repo.identity_doc() else { return None }; + let Ok(doc) = doc.verified() else { return None }; let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; let Ok(issues) = issues.counts() else { return None }; let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; From ac0994a3be2ff7b899e64a0d368571dd0a43b596 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 28 Feb 2023 18:37:26 +0100 Subject: [PATCH 070/264] httpd: Don't load project payload twice --- radicle-httpd/src/api/v1/projects.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 9714d2bd3..565c18ac7 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -78,9 +78,9 @@ async fn project_root_handler( .filter_map(|id| { let Ok(repo) = storage.repository(id) else { return None }; let Ok((_, head)) = repo.head() else { return None }; - let Ok(payload) = repo.project_of(ctx.profile.id()) else { return None }; let Ok((_, doc)) = repo.identity_doc() else { return None }; let Ok(doc) = doc.verified() else { return None }; + let Ok(payload) = doc.project() else { return None }; let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; let Ok(issues) = issues.counts() else { return None }; let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; From 9efea143ab2e2e607d9abec917288b1dbd816766 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 28 Feb 2023 16:21:35 +0100 Subject: [PATCH 071/264] httpd: Sort issues and patches by creation date Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/projects.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 565c18ac7..6986f76f3 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -394,10 +394,10 @@ async fn issues_handler( let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let issues = Issues::open(ctx.profile.public_key, &repo)?; + let mut issues: Vec<_> = issues.all()?.filter_map(|r| r.ok()).collect::>(); + issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let issues = issues - .all()? .into_iter() - .filter_map(|r| r.ok()) .map(|(id, issue, _)| api::json::issue(id, issue)) .skip(page * per_page) .take(per_page) @@ -532,10 +532,10 @@ async fn patches_handler( let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let patches = Patches::open(ctx.profile.public_key, &repo)?; + let mut patches = patches.all()?.filter_map(|r| r.ok()).collect::>(); + patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let patches = patches - .all()? .into_iter() - .filter_map(|r| r.ok()) .map(|(id, patch, _)| api::json::patch(id, patch)) .skip(page * per_page) .take(per_page) From 78132a14eab6735988abbc724d4c3133b3606f7d Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 1 Mar 2023 15:40:30 +0100 Subject: [PATCH 072/264] cob: Don't require a local fork to exist Before this change, the identity document would be loaded for the COB signer. This meant that a fork would be needed for that signer. After this change, it's no longer necessary, since the identity doc head is computed from available remotes. While making those changes, it was also apparent that some of the identity-related functions had bad names and error types, this was fixed as well. --- radicle-httpd/src/api/error.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 8f87a62e1..b37f802c1 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -38,9 +38,9 @@ pub enum Error { #[error(transparent)] CobStore(#[from] radicle::cob::store::Error), - /// Git project error. + /// Identity error. #[error(transparent)] - GitProject(#[from] radicle::storage::git::ProjectError), + Identity(#[from] radicle::identity::IdentityError), /// Project doc error. #[error(transparent)] From 2fdcf85891951881d14ac9ee14992c1322ebd482 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 2 Mar 2023 16:59:42 +0000 Subject: [PATCH 073/264] httpd: Change format of tracing messages Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 6 ++- radicle-httpd/src/lib.rs | 48 ++++++++++++++------- radicle-httpd/src/main.rs | 4 +- radicle-httpd/src/tracing_extra.rs | 68 ++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 radicle-httpd/src/tracing_extra.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index fa7b5c2ae..7152366d5 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -24,8 +24,8 @@ fastrand = { version = "1.7.0" } flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } -radicle-surf = { version = "0.9.0", default-features = false, features = ["serde"] } nonempty = { version = "0.8.1", features = ["serialize"] } +radicle-surf = { version = "0.9.0", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } @@ -40,10 +40,12 @@ tracing-subscriber = { version = "0.3", default-features = false, features = ["s path = "../radicle" version = "0.2.0" +[dependencies.radicle-cli] +path = "../radicle-cli" + [dev-dependencies] hyper = { version = "0.14.17", default-features = false, features = ["client"] } pretty_assertions = { version = "1.3.0" } -radicle-cli = { path = "../radicle-cli" } radicle-crypto = { path = "../radicle-crypto" } tempfile = { version = "3.3.0" } tower = { version = "0.4", features = ["util"] } diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index f69bf0977..59a252934 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -8,12 +8,15 @@ use std::str; use std::sync::Arc; use std::time::Duration; +use ::tracing::Span; use anyhow::Context as _; -use axum::body::{Body, BoxBody}; +use axum::body::{Body, BoxBody, HttpBody}; use axum::http::{Request, Response}; +use axum::middleware; use axum::Router; use tower_http::trace::TraceLayer; -use tracing::Span; + +use tracing_extra::{tracing_middleware, ColoredStatus, Paint, RequestId, TracingInfo}; mod api; mod axum_extra; @@ -21,6 +24,7 @@ mod git; mod raw; #[cfg(test)] mod test; +mod tracing_extra; #[derive(Debug, Clone)] pub struct Options { @@ -38,6 +42,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> { tracing::info!("{}", str::from_utf8(&git_version)?.trim()); let profile = Arc::new(radicle::Profile::load()?); + let request_id = RequestId::new(); tracing::info!("using radicle home at {}", profile.home().display()); @@ -52,23 +57,36 @@ pub async fn run(options: Options) -> anyhow::Result<()> { .merge(git_router) .nest("/api", api_router) .nest("/raw", raw_router) + .layer(middleware::from_fn(tracing_middleware)) .layer( TraceLayer::new_for_http() - .make_span_with(|request: &Request| { - tracing::info_span!( - "request", - method = %request.method(), - uri = %request.uri(), - status = tracing::field::Empty, - latency = tracing::field::Empty, - ) + .make_span_with(move |_request: &Request| { + tracing::info_span!("request", id = %request_id.clone().next()) }) .on_response( - |response: &Response, latency: Duration, span: &Span| { - span.record("status", &tracing::field::debug(response.status())); - span.record("latency", &tracing::field::debug(latency)); - - tracing::info!("Processed"); + |response: &Response, latency: Duration, _span: &Span| { + if let Some(info) = response.extensions().get::() { + tracing::info!( + "{} \"{} {} {:?}\" {} {:?} {}", + info.connect_info.0, + info.method, + info.uri, + info.version, + ColoredStatus(response.status()), + latency, + Paint::dim( + response + .body() + .size_hint() + .exact() + .map(|n| n.to_string()) + .unwrap_or("0".to_string()) + .into() + ), + ); + } else { + tracing::info!("Processed"); + } }, ), ) diff --git a/radicle-httpd/src/main.rs b/radicle-httpd/src/main.rs index 4e13ae3b2..4dac4b78a 100644 --- a/radicle-httpd/src/main.rs +++ b/radicle-httpd/src/main.rs @@ -18,7 +18,9 @@ mod logger { #[cfg(not(feature = "logfmt"))] mod logger { pub fn subscriber() -> impl tracing::Subscriber { - tracing_subscriber::FmtSubscriber::new() + tracing_subscriber::FmtSubscriber::builder() + .with_target(false) + .finish() } } diff --git a/radicle-httpd/src/tracing_extra.rs b/radicle-httpd/src/tracing_extra.rs new file mode 100644 index 000000000..1e0b4c78b --- /dev/null +++ b/radicle-httpd/src/tracing_extra.rs @@ -0,0 +1,68 @@ +use std::fmt; +use std::net::SocketAddr; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::Arc; + +use axum::extract::ConnectInfo; +use axum::http::Request; +use axum::middleware::Next; +use axum::response::IntoResponse; +use axum::Extension; +use hyper::{Method, StatusCode, Uri, Version}; + +pub use radicle_cli::terminal::ansi::Paint; + +#[derive(Clone)] +pub struct RequestId(Arc); + +impl RequestId { + pub fn new() -> RequestId { + RequestId(Arc::new(0.into())) + } + + pub fn next(&mut self) -> u64 { + self.0.fetch_add(1, Ordering::SeqCst) + } +} + +pub struct TracingInfo { + pub connect_info: ConnectInfo, + pub method: Method, + pub version: Version, + pub uri: Uri, +} + +pub struct ColoredStatus(pub StatusCode); + +impl fmt::Display for ColoredStatus { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.0.as_u16() { + 200..=299 => write!(f, "{}", Paint::green(self.0)), + 300..=399 => write!(f, "{}", Paint::blue(self.0)), + 400..=499 => write!(f, "{}", Paint::red(self.0)), + _ => write!(f, "{}", Paint::yellow(self.0)), + } + } +} + +pub async fn tracing_middleware(request: Request, next: Next) -> impl IntoResponse { + let connect_info = *request + .extensions() + .get::>() + .unwrap(); + + let method = request.method().clone(); + let version = request.version(); + let uri = request.uri().clone(); + + let tracing_info = TracingInfo { + connect_info, + method, + version, + uri, + }; + + let response = next.run(request).await; + + (Extension(tracing_info), response) +} From 970abf96b9429bd7e9fbfee650b87b1f55a5afdd Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 Mar 2023 17:48:19 +0100 Subject: [PATCH 074/264] httpd: Add patch creation endpoint Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/error.rs | 4 + radicle-httpd/src/api/v1/projects.rs | 160 +++++++++++++++++++++++---- radicle-httpd/src/test.rs | 3 +- 3 files changed, 144 insertions(+), 23 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index b37f802c1..5608a649c 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -34,6 +34,10 @@ pub enum Error { #[error(transparent)] CobIssue(#[from] radicle::cob::issue::Error), + /// Cob patch error. + #[error(transparent)] + CobPatch(#[from] radicle::cob::patch::Error), + /// Cob store error. #[error(transparent)] CobStore(#[from] radicle::cob::store::Error), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 6986f76f3..53d925bde 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -12,13 +12,12 @@ use serde::{Deserialize, Serialize}; use serde_json::json; use tower_http::set_header::SetResponseHeaderLayer; -use radicle::cob::issue::{Action, Issues}; -use radicle::cob::patch::Patches; +use radicle::cob::{issue, patch}; use radicle::cob::{thread, ActorId, Tag}; use radicle::identity::Id; use radicle::node::NodeId; use radicle::storage::git::paths; -use radicle::storage::{ReadRepository, ReadStorage}; +use radicle::storage::{ReadRepository, ReadStorage, WriteRepository}; use radicle_surf::{Glob, Oid, Repository}; use crate::api::error::Error; @@ -57,7 +56,10 @@ pub fn router(ctx: Context) -> Router { "/projects/:project/issues/:id", patch(issue_update_handler).get(issue_handler), ) - .route("/projects/:project/patches", get(patches_handler)) + .route( + "/projects/:project/patches", + post(patch_create_handler).get(patches_handler), + ) .route("/projects/:project/patches/:id", get(patch_handler)) .with_state(ctx) } @@ -81,9 +83,9 @@ async fn project_root_handler( let Ok((_, doc)) = repo.identity_doc() else { return None }; let Ok(doc) = doc.verified() else { return None }; let Ok(payload) = doc.project() else { return None }; - let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(issues) = issue::Issues::open(ctx.profile.public_key, &repo) else { return None }; let Ok(issues) = issues.counts() else { return None }; - let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(patches) = patch::Patches::open(ctx.profile.public_key, &repo) else { return None }; let Ok(patches) = patches.counts() else { return None }; let delegates = doc.delegates; @@ -393,7 +395,7 @@ async fn issues_handler( let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let issues = Issues::open(ctx.profile.public_key, &repo)?; + let issues = issue::Issues::open(ctx.profile.public_key, &repo)?; let mut issues: Vec<_> = issues.all()?.filter_map(|r| r.ok()).collect::>(); issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let issues = issues @@ -430,7 +432,7 @@ async fn issue_create_handler( .signer() .map_err(|_| Error::Auth("Unauthorized"))?; let repo = storage.repository(project)?; - let mut issues = Issues::open(ctx.profile.public_key, &repo)?; + let mut issues = issue::Issues::open(ctx.profile.public_key, &repo)?; let issue = issues .create( issue.title, @@ -453,7 +455,7 @@ async fn issue_update_handler( State(ctx): State, AuthBearer(token): AuthBearer, Path((project, issue_id)): Path<(Id, Oid)>, - Json(action): Json, + Json(action): Json, ) -> impl IntoResponse { ctx.sessions .write() @@ -464,24 +466,24 @@ async fn issue_update_handler( let storage = &ctx.profile.storage; let signer = ctx.profile.signer().unwrap(); let repo = storage.repository(project)?; - let mut issues = Issues::open(ctx.profile.public_key, &repo)?; + let mut issues = issue::Issues::open(ctx.profile.public_key, &repo)?; let mut issue = issues.get_mut(&issue_id.into())?; match action { - Action::Assign { add, remove } => { + issue::Action::Assign { add, remove } => { issue.assign(add, &signer)?; issue.unassign(remove, &signer)?; } - Action::Lifecycle { state } => { + issue::Action::Lifecycle { state } => { issue.lifecycle(state, &signer)?; } - Action::Tag { add, remove } => { + issue::Action::Tag { add, remove } => { issue.tag(add, remove, &signer)?; } - Action::Edit { title } => { + issue::Action::Edit { title } => { issue.edit(title, &signer)?; } - Action::Thread { action } => match action { + issue::Action::Thread { action } => match action { thread::Action::Comment { body, reply_to } => { if let Some(reply_to) = reply_to { issue.comment(body, reply_to, &signer)?; @@ -512,13 +514,62 @@ async fn issue_handler( ) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let issue = Issues::open(ctx.profile.public_key, &repo)? + let issue = issue::Issues::open(ctx.profile.public_key, &repo)? .get(&issue_id.into())? .ok_or(Error::NotFound)?; Ok::<_, Error>(Json(api::json::issue(issue_id.into(), issue))) } +#[derive(Deserialize, Serialize)] +pub struct PatchCreate { + pub title: String, + pub description: String, + pub target: Oid, + pub oid: Oid, + pub tags: Vec, +} + +/// Create a new patch. +/// `POST /projects/:project/patches` +async fn patch_create_handler( + State(ctx): State, + AuthBearer(token): AuthBearer, + Path(project): Path, + Json(patch): Json, +) -> impl IntoResponse { + ctx.sessions + .read() + .await + .get(&token) + .ok_or(Error::Auth("Unauthorized"))?; + let storage = &ctx.profile.storage; + let signer = ctx + .profile + .signer() + .map_err(|_| Error::Auth("Unauthorized"))?; + let repo = storage.repository(project)?; + let mut patches = patch::Patches::open(ctx.profile.public_key, &repo)?; + let base_oid = repo.raw().merge_base(*patch.target, *patch.oid)?; + + let patch = patches + .create( + patch.title, + patch.description, + patch::MergeTarget::default(), + base_oid, + patch.oid, + &patch.tags, + &signer, + ) + .map_err(Error::from)?; + + Ok::<_, Error>(( + StatusCode::CREATED, + Json(json!({ "success": true, "id": patch.id.to_string() })), + )) +} + /// Get project patches list. /// `GET /projects/:project/patches` async fn patches_handler( @@ -531,7 +582,7 @@ async fn patches_handler( let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let patches = Patches::open(ctx.profile.public_key, &repo)?; + let patches = patch::Patches::open(ctx.profile.public_key, &repo)?; let mut patches = patches.all()?.filter_map(|r| r.ok()).collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let patches = patches @@ -552,7 +603,7 @@ async fn patch_handler( ) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let patch = Patches::open(ctx.profile.public_key, &repo)? + let patch = patch::Patches::open(ctx.profile.public_key, &repo)? .get(&patch_id.into())? .ok_or(Error::NotFound)?; @@ -566,7 +617,7 @@ mod routes { use pretty_assertions::assert_eq; use serde_json::json; - use crate::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; + use crate::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID, SESSION_ID}; const CREATED_ISSUE_ID: &str = "745052a1603000b9566445753d7e2fee1ff5041f"; @@ -1081,7 +1132,7 @@ mod routes { &app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues", Some(Body::from(body)), - Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), + Some(SESSION_ID.to_string()), ) .await; @@ -1144,7 +1195,7 @@ mod routes { &app, format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), Some(Body::from(body)), - Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), + Some(SESSION_ID.to_string()), ) .await; @@ -1214,7 +1265,7 @@ mod routes { &app, format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), Some(Body::from(body)), - Some("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy".to_string()), + Some(SESSION_ID.to_string()), ) .await; @@ -1328,4 +1379,69 @@ mod routes { ) ); } + + #[tokio::test] + async fn test_projects_create_patches() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ + "title": "Update README", + "description": "Do some changes to README", + "target": HEAD, + "oid": HEAD_1, + "tags": [], + })) + .unwrap(); + let response = post( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches", + Some(Body::from(body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::CREATED); + assert_eq!( + response.json().await, + json!( + { + "success": true, + "id": "cd370c8a263e8b0c9836d6a5dd3bf7a633d69acf", + } + ) + ); + + let response = get( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/cd370c8a263e8b0c9836d6a5dd3bf7a633d69acf", + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!( + { + "id": "cd370c8a263e8b0c9836d6a5dd3bf7a633d69acf", + "author": { + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + }, + "title": "Update README", + "description": "Do some changes to README", + "state": "proposed", + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": "a65512fc3b8ee7ec50f053c40266538fa11b82dd", + "description": "", + "reviews": [], + } + ], + } + ) + ); + } } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index ce12a261b..eec1e3b43 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -25,6 +25,7 @@ pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; pub const PATCH_ID: &str = "afb3063f8f0343fa31d2a0d55bac2a6f4a77125e"; pub const ISSUE_ID: &str = "331569cd5e4dcc55104363ebce92c78b0e5d67d4"; +pub const SESSION_ID: &str = "u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"; const PASSWORD: &str = "radicle"; @@ -144,7 +145,7 @@ pub async fn create_session(ctx: Context) { let issued_at = OffsetDateTime::now_utc(); let mut sessions = ctx.sessions().write().await; sessions.insert( - String::from("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"), + String::from(SESSION_ID), auth::Session { status: auth::AuthState::Authorized, public_key: ctx.profile().public_key, From 346737e3f884056e0dee365ccf9ba9279ad7e924 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 6 Mar 2023 10:56:20 +0100 Subject: [PATCH 075/264] Remove need to pass public key in cob store --- radicle-httpd/src/api.rs | 4 ++-- radicle-httpd/src/api/v1/delegates.rs | 4 ++-- radicle-httpd/src/api/v1/projects.rs | 18 +++++++++--------- radicle-httpd/src/test.rs | 5 ++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 49409d671..9592e5fba 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -51,8 +51,8 @@ impl Context { let doc = repo.identity_doc()?.1.verified()?; let payload = doc.project()?; let delegates = doc.delegates; - let issues = Issues::open(self.profile.public_key, &repo)?.counts()?; - let patches = Patches::open(self.profile.public_key, &repo)?.counts()?; + let issues = Issues::open(&repo)?.counts()?; + let patches = Patches::open(&repo)?.counts()?; Ok(project::Info { payload, diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index c025f74f4..f74935174 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -49,9 +49,9 @@ async fn delegates_projects_handler( return None; } - let Ok(issues) = Issues::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(issues) = Issues::open(&repo) else { return None }; let Ok(issues) = issues.counts() else { return None }; - let Ok(patches) = Patches::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(patches) = Patches::open(&repo) else { return None }; let Ok(patches) = patches.counts() else { return None }; Some(Info { diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 53d925bde..9260706a1 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -83,9 +83,9 @@ async fn project_root_handler( let Ok((_, doc)) = repo.identity_doc() else { return None }; let Ok(doc) = doc.verified() else { return None }; let Ok(payload) = doc.project() else { return None }; - let Ok(issues) = issue::Issues::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(issues) = issue::Issues::open(&repo) else { return None }; let Ok(issues) = issues.counts() else { return None }; - let Ok(patches) = patch::Patches::open(ctx.profile.public_key, &repo) else { return None }; + let Ok(patches) = patch::Patches::open(&repo) else { return None }; let Ok(patches) = patches.counts() else { return None }; let delegates = doc.delegates; @@ -395,7 +395,7 @@ async fn issues_handler( let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let issues = issue::Issues::open(ctx.profile.public_key, &repo)?; + let issues = issue::Issues::open(&repo)?; let mut issues: Vec<_> = issues.all()?.filter_map(|r| r.ok()).collect::>(); issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let issues = issues @@ -432,7 +432,7 @@ async fn issue_create_handler( .signer() .map_err(|_| Error::Auth("Unauthorized"))?; let repo = storage.repository(project)?; - let mut issues = issue::Issues::open(ctx.profile.public_key, &repo)?; + let mut issues = issue::Issues::open(&repo)?; let issue = issues .create( issue.title, @@ -466,7 +466,7 @@ async fn issue_update_handler( let storage = &ctx.profile.storage; let signer = ctx.profile.signer().unwrap(); let repo = storage.repository(project)?; - let mut issues = issue::Issues::open(ctx.profile.public_key, &repo)?; + let mut issues = issue::Issues::open(&repo)?; let mut issue = issues.get_mut(&issue_id.into())?; match action { @@ -514,7 +514,7 @@ async fn issue_handler( ) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let issue = issue::Issues::open(ctx.profile.public_key, &repo)? + let issue = issue::Issues::open(&repo)? .get(&issue_id.into())? .ok_or(Error::NotFound)?; @@ -549,7 +549,7 @@ async fn patch_create_handler( .signer() .map_err(|_| Error::Auth("Unauthorized"))?; let repo = storage.repository(project)?; - let mut patches = patch::Patches::open(ctx.profile.public_key, &repo)?; + let mut patches = patch::Patches::open(&repo)?; let base_oid = repo.raw().merge_base(*patch.target, *patch.oid)?; let patch = patches @@ -582,7 +582,7 @@ async fn patches_handler( let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let patches = patch::Patches::open(ctx.profile.public_key, &repo)?; + let patches = patch::Patches::open(&repo)?; let mut patches = patches.all()?.filter_map(|r| r.ok()).collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let patches = patches @@ -603,7 +603,7 @@ async fn patch_handler( ) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = storage.repository(project)?; - let patch = patch::Patches::open(ctx.profile.public_key, &repo)? + let patch = patch::Patches::open(&repo)? .get(&patch_id.into())? .ok_or(Error::NotFound)?; diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index eec1e3b43..0010cbee3 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -17,7 +17,6 @@ use radicle::git::raw as git2; use radicle::storage::ReadStorage; use radicle_cli::commands::rad_init; use radicle_crypto::ssh::keystore::MemorySigner; -use radicle_crypto::Signer; use crate::api::{auth, Context}; @@ -110,7 +109,7 @@ pub fn seed(dir: &Path) -> Context { let storage = &profile.storage; let (_, id) = radicle::rad::repo(&workdir).unwrap(); let repo = storage.repository(id).unwrap(); - let mut issues = Issues::open(*signer.public_key(), &repo).unwrap(); + let mut issues = Issues::open(&repo).unwrap(); issues .create( "Issue #1".to_string(), @@ -122,7 +121,7 @@ pub fn seed(dir: &Path) -> Context { .unwrap(); // eq. rad patch open - let mut patches = Patches::open(*signer.public_key(), &repo).unwrap(); + let mut patches = Patches::open(&repo).unwrap(); let oid = radicle::git::Oid::from_str(HEAD).unwrap(); let base = radicle::git::Oid::from_str(HEAD_1).unwrap(); patches From 5b6ccaf7dc8f7ab22c3cb2b5180df0854ea6e74e Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 6 Mar 2023 12:00:22 +0100 Subject: [PATCH 076/264] cli: Give more control over `init` announcement Instead of always announcing the inventory, by default we don't. Users can run `rad push` to trigger an announcement. --- radicle-httpd/src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 0010cbee3..9f7e51464 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -95,7 +95,7 @@ pub fn seed(dir: &Path) -> Context { interactive: false.into(), setup_signing: false, set_upstream: false, - sync: false, + announce: false, track: false, }, &profile, From 1535149dd09e582339396f23882a09922401cff1 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 6 Mar 2023 12:16:30 +0100 Subject: [PATCH 077/264] cli: Add `--verbose` flag to `init` --- radicle-httpd/src/test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 9f7e51464..d3a65691d 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -97,6 +97,7 @@ pub fn seed(dir: &Path) -> Context { set_upstream: false, announce: false, track: false, + verbose: false, }, &profile, ) From 11ea75f0ca0f5aa57743ac48a0e0b802fa2c543c Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 6 Mar 2023 13:13:18 +0100 Subject: [PATCH 078/264] Use commit oid for COB parent We were using the blob oid, which was causing the commit tree to be invalid. --- radicle-httpd/src/api/v1/projects.rs | 24 +++++++++++++----------- radicle-httpd/src/test.rs | 9 ++++----- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 9260706a1..19a0a0719 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -617,9 +617,11 @@ mod routes { use pretty_assertions::assert_eq; use serde_json::json; - use crate::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID, SESSION_ID}; + use crate::test::{ + self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID, SESSION_ID, TIMESTAMP, + }; - const CREATED_ISSUE_ID: &str = "745052a1603000b9566445753d7e2fee1ff5041f"; + const CREATED_ISSUE_ID: &str = "7596e4b788c37d633a8bd665e4d3a5a632c90eab"; #[tokio::test] async fn test_projects_root() { @@ -1105,7 +1107,7 @@ mod routes { }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], - "timestamp": 1673001014, + "timestamp": TIMESTAMP, "replyTo": null } ], @@ -1167,7 +1169,7 @@ mod routes { }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], - "timestamp": 1673001014, + "timestamp": TIMESTAMP, "replyTo": null, }], "tags": [ @@ -1228,7 +1230,7 @@ mod routes { }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], - "timestamp": 1673001014, + "timestamp": TIMESTAMP, "replyTo": null, }, { @@ -1238,7 +1240,7 @@ mod routes { }, "body": "This is first-level comment", "reactions": [], - "timestamp": 1673001014, + "timestamp": TIMESTAMP, "replyTo": null, }, ], @@ -1298,7 +1300,7 @@ mod routes { }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], - "timestamp": 1673001014, + "timestamp": TIMESTAMP, "replyTo": null, }, { @@ -1308,7 +1310,7 @@ mod routes { }, "body": "This is a reply to the first comment", "reactions": [], - "timestamp": 1673001014, + "timestamp": TIMESTAMP, "replyTo": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", }, ], @@ -1408,14 +1410,14 @@ mod routes { json!( { "success": true, - "id": "cd370c8a263e8b0c9836d6a5dd3bf7a633d69acf", + "id": "48d6d96e1f80b3ccd5bd9675d407e09e7bbaa7ab", } ) ); let response = get( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/cd370c8a263e8b0c9836d6a5dd3bf7a633d69acf", + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/48d6d96e1f80b3ccd5bd9675d407e09e7bbaa7ab", ) .await; @@ -1424,7 +1426,7 @@ mod routes { response.json().await, json!( { - "id": "cd370c8a263e8b0c9836d6a5dd3bf7a633d69acf", + "id": "48d6d96e1f80b3ccd5bd9675d407e09e7bbaa7ab", "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index d3a65691d..af7063cf0 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -22,9 +22,10 @@ use crate::api::{auth, Context}; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; -pub const PATCH_ID: &str = "afb3063f8f0343fa31d2a0d55bac2a6f4a77125e"; -pub const ISSUE_ID: &str = "331569cd5e4dcc55104363ebce92c78b0e5d67d4"; +pub const PATCH_ID: &str = "3cccb7ac7325215731c62c9c5aef2ec51ba91317"; +pub const ISSUE_ID: &str = "dc368184c379a7802c78b729ed631d2712ce22ab"; pub const SESSION_ID: &str = "u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"; +pub const TIMESTAMP: u64 = 1671125284; const PASSWORD: &str = "radicle"; @@ -32,6 +33,7 @@ pub fn seed(dir: &Path) -> Context { let workdir = dir.join("hello-world"); let rad_home = dir.join("radicle"); + env::set_var("RAD_COMMIT_TIME", TIMESTAMP.to_string()); env::set_var("RAD_PASSPHRASE", PASSWORD); env::set_var( "RAD_SEED", @@ -103,9 +105,6 @@ pub fn seed(dir: &Path) -> Context { ) .unwrap(); - // eq. rad issue new - env::set_var("RAD_COMMIT_TIME", "1673001014"); - let signer = MemorySigner::load(&profile.keystore, PASSWORD.to_owned().into()).unwrap(); let storage = &profile.storage; let (_, id) = radicle::rad::repo(&workdir).unwrap(); From 7104cde3e7a660b37a2835eea70807e3737f0232 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 6 Mar 2023 17:23:08 +0000 Subject: [PATCH 079/264] httpd: Set a max allowed size of 4 MiB when serving files raw Signed-off-by: xphoniex --- radicle-httpd/src/error.rs | 4 ++ radicle-httpd/src/raw.rs | 98 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 5 deletions(-) diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs index a722aa8ef..96572bc82 100644 --- a/radicle-httpd/src/error.rs +++ b/radicle-httpd/src/error.rs @@ -35,6 +35,10 @@ pub enum Error { /// Surf error. #[error(transparent)] Surf(#[from] radicle_surf::Error), + + // Surf file error. + #[error(transparent)] + SurfFile(#[from] radicle_surf::fs::error::File), } impl Error { diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs index 648defc91..9bc87a372 100644 --- a/radicle-httpd/src/raw.rs +++ b/radicle-httpd/src/raw.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use axum::extract::State; -use axum::http::header; +use axum::http::{header, StatusCode}; use axum::response::IntoResponse; use axum::routing::get; use axum::Router; @@ -15,6 +15,80 @@ use radicle_surf::{Oid, Repository}; use crate::axum_extra::Path; use crate::error::Error; +const MAX_BLOB_SIZE: usize = 4_194_304; + +static MIMES: &[(&str, &str)] = &[ + ("3gp", "video/3gpp"), + ("7z", "application/x-7z-compressed"), + ("aac", "audio/aac"), + ("avi", "video/x-msvideo"), + ("bin", "application/octet-stream"), + ("bmp", "image/bmp"), + ("bz", "application/x-bzip"), + ("bz2", "application/x-bzip2"), + ("csh", "application/x-csh"), + ("css", "text/css"), + ("csv", "text/csv"), + ("doc", "application/msword"), + ( + "docx", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ), + ("epub", "application/epub+zip"), + ("gz", "application/gzip"), + ("gif", "image/gif"), + ("htm", "text/html"), + ("html", "text/html"), + ("ico", "image/vnd.microsoft.icon"), + ("jar", "application/java-archive"), + ("jpeg", "image/jpeg"), + ("jpg", "image/jpeg"), + ("js", "text/javascript"), + ("json", "application/json"), + ("mjs", "text/javascript"), + ("mp3", "audio/mpeg"), + ("mp4", "video/mp4"), + ("mpeg", "video/mpeg"), + ("odp", "application/vnd.oasis.opendocument.presentation"), + ("ods", "application/vnd.oasis.opendocument.spreadsheet"), + ("odt", "application/vnd.oasis.opendocument.text"), + ("oga", "audio/ogg"), + ("ogv", "video/ogg"), + ("ogx", "application/ogg"), + ("otf", "font/otf"), + ("png", "image/png"), + ("pdf", "application/pdf"), + ("php", "application/x-httpd-php"), + ("ppt", "application/vnd.ms-powerpoint"), + ( + "pptx", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ), + ("rar", "application/vnd.rar"), + ("rtf", "application/rtf"), + ("sh", "application/x-sh"), + ("svg", "image/svg+xml"), + ("tar", "application/x-tar"), + ("tif", "image/tiff"), + ("tiff", "image/tiff"), + ("ttf", "font/ttf"), + ("txt", "text/plain"), + ("wav", "audio/wav"), + ("weba", "audio/webm"), + ("webm", "video/webm"), + ("webp", "image/webp"), + ("woff", "font/woff"), + ("woff2", "font/woff2"), + ("xhtml", "application/xhtml+xml"), + ("xls", "application/vnd.ms-excel"), + ( + "xlsx", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ), + ("xml", "application/xml"), + ("zip", "application/zip"), +]; + pub fn router(profile: Arc) -> Router { Router::new() .route("/:project/:sha/*path", get(file_handler)) @@ -27,12 +101,26 @@ async fn file_handler( ) -> impl IntoResponse { let storage = &profile.storage; let repo = Repository::open(paths::repository(storage, &project))?; - let blob = repo.blob(sha, &path)?; - let mut response_headers = HeaderMap::new(); - response_headers.insert(header::CONTENT_TYPE, "text; charset=utf-8".parse().unwrap()); - Ok::<_, Error>((response_headers, blob.content().to_owned())) + if repo.file(sha, &path)?.content(&repo)?.size() > MAX_BLOB_SIZE { + return Ok::<_, Error>((StatusCode::PAYLOAD_TOO_LARGE, response_headers, vec![])); + } + + let blob = repo.blob(sha, &path)?; + let mime = { + if let Some(ext) = path.split('.').last() { + MIMES + .binary_search_by(|(k, _)| k.cmp(&ext)) + .map(|k| MIMES[k].1) + .unwrap_or("text; charset=utf-8") + } else { + "application/octet-stream" + } + }; + response_headers.insert(header::CONTENT_TYPE, mime.parse().unwrap()); + + Ok::<_, Error>((StatusCode::OK, response_headers, blob.content().to_owned())) } #[cfg(test)] From 2b48df9526225cb9c5ae49762feb03cbc21a4e00 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 7 Mar 2023 09:36:18 +0000 Subject: [PATCH 080/264] httpd: Use `radicle-term` instead of `radicle-cli::terminal` Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 5 +++-- radicle-httpd/src/tracing_extra.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 7152366d5..619d0304f 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -40,12 +40,13 @@ tracing-subscriber = { version = "0.3", default-features = false, features = ["s path = "../radicle" version = "0.2.0" -[dependencies.radicle-cli] -path = "../radicle-cli" +[dependencies.radicle-term] +path = "../radicle-term" [dev-dependencies] hyper = { version = "0.14.17", default-features = false, features = ["client"] } pretty_assertions = { version = "1.3.0" } +radicle-cli = { path = "../radicle-cli" } radicle-crypto = { path = "../radicle-crypto" } tempfile = { version = "3.3.0" } tower = { version = "0.4", features = ["util"] } diff --git a/radicle-httpd/src/tracing_extra.rs b/radicle-httpd/src/tracing_extra.rs index 1e0b4c78b..91cecae51 100644 --- a/radicle-httpd/src/tracing_extra.rs +++ b/radicle-httpd/src/tracing_extra.rs @@ -10,7 +10,7 @@ use axum::response::IntoResponse; use axum::Extension; use hyper::{Method, StatusCode, Uri, Version}; -pub use radicle_cli::terminal::ansi::Paint; +pub use radicle_term::ansi::Paint; #[derive(Clone)] pub struct RequestId(Arc); From 0535b0c5c28b77732b9b39e075dd62f87a9de5b0 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 7 Mar 2023 13:39:13 +0000 Subject: [PATCH 081/264] httpd: Remove `radicle_cli` from tests Signed-off-by: xphoniex --- radicle-httpd/src/test.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index af7063cf0..31de29296 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -13,9 +13,8 @@ use tower::ServiceExt; use radicle::cob::issue::Issues; use radicle::cob::patch::{MergeTarget, Patches}; -use radicle::git::raw as git2; +use radicle::git::{raw as git2, RefString}; use radicle::storage::ReadStorage; -use radicle_cli::commands::rad_init; use radicle_crypto::ssh::keystore::MemorySigner; use crate::api::{auth, Context}; @@ -88,20 +87,18 @@ pub fn seed(dir: &Path) -> Context { radicle::Profile::init(rad_home.try_into().unwrap(), PASSWORD.to_owned()).unwrap(); // rad init - rad_init::init( - rad_init::Options { - path: Some(workdir.clone()), - name: Some("hello-world".to_string()), - description: Some("Rad repository for tests".to_string()), - branch: None, - interactive: false.into(), - setup_signing: false, - set_upstream: false, - announce: false, - track: false, - verbose: false, - }, - &profile, + let repo = git2::Repository::open(&workdir).unwrap(); + let name = "hello-world".to_string(); + let description = "Rad repository for tests".to_string(); + let signer = profile.signer().unwrap(); + let branch = RefString::try_from("master").unwrap(); + radicle::rad::init( + &repo, + &name, + &description, + branch, + &signer, + &profile.storage, ) .unwrap(); From 79934c3dfe0599997987d72c1830d0aa64df260d Mon Sep 17 00:00:00 2001 From: xphoniex Date: Wed, 8 Mar 2023 13:12:31 +0000 Subject: [PATCH 082/264] httpd: Run tests off another remote for `test::seed` Keep `test::contributor(..)` to test interactions with a contributor node. Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/api/v1/projects.rs | 102 ++++++++++++++++----------- radicle-httpd/src/test.rs | 33 ++++++--- 3 files changed, 88 insertions(+), 49 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 619d0304f..6f05ec6ac 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -47,6 +47,6 @@ path = "../radicle-term" hyper = { version = "0.14.17", default-features = false, features = ["client"] } pretty_assertions = { version = "1.3.0" } radicle-cli = { path = "../radicle-cli" } -radicle-crypto = { path = "../radicle-crypto" } +radicle-crypto = { path = "../radicle-crypto", features = ["test"] } tempfile = { version = "3.3.0" } tower = { version = "0.4", features = ["util"] } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 19a0a0719..11520f6cf 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -618,11 +618,10 @@ mod routes { use serde_json::json; use crate::test::{ - self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID, SESSION_ID, TIMESTAMP, + self, get, patch, post, CONTRIBUTOR_ISSUE_ID, CONTRIBUTOR_PUB_KEY, CONTRIBUTOR_RID, HEAD, + HEAD_1, ISSUE_COMMENT_ID, ISSUE_DISCUSSION_ID, ISSUE_ID, PATCH_ID, SESSION_ID, TIMESTAMP, }; - const CREATED_ISSUE_ID: &str = "7596e4b788c37d633a8bd665e4d3a5a632c90eab"; - #[tokio::test] async fn test_projects_root() { let tmp = tempfile::tempdir().unwrap(); @@ -992,7 +991,11 @@ mod routes { async fn test_projects_remotes() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi").await; + let response = get( + &app, + "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + ) + .await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -1101,7 +1104,7 @@ mod routes { "assignees": [], "discussion": [ { - "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", + "id": ISSUE_DISCUSSION_ID, "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, @@ -1119,10 +1122,14 @@ mod routes { #[tokio::test] async fn test_projects_issues_create() { + const CREATED_ISSUE_ID: &str = "1b706bb9af971d43ec38718c71402e36055c5c89"; + let tmp = tempfile::tempdir().unwrap(); - let ctx = test::seed(tmp.path()); + let ctx = test::contributor(tmp.path()); let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ "title": "Issue #2", "description": "Change 'hello world' to 'hello everyone'", @@ -1130,9 +1137,10 @@ mod routes { "assignees": [], })) .unwrap(); + let response = post( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues", + format!("/projects/{CONTRIBUTOR_RID}/issues"), Some(Body::from(body)), Some(SESSION_ID.to_string()), ) @@ -1146,7 +1154,7 @@ mod routes { let response = get( &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{CREATED_ISSUE_ID}"), + format!("/projects/{CONTRIBUTOR_RID}/issues/{CREATED_ISSUE_ID}"), ) .await; @@ -1155,7 +1163,7 @@ mod routes { json!({ "id": CREATED_ISSUE_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "assignees": [], "title": "Issue #2", @@ -1163,9 +1171,9 @@ mod routes { "status": "open", }, "discussion": [{ - "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", + "id": ISSUE_DISCUSSION_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1182,9 +1190,11 @@ mod routes { #[tokio::test] async fn test_projects_issues_comment() { let tmp = tempfile::tempdir().unwrap(); - let ctx = test::seed(tmp.path()); + let ctx = test::contributor(tmp.path()); let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ "type": "thread", "action": { @@ -1193,9 +1203,10 @@ mod routes { } })) .unwrap(); + let response = patch( &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), + format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"), Some(Body::from(body)), Some(SESSION_ID.to_string()), ) @@ -1206,16 +1217,16 @@ mod routes { let response = get( &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), + format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"), ) .await; assert_eq!( response.json().await, json!({ - "id": ISSUE_ID, + "id": CONTRIBUTOR_ISSUE_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "assignees": [], "title": "Issue #1", @@ -1224,9 +1235,9 @@ mod routes { }, "discussion": [ { - "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", + "id": ISSUE_DISCUSSION_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1234,9 +1245,9 @@ mod routes { "replyTo": null, }, { - "id": "f7da49e705f60c39265dbdd748d786a620bc8030", + "id": "265af21e409eacc8eb150b73882ac3ada9d4aea3", "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "body": "This is first-level comment", "reactions": [], @@ -1252,20 +1263,26 @@ mod routes { #[tokio::test] async fn test_projects_issues_reply() { let tmp = tempfile::tempdir().unwrap(); - let ctx = test::seed(tmp.path()); + let ctx = test::contributor(tmp.path()); let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ "type":"thread", "action": { "type": "comment", "body": "This is a reply to the first comment", - "replyTo": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", + "replyTo": ISSUE_DISCUSSION_ID, }})) .unwrap(); + + let response = get(&app, format!("/projects/{CONTRIBUTOR_RID}/issues")).await; + println!("{:?}", response.json().await); + let response = patch( &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), + format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"), Some(Body::from(body)), Some(SESSION_ID.to_string()), ) @@ -1276,16 +1293,16 @@ mod routes { let response = get( &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues/{ISSUE_ID}"), + format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"), ) .await; assert_eq!( response.json().await, json!({ - "id": ISSUE_ID, + "id": CONTRIBUTOR_ISSUE_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "assignees": [], "title": "Issue #1", @@ -1294,9 +1311,9 @@ mod routes { }, "discussion": [ { - "id": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", + "id": ISSUE_DISCUSSION_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1304,14 +1321,14 @@ mod routes { "replyTo": null, }, { - "id": "ab98b5b794d02c23d29769a39fe8e0b74624f3d8", + "id": ISSUE_COMMENT_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + "id": CONTRIBUTOR_PUB_KEY, }, "body": "This is a reply to the first comment", "reactions": [], "timestamp": TIMESTAMP, - "replyTo": "f0afe34f5bf4248df432f6b6a8818bcae360bbc2", + "replyTo": ISSUE_DISCUSSION_ID, }, ], "tags": [], @@ -1341,7 +1358,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "d6ba305f78e2fa1ebcc55d8c3be8806bbce25fb4", + "id": "47878ed82515772f4c44e4796c330f4a74473559", "description": "", "reviews": [], } @@ -1372,7 +1389,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "d6ba305f78e2fa1ebcc55d8c3be8806bbce25fb4", + "id": "47878ed82515772f4c44e4796c330f4a74473559", "description": "", "reviews": [], } @@ -1384,10 +1401,14 @@ mod routes { #[tokio::test] async fn test_projects_create_patches() { + const CREATED_PATCH_ID: &str = "22f8fbe09f7430579dd0730e4f2394362d844647"; + let tmp = tempfile::tempdir().unwrap(); - let ctx = test::seed(tmp.path()); + let ctx = test::contributor(tmp.path()); let app = super::router(ctx.to_owned()); + test::create_session(ctx).await; + let body = serde_json::to_vec(&json!({ "title": "Update README", "description": "Do some changes to README", @@ -1396,9 +1417,10 @@ mod routes { "tags": [], })) .unwrap(); + let response = post( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches", + format!("/projects/{CONTRIBUTOR_RID}/patches"), Some(Body::from(body)), Some(SESSION_ID.to_string()), ) @@ -1410,14 +1432,14 @@ mod routes { json!( { "success": true, - "id": "48d6d96e1f80b3ccd5bd9675d407e09e7bbaa7ab", + "id": CREATED_PATCH_ID, } ) ); let response = get( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/48d6d96e1f80b3ccd5bd9675d407e09e7bbaa7ab", + format!("/projects/{CONTRIBUTOR_RID}/patches/{CREATED_PATCH_ID}"), ) .await; @@ -1426,9 +1448,9 @@ mod routes { response.json().await, json!( { - "id": "48d6d96e1f80b3ccd5bd9675d407e09e7bbaa7ab", + "id": CREATED_PATCH_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": CONTRIBUTOR_PUB_KEY }, "title": "Update README", "description": "Do some changes to README", @@ -1437,7 +1459,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "a65512fc3b8ee7ec50f053c40266538fa11b82dd", + "id": "73efc59cd9f787deff0ae1629f47f1d90f307282", "description": "", "reviews": [], } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 31de29296..2c5214dc6 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -15,20 +15,33 @@ use radicle::cob::issue::Issues; use radicle::cob::patch::{MergeTarget, Patches}; use radicle::git::{raw as git2, RefString}; use radicle::storage::ReadStorage; -use radicle_crypto::ssh::keystore::MemorySigner; +use radicle_crypto::test::signer::MockSigner; use crate::api::{auth, Context}; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; -pub const PATCH_ID: &str = "3cccb7ac7325215731c62c9c5aef2ec51ba91317"; -pub const ISSUE_ID: &str = "dc368184c379a7802c78b729ed631d2712ce22ab"; +pub const PATCH_ID: &str = "73d5187bb38835a232ce6ff41102a94bb92e5130"; +pub const ISSUE_ID: &str = "959afe03c1dbea4f47462bb7824584a78741f59c"; +pub const ISSUE_DISCUSSION_ID: &str = "e2874702515026daf62d4385cafb88fef1fad0c8"; +pub const ISSUE_COMMENT_ID: &str = "905d72c1f13f282f86cb93ce9f7eb9464a08ba79"; pub const SESSION_ID: &str = "u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"; pub const TIMESTAMP: u64 = 1671125284; +pub const CONTRIBUTOR_RID: &str = "rad:z4XaCmN3jLSeiMvW15YTDpNbDHFhG"; +pub const CONTRIBUTOR_PUB_KEY: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; +pub const CONTRIBUTOR_ISSUE_ID: &str = "b991ccfa866e164b81a4aa0a7e082357b6c75306"; const PASSWORD: &str = "radicle"; pub fn seed(dir: &Path) -> Context { + seed_with_signer(dir, false) +} + +pub fn contributor(dir: &Path) -> Context { + seed_with_signer(dir, true) +} + +fn seed_with_signer(dir: &Path, self_signer: bool) -> Context { let workdir = dir.join("hello-world"); let rad_home = dir.join("radicle"); @@ -36,7 +49,7 @@ pub fn seed(dir: &Path) -> Context { env::set_var("RAD_PASSPHRASE", PASSWORD); env::set_var( "RAD_SEED", - "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffee", ); fs::create_dir_all(&workdir).unwrap(); @@ -86,13 +99,19 @@ pub fn seed(dir: &Path) -> Context { let profile = radicle::Profile::init(rad_home.try_into().unwrap(), PASSWORD.to_owned()).unwrap(); + let mock_signer = MockSigner::from_seed([0xff; 32]); + // rad init let repo = git2::Repository::open(&workdir).unwrap(); let name = "hello-world".to_string(); let description = "Rad repository for tests".to_string(); - let signer = profile.signer().unwrap(); + let signer = if self_signer { + profile.signer().unwrap() + } else { + Box::new(mock_signer) + }; let branch = RefString::try_from("master").unwrap(); - radicle::rad::init( + let (id, _, _) = radicle::rad::init( &repo, &name, &description, @@ -102,9 +121,7 @@ pub fn seed(dir: &Path) -> Context { ) .unwrap(); - let signer = MemorySigner::load(&profile.keystore, PASSWORD.to_owned().into()).unwrap(); let storage = &profile.storage; - let (_, id) = radicle::rad::repo(&workdir).unwrap(); let repo = storage.repository(id).unwrap(); let mut issues = Issues::open(&repo).unwrap(); issues From 2b44ff1b55e371c419cf2076b7f97b4404b41a34 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 9 Mar 2023 11:20:34 +0100 Subject: [PATCH 083/264] cob: Use `EntryId` as the operation identifier By introducing a small limitation: only allowing entries in the change graph to be addressable, instead of individual operations; we drastically simplify the CRDT implementation. There are four advantages: 1. Op ids are just regular SHA-1s 2. There's no need for relative IDs, ops never refer to other ops within the same commit 3. There's no need for a nonce, since commits can't collide, and neither can op IDs 4. `OpId` can just be an alias of `EntryId` The disadvantage of course, is that we have to be mindful of how we create op transactions, since each transaction creates an addressable unit. For example, we must not include multiple patch revisions in the same transaction. --- radicle-httpd/src/api/error.rs | 11 +++- radicle-httpd/src/api/json.rs | 4 +- radicle-httpd/src/api/v1/projects.rs | 22 ++++--- radicle-httpd/src/test.rs | 89 ++++++++++++++++------------ 4 files changed, 71 insertions(+), 55 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 5608a649c..91d593a1a 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -81,10 +81,17 @@ impl IntoResponse for Error { StatusCode::INTERNAL_SERVER_ERROR, Some(e.message().to_owned()), ), - _ => { + other => { tracing::error!("Error: {:?}", &self); - (StatusCode::INTERNAL_SERVER_ERROR, None) + if cfg!(debug_assertions) { + ( + StatusCode::INTERNAL_SERVER_ERROR, + Some(format!("{other:?}")), + ) + } else { + (StatusCode::INTERNAL_SERVER_ERROR, None) + } } }; diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index dd4bed70b..c542f556f 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -9,7 +9,7 @@ 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, OpId, Timestamp}; +use radicle::cob::{Author, Timestamp}; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; use radicle_surf::{Commit, Stats}; @@ -132,7 +132,7 @@ fn name_in_path(path: &str) -> &str { #[derive(Serialize)] #[serde(rename_all = "camelCase")] struct Comment { - id: OpId, + id: CommentId, author: Author, body: String, reactions: [String; 0], diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 11520f6cf..0720b25d7 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1104,7 +1104,7 @@ mod routes { "assignees": [], "discussion": [ { - "id": ISSUE_DISCUSSION_ID, + "id": ISSUE_ID, "author": { "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, @@ -1122,7 +1122,7 @@ mod routes { #[tokio::test] async fn test_projects_issues_create() { - const CREATED_ISSUE_ID: &str = "1b706bb9af971d43ec38718c71402e36055c5c89"; + const CREATED_ISSUE_ID: &str = "b457364fbe2ef0eac69a835a087f60ee13ccb367"; let tmp = tempfile::tempdir().unwrap(); let ctx = test::contributor(tmp.path()); @@ -1171,7 +1171,7 @@ mod routes { "status": "open", }, "discussion": [{ - "id": ISSUE_DISCUSSION_ID, + "id": CREATED_ISSUE_ID, "author": { "id": CONTRIBUTOR_PUB_KEY, }, @@ -1245,7 +1245,7 @@ mod routes { "replyTo": null, }, { - "id": "265af21e409eacc8eb150b73882ac3ada9d4aea3", + "id": "9685b141c2e939c3d60f8ca34f8c7bf01a609af1", "author": { "id": CONTRIBUTOR_PUB_KEY, }, @@ -1269,7 +1269,7 @@ mod routes { test::create_session(ctx).await; let body = serde_json::to_vec(&json!({ - "type":"thread", + "type": "thread", "action": { "type": "comment", "body": "This is a reply to the first comment", @@ -1277,9 +1277,7 @@ mod routes { }})) .unwrap(); - let response = get(&app, format!("/projects/{CONTRIBUTOR_RID}/issues")).await; - println!("{:?}", response.json().await); - + let _ = get(&app, format!("/projects/{CONTRIBUTOR_RID}/issues")).await; let response = patch( &app, format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"), @@ -1358,7 +1356,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "47878ed82515772f4c44e4796c330f4a74473559", + "id": PATCH_ID, "description": "", "reviews": [], } @@ -1389,7 +1387,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "47878ed82515772f4c44e4796c330f4a74473559", + "id": PATCH_ID, "description": "", "reviews": [], } @@ -1401,7 +1399,7 @@ mod routes { #[tokio::test] async fn test_projects_create_patches() { - const CREATED_PATCH_ID: &str = "22f8fbe09f7430579dd0730e4f2394362d844647"; + const CREATED_PATCH_ID: &str = "54505091ff3561466cfbe83e7e23c21cb1bb8a17"; let tmp = tempfile::tempdir().unwrap(); let ctx = test::contributor(tmp.path()); @@ -1459,7 +1457,7 @@ mod routes { "tags": [], "revisions": [ { - "id": "73efc59cd9f787deff0ae1629f47f1d90f307282", + "id": CREATED_PATCH_ID, "description": "", "reviews": [], } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 2c5214dc6..5ba342468 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -1,4 +1,3 @@ -use std::convert::TryInto as _; use std::path::Path; use std::str::FromStr; use std::sync::Arc; @@ -13,47 +12,77 @@ use tower::ServiceExt; use radicle::cob::issue::Issues; use radicle::cob::patch::{MergeTarget, Patches}; +use radicle::crypto::ssh::keystore::MemorySigner; +use radicle::crypto::ssh::Keystore; +use radicle::crypto::{KeyPair, Seed, Signer}; use radicle::git::{raw as git2, RefString}; +use radicle::profile::Home; use radicle::storage::ReadStorage; +use radicle::Storage; use radicle_crypto::test::signer::MockSigner; use crate::api::{auth, Context}; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; -pub const PATCH_ID: &str = "73d5187bb38835a232ce6ff41102a94bb92e5130"; -pub const ISSUE_ID: &str = "959afe03c1dbea4f47462bb7824584a78741f59c"; -pub const ISSUE_DISCUSSION_ID: &str = "e2874702515026daf62d4385cafb88fef1fad0c8"; -pub const ISSUE_COMMENT_ID: &str = "905d72c1f13f282f86cb93ce9f7eb9464a08ba79"; +pub const PATCH_ID: &str = "6ec9a764a888576abc7e582dbf82a31e23a9789d"; +pub const ISSUE_ID: &str = "5ad77fa3f476beed9a26f49b2b3b844e61bef792"; +pub const ISSUE_DISCUSSION_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; +pub const ISSUE_COMMENT_ID: &str = "845218041bf9eb8155bfa4aaa8f0c91ce18e5c13"; pub const SESSION_ID: &str = "u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"; pub const TIMESTAMP: u64 = 1671125284; pub const CONTRIBUTOR_RID: &str = "rad:z4XaCmN3jLSeiMvW15YTDpNbDHFhG"; pub const CONTRIBUTOR_PUB_KEY: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; -pub const CONTRIBUTOR_ISSUE_ID: &str = "b991ccfa866e164b81a4aa0a7e082357b6c75306"; +pub const CONTRIBUTOR_ISSUE_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; const PASSWORD: &str = "radicle"; +/// Create a new profile. +pub fn profile(home: &Path, seed: [u8; 32]) -> radicle::Profile { + let home = Home::new(home).unwrap(); + let storage = Storage::open(home.storage()).unwrap(); + let keystore = Keystore::new(&home.keys()); + let keypair = KeyPair::from_seed(Seed::from(seed)); + + radicle::storage::git::transport::local::register(storage.clone()); + keystore + .store(keypair.clone(), "radicle", PASSWORD.to_owned()) + .unwrap(); + + radicle::Profile { + home, + storage, + keystore, + public_key: keypair.pk.into(), + } +} + pub fn seed(dir: &Path) -> Context { - seed_with_signer(dir, false) + let home = dir.join("radicle"); + let profile = profile(home.as_path(), [0xff; 32]); + let signer = Box::new(MockSigner::from_seed([0xff; 32])); + + seed_with_signer(dir, profile, &signer) } pub fn contributor(dir: &Path) -> Context { - seed_with_signer(dir, true) + let mut seed = [0xff; 32]; + *seed.last_mut().unwrap() = 0xee; + + let home = dir.join("radicle"); + let profile = profile(home.as_path(), seed); + let signer = MemorySigner::load(&profile.keystore, PASSWORD.to_owned().into()).unwrap(); + + seed_with_signer(dir, profile, &signer) } -fn seed_with_signer(dir: &Path, self_signer: bool) -> Context { +fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G) -> Context { let workdir = dir.join("hello-world"); - let rad_home = dir.join("radicle"); env::set_var("RAD_COMMIT_TIME", TIMESTAMP.to_string()); env::set_var("RAD_PASSPHRASE", PASSWORD); - env::set_var( - "RAD_SEED", - "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffee", - ); fs::create_dir_all(&workdir).unwrap(); - fs::create_dir_all(&rad_home).unwrap(); // add commits to workdir (repo) let repo = git2::Repository::init(&workdir).unwrap(); @@ -95,42 +124,24 @@ fn seed_with_signer(dir: &Path, self_signer: bool) -> Context { ) .unwrap(); - // eq. rad auth - let profile = - radicle::Profile::init(rad_home.try_into().unwrap(), PASSWORD.to_owned()).unwrap(); - - let mock_signer = MockSigner::from_seed([0xff; 32]); - // rad init let repo = git2::Repository::open(&workdir).unwrap(); let name = "hello-world".to_string(); let description = "Rad repository for tests".to_string(); - let signer = if self_signer { - profile.signer().unwrap() - } else { - Box::new(mock_signer) - }; let branch = RefString::try_from("master").unwrap(); - let (id, _, _) = radicle::rad::init( - &repo, - &name, - &description, - branch, - &signer, - &profile.storage, - ) - .unwrap(); + let (id, _, _) = + radicle::rad::init(&repo, &name, &description, branch, signer, &profile.storage).unwrap(); let storage = &profile.storage; let repo = storage.repository(id).unwrap(); let mut issues = Issues::open(&repo).unwrap(); - issues + let _ = issues .create( "Issue #1".to_string(), "Change 'hello world' to 'hello everyone'".to_string(), &[], &[], - &signer, + signer, ) .unwrap(); @@ -138,7 +149,7 @@ fn seed_with_signer(dir: &Path, self_signer: bool) -> Context { let mut patches = Patches::open(&repo).unwrap(); let oid = radicle::git::Oid::from_str(HEAD).unwrap(); let base = radicle::git::Oid::from_str(HEAD_1).unwrap(); - patches + let _ = patches .create( "A new `hello word`", "change `hello world` in README to something else", @@ -146,7 +157,7 @@ fn seed_with_signer(dir: &Path, self_signer: bool) -> Context { base, oid, &[], - &signer, + signer, ) .unwrap(); From 41e151266f0aada2212168a38cdeb99b297dcde4 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Sun, 12 Mar 2023 18:13:17 +0100 Subject: [PATCH 084/264] httpd: Basic alias support for git Allows cloning via git using an alias instead of the full RID. Eg. `git clone https://seed.radicle.xyz/heartwood.git` --- radicle-httpd/src/error.rs | 5 ++++ radicle-httpd/src/git.rs | 59 ++++++++++++++++++++++++++++++++------ radicle-httpd/src/lib.rs | 5 +++- radicle-httpd/src/main.rs | 13 +++++++-- radicle-httpd/src/test.rs | 1 + 5 files changed, 72 insertions(+), 11 deletions(-) diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs index 96572bc82..29ada51d8 100644 --- a/radicle-httpd/src/error.rs +++ b/radicle-httpd/src/error.rs @@ -4,6 +4,10 @@ use axum::response::{IntoResponse, Response}; /// Errors relating to the HTTP backend. #[derive(Debug, thiserror::Error)] pub enum Error { + /// The entity was not found. + #[error("not found")] + NotFound, + /// I/O error. #[error("i/o error: {0}")] Io(#[from] std::io::Error), @@ -47,6 +51,7 @@ impl Error { Error::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE, Error::InvalidId => http::StatusCode::NOT_FOUND, Error::Id(_) => http::StatusCode::NOT_FOUND, + Error::NotFound => http::StatusCode::NOT_FOUND, _ => http::StatusCode::INTERNAL_SERVER_ERROR, } } diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index 0785f096f..140428389 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -21,14 +21,14 @@ use radicle::profile::Profile; use crate::error::Error; -pub fn router(profile: Arc) -> Router { +pub fn router(profile: Arc, aliases: HashMap) -> Router { Router::new() .route("/:project/*request", any(git_handler)) - .with_state(profile) + .with_state((profile, aliases)) } async fn git_handler( - State(profile): State>, + State((profile, aliases)): State<(Arc, HashMap)>, AxumPath((project, request)): AxumPath<(String, String)>, method: Method, headers: HeaderMap, @@ -37,10 +37,21 @@ async fn git_handler( body: Bytes, ) -> impl IntoResponse { let query = query.0.unwrap_or_default(); - let id: Id = project.strip_suffix(".git").unwrap_or(&project).parse()?; + let name = project.strip_suffix(".git").unwrap_or(&project); + let rid: Id = match name.parse() { + Ok(rid) => rid, + Err(_) => { + let Some(rid) = aliases.get(name) else { + return Err(Error::NotFound); + }; + *rid + } + }; - let (status, headers, body) = - git_http_backend(&profile, method, headers, body, remote, id, &request, query).await?; + let (status, headers, body) = git_http_backend( + &profile, method, headers, body, remote, rid, &request, query, + ) + .await?; let mut response_headers = HeaderMap::new(); for (name, vec) in headers.iter() { @@ -195,22 +206,54 @@ async fn git_http_backend( #[cfg(test)] mod routes { + use std::collections::HashMap; use std::net::SocketAddr; + use std::str::FromStr; use axum::extract::connect_info::MockConnectInfo; use axum::http::StatusCode; + use radicle::identity::Id; - use crate::test::{self, get}; + use crate::test::{self, get, RID}; #[tokio::test] async fn test_invalid_route_returns_404() { let tmp = tempfile::tempdir().unwrap(); let ctx = test::seed(tmp.path()); - let app = super::router(ctx.profile().to_owned()) + let app = super::router(ctx.profile().to_owned(), HashMap::new()) .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); let response = get(&app, "/aa/a").await; assert_eq!(response.status(), StatusCode::NOT_FOUND); } + + #[tokio::test] + async fn test_info_request() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.profile().to_owned(), HashMap::new()) + .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); + + let response = get(&app, format!("/{RID}.git/info/refs")).await; + + assert_eq!(response.status(), StatusCode::OK); + } + + #[tokio::test] + async fn test_aliases() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router( + ctx.profile().to_owned(), + HashMap::from_iter([(String::from("heartwood"), Id::from_str(RID).unwrap())]), + ) + .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); + + let response = get(&app, "/woodheart.git/info/refs").await; + assert_eq!(response.status(), StatusCode::NOT_FOUND); + + let response = get(&app, "/heartwood.git/info/refs").await; + assert_eq!(response.status(), StatusCode::OK); + } } diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 59a252934..650bbf82b 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -2,6 +2,7 @@ #![allow(clippy::too_many_arguments)] pub mod error; +use std::collections::HashMap; use std::net::SocketAddr; use std::process::Command; use std::str; @@ -14,6 +15,7 @@ use axum::body::{Body, BoxBody, HttpBody}; use axum::http::{Request, Response}; use axum::middleware; use axum::Router; +use radicle::identity::Id; use tower_http::trace::TraceLayer; use tracing_extra::{tracing_middleware, ColoredStatus, Paint, RequestId, TracingInfo}; @@ -28,6 +30,7 @@ mod tracing_extra; #[derive(Debug, Clone)] pub struct Options { + pub aliases: HashMap, pub listen: SocketAddr, } @@ -48,7 +51,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> { let ctx = api::Context::new(profile.clone()); let api_router = api::router(ctx); - let git_router = git::router(profile.clone()); + let git_router = git::router(profile.clone(), options.aliases); let raw_router = raw::router(profile); tracing::info!("listening on http://{}", options.listen); diff --git a/radicle-httpd/src/main.rs b/radicle-httpd/src/main.rs index 4dac4b78a..2d4c07593 100644 --- a/radicle-httpd/src/main.rs +++ b/radicle-httpd/src/main.rs @@ -1,5 +1,6 @@ -use std::process; +use std::{collections::HashMap, process}; +use radicle::prelude::Id; use radicle_httpd as httpd; use tracing::dispatcher::Dispatch; @@ -49,6 +50,7 @@ fn parse_options() -> Result { let mut parser = lexopt::Parser::from_env(); let mut listen = None; + let mut aliases = HashMap::new(); while let Some(arg) = parser.next()? { match arg { @@ -56,14 +58,21 @@ fn parse_options() -> Result { let addr = parser.value()?.parse()?; listen = Some(addr); } + Long("alias") | Short('a') => { + let alias: String = parser.value()?.parse()?; + let id: Id = parser.value()?.parse()?; + + aliases.insert(alias, id); + } Long("help") => { - println!("usage: radicle-httpd [--listen ]"); + println!("usage: radicle-httpd [--listen ] [--alias ].."); process::exit(0); } _ => return Err(arg.unexpected()), } } Ok(httpd::Options { + aliases, listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8080).into()), }) } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 5ba342468..13d95b633 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -23,6 +23,7 @@ use radicle_crypto::test::signer::MockSigner; use crate::api::{auth, Context}; +pub const RID: &str = "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp"; pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; pub const PATCH_ID: &str = "6ec9a764a888576abc7e582dbf82a31e23a9789d"; From 44bf989d2e54ac86f6e9988a8a0162ed6a0bc374 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Fri, 10 Mar 2023 12:54:14 +0000 Subject: [PATCH 085/264] httpd: Add root handlers for `/api/v1` and `/api` Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 34 +++++----------------------- radicle-httpd/src/api/v1.rs | 45 ++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 9592e5fba..883426bec 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -4,7 +4,6 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use axum::extract::State; use axum::http::header::{AUTHORIZATION, CONTENT_TYPE}; use axum::http::Method; use axum::response::{IntoResponse, Json}; @@ -76,12 +75,8 @@ impl Context { } pub fn router(ctx: Context) -> Router { - let root_router = Router::new() - .route("/", get(root_handler)) - .with_state(ctx.clone()); - Router::new() - .merge(root_router) + .route("/", get(root_handler)) .merge(v1::router(ctx)) .layer( CorsLayer::new() @@ -98,32 +93,13 @@ pub fn router(ctx: Context) -> Router { ) } -async fn root_handler(State(ctx): State) -> impl IntoResponse { +async fn root_handler() -> impl IntoResponse { let response = json!({ - "message": "Welcome!", - "service": "radicle-httpd", - "version": format!("{}-{}", VERSION, env!("GIT_HEAD")), - "node": { "id": ctx.profile.public_key }, - "path": "/", + "path": "/api", "links": [ { - "href": "/v1/projects", - "rel": "projects", - "type": "GET" - }, - { - "href": "/v1/node", - "rel": "node", - "type": "GET" - }, - { - "href": "/v1/delegates/:did/projects", - "rel": "projects", - "type": "GET" - }, - { - "href": "/v1/stats", - "rel": "stats", + "href": "/v1", + "rel": "v1", "type": "GET" } ] diff --git a/radicle-httpd/src/api/v1.rs b/radicle-httpd/src/api/v1.rs index 97829cdf3..112be1e51 100644 --- a/radicle-httpd/src/api/v1.rs +++ b/radicle-httpd/src/api/v1.rs @@ -4,12 +4,21 @@ mod projects; mod sessions; mod stats; +use axum::extract::State; +use axum::response::{IntoResponse, Json}; +use axum::routing::get; use axum::Router; +use serde_json::json; -use crate::api::Context; +use crate::api::{Context, VERSION}; pub fn router(ctx: Context) -> Router { + let root_router = Router::new() + .route("/", get(root_handler)) + .with_state(ctx.clone()); + let routes = Router::new() + .merge(root_router) .merge(node::router(ctx.clone())) .merge(sessions::router(ctx.clone())) .merge(delegates::router(ctx.clone())) @@ -18,3 +27,37 @@ pub fn router(ctx: Context) -> Router { Router::new().nest("/v1", routes) } + +async fn root_handler(State(ctx): State) -> impl IntoResponse { + let response = json!({ + "message": "Welcome!", + "service": "radicle-httpd", + "version": format!("{}-{}", VERSION, env!("GIT_HEAD")), + "node": { "id": ctx.profile.public_key }, + "path": "/api/v1", + "links": [ + { + "href": "/projects", + "rel": "projects", + "type": "GET" + }, + { + "href": "/node", + "rel": "node", + "type": "GET" + }, + { + "href": "/delegates/:did/projects", + "rel": "projects", + "type": "GET" + }, + { + "href": "/stats", + "rel": "stats", + "type": "GET" + } + ] + }); + + Json(response) +} From 0cf170e6cde2433c7f2f9c0215fab0595a24c908 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 13 Mar 2023 13:55:12 +0100 Subject: [PATCH 086/264] Make Patch `state` field similar to Issue Since they are similar objects, they should have similar structure. This also makes it easier to add state metadata like for issues. --- radicle-httpd/src/api/v1/projects.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 0720b25d7..db28382af 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1351,7 +1351,7 @@ mod routes { }, "title": "A new `hello word`", "description": "change `hello world` in README to something else", - "state": "proposed", + "state": { "status": "proposed" }, "target": "delegates", "tags": [], "revisions": [ @@ -1382,7 +1382,7 @@ mod routes { }, "title": "A new `hello word`", "description": "change `hello world` in README to something else", - "state": "proposed", + "state": { "status": "proposed" }, "target": "delegates", "tags": [], "revisions": [ @@ -1452,7 +1452,7 @@ mod routes { }, "title": "Update README", "description": "Do some changes to README", - "state": "proposed", + "state": { "status": "proposed" }, "target": "delegates", "tags": [], "revisions": [ From f68f0af76fb526fbe9fd4a72700cc1977191b7b6 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 13 Mar 2023 12:55:46 +0000 Subject: [PATCH 087/264] httpd: Move `404` test from `git` to root router Signed-off-by: xphoniex --- radicle-httpd/src/git.rs | 12 ------- radicle-httpd/src/lib.rs | 73 +++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index 140428389..4be5946a3 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -216,18 +216,6 @@ mod routes { use crate::test::{self, get, RID}; - #[tokio::test] - async fn test_invalid_route_returns_404() { - let tmp = tempfile::tempdir().unwrap(); - let ctx = test::seed(tmp.path()); - let app = super::router(ctx.profile().to_owned(), HashMap::new()) - .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); - - let response = get(&app, "/aa/a").await; - - assert_eq!(response.status(), StatusCode::NOT_FOUND); - } - #[tokio::test] async fn test_info_request() { let tmp = tempfile::tempdir().unwrap(); diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 650bbf82b..888ecdb15 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -9,14 +9,16 @@ use std::str; use std::sync::Arc; use std::time::Duration; -use ::tracing::Span; use anyhow::Context as _; use axum::body::{Body, BoxBody, HttpBody}; use axum::http::{Request, Response}; use axum::middleware; use axum::Router; -use radicle::identity::Id; use tower_http::trace::TraceLayer; +use tracing::Span; + +use radicle::identity::Id; +use radicle::Profile; use tracing_extra::{tracing_middleware, ColoredStatus, Paint, RequestId, TracingInfo}; @@ -44,22 +46,17 @@ pub async fn run(options: Options) -> anyhow::Result<()> { tracing::info!("{}", str::from_utf8(&git_version)?.trim()); - let profile = Arc::new(radicle::Profile::load()?); + let listen = options.listen; + + tracing::info!("listening on http://{}", listen); + + let profile = Profile::load()?; let request_id = RequestId::new(); tracing::info!("using radicle home at {}", profile.home().display()); - let ctx = api::Context::new(profile.clone()); - let api_router = api::router(ctx); - let git_router = git::router(profile.clone(), options.aliases); - let raw_router = raw::router(profile); - - tracing::info!("listening on http://{}", options.listen); - - let app = Router::new() - .merge(git_router) - .nest("/api", api_router) - .nest("/raw", raw_router) + let app = + router(options, profile)? .layer(middleware::from_fn(tracing_middleware)) .layer( TraceLayer::new_for_http() @@ -95,8 +92,54 @@ pub async fn run(options: Options) -> anyhow::Result<()> { ) .into_make_service_with_connect_info::(); - axum::Server::bind(&options.listen) + axum::Server::bind(&listen) .serve(app) .await .map_err(anyhow::Error::from) } + +/// Create a router consisting of other sub-routers. +fn router(options: Options, profile: Profile) -> anyhow::Result { + let profile = Arc::new(profile); + let ctx = api::Context::new(profile.clone()); + + let api_router = api::router(ctx); + let git_router = git::router(profile.clone(), options.aliases); + let raw_router = raw::router(profile); + + let app = Router::new() + .merge(git_router) + .nest("/api", api_router) + .nest("/raw", raw_router); + + Ok(app) +} + +#[cfg(test)] +mod routes { + use std::collections::HashMap; + use std::net::SocketAddr; + + use axum::extract::connect_info::MockConnectInfo; + use axum::http::StatusCode; + + use crate::test::{self, get}; + + #[tokio::test] + async fn test_invalid_route_returns_404() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router( + super::Options { + aliases: HashMap::new(), + listen: SocketAddr::from(([0, 0, 0, 0], 8080)), + }, + test::profile(tmp.path(), [0xff; 32]), + ) + .unwrap() + .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); + + let response = get(&app, "/aa/a").await; + + assert_eq!(response.status(), StatusCode::NOT_FOUND); + } +} From 6ac94ca1d4c685fff4b1f1c390d0c9a7cdebb189 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 13 Mar 2023 12:22:48 +0000 Subject: [PATCH 088/264] httpd: Separate `Error` types of git route and `/raw` Signed-off-by: xphoniex --- radicle-httpd/src/api/error.rs | 2 +- radicle-httpd/src/error.rs | 56 ++++++++++++++++++++++------------ radicle-httpd/src/git.rs | 2 +- radicle-httpd/src/raw.rs | 2 +- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 91d593a1a..1edb48ff3 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -3,7 +3,7 @@ use axum::response::{IntoResponse, Response}; use axum::Json; use serde_json::json; -/// Errors relating to the HTTP backend. +/// Errors relating to the API backend. #[derive(Debug, thiserror::Error)] pub enum Error { /// The entity was not found. diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs index 29ada51d8..8bd18ce02 100644 --- a/radicle-httpd/src/error.rs +++ b/radicle-httpd/src/error.rs @@ -1,9 +1,9 @@ use axum::http; use axum::response::{IntoResponse, Response}; -/// Errors relating to the HTTP backend. +/// Errors relating to the Git backend. #[derive(Debug, thiserror::Error)] -pub enum Error { +pub enum GitError { /// The entity was not found. #[error("not found")] NotFound, @@ -24,10 +24,6 @@ pub enum Error { #[error("backend error")] Backend, - /// Id is not valid. - #[error("id is not valid")] - InvalidId, - /// HeaderName error. #[error(transparent)] InvalidHeaderName(#[from] axum::http::header::InvalidHeaderName), @@ -35,29 +31,49 @@ pub enum Error { /// HeaderValue error. #[error(transparent)] InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue), - - /// Surf error. - #[error(transparent)] - Surf(#[from] radicle_surf::Error), - - // Surf file error. - #[error(transparent)] - SurfFile(#[from] radicle_surf::fs::error::File), } -impl Error { +impl GitError { pub fn status(&self) -> http::StatusCode { match self { - Error::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE, - Error::InvalidId => http::StatusCode::NOT_FOUND, - Error::Id(_) => http::StatusCode::NOT_FOUND, - Error::NotFound => http::StatusCode::NOT_FOUND, + GitError::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE, + GitError::Id(_) => http::StatusCode::NOT_FOUND, + GitError::NotFound => http::StatusCode::NOT_FOUND, _ => http::StatusCode::INTERNAL_SERVER_ERROR, } } } -impl IntoResponse for Error { +impl IntoResponse for GitError { + fn into_response(self) -> Response { + tracing::error!("{}", self); + + self.status().into_response() + } +} + +/// Errors relating to the `/raw` route. +#[derive(Debug, thiserror::Error)] +pub enum RawError { + /// Surf error. + #[error(transparent)] + Surf(#[from] radicle_surf::Error), + + /// Surf file error. + #[error(transparent)] + SurfFile(#[from] radicle_surf::fs::error::File), +} + +impl RawError { + pub fn status(&self) -> http::StatusCode { + match self { + RawError::SurfFile(_) => http::StatusCode::NOT_FOUND, + _ => http::StatusCode::INTERNAL_SERVER_ERROR, + } + } +} + +impl IntoResponse for RawError { fn into_response(self) -> Response { tracing::error!("{}", self); diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index 4be5946a3..6b9a521d3 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -19,7 +19,7 @@ use hyper::body::Buf as _; use radicle::identity::Id; use radicle::profile::Profile; -use crate::error::Error; +use crate::error::GitError as Error; pub fn router(profile: Arc, aliases: HashMap) -> Router { Router::new() diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs index 9bc87a372..cde23eded 100644 --- a/radicle-httpd/src/raw.rs +++ b/radicle-httpd/src/raw.rs @@ -13,7 +13,7 @@ use radicle::storage::git::paths; use radicle_surf::{Oid, Repository}; use crate::axum_extra::Path; -use crate::error::Error; +use crate::error::RawError as Error; const MAX_BLOB_SIZE: usize = 4_194_304; From 1c0dd54bbf0747dceaaf3a17424c2315d9c918be Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 13 Mar 2023 12:52:02 +0000 Subject: [PATCH 089/264] httpd: Use `RID` in tests Signed-off-by: xphoniex --- radicle-httpd/src/api/v1/delegates.rs | 4 +- radicle-httpd/src/api/v1/projects.rs | 55 ++++++++------------------- radicle-httpd/src/raw.rs | 8 +--- 3 files changed, 20 insertions(+), 47 deletions(-) diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index f74935174..0d95c6de6 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -75,7 +75,7 @@ mod routes { use axum::http::StatusCode; use serde_json::json; - use crate::test::{self, get, HEAD}; + use crate::test::{self, get, HEAD, RID}; #[tokio::test] async fn test_delegates_projects() { @@ -106,7 +106,7 @@ mod routes { "open": 1, "closed": 0, }, - "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" + "id": RID } ]) ); diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index db28382af..d81b7562b 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -619,7 +619,8 @@ mod routes { use crate::test::{ self, get, patch, post, CONTRIBUTOR_ISSUE_ID, CONTRIBUTOR_PUB_KEY, CONTRIBUTOR_RID, HEAD, - HEAD_1, ISSUE_COMMENT_ID, ISSUE_DISCUSSION_ID, ISSUE_ID, PATCH_ID, SESSION_ID, TIMESTAMP, + HEAD_1, ISSUE_COMMENT_ID, ISSUE_DISCUSSION_ID, ISSUE_ID, PATCH_ID, RID, SESSION_ID, + TIMESTAMP, }; #[tokio::test] @@ -647,7 +648,7 @@ mod routes { "open": 1, "closed": 0, }, - "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" + "id": RID } ]) ); @@ -657,7 +658,7 @@ mod routes { async fn test_projects() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp").await; + let response = get(&app, format!("/projects/{RID}")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -677,7 +678,7 @@ mod routes { "open": 1, "closed": 0, }, - "id": "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp" + "id": RID }) ); } @@ -686,7 +687,7 @@ mod routes { async fn test_projects_commits_root() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/commits").await; + let response = get(&app, format!("/projects/{RID}/commits")).await; assert_eq!(response.status(), StatusCode::FOUND); assert_eq!( @@ -810,11 +811,7 @@ mod routes { async fn test_projects_commits() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get( - &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/commits/{HEAD}"), - ) - .await; + let response = get(&app, format!("/projects/{RID}/commits/{HEAD}")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -877,11 +874,7 @@ mod routes { async fn test_projects_tree() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get( - &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/tree/{HEAD}/"), - ) - .await; + let response = get(&app, format!("/projects/{RID}/tree/{HEAD}/")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -924,11 +917,7 @@ mod routes { ) ); - let response = get( - &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/tree/{HEAD}/dir1"), - ) - .await; + let response = get(&app, format!("/projects/{RID}/tree/{HEAD}/dir1")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -970,7 +959,7 @@ mod routes { async fn test_projects_remotes_root() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes").await; + let response = get(&app, format!("/projects/{RID}/remotes")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -993,7 +982,7 @@ mod routes { let app = super::router(test::seed(tmp.path())); let response = get( &app, - "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi", + format!("/projects/{RID}/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"), ) .await; @@ -1014,11 +1003,7 @@ mod routes { async fn test_projects_blob() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get( - &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/blob/{HEAD}/README"), - ) - .await; + let response = get(&app, format!("/projects/{RID}/blob/{HEAD}/README")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -1050,11 +1035,7 @@ mod routes { async fn test_projects_readme() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get( - &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/readme/{HEAD}"), - ) - .await; + let response = get(&app, format!("/projects/{RID}/readme/{HEAD}")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -1086,7 +1067,7 @@ mod routes { async fn test_projects_issues_root() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/issues").await; + let response = get(&app, format!("/projects/{RID}/issues")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -1338,7 +1319,7 @@ mod routes { async fn test_projects_patches() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get(&app, "/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches").await; + let response = get(&app, format!("/projects/{RID}/patches")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -1365,11 +1346,7 @@ mod routes { ]) ); - let response = get( - &app, - format!("/projects/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/patches/{PATCH_ID}"), - ) - .await; + let response = get(&app, format!("/projects/{RID}/patches/{PATCH_ID}")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs index cde23eded..909b0ef40 100644 --- a/radicle-httpd/src/raw.rs +++ b/radicle-httpd/src/raw.rs @@ -127,7 +127,7 @@ async fn file_handler( mod routes { use axum::http::StatusCode; - use crate::test::{self, get, HEAD}; + use crate::test::{self, get, HEAD, RID}; #[tokio::test] async fn test_file_handler() { @@ -135,11 +135,7 @@ mod routes { let ctx = test::seed(tmp.path()); let app = super::router(ctx.profile().to_owned()); - let response = get( - &app, - format!("/rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp/{HEAD}/dir1/README"), - ) - .await; + let response = get(&app, format!("/{RID}/{HEAD}/dir1/README")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!(response.body().await, "Hello World from dir1!\n"); From caa7078d627db5c10a01d9decc29ae6d29fa6a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Tue, 14 Mar 2023 17:17:20 +0100 Subject: [PATCH 090/264] httpd: Fix status code in commit history endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rūdolfs Ošiņš --- radicle-httpd/src/api/v1/projects.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index d81b7562b..ebe51cbca 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -138,12 +138,12 @@ async fn history_handler( per_page, } = qs; - let (sha, fallback_to_head) = match parent { - Some(commit) => (commit, false), + let sha = match parent { + Some(commit) => commit, None => { let info = ctx.project_info(project)?; - (info.head.to_string(), true) + info.head.to_string() } }; @@ -201,10 +201,6 @@ async fn history_handler( "stats": repo.stats()?, }); - if fallback_to_head { - return Ok::<_, Error>((StatusCode::FOUND, Json(response))); - } - Ok::<_, Error>((StatusCode::OK, Json(response))) } @@ -689,7 +685,7 @@ mod routes { let app = super::router(test::seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/commits")).await; - assert_eq!(response.status(), StatusCode::FOUND); + assert_eq!(response.status(), StatusCode::OK); assert_eq!( response.json().await, json!({ From 3f0141b087462d1f96a3a1432de5811c2dafba1a Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 17 Mar 2023 11:03:08 +0100 Subject: [PATCH 091/264] httpd: Add timestamp to revisions json Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/json.rs | 1 + radicle-httpd/src/api/v1/projects.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index c542f556f..a8ce37936 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -115,6 +115,7 @@ pub(crate) fn patch(id: PatchId, patch: Patch) -> Value { json!({ "id": id, "description": rev.description(), + "timestamp": rev.timestamp, "reviews": rev.reviews().collect::>(), }) }).collect::>(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index ebe51cbca..8ff92a580 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1335,6 +1335,7 @@ mod routes { { "id": PATCH_ID, "description": "", + "timestamp": 1671125284, "reviews": [], } ], @@ -1362,6 +1363,7 @@ mod routes { { "id": PATCH_ID, "description": "", + "timestamp": 1671125284, "reviews": [], } ], @@ -1432,6 +1434,7 @@ mod routes { { "id": CREATED_PATCH_ID, "description": "", + "timestamp": 1671125284, "reviews": [], } ], From da96a82fc1f74168c6bf5f361febbb97f1eee678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Fri, 17 Mar 2023 15:53:22 +0100 Subject: [PATCH 092/264] httpd: Use camelCase in query params MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's more ergonomic to consume in JS/TS. Signed-off-by: Rūdolfs Ošiņš --- radicle-httpd/src/api.rs | 2 +- radicle-httpd/src/api/v1/projects.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 883426bec..84488ec35 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -109,7 +109,7 @@ async fn root_handler() -> impl IntoResponse { } #[derive(Serialize, Deserialize, Clone)] -#[serde(rename_all = "kebab-case")] +#[serde(rename_all = "camelCase")] pub struct PaginationQuery { pub page: Option, pub per_page: Option, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 8ff92a580..130268323 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -114,7 +114,7 @@ async fn project_handler(State(ctx): State, Path(id): Path) -> impl } #[derive(Serialize, Deserialize, Clone)] -#[serde(rename_all = "kebab-case")] +#[serde(rename_all = "camelCase")] pub struct CommitsQueryString { pub parent: Option, pub since: Option, From fcea4aeb7d8b18738ebafb762af37e69911221a1 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 20 Mar 2023 17:18:44 +0100 Subject: [PATCH 093/264] httpd: Add more revision properties to patch For display purposes in the web client, we add with this commit the some needed properties to the revisions in the patch endpoint. Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/json.rs | 4 ++++ radicle-httpd/src/api/v1/projects.rs | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index a8ce37936..165dda206 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -115,6 +115,10 @@ pub(crate) fn patch(id: PatchId, patch: Patch) -> Value { json!({ "id": id, "description": rev.description(), + "base": rev.base, + "oid": rev.oid, + "merges": rev.merges().collect::>(), + "discussions": rev.discussion.comments().collect::(), "timestamp": rev.timestamp, "reviews": rev.reviews().collect::>(), }) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 130268323..e992617ff 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1335,6 +1335,10 @@ mod routes { { "id": PATCH_ID, "description": "", + "base": HEAD_1, + "oid": HEAD, + "merges": [], + "discussions": [], "timestamp": 1671125284, "reviews": [], } @@ -1363,6 +1367,10 @@ mod routes { { "id": PATCH_ID, "description": "", + "base": HEAD_1, + "oid": HEAD, + "merges": [], + "discussions": [], "timestamp": 1671125284, "reviews": [], } @@ -1374,7 +1382,7 @@ mod routes { #[tokio::test] async fn test_projects_create_patches() { - const CREATED_PATCH_ID: &str = "54505091ff3561466cfbe83e7e23c21cb1bb8a17"; + const CREATED_PATCH_ID: &str = "9170195973fb145b327b1f5cd728a6e46b3bb082"; let tmp = tempfile::tempdir().unwrap(); let ctx = test::contributor(tmp.path()); @@ -1385,8 +1393,8 @@ mod routes { let body = serde_json::to_vec(&json!({ "title": "Update README", "description": "Do some changes to README", - "target": HEAD, - "oid": HEAD_1, + "target": HEAD_1, + "oid": HEAD, "tags": [], })) .unwrap(); @@ -1434,6 +1442,10 @@ mod routes { { "id": CREATED_PATCH_ID, "description": "", + "base": HEAD_1, + "oid": HEAD, + "merges": [], + "discussions": [], "timestamp": 1671125284, "reviews": [], } From 7dabd58aa563c3f1a34d60f8dd4eb4cb389b0c1e Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 20 Mar 2023 21:25:48 +0100 Subject: [PATCH 094/264] httpd: Add commit diff endpoint Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/projects.rs | 471 +++++++++++++++++++++------ radicle-httpd/src/test.rs | 36 +- 2 files changed, 396 insertions(+), 111 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index e992617ff..f3ef7dfdc 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -33,6 +33,7 @@ pub fn router(ctx: Context) -> Router { .route("/projects/:project", get(project_handler)) .route("/projects/:project/commits", get(history_handler)) .route("/projects/:project/commits/:sha", get(commit_handler)) + .route("/projects/:project/diff/:base/:oid", get(diff_handler)) .route( "/projects/:project/activity", get( @@ -230,6 +231,35 @@ async fn commit_handler( Ok::<_, Error>(Json(response)) } +/// Get diff between two commits +/// `GET /projects/:project/diff/:base/:oid` +async fn diff_handler( + State(ctx): State, + Path((project, base, oid)): Path<(Id, Oid, Oid)>, +) -> impl IntoResponse { + let storage = &ctx.profile.storage; + let repo = Repository::open(paths::repository(storage, &project))?; + let base = repo.commit(base)?; + let commit = repo.commit(oid)?; + let diff = repo.diff(base.id, commit.id)?; + + let commits = repo + .history(commit.id)? + .take_while(|c| { + if let Ok(c) = c { + c.id != base.id + } else { + false + } + }) + .map(|r| r.map(|c| api::json::commit(&c))) + .collect::, _>>()?; + + let response = json!({ "diff": diff, "commits": commits }); + + Ok::<_, Error>(Json(response)) +} + /// Get project activity for the past year. /// `GET /projects/:project/activity` async fn activity_handler( @@ -615,8 +645,8 @@ mod routes { use crate::test::{ self, get, patch, post, CONTRIBUTOR_ISSUE_ID, CONTRIBUTOR_PUB_KEY, CONTRIBUTOR_RID, HEAD, - HEAD_1, ISSUE_COMMENT_ID, ISSUE_DISCUSSION_ID, ISSUE_ID, PATCH_ID, RID, SESSION_ID, - TIMESTAMP, + INITIAL_COMMIT, ISSUE_COMMENT_ID, ISSUE_DISCUSSION_ID, ISSUE_ID, PARENT, PATCH_ID, RID, + SESSION_ID, TIMESTAMP, }; #[tokio::test] @@ -702,11 +732,30 @@ mod routes { "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", - "time": 1673001014 + "time": 1673003014 }, }, "diff": { "added": [ + { + "path": "README", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Hello World!\n", + "lineNo": 1, + "type": "addition", + }, + ], + }, + ], + "eof": "noneMissing", + }, + }, { "path": "dir1/README", "diff": { @@ -727,14 +776,34 @@ mod routes { } } ], - "deleted": [], + "deleted": [ + { + "path": "CONTRIBUTING", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -1 +0,0 @@\n", + "lines": [ + { + "line": "Thank you very much!\n", + "lineNo": 1, + "type": "deletion", + }, + ], + }, + ], + "eof": "noneMissing", + }, + }, + ], "moved": [], "copied": [], "modified": [], "stats": { - "filesChanged": 1, - "insertions": 1, - "deletions": 0 + "filesChanged": 3, + "insertions": 2, + "deletions": 1 } }, "branches": [ @@ -743,17 +812,88 @@ mod routes { }, { "commit": { - "id": HEAD_1, + "id": PARENT, "author": { "name": "Alice Liddell", "email": "alice@radicle.xyz" }, + "summary": "Add contributing file", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + "time": 1673002014, + }, + }, + "diff": { + "added": [ + { + "path": "CONTRIBUTING", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Thank you very much!\n", + "lineNo": 1, + "type": "addition", + }, + ], + }, + ], + "eof": "noneMissing", + }, + }, + ], + "deleted": [ + { + "path": "README", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -1 +0,0 @@\n", + "lines": [ + { + "line": "Hello World!\n", + "lineNo": 1, + "type": "deletion", + }, + ], + }, + ], + "eof": "noneMissing", + }, + }, + ], + "moved": [], + "copied": [], + "modified": [], + "stats": { + "filesChanged": 2, + "insertions": 1, + "deletions": 1, + }, + }, + "branches": [ + "refs/heads/master", + ], + }, + { + "commit": { + "id": INITIAL_COMMIT, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + }, "summary": "Initial commit", "description": "", "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", - "time": 1673001014 + "time": 1673001014, }, }, "diff": { @@ -794,11 +934,10 @@ mod routes { } ], "stats": { - "commits": 2, + "commits": 3, "branches": 1, "contributors": 1 } - }) ); } @@ -824,11 +963,30 @@ mod routes { "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", - "time": 1673001014 + "time": 1673003014 }, }, "diff": { "added": [ + { + "path": "README", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Hello World!\n", + "lineNo": 1, + "type": "addition", + }, + ], + }, + ], + "eof": "noneMissing", + }, + }, { "path": "dir1/README", "diff": { @@ -849,14 +1007,34 @@ mod routes { } } ], - "deleted": [], + "deleted": [ + { + "path": "CONTRIBUTING", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -1 +0,0 @@\n", + "lines": [ + { + "line": "Thank you very much!\n", + "lineNo": 1, + "type": "deletion", + }, + ], + }, + ], + "eof": "noneMissing", + }, + }, + ], "moved": [], "copied": [], "modified": [], "stats": { - "filesChanged": 1, - "insertions": 1, - "deletions": 0 + "filesChanged": 3, + "insertions": 2, + "deletions": 1 } }, "branches": [ @@ -899,14 +1077,14 @@ mod routes { "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", - "time": 1673001014 + "time": 1673003014 }, }, "name": "", "path": "", "stats": { + "commits": 3, "branches": 1, - "commits": 2, "contributors": 1 } } @@ -937,14 +1115,14 @@ mod routes { "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", - "time": 1673001014 + "time": 1673003014 }, }, "name": "dir1", "path": "dir1", "stats": { "branches": 1, - "commits": 2, + "commits": 3, "contributors": 1 } }) @@ -1006,23 +1184,23 @@ mod routes { response.json().await, json!({ "binary": false, - "content": "Hello World!\n", - "lastCommit": { - "id": HEAD_1, - "author": { - "name": "Alice Liddell", - "email": "alice@radicle.xyz" - }, - "summary": "Initial commit", - "description": "", - "committer": { - "name": "Alice Liddell", - "email": "alice@radicle.xyz", - "time": 1673001014 - }, - }, "name": "README", - "path": "README" + "path": "README", + "lastCommit": { + "id": HEAD, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Add another folder", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + "time": 1673003014 + }, + }, + "content": "Hello World!\n", }) ); } @@ -1031,7 +1209,7 @@ mod routes { async fn test_projects_readme() { let tmp = tempfile::tempdir().unwrap(); let app = super::router(test::seed(tmp.path())); - let response = get(&app, format!("/projects/{RID}/readme/{HEAD}")).await; + let response = get(&app, format!("/projects/{RID}/readme/{INITIAL_COMMIT}")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( @@ -1040,18 +1218,18 @@ mod routes { "binary": false, "content": "Hello World!\n", "lastCommit": { - "id": HEAD_1, - "author": { - "name": "Alice Liddell", - "email": "alice@radicle.xyz" - }, - "summary": "Initial commit", - "description": "", - "committer": { - "name": "Alice Liddell", - "email": "alice@radicle.xyz", - "time": 1673001014 - }, + "id": INITIAL_COMMIT, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz" + }, + "summary": "Initial commit", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + "time": 1673001014 + }, }, "name": "README", "path": "README" @@ -1059,6 +1237,86 @@ mod routes { ); } + #[tokio::test] + async fn test_projects_diff() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router(test::seed(tmp.path())); + let response = get( + &app, + format!("/projects/{RID}/diff/{INITIAL_COMMIT}/{HEAD}"), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.json().await, + json!({ + "diff": { + "added": [ + { + "path": "dir1/README", + "diff": { + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Hello World from dir1!\n", + "lineNo": 1, + "type": "addition", + }, + ], + }, + ], + "eof": "noneMissing", + }, + }, + ], + "deleted": [], + "moved": [], + "copied": [], + "modified": [], + "stats": { + "filesChanged": 1, + "insertions": 1, + "deletions": 0, + }, + }, + "commits": [ + { + "id": HEAD, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + }, + "summary": "Add another folder", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + "time": 1673003014, + }, + }, + { + "id": PARENT, + "author": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + }, + "summary": "Add contributing file", + "description": "", + "committer": { + "name": "Alice Liddell", + "email": "alice@radicle.xyz", + "time": 1673002014, + } + } + ], + }) + ); + } + #[tokio::test] async fn test_projects_issues_root() { let tmp = tempfile::tempdir().unwrap(); @@ -1072,18 +1330,18 @@ mod routes { { "id": ISSUE_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "title": "Issue #1", "state": { - "status": "open" + "status": "open" }, "assignees": [], "discussion": [ { "id": ISSUE_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1140,22 +1398,22 @@ mod routes { json!({ "id": CREATED_ISSUE_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_PUB_KEY, }, "assignees": [], "title": "Issue #2", "state": { - "status": "open", + "status": "open", }, "discussion": [{ - "id": CREATED_ISSUE_ID, - "author": { - "id": CONTRIBUTOR_PUB_KEY, - }, - "body": "Change 'hello world' to 'hello everyone'", - "reactions": [], - "timestamp": TIMESTAMP, - "replyTo": null, + "id": CREATED_ISSUE_ID, + "author": { + "id": CONTRIBUTOR_PUB_KEY, + }, + "body": "Change 'hello world' to 'hello everyone'", + "reactions": [], + "timestamp": TIMESTAMP, + "replyTo": null, }], "tags": [ "bug", @@ -1203,18 +1461,18 @@ mod routes { json!({ "id": CONTRIBUTOR_ISSUE_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_PUB_KEY, }, "assignees": [], "title": "Issue #1", "state": { - "status": "open", + "status": "open", }, "discussion": [ { "id": ISSUE_DISCUSSION_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_PUB_KEY, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1224,7 +1482,7 @@ mod routes { { "id": "9685b141c2e939c3d60f8ca34f8c7bf01a609af1", "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_PUB_KEY, }, "body": "This is first-level comment", "reactions": [], @@ -1251,7 +1509,8 @@ mod routes { "type": "comment", "body": "This is a reply to the first comment", "replyTo": ISSUE_DISCUSSION_ID, - }})) + } + })) .unwrap(); let _ = get(&app, format!("/projects/{CONTRIBUTOR_RID}/issues")).await; @@ -1277,18 +1536,18 @@ mod routes { json!({ "id": CONTRIBUTOR_ISSUE_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_PUB_KEY, }, "assignees": [], "title": "Issue #1", "state": { - "status": "open", + "status": "open", }, "discussion": [ { "id": ISSUE_DISCUSSION_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_PUB_KEY, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1298,7 +1557,7 @@ mod routes { { "id": ISSUE_COMMENT_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_PUB_KEY, }, "body": "This is a reply to the first comment", "reactions": [], @@ -1324,7 +1583,7 @@ mod routes { { "id": PATCH_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "title": "A new `hello word`", "description": "change `hello world` in README to something else", @@ -1332,16 +1591,16 @@ mod routes { "target": "delegates", "tags": [], "revisions": [ - { - "id": PATCH_ID, - "description": "", - "base": HEAD_1, - "oid": HEAD, - "merges": [], - "discussions": [], - "timestamp": 1671125284, - "reviews": [], - } + { + "id": PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": 1671125284, + "reviews": [], + } ], } ]) @@ -1356,7 +1615,7 @@ mod routes { { "id": PATCH_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" }, "title": "A new `hello word`", "description": "change `hello world` in README to something else", @@ -1364,16 +1623,16 @@ mod routes { "target": "delegates", "tags": [], "revisions": [ - { - "id": PATCH_ID, - "description": "", - "base": HEAD_1, - "oid": HEAD, - "merges": [], - "discussions": [], - "timestamp": 1671125284, - "reviews": [], - } + { + "id": PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": 1671125284, + "reviews": [], + } ], } ) @@ -1382,7 +1641,7 @@ mod routes { #[tokio::test] async fn test_projects_create_patches() { - const CREATED_PATCH_ID: &str = "9170195973fb145b327b1f5cd728a6e46b3bb082"; + const CREATED_PATCH_ID: &str = "f69641cba6d7df2c22844d7f39225b5cda54d363"; let tmp = tempfile::tempdir().unwrap(); let ctx = test::contributor(tmp.path()); @@ -1393,7 +1652,7 @@ mod routes { let body = serde_json::to_vec(&json!({ "title": "Update README", "description": "Do some changes to README", - "target": HEAD_1, + "target": INITIAL_COMMIT, "oid": HEAD, "tags": [], })) @@ -1431,7 +1690,7 @@ mod routes { { "id": CREATED_PATCH_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY + "id": CONTRIBUTOR_PUB_KEY }, "title": "Update README", "description": "Do some changes to README", @@ -1439,16 +1698,16 @@ mod routes { "target": "delegates", "tags": [], "revisions": [ - { - "id": CREATED_PATCH_ID, - "description": "", - "base": HEAD_1, - "oid": HEAD, - "merges": [], - "discussions": [], - "timestamp": 1671125284, - "reviews": [], - } + { + "id": CREATED_PATCH_ID, + "description": "", + "base": INITIAL_COMMIT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": 1671125284, + "reviews": [], + } ], } ) diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 13d95b633..c144041e0 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -24,9 +24,10 @@ use radicle_crypto::test::signer::MockSigner; use crate::api::{auth, Context}; pub const RID: &str = "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp"; -pub const HEAD: &str = "1e978d19f251cd9821d9d9a76d1bd436bf0690d5"; -pub const HEAD_1: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; -pub const PATCH_ID: &str = "6ec9a764a888576abc7e582dbf82a31e23a9789d"; +pub const HEAD: &str = "e8c676b9e3b42308dc9d218b70faa5408f8e58ca"; +pub const PARENT: &str = "ee8d6a29304623a78ebfa5eeed5af674d0e58f83"; +pub const INITIAL_COMMIT: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; +pub const PATCH_ID: &str = "a6afa179631163169bf9fb0921229317ec3950af"; pub const ISSUE_ID: &str = "5ad77fa3f476beed9a26f49b2b3b844e61bef792"; pub const ISSUE_DISCUSSION_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; pub const ISSUE_COMMENT_ID: &str = "845218041bf9eb8155bfa4aaa8f0c91ce18e5c13"; @@ -100,6 +101,29 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G repo.checkout_tree(tree.as_object(), None).unwrap(); + let tree = radicle::git::write_tree( + Path::new("CONTRIBUTING"), + "Thank you very much!\n".as_bytes(), + &repo, + ) + .unwrap(); + let sig_time = git2::Time::new(1673002014, 0); + let sig = git2::Signature::new("Alice Liddell", "alice@radicle.xyz", &sig_time).unwrap(); + + let oid2 = repo + .commit( + Some("HEAD"), + &sig, + &sig, + "Add contributing file\n", + &tree, + &[&commit], + ) + .unwrap(); + let commit2 = repo.find_commit(oid2).unwrap(); + + repo.checkout_tree(tree.as_object(), None).unwrap(); + fs::create_dir(workdir.join("dir1")).unwrap(); fs::write( workdir.join("dir1").join("README"), @@ -115,13 +139,15 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G let oid = index.write_tree().unwrap(); let tree = repo.find_tree(oid).unwrap(); + let sig_time = git2::Time::new(1673003014, 0); + let sig = git2::Signature::new("Alice Liddell", "alice@radicle.xyz", &sig_time).unwrap(); repo.commit( Some("HEAD"), &sig, &sig, "Add another folder\n", &tree, - &[&commit], + &[&commit2], ) .unwrap(); @@ -149,7 +175,7 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G // eq. rad patch open let mut patches = Patches::open(&repo).unwrap(); let oid = radicle::git::Oid::from_str(HEAD).unwrap(); - let base = radicle::git::Oid::from_str(HEAD_1).unwrap(); + let base = radicle::git::Oid::from_str(PARENT).unwrap(); let _ = patches .create( "A new `hello word`", From 2aff899dc26d3197d53392172dc3b8756255b3f9 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 21 Mar 2023 21:18:46 +0100 Subject: [PATCH 095/264] httpd: Add patch update endpoint Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/projects.rs | 625 ++++++++++++++++++++++++--- radicle-httpd/src/test.rs | 10 +- 2 files changed, 578 insertions(+), 57 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index f3ef7dfdc..ff6a7e31e 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -12,8 +12,7 @@ use serde::{Deserialize, Serialize}; use serde_json::json; use tower_http::set_header::SetResponseHeaderLayer; -use radicle::cob::{issue, patch}; -use radicle::cob::{thread, ActorId, Tag}; +use radicle::cob::{issue, patch, thread, ActorId, Tag}; use radicle::identity::Id; use radicle::node::NodeId; use radicle::storage::git::paths; @@ -61,7 +60,10 @@ pub fn router(ctx: Context) -> Router { "/projects/:project/patches", post(patch_create_handler).get(patches_handler), ) - .route("/projects/:project/patches/:id", get(patch_handler)) + .route( + "/projects/:project/patches/:id", + get(patch_handler).patch(patch_update_handler), + ) .with_state(ctx) } @@ -595,6 +597,81 @@ async fn patch_create_handler( Json(json!({ "success": true, "id": patch.id.to_string() })), )) } +/// Update an patch. +/// `PATCH /projects/:project/patches/:id` +async fn patch_update_handler( + State(ctx): State, + AuthBearer(token): AuthBearer, + Path((project, patch_id)): Path<(Id, Oid)>, + Json(action): Json, +) -> impl IntoResponse { + ctx.sessions + .write() + .await + .get(&token) + .ok_or(Error::Auth("Unauthorized"))?; + let storage = &ctx.profile.storage; + let signer = ctx + .profile + .signer() + .map_err(|_| Error::Auth("Unauthorized"))?; + let repo = storage.repository(project)?; + let mut patches = patch::Patches::open(&repo)?; + let mut patch = patches.get_mut(&patch_id.into())?; + match action { + patch::Action::Edit { + title, + description, + target, + } => { + patch.edit(title, description, target, &signer)?; + } + patch::Action::Tag { add, remove } => { + patch.tag(add, remove, &signer)?; + } + patch::Action::Revision { + description, + base, + oid, + } => { + patch.update(description, base, oid, &signer)?; + } + patch::Action::Redact { .. } => { + todo!() + } + patch::Action::Review { + revision, + comment, + verdict, + inline, + } => { + patch.review(revision, verdict, comment, inline, &signer)?; + } + patch::Action::Merge { revision, commit } => { + patch.merge(revision, commit, &signer)?; + } + patch::Action::Thread { action, revision } => match action { + thread::Action::Comment { body, reply_to } => { + if let Some(reply_to) = reply_to { + patch.comment(revision, body, Some(reply_to), &signer)?; + } else { + patch.thread(revision, body, &signer)?; + } + } + thread::Action::Edit { .. } => { + todo!(); + } + thread::Action::Redact { .. } => { + todo!(); + } + thread::Action::React { .. } => { + todo!(); + } + }, + }; + + Ok::<_, Error>(Json(json!({ "success": true }))) +} /// Get project patches list. /// `GET /projects/:project/patches` @@ -643,16 +720,12 @@ mod routes { use pretty_assertions::assert_eq; use serde_json::json; - use crate::test::{ - self, get, patch, post, CONTRIBUTOR_ISSUE_ID, CONTRIBUTOR_PUB_KEY, CONTRIBUTOR_RID, HEAD, - INITIAL_COMMIT, ISSUE_COMMENT_ID, ISSUE_DISCUSSION_ID, ISSUE_ID, PARENT, PATCH_ID, RID, - SESSION_ID, TIMESTAMP, - }; + use crate::test::*; #[tokio::test] async fn test_projects_root() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, "/projects").await; assert_eq!(response.status(), StatusCode::OK); @@ -663,7 +736,7 @@ mod routes { "name": "hello-world", "description": "Rad repository for tests", "defaultBranch": "master", - "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], + "delegates": [DID], "head": HEAD, "patches": { "proposed": 1, @@ -683,7 +756,7 @@ mod routes { #[tokio::test] async fn test_projects() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}")).await; assert_eq!(response.status(), StatusCode::OK); @@ -693,7 +766,7 @@ mod routes { "name": "hello-world", "description": "Rad repository for tests", "defaultBranch": "master", - "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], + "delegates": [DID], "head": HEAD, "patches": { "proposed": 1, @@ -712,7 +785,7 @@ mod routes { #[tokio::test] async fn test_projects_commits_root() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/commits")).await; assert_eq!(response.status(), StatusCode::OK); @@ -945,7 +1018,7 @@ mod routes { #[tokio::test] async fn test_projects_commits() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/commits/{HEAD}")).await; assert_eq!(response.status(), StatusCode::OK); @@ -1047,7 +1120,7 @@ mod routes { #[tokio::test] async fn test_projects_tree() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/tree/{HEAD}/")).await; assert_eq!(response.status(), StatusCode::OK); @@ -1132,7 +1205,7 @@ mod routes { #[tokio::test] async fn test_projects_remotes_root() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/remotes")).await; assert_eq!(response.status(), StatusCode::OK); @@ -1153,7 +1226,7 @@ mod routes { #[tokio::test] async fn test_projects_remotes() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get( &app, format!("/projects/{RID}/remotes/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"), @@ -1176,7 +1249,7 @@ mod routes { #[tokio::test] async fn test_projects_blob() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/blob/{HEAD}/README")).await; assert_eq!(response.status(), StatusCode::OK); @@ -1208,7 +1281,7 @@ mod routes { #[tokio::test] async fn test_projects_readme() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/readme/{INITIAL_COMMIT}")).await; assert_eq!(response.status(), StatusCode::OK); @@ -1240,7 +1313,7 @@ mod routes { #[tokio::test] async fn test_projects_diff() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get( &app, format!("/projects/{RID}/diff/{INITIAL_COMMIT}/{HEAD}"), @@ -1320,7 +1393,7 @@ mod routes { #[tokio::test] async fn test_projects_issues_root() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); + let app = super::router(seed(tmp.path())); let response = get(&app, format!("/projects/{RID}/issues")).await; assert_eq!(response.status(), StatusCode::OK); @@ -1330,7 +1403,7 @@ mod routes { { "id": ISSUE_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": DID }, "title": "Issue #1", "state": { @@ -1341,7 +1414,7 @@ mod routes { { "id": ISSUE_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": DID }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1360,10 +1433,10 @@ mod routes { const CREATED_ISSUE_ID: &str = "b457364fbe2ef0eac69a835a087f60ee13ccb367"; let tmp = tempfile::tempdir().unwrap(); - let ctx = test::contributor(tmp.path()); + let ctx = contributor(tmp.path()); let app = super::router(ctx.to_owned()); - test::create_session(ctx).await; + create_session(ctx).await; let body = serde_json::to_vec(&json!({ "title": "Issue #2", @@ -1398,7 +1471,7 @@ mod routes { json!({ "id": CREATED_ISSUE_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "assignees": [], "title": "Issue #2", @@ -1408,7 +1481,7 @@ mod routes { "discussion": [{ "id": CREATED_ISSUE_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1425,10 +1498,10 @@ mod routes { #[tokio::test] async fn test_projects_issues_comment() { let tmp = tempfile::tempdir().unwrap(); - let ctx = test::contributor(tmp.path()); + let ctx = contributor(tmp.path()); let app = super::router(ctx.to_owned()); - test::create_session(ctx).await; + create_session(ctx).await; let body = serde_json::to_vec(&json!({ "type": "thread", @@ -1461,7 +1534,7 @@ mod routes { json!({ "id": CONTRIBUTOR_ISSUE_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "assignees": [], "title": "Issue #1", @@ -1472,7 +1545,7 @@ mod routes { { "id": ISSUE_DISCUSSION_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1482,7 +1555,7 @@ mod routes { { "id": "9685b141c2e939c3d60f8ca34f8c7bf01a609af1", "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "body": "This is first-level comment", "reactions": [], @@ -1498,10 +1571,10 @@ mod routes { #[tokio::test] async fn test_projects_issues_reply() { let tmp = tempfile::tempdir().unwrap(); - let ctx = test::contributor(tmp.path()); + let ctx = contributor(tmp.path()); let app = super::router(ctx.to_owned()); - test::create_session(ctx).await; + create_session(ctx).await; let body = serde_json::to_vec(&json!({ "type": "thread", @@ -1536,7 +1609,7 @@ mod routes { json!({ "id": CONTRIBUTOR_ISSUE_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "assignees": [], "title": "Issue #1", @@ -1547,7 +1620,7 @@ mod routes { { "id": ISSUE_DISCUSSION_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "body": "Change 'hello world' to 'hello everyone'", "reactions": [], @@ -1557,7 +1630,7 @@ mod routes { { "id": ISSUE_COMMENT_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY, + "id": CONTRIBUTOR_DID, }, "body": "This is a reply to the first comment", "reactions": [], @@ -1573,32 +1646,33 @@ mod routes { #[tokio::test] async fn test_projects_patches() { let tmp = tempfile::tempdir().unwrap(); - let app = super::router(test::seed(tmp.path())); - let response = get(&app, format!("/projects/{RID}/patches")).await; + let ctx = contributor(tmp.path()); + let app = super::router(ctx.to_owned()); + let response = get(&app, format!("/projects/{CONTRIBUTOR_RID}/patches")).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( response.json().await, json!([ { - "id": PATCH_ID, + "id": CONTRIBUTOR_PATCH_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": CONTRIBUTOR_DID }, - "title": "A new `hello word`", + "title": "A new `hello world`", "description": "change `hello world` in README to something else", "state": { "status": "proposed" }, "target": "delegates", "tags": [], "revisions": [ { - "id": PATCH_ID, + "id": CONTRIBUTOR_PATCH_ID, "description": "", "base": PARENT, "oid": HEAD, "merges": [], "discussions": [], - "timestamp": 1671125284, + "timestamp": TIMESTAMP, "reviews": [], } ], @@ -1606,31 +1680,35 @@ mod routes { ]) ); - let response = get(&app, format!("/projects/{RID}/patches/{PATCH_ID}")).await; + let response = get( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + ) + .await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( response.json().await, json!( { - "id": PATCH_ID, + "id": CONTRIBUTOR_PATCH_ID, "author": { - "id": "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi" + "id": CONTRIBUTOR_DID }, - "title": "A new `hello word`", + "title": "A new `hello world`", "description": "change `hello world` in README to something else", "state": { "status": "proposed" }, "target": "delegates", "tags": [], "revisions": [ { - "id": PATCH_ID, + "id": CONTRIBUTOR_PATCH_ID, "description": "", "base": PARENT, "oid": HEAD, "merges": [], "discussions": [], - "timestamp": 1671125284, + "timestamp": TIMESTAMP, "reviews": [], } ], @@ -1644,10 +1722,10 @@ mod routes { const CREATED_PATCH_ID: &str = "f69641cba6d7df2c22844d7f39225b5cda54d363"; let tmp = tempfile::tempdir().unwrap(); - let ctx = test::contributor(tmp.path()); + let ctx = contributor(tmp.path()); let app = super::router(ctx.to_owned()); - test::create_session(ctx).await; + create_session(ctx).await; let body = serde_json::to_vec(&json!({ "title": "Update README", @@ -1690,7 +1768,7 @@ mod routes { { "id": CREATED_PATCH_ID, "author": { - "id": CONTRIBUTOR_PUB_KEY + "id": CONTRIBUTOR_DID }, "title": "Update README", "description": "Do some changes to README", @@ -1705,7 +1783,7 @@ mod routes { "oid": HEAD, "merges": [], "discussions": [], - "timestamp": 1671125284, + "timestamp": TIMESTAMP, "reviews": [], } ], @@ -1713,4 +1791,443 @@ mod routes { ) ); } + + #[tokio::test] + async fn test_projects_patches_tag() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = contributor(tmp.path()); + let app = super::router(ctx.to_owned()); + create_session(ctx).await; + let body = serde_json::to_vec(&json!({ + "type": "tag", + "add": ["bug","design"], + "remove": [] + })) + .unwrap(); + let response = patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + Some(Body::from(body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let response = get( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": CONTRIBUTOR_PATCH_ID, + "author": { + "id": CONTRIBUTOR_DID, + }, + "title": "A new `hello world`", + "description": "change `hello world` in README to something else", + "state": { "status": "proposed" }, + "target": "delegates", + "tags": [ + "bug", + "design" + ], + "revisions": [ + { + "id": CONTRIBUTOR_PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": TIMESTAMP, + "reviews": [], + }, + ], + }) + ); + } + + #[tokio::test] + async fn test_projects_patches_revisions() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = contributor(tmp.path()); + let app = super::router(ctx.to_owned()); + create_session(ctx).await; + let body = serde_json::to_vec(&json!({ + "type": "revision", + "description": "This is a new revision", + "base": PARENT, + "oid": HEAD, + })) + .unwrap(); + let response = patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + Some(Body::from(body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let response = get( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": CONTRIBUTOR_PATCH_ID, + "author": { + "id": CONTRIBUTOR_DID, + }, + "title": "A new `hello world`", + "description": "change `hello world` in README to something else", + "state": { "status": "proposed" }, + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": CONTRIBUTOR_PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": TIMESTAMP, + "reviews": [], + }, + { + "id": "bdc0364b51a3346653a795b73332eb792283be37", + "description": "This is a new revision", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": TIMESTAMP, + "reviews": [], + } + ], + }) + ); + } + + #[tokio::test] + async fn test_projects_patches_edit() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = contributor(tmp.path()); + let app = super::router(ctx.to_owned()); + create_session(ctx).await; + let body = serde_json::to_vec(&json!({ + "type": "edit", + "title": "This is a updated title", + "description": "Let's write some description", + "target": "delegates", + })) + .unwrap(); + let response = patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + Some(Body::from(body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let response = get( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": CONTRIBUTOR_PATCH_ID, + "author": { + "id": CONTRIBUTOR_DID, + }, + "title": "This is a updated title", + "description": "Let's write some description", + "state": { "status": "proposed" }, + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": CONTRIBUTOR_PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": TIMESTAMP, + "reviews": [], + }, + ], + }) + ); + } + + #[tokio::test] + async fn test_projects_patches_discussions() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = contributor(tmp.path()); + let app = super::router(ctx.to_owned()); + create_session(ctx).await; + let thread_body = serde_json::to_vec(&json!({ + "type": "thread", + "revision": CONTRIBUTOR_PATCH_ID, + "action": { + "type": "comment", + "body": "This is a root level comment" + } + })) + .unwrap(); + let response = patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + Some(Body::from(thread_body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let reply_body = serde_json::to_vec(&json!({ + "type": "thread", + "revision": CONTRIBUTOR_PATCH_ID, + "action": { + "type": "comment", + "body": "This is a root level comment", + "replyTo": CONTRIBUTOR_COMMENT_1, + } + })) + .unwrap(); + let response = patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + Some(Body::from(reply_body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let response = get( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": CONTRIBUTOR_PATCH_ID, + "author": { + "id": CONTRIBUTOR_DID, + }, + "title": "A new `hello world`", + "description": "change `hello world` in README to something else", + "state": { "status": "proposed" }, + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": CONTRIBUTOR_PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [ + { + "id": CONTRIBUTOR_COMMENT_1, + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "This is a root level comment", + "reactions": [], + "timestamp": TIMESTAMP, + "replyTo": null, + }, + { + "id": CONTRIBUTOR_COMMENT_2, + "author": { + "id": CONTRIBUTOR_DID, + }, + "body": "This is a root level comment", + "reactions": [], + "timestamp": TIMESTAMP, + "replyTo": CONTRIBUTOR_COMMENT_1, + }, + ], + "timestamp": TIMESTAMP, + "reviews": [], + }, + ], + }) + ); + } + + #[tokio::test] + async fn test_projects_patches_reviews() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = contributor(tmp.path()); + let app = super::router(ctx.to_owned()); + create_session(ctx).await; + let thread_body = serde_json::to_vec(&json!({ + "type": "review", + "revision": CONTRIBUTOR_PATCH_ID, + "comment": "A small review", + "verdict": "accept", + "inline": [ + { + "location": { + "blob": HEAD, + "commit": HEAD, + "lines": { + "start": 1, + "end": 3, + }, + }, + "comment": "This is a comment on line 1", + "timestamp": TIMESTAMP, + } + ] + })) + .unwrap(); + let response = patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + Some(Body::from(thread_body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let response = get( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": CONTRIBUTOR_PATCH_ID, + "author": { + "id": CONTRIBUTOR_DID, + }, + "title": "A new `hello world`", + "description": "change `hello world` in README to something else", + "state": { "status": "proposed" }, + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": CONTRIBUTOR_PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [], + "discussions": [], + "timestamp": TIMESTAMP, + "reviews": [ + [ + CONTRIBUTOR_NID, + { + "verdict": "accept", + "comment": "A small review", + "inline": [ + { + "location": { + "blob": HEAD, + "commit": HEAD, + "lines": { + "start": 1, + "end": 3, + }, + }, + "comment": "This is a comment on line 1", + "timestamp": TIMESTAMP, + } + ], + "timestamp": TIMESTAMP, + }, + ], + ], + }, + ], + }) + ); + } + + #[tokio::test] + async fn test_projects_patches_merges() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = contributor(tmp.path()); + let app = super::router(ctx.to_owned()); + create_session(ctx).await; + let thread_body = serde_json::to_vec(&json!({ + "type": "merge", + "revision": CONTRIBUTOR_PATCH_ID, + "commit": PARENT, + })) + .unwrap(); + let response = patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + Some(Body::from(thread_body)), + Some(SESSION_ID.to_string()), + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let response = get( + &app, + format!("/projects/{CONTRIBUTOR_RID}/patches/{CONTRIBUTOR_PATCH_ID}"), + ) + .await; + + assert_eq!( + response.json().await, + json!({ + "id": CONTRIBUTOR_PATCH_ID, + "author": { + "id": CONTRIBUTOR_DID, + }, + "title": "A new `hello world`", + "description": "change `hello world` in README to something else", + "state": { "status": "proposed" }, + "target": "delegates", + "tags": [], + "revisions": [ + { + "id": CONTRIBUTOR_PATCH_ID, + "description": "", + "base": PARENT, + "oid": HEAD, + "merges": [ + { + "node": CONTRIBUTOR_NID, + "commit": PARENT, + "timestamp": TIMESTAMP, + }, + ], + "discussions": [], + "timestamp": TIMESTAMP, + "reviews": [], + }, + ], + }) + ); + } } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index c144041e0..9bf1940e3 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -27,15 +27,19 @@ pub const RID: &str = "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp"; pub const HEAD: &str = "e8c676b9e3b42308dc9d218b70faa5408f8e58ca"; pub const PARENT: &str = "ee8d6a29304623a78ebfa5eeed5af674d0e58f83"; pub const INITIAL_COMMIT: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7"; -pub const PATCH_ID: &str = "a6afa179631163169bf9fb0921229317ec3950af"; +pub const DID: &str = "did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"; pub const ISSUE_ID: &str = "5ad77fa3f476beed9a26f49b2b3b844e61bef792"; pub const ISSUE_DISCUSSION_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; pub const ISSUE_COMMENT_ID: &str = "845218041bf9eb8155bfa4aaa8f0c91ce18e5c13"; pub const SESSION_ID: &str = "u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"; pub const TIMESTAMP: u64 = 1671125284; pub const CONTRIBUTOR_RID: &str = "rad:z4XaCmN3jLSeiMvW15YTDpNbDHFhG"; -pub const CONTRIBUTOR_PUB_KEY: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; +pub const CONTRIBUTOR_DID: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; +pub const CONTRIBUTOR_NID: &str = "z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; pub const CONTRIBUTOR_ISSUE_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; +pub const CONTRIBUTOR_PATCH_ID: &str = "4b7ea91da1f551bc5ea5d2057125d7ba42d451cb"; +pub const CONTRIBUTOR_COMMENT_1: &str = "8f6e72c20da0e6ee2af004f059ced23d409a285a"; +pub const CONTRIBUTOR_COMMENT_2: &str = "c88ce78b17a705f389e72ec5c80da7360a675e51"; const PASSWORD: &str = "radicle"; @@ -178,7 +182,7 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G let base = radicle::git::Oid::from_str(PARENT).unwrap(); let _ = patches .create( - "A new `hello word`", + "A new `hello world`", "change `hello world` in README to something else", MergeTarget::Delegates, base, From 27ab41bf3ac2c4d37d0c8de0446a0b593d9eb914 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 22 Mar 2023 20:31:48 +0100 Subject: [PATCH 096/264] httpd: Add `CorsLayer` to `/raw` endpoint Signed-off-by: Sebastian Martinez --- radicle-httpd/src/raw.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs index 909b0ef40..ddb9518eb 100644 --- a/radicle-httpd/src/raw.rs +++ b/radicle-httpd/src/raw.rs @@ -1,11 +1,13 @@ use std::sync::Arc; +use std::time::Duration; use axum::extract::State; -use axum::http::{header, StatusCode}; +use axum::http::{header, Method, StatusCode}; use axum::response::IntoResponse; use axum::routing::get; use axum::Router; use hyper::HeaderMap; +use tower_http::cors; use radicle::prelude::Id; use radicle::profile::Profile; @@ -93,6 +95,13 @@ pub fn router(profile: Arc) -> Router { Router::new() .route("/:project/:sha/*path", get(file_handler)) .with_state(profile) + .layer( + cors::CorsLayer::new() + .max_age(Duration::from_secs(86400)) + .allow_origin(cors::Any) + .allow_methods([Method::GET]) + .allow_headers([header::CONTENT_TYPE]), + ) } async fn file_handler( From e4020eaea1364c23e8078c5ea7e3a5b31bb15f25 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 22 Mar 2023 17:40:35 +0100 Subject: [PATCH 097/264] httpd: Add `path` property to `CodeLocation` For the web client to be able to show file we need to provide the web client with the file path. Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/projects.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index ff6a7e31e..2fcc7ad38 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -2089,7 +2089,8 @@ mod routes { "inline": [ { "location": { - "blob": HEAD, + "blob": "82eb77880c693655bce074e3dbbd9fa711dc018b", + "path": "./README.md", "commit": HEAD, "lines": { "start": 1, @@ -2148,7 +2149,8 @@ mod routes { "inline": [ { "location": { - "blob": HEAD, + "blob": "82eb77880c693655bce074e3dbbd9fa711dc018b", + "path": "./README.md", "commit": HEAD, "lines": { "start": 1, From 5d7a2f19d668a221737fbd2055fd648a4d756d97 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 24 Mar 2023 11:51:32 +0100 Subject: [PATCH 098/264] 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 165dda206..d28d8450a 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 2fcc7ad38..798bbf726 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, From 8826ec4f9cafdaf190dae19cc642f5e5c4b9ed51 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 24 Mar 2023 10:17:59 +0100 Subject: [PATCH 099/264] httpd: Add reactions to issue and patch json Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/json.rs | 48 +++++++++++++--------------- radicle-httpd/src/api/v1/projects.rs | 27 ++++++++++++++-- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index d28d8450a..91482622f 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -8,8 +8,9 @@ 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::{ActorId, Author, Timestamp}; +use radicle::cob::thread; +use radicle::cob::thread::{CommentId, Thread}; +use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::git::RefString; use radicle::storage::{git, refs, ReadRepository}; use radicle_surf::blob::Blob; @@ -98,7 +99,10 @@ pub(crate) fn issue(id: IssueId, issue: Issue) -> Value { "title": issue.title(), "state": issue.state(), "assignees": issue.assigned().collect::>(), - "discussion": issue.comments().collect::(), + "discussion": issue + .comments() + .map(|(id, comment)| Comment::new(id, comment, issue.thread())) + .collect::>(), "tags": issue.tags().collect::>(), }) } @@ -121,7 +125,10 @@ pub(crate) fn patch(id: PatchId, patch: Patch, repo: &git::Repository) -> Value "oid": rev.oid, "refs": get_refs(repo, patch.author().id(), &rev.oid).unwrap_or(vec![]), "merges": rev.merges().collect::>(), - "discussions": rev.discussion.comments().collect::(), + "discussions": rev.discussion + .comments() + .map(|(id, comment)| Comment::new(id, comment, &rev.discussion)) + .collect::>(), "timestamp": rev.timestamp, "reviews": rev.reviews().collect::>(), }) @@ -160,33 +167,24 @@ fn get_refs( #[derive(Serialize)] #[serde(rename_all = "camelCase")] -struct Comment { +struct Comment<'a> { id: CommentId, author: Author, - body: String, - reactions: [String; 0], + body: &'a str, + reactions: Vec<(&'a ActorId, &'a Reaction)>, timestamp: Timestamp, reply_to: Option, } -#[derive(Serialize)] -struct Comments(Vec); - -impl<'a> FromIterator<(&'a CommentId, &'a thread::Comment)> for Comments { - fn from_iter>(iter: I) -> Self { - let mut comments = Vec::new(); - - for (id, comment) in iter { - comments.push(Comment { - id: id.to_owned(), - author: comment.author().into(), - body: comment.body().to_owned(), - reactions: [], - timestamp: comment.timestamp(), - reply_to: comment.reply_to(), - }); +impl<'a> Comment<'a> { + fn new(id: &'a CommentId, comment: &'a thread::Comment, thread: &'a Thread) -> Self { + Self { + id: *id, + author: Author::new(comment.author()), + body: comment.body(), + reactions: thread.reactions(id).collect::>(), + timestamp: comment.timestamp(), + reply_to: comment.reply_to(), } - - Comments(comments) } } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 798bbf726..5c9f0d904 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1523,6 +1523,24 @@ mod routes { assert_eq!(response.status(), StatusCode::OK); assert_eq!(response.json().await, json!({ "success": true })); + let body = serde_json::to_vec(&json!({ + "type": "thread", + "action": { + "type": "react", + "to": "9685b141c2e939c3d60f8ca34f8c7bf01a609af1", + "reaction": "🚀", + "active": true, + } + })) + .unwrap(); + patch( + &app, + format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"), + Some(Body::from(body)), + Some(SESSION_ID.to_string()), + ) + .await; + let response = get( &app, format!("/projects/{CONTRIBUTOR_RID}/issues/{CONTRIBUTOR_ISSUE_ID}"), @@ -1536,11 +1554,11 @@ mod routes { "author": { "id": CONTRIBUTOR_DID, }, - "assignees": [], "title": "Issue #1", "state": { "status": "open", }, + "assignees": [], "discussion": [ { "id": ISSUE_DISCUSSION_ID, @@ -1558,7 +1576,12 @@ mod routes { "id": CONTRIBUTOR_DID, }, "body": "This is first-level comment", - "reactions": [], + "reactions": [ + [ + "z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8", + "🚀", + ], + ], "timestamp": TIMESTAMP, "replyTo": null, }, From c809bd5b420eab4b31d5e28a3a5e71e22c70bf75 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 30 Mar 2023 10:39:09 +0200 Subject: [PATCH 100/264] cob: Add `patch::Action::EditRevision` To be able to change the description of a patch revision, without the need to create a new revision. Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/projects.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 5c9f0d904..1cd11fc38 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -626,6 +626,12 @@ async fn patch_update_handler( } => { patch.edit(title, description, target, &signer)?; } + patch::Action::EditRevision { + revision, + description, + } => { + patch.edit_revision(revision, description, &signer)?; + } patch::Action::Tag { add, remove } => { patch.tag(add, remove, &signer)?; } From 609bf226e8bd5685dc0ee0243d89ef20bd084e00 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 30 Mar 2023 10:43:03 +0200 Subject: [PATCH 101/264] httpd: Add `patch::Action::Lifecycle` Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/projects.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 1cd11fc38..4aa6f8a24 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -645,6 +645,9 @@ async fn patch_update_handler( patch::Action::Redact { .. } => { todo!() } + patch::Action::Lifecycle { state } => { + patch.lifecycle(state, &signer)?; + } patch::Action::Review { revision, comment, From f7fc778c3b26861905ab00c2367a99a7919f31bf Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 30 Mar 2023 10:43:35 +0200 Subject: [PATCH 102/264] Rename `patch::State::Proposed to `patch::State::Open` Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/delegates.rs | 2 +- radicle-httpd/src/api/v1/projects.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 0d95c6de6..589cbd512 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -98,7 +98,7 @@ mod routes { "delegates": ["did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi"], "head": HEAD, "patches": { - "proposed": 1, + "open": 1, "draft": 0, "archived": 0, }, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 4aa6f8a24..ea8969ebc 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -748,7 +748,7 @@ mod routes { "delegates": [DID], "head": HEAD, "patches": { - "proposed": 1, + "open": 1, "draft": 0, "archived": 0, }, @@ -778,7 +778,7 @@ mod routes { "delegates": [DID], "head": HEAD, "patches": { - "proposed": 1, + "open": 1, "draft": 0, "archived": 0, }, @@ -1693,7 +1693,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ @@ -1732,7 +1732,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ @@ -1810,7 +1810,7 @@ mod routes { }, "title": "Update README", "description": "Do some changes to README", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ @@ -1870,7 +1870,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [ "bug", @@ -1933,7 +1933,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ @@ -2006,7 +2006,7 @@ mod routes { }, "title": "This is a updated title", "description": "Let's write some description", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ @@ -2088,7 +2088,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ @@ -2184,7 +2184,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ @@ -2267,7 +2267,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "proposed" }, + "state": { "status": "open" }, "target": "delegates", "tags": [], "revisions": [ From fff45817bcd9c1b3cc3d8f0cc00ca963e678b98f Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 30 Mar 2023 17:45:00 +0200 Subject: [PATCH 103/264] Reduce pub accessors in patch and issue cob structs Replaces pub accessors to struct fields with methods Also creates some methods to compute necessary information Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/json.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 91482622f..996db6ca7 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -121,15 +121,14 @@ pub(crate) fn patch(id: PatchId, patch: Patch, repo: &git::Repository) -> Value json!({ "id": id, "description": rev.description(), - "base": rev.base, - "oid": rev.oid, - "refs": get_refs(repo, patch.author().id(), &rev.oid).unwrap_or(vec![]), + "base": rev.base(), + "oid": rev.head(), + "refs": get_refs(repo, patch.author().id(), &rev.head()).unwrap_or(vec![]), "merges": rev.merges().collect::>(), - "discussions": rev.discussion - .comments() - .map(|(id, comment)| Comment::new(id, comment, &rev.discussion)) + "discussions": rev.discussion().comments() + .map(|(id, comment)| Comment::new(id, comment, rev.discussion())) .collect::>(), - "timestamp": rev.timestamp, + "timestamp": rev.timestamp(), "reviews": rev.reviews().collect::>(), }) }).collect::>(), From 48105b8b359b66e2daa53bba2e98adaf7ee446f1 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 3 Apr 2023 13:33:43 +0200 Subject: [PATCH 104/264] Set patch status to `merged` automatically When a merge is applied to a patch, we check whether the threshold of delegates has been reached, at which point we set the patch status to `merged`. This requires threading the identity document oid through the "apply" process. --- radicle-httpd/src/api/v1/delegates.rs | 1 + radicle-httpd/src/api/v1/projects.rs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 589cbd512..603eb287d 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -101,6 +101,7 @@ mod routes { "open": 1, "draft": 0, "archived": 0, + "merged": 0, }, "issues": { "open": 1, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index ea8969ebc..5d8903fe4 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -751,6 +751,7 @@ mod routes { "open": 1, "draft": 0, "archived": 0, + "merged": 0, }, "issues": { "open": 1, @@ -781,6 +782,7 @@ mod routes { "open": 1, "draft": 0, "archived": 0, + "merged": 0, }, "issues": { "open": 1, @@ -2267,7 +2269,7 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "open" }, + "state": { "status": "merged" }, "target": "delegates", "tags": [], "revisions": [ From e713e99e57fe8ae500a96921e1cafe6961af5ab8 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 13 Apr 2023 09:34:05 +0200 Subject: [PATCH 105/264] Update `fastrand` dependency to 1.9.0 --- radicle-httpd/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 6f05ec6ac..bf9a73b84 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -20,7 +20,7 @@ axum = { version = "0.6.7", default-features = false, features = ["headers", "js axum-auth = { version= "0.4.0", default-features = false, features = ["auth-bearer"] } axum-server = { version = "0.4.4", default-features = false } chrono = { version = "0.4.22" } -fastrand = { version = "1.7.0" } +fastrand = { version = "1.9.0" } flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } From 7edf4543d5b78add972f011b604550f900c10be8 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 12 Apr 2023 13:36:23 +0200 Subject: [PATCH 106/264] Include dependencies for certain COB actions We ensure that for example, the given commits are fetched before the revision or merge actions in a patch. --- radicle-httpd/src/api/v1/projects.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 5d8903fe4..9e9c2913f 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1953,7 +1953,7 @@ mod routes { "reviews": [], }, { - "id": "bdc0364b51a3346653a795b73332eb792283be37", + "id": "69cfd3fc589782afbcd47027c1172f8a55dd74c7", "description": "This is a new revision", "base": PARENT, "oid": HEAD, From 9166a4e349f25f1892c7354979ac212eab1817f5 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 12 Apr 2023 14:01:05 +0200 Subject: [PATCH 107/264] cob: Deduplicate change parents --- radicle-httpd/src/api/v1/projects.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 9e9c2913f..2efe091d8 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1953,7 +1953,7 @@ mod routes { "reviews": [], }, { - "id": "69cfd3fc589782afbcd47027c1172f8a55dd74c7", + "id": "b1a8c5b3e1686891808fcac0d795d0bad1e69709", "description": "This is a new revision", "base": PARENT, "oid": HEAD, From c70252429c87a555b96b0cd9ec9cc12f0e455c6f Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 20 Apr 2023 18:56:56 +0200 Subject: [PATCH 108/264] httpd: Add cob state to listing endpoints This commit adds the cob state of either patches or issues to the corresponding endpoint. So we can either query a cob listing with the required state or expect the default state. And once filtered by cob state we paginate. Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api.rs | 54 +++++++++++++++++++++++++--- radicle-httpd/src/api/v1/projects.rs | 37 +++++++++++++++---- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 84488ec35..a370a150e 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -14,8 +14,8 @@ use serde_json::json; use tokio::sync::RwLock; use tower_http::cors::{self, CorsLayer}; -use radicle::cob::issue::Issues; -use radicle::cob::patch::Patches; +use radicle::cob::issue; +use radicle::cob::patch; use radicle::identity::Id; use radicle::storage::{ReadRepository, ReadStorage}; use radicle::Profile; @@ -50,8 +50,8 @@ impl Context { let doc = repo.identity_doc()?.1.verified()?; let payload = doc.project()?; let delegates = doc.delegates; - let issues = Issues::open(&repo)?.counts()?; - let patches = Patches::open(&repo)?.counts()?; + let issues = issue::Issues::open(&repo)?.counts()?; + let patches = patch::Patches::open(&repo)?.counts()?; Ok(project::Info { payload, @@ -115,6 +115,52 @@ pub struct PaginationQuery { pub per_page: Option, } +#[derive(Serialize, Deserialize, Clone)] +#[serde(rename_all = "camelCase")] +pub struct CobsQuery { + pub page: Option, + pub per_page: Option, + pub state: Option, +} + +#[derive(Default, Serialize, Deserialize, Clone)] +#[serde(rename_all = "camelCase")] +pub enum IssueState { + Closed, + #[default] + Open, +} + +impl IssueState { + pub fn matches(&self, issue: &issue::State) -> bool { + match self { + Self::Open => matches!(issue, issue::State::Open), + Self::Closed => matches!(issue, issue::State::Closed { .. }), + } + } +} + +#[derive(Default, Serialize, Deserialize, Clone)] +#[serde(rename_all = "camelCase")] +pub enum PatchState { + #[default] + Open, + Draft, + Archived, + Merged, +} + +impl PatchState { + pub fn matches(&self, patch: &patch::State) -> bool { + match self { + Self::Open => matches!(patch, patch::State::Open), + Self::Draft => matches!(patch, patch::State::Draft), + Self::Archived => matches!(patch, patch::State::Archived), + Self::Merged => matches!(patch, patch::State::Merged), + } + } +} + mod project { use nonempty::NonEmpty; use serde::Serialize; diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 2efe091d8..7e04d9725 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -21,7 +21,7 @@ use radicle_surf::{Glob, Oid, Repository}; use crate::api::error::Error; use crate::api::project::Info; -use crate::api::{self, Context, PaginationQuery}; +use crate::api::{self, CobsQuery, Context, PaginationQuery}; use crate::axum_extra::{Path, Query}; const CACHE_1_HOUR: &str = "public, max-age=3600, must-revalidate"; @@ -416,15 +416,27 @@ async fn readme_handler( async fn issues_handler( State(ctx): State, Path(project): Path, - Query(qs): Query, + Query(qs): Query>, ) -> impl IntoResponse { - let PaginationQuery { page, per_page } = qs; + let CobsQuery { + page, + per_page, + state, + } = qs; let page = page.unwrap_or(0); let per_page = per_page.unwrap_or(10); + let state = state.unwrap_or_default(); let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let issues = issue::Issues::open(&repo)?; - let mut issues: Vec<_> = issues.all()?.filter_map(|r| r.ok()).collect::>(); + let mut issues: Vec<_> = issues + .all()? + .filter_map(|r| { + let (id, issue, clock) = r.ok()?; + (state.matches(issue.state())).then_some((id, issue, clock)) + }) + .collect::>(); + issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let issues = issues .into_iter() @@ -687,15 +699,26 @@ async fn patch_update_handler( async fn patches_handler( State(ctx): State, Path(project): Path, - Query(qs): Query, + Query(qs): Query>, ) -> impl IntoResponse { - let PaginationQuery { page, per_page } = qs; + let CobsQuery { + page, + per_page, + state, + } = qs; let page = page.unwrap_or(0); let per_page = per_page.unwrap_or(10); + let state = state.unwrap_or_default(); let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let patches = patch::Patches::open(&repo)?; - let mut patches = patches.all()?.filter_map(|r| r.ok()).collect::>(); + let mut patches = patches + .all()? + .filter_map(|r| { + let (id, patch, clock) = r.ok()?; + (state.matches(&patch.state())).then_some((id, patch, clock)) + }) + .collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); let patches = patches .into_iter() From 5971c1c9f9ee77608b3ed8a47add498e1c05c74d Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 26 Apr 2023 11:06:55 +0200 Subject: [PATCH 109/264] radicle: Remove `delegate` from `Remote` type It wasn't being set correctly, and it's not the place for it, so we remove it and set it in `httpd`. Note that `Remote` is now simply a wrapper around `SignedRefs`. If we want to remove the type or turn it into an alias, it will require touching lots of different parts of the codebase. I opted not to do that here as it's lower priority. --- radicle-httpd/src/api/v1/projects.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 7e04d9725..14b98619e 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -318,6 +318,7 @@ async fn tree_handler( async fn remotes_handler(State(ctx): State, Path(project): Path) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = storage.repository(project)?; + let delegates = repo.delegates()?; let remotes = repo .remotes()? .filter_map(|r| r.map(|r| r.1).ok()) @@ -335,7 +336,7 @@ async fn remotes_handler(State(ctx): State, Path(project): Path) -> json!({ "id": remote.id, "heads": refs, - "delegate": remote.delegate, + "delegate": delegates.contains(&remote.id.into()), }) }) .collect::>(); @@ -351,6 +352,7 @@ async fn remote_handler( ) -> impl IntoResponse { let storage = &ctx.profile.storage; let repo = storage.repository(project)?; + let delegates = repo.delegates()?; let remote = repo.remote(&node_id)?; let refs = remote .refs @@ -364,7 +366,7 @@ async fn remote_handler( let remote = json!({ "id": remote.id, "heads": refs, - "delegate": remote.delegate, + "delegate": delegates.contains(&remote.id.into()), }); Ok::<_, Error>(Json(remote)) @@ -1251,7 +1253,7 @@ mod routes { "heads": { "master": HEAD }, - "delegate": false + "delegate": true } ]) ); @@ -1275,7 +1277,7 @@ mod routes { "heads": { "master": HEAD }, - "delegate": false + "delegate": true }) ); } From 32fde2aecfb5d64354c8c778451b6f114d50f1c3 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 28 Apr 2023 20:47:18 +0200 Subject: [PATCH 110/264] cob: Include head in initial patch entry in DAG This change ensures that we include as a parent to the initial commit of a patch COB, the head of the branch we are proposing as a patch. This was already happening for patch updates, but we forgot to also include it for the initial patch revision. This ensures that the code is always fetched alongside the patch. --- radicle-httpd/src/api/v1/projects.rs | 4 ++-- radicle-httpd/src/test.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 14b98619e..0d45592b9 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1784,7 +1784,7 @@ mod routes { #[tokio::test] async fn test_projects_create_patches() { - const CREATED_PATCH_ID: &str = "f69641cba6d7df2c22844d7f39225b5cda54d363"; + const CREATED_PATCH_ID: &str = "198b7bc8d19ef2f09e9fbd30645a46e4cd520713"; let tmp = tempfile::tempdir().unwrap(); let ctx = contributor(tmp.path()); @@ -1978,7 +1978,7 @@ mod routes { "reviews": [], }, { - "id": "b1a8c5b3e1686891808fcac0d795d0bad1e69709", + "id": "660ce23a54788d85d44a133357b53fc99f5d7a92", "description": "This is a new revision", "base": PARENT, "oid": HEAD, diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 9bf1940e3..650e8d305 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -37,9 +37,9 @@ pub const CONTRIBUTOR_RID: &str = "rad:z4XaCmN3jLSeiMvW15YTDpNbDHFhG"; pub const CONTRIBUTOR_DID: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; pub const CONTRIBUTOR_NID: &str = "z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; pub const CONTRIBUTOR_ISSUE_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; -pub const CONTRIBUTOR_PATCH_ID: &str = "4b7ea91da1f551bc5ea5d2057125d7ba42d451cb"; -pub const CONTRIBUTOR_COMMENT_1: &str = "8f6e72c20da0e6ee2af004f059ced23d409a285a"; -pub const CONTRIBUTOR_COMMENT_2: &str = "c88ce78b17a705f389e72ec5c80da7360a675e51"; +pub const CONTRIBUTOR_PATCH_ID: &str = "dcac7b6a320ce94e9a801ff9d766a04d24f63062"; +pub const CONTRIBUTOR_COMMENT_1: &str = "ec1c721cb3f726c3c0455519034c3a8b1a6ebaef"; +pub const CONTRIBUTOR_COMMENT_2: &str = "4458381a73ff68063311d440538aff4b48122b9c"; const PASSWORD: &str = "radicle"; From c54aa003378e9786f3425b551a6e84037f22222a Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Fri, 5 May 2023 12:59:59 +0100 Subject: [PATCH 111/264] radicle: update radicle-git-ext This change updates to the latest radicle-git-ext, which required a few changes within the radicle family of crates. One set of changes is that the radicle-git-ext crate subsumes git-commit, git-trailers, and git-ref-format -- so all those imports go through radicle-git-ext. The commit code requires that parent `Oid`s point to commits, which made some tests fail. One of the reasons for these failures is that the patch tests used fabricated `Oid`s and so the verification would fail. This is fixed by allowing the test context code to create `PatchRequest`s that create valid commits. The other reason for failure was that the identity `Action` passed an `Oid` that pointed to a blob. This is fixed by removing that code and making a note of it in documentation. The final major change was moving the Refspec type into git-ref-format and removing the AsRefspecs trait. The trait was not required and could its usage could be replaced by simpler code. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-httpd/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index bf9a73b84..95d693abc 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -25,7 +25,7 @@ flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } nonempty = { version = "0.8.1", features = ["serialize"] } -radicle-surf = { version = "0.9.0", default-features = false, features = ["serde"] } +radicle-surf = { version = "0.10.3", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } From be9eb3f8e249b220995eb6f00853a60183ecc096 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 10 May 2023 15:08:53 +0200 Subject: [PATCH 112/264] Update dependencies * radicle-surf * radicle-git-ext * amplify --- radicle-httpd/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 95d693abc..f01db8dc6 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -25,7 +25,7 @@ flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } nonempty = { version = "0.8.1", features = ["serialize"] } -radicle-surf = { version = "0.10.3", default-features = false, features = ["serde"] } +radicle-surf = { version = "0.11.0", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } From 8880c49c1d8ec2caa5f98e4f5a9a5bfa40ff18f9 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Wed, 10 May 2023 16:32:21 +0000 Subject: [PATCH 113/264] httpd: Add `alias` field to certain routes * `/patches`, `/issues`, `/remotes` Signed-off-by: xphoniex --- radicle-httpd/src/api/error.rs | 4 +++ radicle-httpd/src/api/json.rs | 41 ++++++++++++++++++++++------ radicle-httpd/src/api/v1/projects.rs | 40 +++++++++++++++++++++------ radicle-httpd/src/test.rs | 4 +++ 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 1edb48ff3..e20c6e84d 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -69,6 +69,10 @@ pub enum Error { /// Identity doc error. #[error(transparent)] IdentityDoc(#[from] radicle::identity::doc::DocError), + + /// Tracking store error. + #[error(transparent)] + TrackingStore(#[from] radicle::node::tracking::store::Error), } impl IntoResponse for Error { diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 996db6ca7..2f20e2e04 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -12,6 +12,7 @@ use radicle::cob::thread; use radicle::cob::thread::{CommentId, Thread}; use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::git::RefString; +use radicle::node::tracking::store as TrackingStore; use radicle::storage::{git, refs, ReadRepository}; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; @@ -92,26 +93,31 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value { } /// Returns JSON for an `issue`. -pub(crate) fn issue(id: IssueId, issue: Issue) -> Value { +pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &TrackingStore::Config) -> Value { json!({ "id": id.to_string(), - "author": issue.author(), + "author": author(&issue.author(), aliases.alias(issue.author().id())), "title": issue.title(), "state": issue.state(), "assignees": issue.assigned().collect::>(), "discussion": issue .comments() - .map(|(id, comment)| Comment::new(id, comment, issue.thread())) + .map(|(id, comment)| Comment::new(id, comment, issue.thread(), aliases)) .collect::>(), "tags": issue.tags().collect::>(), }) } /// Returns JSON for a `patch`. -pub(crate) fn patch(id: PatchId, patch: Patch, repo: &git::Repository) -> Value { +pub(crate) fn patch( + id: PatchId, + patch: Patch, + repo: &git::Repository, + aliases: &TrackingStore::Config, +) -> Value { json!({ "id": id.to_string(), - "author": patch.author(), + "author": author(patch.author(), aliases.alias(patch.author().id())), "title": patch.title(), "description": patch.description(), "state": patch.state(), @@ -126,7 +132,7 @@ pub(crate) fn patch(id: PatchId, patch: Patch, repo: &git::Repository) -> Value "refs": get_refs(repo, patch.author().id(), &rev.head()).unwrap_or(vec![]), "merges": rev.merges().collect::>(), "discussions": rev.discussion().comments() - .map(|(id, comment)| Comment::new(id, comment, rev.discussion())) + .map(|(id, comment)| Comment::new(id, comment, rev.discussion(), aliases)) .collect::>(), "timestamp": rev.timestamp(), "reviews": rev.reviews().collect::>(), @@ -135,6 +141,17 @@ pub(crate) fn patch(id: PatchId, patch: Patch, repo: &git::Repository) -> Value }) } +/// Returns JSON for an `author` and fills in `alias` when present. +fn author(author: &Author, alias: Option) -> Value { + match alias { + Some(alias) => json!({ + "id": author.id, + "alias": alias, + }), + None => json!(author), + } +} + /// Returns the name part of a path string. fn name_in_path(path: &str) -> &str { match path.rsplit('/').next() { @@ -168,7 +185,7 @@ fn get_refs( #[serde(rename_all = "camelCase")] struct Comment<'a> { id: CommentId, - author: Author, + author: Value, body: &'a str, reactions: Vec<(&'a ActorId, &'a Reaction)>, timestamp: Timestamp, @@ -176,10 +193,16 @@ struct Comment<'a> { } impl<'a> Comment<'a> { - fn new(id: &'a CommentId, comment: &'a thread::Comment, thread: &'a Thread) -> Self { + fn new( + id: &'a CommentId, + comment: &'a thread::Comment, + thread: &'a Thread, + aliases: &TrackingStore::Config, + ) -> Self { + let comment_author = Author::new(comment.author()); Self { id: *id, - author: Author::new(comment.author()), + author: author(&comment_author, aliases.alias(comment_author.id())), body: comment.body(), reactions: thread.reactions(id).collect::>(), timestamp: comment.timestamp(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 0d45592b9..ae1c3daee 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -319,6 +319,7 @@ async fn remotes_handler(State(ctx): State, Path(project): Path) -> let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let delegates = repo.delegates()?; + let tracking_store = &ctx.profile.tracking()?; let remotes = repo .remotes()? .filter_map(|r| r.map(|r| r.1).ok()) @@ -333,11 +334,19 @@ async fn remotes_handler(State(ctx): State, Path(project): Path) -> }) .collect::>(); - json!({ - "id": remote.id, - "heads": refs, - "delegate": delegates.contains(&remote.id.into()), - }) + match tracking_store.alias(&remote.id) { + Some(alias) => json!({ + "id": remote.id, + "alias": alias, + "heads": refs, + "delegate": delegates.contains(&remote.id.into()), + }), + None => json!({ + "id": remote.id, + "heads": refs, + "delegate": delegates.contains(&remote.id.into()), + }), + } }) .collect::>(); @@ -440,9 +449,10 @@ async fn issues_handler( .collect::>(); issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); + let tracking_store = &ctx.profile.tracking()?; let issues = issues .into_iter() - .map(|(id, issue, _)| api::json::issue(id, issue)) + .map(|(id, issue, _)| api::json::issue(id, issue, tracking_store)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -559,8 +569,13 @@ async fn issue_handler( let issue = issue::Issues::open(&repo)? .get(&issue_id.into())? .ok_or(Error::NotFound)?; + let tracking_store = &ctx.profile.tracking()?; - Ok::<_, Error>(Json(api::json::issue(issue_id.into(), issue))) + Ok::<_, Error>(Json(api::json::issue( + issue_id.into(), + issue, + tracking_store, + ))) } #[derive(Deserialize, Serialize)] @@ -722,9 +737,10 @@ async fn patches_handler( }) .collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); + let tracking_store = &ctx.profile.tracking()?; let patches = patches .into_iter() - .map(|(id, patch, _)| api::json::patch(id, patch, &repo)) + .map(|(id, patch, _)| api::json::patch(id, patch, &repo, tracking_store)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -743,8 +759,14 @@ async fn patch_handler( let patch = patch::Patches::open(&repo)? .get(&patch_id.into())? .ok_or(Error::NotFound)?; + let tracking_store = &ctx.profile.tracking()?; - Ok::<_, Error>(Json(api::json::patch(patch_id.into(), patch, &repo))) + Ok::<_, Error>(Json(api::json::patch( + patch_id.into(), + patch, + &repo, + tracking_store, + ))) } #[cfg(test)] diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 650e8d305..b9643197a 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -16,6 +16,7 @@ use radicle::crypto::ssh::keystore::MemorySigner; use radicle::crypto::ssh::Keystore; use radicle::crypto::{KeyPair, Seed, Signer}; use radicle::git::{raw as git2, RefString}; +use radicle::node::tracking::store as TrackingStore; use radicle::profile::Home; use radicle::storage::ReadStorage; use radicle::Storage; @@ -83,6 +84,9 @@ pub fn contributor(dir: &Path) -> Context { } fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G) -> Context { + let tracking_db = dir.join("radicle").join("node").join("tracking.db"); + TrackingStore::Config::open(tracking_db).unwrap(); + let workdir = dir.join("hello-world"); env::set_var("RAD_COMMIT_TIME", TIMESTAMP.to_string()); From 90cf62b0274c85f8b10c38ad6d046e1c5aa868ae Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 16 May 2023 17:32:01 +0200 Subject: [PATCH 114/264] cob: Redesign merge state Move `merges` from `Revision` to `Patch`, and key them by actor. This ensures that only one merge is possible per actor. We also make sure that only one revision can be merged at a time, and signal a conflict if more than one revision is considered merged. Finally, we make sure that merges have the same target-branch commit to be considered equal. --- radicle-httpd/src/api.rs | 4 ++-- radicle-httpd/src/api/json.rs | 9 ++++++- radicle-httpd/src/api/v1/projects.rs | 36 +++++++++++++++------------- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index a370a150e..750a2984a 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -153,10 +153,10 @@ pub enum PatchState { impl PatchState { pub fn matches(&self, patch: &patch::State) -> bool { match self { - Self::Open => matches!(patch, patch::State::Open), + Self::Open => matches!(patch, patch::State::Open { .. }), Self::Draft => matches!(patch, patch::State::Draft), Self::Archived => matches!(patch, patch::State::Archived), - Self::Merged => matches!(patch, patch::State::Merged), + Self::Merged => matches!(patch, patch::State::Merged { .. }), } } } diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 2f20e2e04..88fc3caa4 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -123,6 +123,14 @@ pub(crate) fn patch( "state": patch.state(), "target": patch.target(), "tags": patch.tags().collect::>(), + "merges": patch.merges().map(|(a, m)| { + json!({ + "author": a, + "revision": m.revision, + "commit": m.commit, + "timestamp": m.timestamp + }) + }).collect::>(), "revisions": patch.revisions().map(|(id, rev)| { json!({ "id": id, @@ -130,7 +138,6 @@ pub(crate) fn patch( "base": rev.base(), "oid": rev.head(), "refs": get_refs(repo, patch.author().id(), &rev.head()).unwrap_or(vec![]), - "merges": rev.merges().collect::>(), "discussions": rev.discussion().comments() .map(|(id, comment)| Comment::new(id, comment, rev.discussion(), aliases)) .collect::>(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index ae1c3daee..c6baea27c 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1745,6 +1745,7 @@ mod routes { "state": { "status": "open" }, "target": "delegates", "tags": [], + "merges": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -1754,7 +1755,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], @@ -1784,6 +1784,7 @@ mod routes { "state": { "status": "open" }, "target": "delegates", "tags": [], + "merges": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -1793,7 +1794,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], @@ -1862,6 +1862,7 @@ mod routes { "state": { "status": "open" }, "target": "delegates", "tags": [], + "merges": [], "revisions": [ { "id": CREATED_PATCH_ID, @@ -1871,7 +1872,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], @@ -1925,6 +1925,7 @@ mod routes { "bug", "design" ], + "merges": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -1934,7 +1935,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], @@ -1985,6 +1985,7 @@ mod routes { "state": { "status": "open" }, "target": "delegates", "tags": [], + "merges": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -1994,7 +1995,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], @@ -2007,7 +2007,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], @@ -2058,6 +2057,7 @@ mod routes { "state": { "status": "open" }, "target": "delegates", "tags": [], + "merges": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2067,7 +2067,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], @@ -2140,6 +2139,7 @@ mod routes { "state": { "status": "open" }, "target": "delegates", "tags": [], + "merges": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2149,7 +2149,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [ { "id": CONTRIBUTOR_COMMENT_1, @@ -2236,6 +2235,7 @@ mod routes { "state": { "status": "open" }, "target": "delegates", "tags": [], + "merges": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2245,7 +2245,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [], "discussions": [], "timestamp": TIMESTAMP, "reviews": [ @@ -2316,9 +2315,19 @@ mod routes { }, "title": "A new `hello world`", "description": "change `hello world` in README to something else", - "state": { "status": "merged" }, + "state": { + "status": "merged", + "revision": CONTRIBUTOR_PATCH_ID, + "commit": PARENT, + }, "target": "delegates", "tags": [], + "merges": [{ + "author": CONTRIBUTOR_NID, + "revision": CONTRIBUTOR_PATCH_ID, + "commit": PARENT, + "timestamp": TIMESTAMP, + }], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2328,13 +2337,6 @@ mod routes { "refs": [ "refs/heads/master", ], - "merges": [ - { - "node": CONTRIBUTOR_NID, - "commit": PARENT, - "timestamp": TIMESTAMP, - }, - ], "discussions": [], "timestamp": TIMESTAMP, "reviews": [], From e17e7896028792e1c902af420431b31482edd5a8 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 8 May 2023 15:50:46 +0000 Subject: [PATCH 115/264] cli: Show alias on `rad issue` and `rad patch` Signed-off-by: xphoniex --- radicle-httpd/src/api/v1/projects.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index c6baea27c..5f873558a 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -733,7 +733,7 @@ async fn patches_handler( .all()? .filter_map(|r| { let (id, patch, clock) = r.ok()?; - (state.matches(&patch.state())).then_some((id, patch, clock)) + (state.matches(patch.state())).then_some((id, patch, clock)) }) .collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); From 3d2e57edcc3f9e74614f93a62d371fc91cad7576 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 25 May 2023 13:03:52 +0200 Subject: [PATCH 116/264] cob: Remove `description` from `Patch` Since patches always have a "root" revision, use that revision's description as the patch description. --- radicle-httpd/src/api/json.rs | 1 - radicle-httpd/src/api/v1/projects.rs | 39 ++++++++++------------------ radicle-httpd/src/test.rs | 6 ++--- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 88fc3caa4..8f97f0093 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -119,7 +119,6 @@ pub(crate) fn patch( "id": id.to_string(), "author": author(patch.author(), aliases.alias(patch.author().id())), "title": patch.title(), - "description": patch.description(), "state": patch.state(), "target": patch.target(), "tags": patch.tags().collect::>(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 5f873558a..678ad1e06 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -648,12 +648,8 @@ async fn patch_update_handler( let mut patches = patch::Patches::open(&repo)?; let mut patch = patches.get_mut(&patch_id.into())?; match action { - patch::Action::Edit { - title, - description, - target, - } => { - patch.edit(title, description, target, &signer)?; + patch::Action::Edit { title, target } => { + patch.edit(title, target, &signer)?; } patch::Action::EditRevision { revision, @@ -1741,7 +1737,6 @@ mod routes { "id": CONTRIBUTOR_DID }, "title": "A new `hello world`", - "description": "change `hello world` in README to something else", "state": { "status": "open" }, "target": "delegates", "tags": [], @@ -1749,7 +1744,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ @@ -1780,7 +1775,6 @@ mod routes { "id": CONTRIBUTOR_DID }, "title": "A new `hello world`", - "description": "change `hello world` in README to something else", "state": { "status": "open" }, "target": "delegates", "tags": [], @@ -1788,7 +1782,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ @@ -1806,7 +1800,7 @@ mod routes { #[tokio::test] async fn test_projects_create_patches() { - const CREATED_PATCH_ID: &str = "198b7bc8d19ef2f09e9fbd30645a46e4cd520713"; + const CREATED_PATCH_ID: &str = "768e76ae6611d9392f04122a5aa7a587b47b9e19"; let tmp = tempfile::tempdir().unwrap(); let ctx = contributor(tmp.path()); @@ -1858,7 +1852,6 @@ mod routes { "id": CONTRIBUTOR_DID }, "title": "Update README", - "description": "Do some changes to README", "state": { "status": "open" }, "target": "delegates", "tags": [], @@ -1866,7 +1859,7 @@ mod routes { "revisions": [ { "id": CREATED_PATCH_ID, - "description": "", + "description": "Do some changes to README", "base": INITIAL_COMMIT, "oid": HEAD, "refs": [ @@ -1918,7 +1911,6 @@ mod routes { "id": CONTRIBUTOR_DID, }, "title": "A new `hello world`", - "description": "change `hello world` in README to something else", "state": { "status": "open" }, "target": "delegates", "tags": [ @@ -1929,7 +1921,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ @@ -1981,7 +1973,6 @@ mod routes { "id": CONTRIBUTOR_DID, }, "title": "A new `hello world`", - "description": "change `hello world` in README to something else", "state": { "status": "open" }, "target": "delegates", "tags": [], @@ -1989,7 +1980,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ @@ -2000,7 +1991,7 @@ mod routes { "reviews": [], }, { - "id": "660ce23a54788d85d44a133357b53fc99f5d7a92", + "id": "181e4219bc132e7716126a84200d4dbd628dd6be", "description": "This is a new revision", "base": PARENT, "oid": HEAD, @@ -2053,7 +2044,6 @@ mod routes { "id": CONTRIBUTOR_DID, }, "title": "This is a updated title", - "description": "Let's write some description", "state": { "status": "open" }, "target": "delegates", "tags": [], @@ -2061,7 +2051,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ @@ -2135,7 +2125,6 @@ mod routes { "id": CONTRIBUTOR_DID, }, "title": "A new `hello world`", - "description": "change `hello world` in README to something else", "state": { "status": "open" }, "target": "delegates", "tags": [], @@ -2143,7 +2132,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ @@ -2231,7 +2220,6 @@ mod routes { "id": CONTRIBUTOR_DID, }, "title": "A new `hello world`", - "description": "change `hello world` in README to something else", "state": { "status": "open" }, "target": "delegates", "tags": [], @@ -2239,7 +2227,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ @@ -2314,7 +2302,6 @@ mod routes { "id": CONTRIBUTOR_DID, }, "title": "A new `hello world`", - "description": "change `hello world` in README to something else", "state": { "status": "merged", "revision": CONTRIBUTOR_PATCH_ID, @@ -2331,7 +2318,7 @@ mod routes { "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, - "description": "", + "description": "change `hello world` in README to something else", "base": PARENT, "oid": HEAD, "refs": [ diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index b9643197a..bbb0319e6 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -38,9 +38,9 @@ pub const CONTRIBUTOR_RID: &str = "rad:z4XaCmN3jLSeiMvW15YTDpNbDHFhG"; pub const CONTRIBUTOR_DID: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; pub const CONTRIBUTOR_NID: &str = "z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; pub const CONTRIBUTOR_ISSUE_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; -pub const CONTRIBUTOR_PATCH_ID: &str = "dcac7b6a320ce94e9a801ff9d766a04d24f63062"; -pub const CONTRIBUTOR_COMMENT_1: &str = "ec1c721cb3f726c3c0455519034c3a8b1a6ebaef"; -pub const CONTRIBUTOR_COMMENT_2: &str = "4458381a73ff68063311d440538aff4b48122b9c"; +pub const CONTRIBUTOR_PATCH_ID: &str = "044b577cc7551cd09d4b2f03566a553762180de4"; +pub const CONTRIBUTOR_COMMENT_1: &str = "92aab76516ae7f60a9b2952043ba578383de7d46"; +pub const CONTRIBUTOR_COMMENT_2: &str = "cb360eee0ec70563d5c4c3613fdc076648523248"; const PASSWORD: &str = "radicle"; From cc7fdebb6a9a4a748643fd212d141d72ba69f24b Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 25 May 2023 13:08:45 +0200 Subject: [PATCH 117/264] cob: Add `reviewers` field on `Patch` We aren't ready to support this yet from an end-user perspective, but we add it in the COB already to prepare. --- radicle-httpd/src/api/json.rs | 1 + radicle-httpd/src/api/v1/projects.rs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 8f97f0093..313681809 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -130,6 +130,7 @@ pub(crate) fn patch( "timestamp": m.timestamp }) }).collect::>(), + "reviewers": patch.reviewers().collect::>(), "revisions": patch.revisions().map(|(id, rev)| { json!({ "id": id, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 678ad1e06..15257096c 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1741,6 +1741,7 @@ mod routes { "target": "delegates", "tags": [], "merges": [], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -1779,6 +1780,7 @@ mod routes { "target": "delegates", "tags": [], "merges": [], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -1856,6 +1858,7 @@ mod routes { "target": "delegates", "tags": [], "merges": [], + "reviewers": [], "revisions": [ { "id": CREATED_PATCH_ID, @@ -1918,6 +1921,7 @@ mod routes { "design" ], "merges": [], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -1977,6 +1981,7 @@ mod routes { "target": "delegates", "tags": [], "merges": [], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2048,6 +2053,7 @@ mod routes { "target": "delegates", "tags": [], "merges": [], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2129,6 +2135,7 @@ mod routes { "target": "delegates", "tags": [], "merges": [], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2224,6 +2231,7 @@ mod routes { "target": "delegates", "tags": [], "merges": [], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, @@ -2315,6 +2323,7 @@ mod routes { "commit": PARENT, "timestamp": TIMESTAMP, }], + "reviewers": [], "revisions": [ { "id": CONTRIBUTOR_PATCH_ID, From c7357832469f8b8cbf245a93353d564cf74c9e7a Mon Sep 17 00:00:00 2001 From: xphoniex Date: Wed, 24 May 2023 16:28:49 +0000 Subject: [PATCH 118/264] httpd: Add author to revision of a patch Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 1 + radicle-httpd/src/api/v1/projects.rs | 30 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 313681809..a150f30e5 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -134,6 +134,7 @@ pub(crate) fn patch( "revisions": patch.revisions().map(|(id, rev)| { json!({ "id": id, + "author": author(rev.author(), aliases.alias(rev.author().id())), "description": rev.description(), "base": rev.base(), "oid": rev.head(), diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 15257096c..c18c68b2d 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1746,6 +1746,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ @@ -1785,6 +1788,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ @@ -1863,6 +1869,9 @@ mod routes { { "id": CREATED_PATCH_ID, "description": "Do some changes to README", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": INITIAL_COMMIT, "oid": HEAD, "refs": [ @@ -1926,6 +1935,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ @@ -1986,6 +1998,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ @@ -1997,6 +2012,9 @@ mod routes { }, { "id": "181e4219bc132e7716126a84200d4dbd628dd6be", + "author": { + "id": CONTRIBUTOR_DID, + }, "description": "This is a new revision", "base": PARENT, "oid": HEAD, @@ -2058,6 +2076,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ @@ -2140,6 +2161,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ @@ -2236,6 +2260,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ @@ -2328,6 +2355,9 @@ mod routes { { "id": CONTRIBUTOR_PATCH_ID, "description": "change `hello world` in README to something else", + "author": { + "id": CONTRIBUTOR_DID, + }, "base": PARENT, "oid": HEAD, "refs": [ From 273a527cd7cf27b6e33f0cad19e79ec0030b3b87 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Tue, 23 May 2023 11:11:15 +0000 Subject: [PATCH 119/264] httpd: Return `alias` field on `merges` of a patch Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 62 ++++++++++++++++++++++++---- radicle-httpd/src/api/v1/projects.rs | 48 ++++++++++----------- 2 files changed, 78 insertions(+), 32 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index a150f30e5..011ad99f2 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -7,12 +7,15 @@ use serde::Serialize; use serde_json::{json, Value}; 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::thread; use radicle::cob::thread::{CommentId, Thread}; use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::git::RefString; use radicle::node::tracking::store as TrackingStore; +use radicle::prelude::NodeId; use radicle::storage::{git, refs, ReadRepository}; use radicle_surf::blob::Blob; use radicle_surf::tree::Tree; @@ -122,14 +125,7 @@ pub(crate) fn patch( "state": patch.state(), "target": patch.target(), "tags": patch.tags().collect::>(), - "merges": patch.merges().map(|(a, m)| { - json!({ - "author": a, - "revision": m.revision, - "commit": m.commit, - "timestamp": m.timestamp - }) - }).collect::>(), + "merges": patch.merges().map(|(nid, m)| merge(m, nid, aliases.alias(nid))).collect::>(), "reviewers": patch.reviewers().collect::>(), "revisions": patch.revisions().map(|(id, rev)| { json!({ @@ -143,7 +139,7 @@ pub(crate) fn patch( .map(|(id, comment)| Comment::new(id, comment, rev.discussion(), aliases)) .collect::>(), "timestamp": rev.timestamp(), - "reviews": rev.reviews().collect::>(), + "reviews": rev.reviews().map(|(nid, _review)| review(nid, aliases.alias(nid), _review)).collect::>(), }) }).collect::>(), }) @@ -160,6 +156,54 @@ fn author(author: &Author, alias: Option) -> Value { } } +/// Returns JSON for a patch `Merge` and fills in `alias` when present. +fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> 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, review: &Review) -> Value { + match alias { + Some(alias) => json!({ + "author": { + "id": nid, + "alias": alias, + }, + "verdict": review.verdict(), + "comment": review.comment(), + "inline": review.inline().collect::>(), + "timestamp": review.timestamp(), + }), + None => json!({ + "author": { + "id": nid, + }, + "verdict": review.verdict(), + "comment": review.comment(), + "inline": review.inline().collect::>(), + "timestamp": review.timestamp(), + }), + } +} + /// Returns the name part of a path string. fn name_in_path(path: &str) -> &str { match path.rsplit('/').next() { diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index c18c68b2d..55fccb3a9 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -2271,29 +2271,29 @@ mod routes { "discussions": [], "timestamp": TIMESTAMP, "reviews": [ - [ - CONTRIBUTOR_NID, - { - "verdict": "accept", - "comment": "A small review", - "inline": [ - { - "location": { - "blob": "82eb77880c693655bce074e3dbbd9fa711dc018b", - "path": "./README.md", - "commit": HEAD, - "lines": { - "start": 1, - "end": 3, - }, - }, - "comment": "This is a comment on line 1", - "timestamp": TIMESTAMP, - } - ], - "timestamp": TIMESTAMP, + { + "author": { + "id": CONTRIBUTOR_NID, }, - ], + "verdict": "accept", + "comment": "A small review", + "inline": [ + { + "location": { + "blob": "82eb77880c693655bce074e3dbbd9fa711dc018b", + "path": "./README.md", + "commit": HEAD, + "lines": { + "start": 1, + "end": 3, + }, + }, + "comment": "This is a comment on line 1", + "timestamp": TIMESTAMP, + } + ], + "timestamp": TIMESTAMP, + }, ], }, ], @@ -2345,7 +2345,9 @@ mod routes { "target": "delegates", "tags": [], "merges": [{ - "author": CONTRIBUTOR_NID, + "author": { + "id": CONTRIBUTOR_NID, + }, "revision": CONTRIBUTOR_PATCH_ID, "commit": PARENT, "timestamp": TIMESTAMP, From d0d47f791b74cf2bf935535e93583990b9a382da Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 1 Jun 2023 12:26:27 +0000 Subject: [PATCH 120/264] node: Add access to routing.db on `profile` httpd: Use routing.db to return `trackings` of an RID Signed-off-by: xphoniex --- radicle-httpd/src/api.rs | 5 +++++ radicle-httpd/src/api/error.rs | 4 ++++ radicle-httpd/src/api/v1/delegates.rs | 8 +++++++- radicle-httpd/src/api/v1/projects.rs | 10 ++++++++-- radicle-httpd/src/test.rs | 3 +++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 750a2984a..a4a71919c 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -17,6 +17,7 @@ use tower_http::cors::{self, CorsLayer}; use radicle::cob::issue; use radicle::cob::patch; use radicle::identity::Id; +use radicle::node::routing::Store; use radicle::storage::{ReadRepository, ReadStorage}; use radicle::Profile; @@ -52,6 +53,8 @@ impl Context { let delegates = doc.delegates; let issues = issue::Issues::open(&repo)?.counts()?; let patches = patch::Patches::open(&repo)?.counts()?; + let routing = &self.profile.routing()?; + let trackings = routing.count(&id).unwrap_or_default(); Ok(project::Info { payload, @@ -60,6 +63,7 @@ impl Context { issues, patches, id, + trackings, }) } @@ -183,5 +187,6 @@ mod project { pub patches: cob::patch::PatchCounts, pub issues: cob::issue::IssueCounts, pub id: Id, + pub trackings: usize, } } diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index e20c6e84d..f1763e266 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -73,6 +73,10 @@ pub enum Error { /// Tracking store error. #[error(transparent)] TrackingStore(#[from] radicle::node::tracking::store::Error), + + /// Routing store error. + #[error(transparent)] + RoutingStore(#[from] radicle::node::routing::Error), } impl IntoResponse for Error { diff --git a/radicle-httpd/src/api/v1/delegates.rs b/radicle-httpd/src/api/v1/delegates.rs index 603eb287d..825416517 100644 --- a/radicle-httpd/src/api/v1/delegates.rs +++ b/radicle-httpd/src/api/v1/delegates.rs @@ -6,6 +6,7 @@ use axum::{Json, Router}; use radicle::cob::issue::Issues; use radicle::cob::patch::Patches; use radicle::identity::Did; +use radicle::node::routing::Store; use radicle::storage::{ReadRepository, ReadStorage}; use crate::api::error::Error; @@ -34,6 +35,7 @@ async fn delegates_projects_handler( let page = page.unwrap_or(0); let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; + let routing = &ctx.profile.routing()?; let projects = storage .repositories()? .into_iter() @@ -54,6 +56,8 @@ async fn delegates_projects_handler( let Ok(patches) = Patches::open(&repo) else { return None }; let Ok(patches) = patches.counts() else { return None }; + let trackings = routing.count(&id).unwrap_or_default(); + Some(Info { payload, delegates, @@ -61,6 +65,7 @@ async fn delegates_projects_handler( issues, patches, id, + trackings, }) }) .skip(page * per_page) @@ -107,7 +112,8 @@ mod routes { "open": 1, "closed": 0, }, - "id": RID + "id": RID, + "trackings": 0, } ]) ); diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 55fccb3a9..38653962d 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -14,6 +14,7 @@ use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::{issue, patch, thread, ActorId, Tag}; use radicle::identity::Id; +use radicle::node::routing::Store; use radicle::node::NodeId; use radicle::storage::git::paths; use radicle::storage::{ReadRepository, ReadStorage, WriteRepository}; @@ -77,6 +78,7 @@ async fn project_root_handler( let page = page.unwrap_or(0); let per_page = per_page.unwrap_or(10); let storage = &ctx.profile.storage; + let routing = &ctx.profile.routing()?; let projects = storage .repositories()? .into_iter() @@ -91,6 +93,7 @@ async fn project_root_handler( let Ok(patches) = patch::Patches::open(&repo) else { return None }; let Ok(patches) = patches.counts() else { return None }; let delegates = doc.delegates; + let trackings = routing.count(&id).unwrap_or_default(); Some(Info { payload, @@ -99,6 +102,7 @@ async fn project_root_handler( issues, patches, id, + trackings, }) }) .skip(page * per_page) @@ -800,7 +804,8 @@ mod routes { "open": 1, "closed": 0, }, - "id": RID + "id": RID, + "trackings": 0, } ]) ); @@ -831,7 +836,8 @@ mod routes { "open": 1, "closed": 0, }, - "id": RID + "id": RID, + "trackings": 0, }) ); } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index bbb0319e6..14a922bc9 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -16,6 +16,7 @@ use radicle::crypto::ssh::keystore::MemorySigner; use radicle::crypto::ssh::Keystore; use radicle::crypto::{KeyPair, Seed, Signer}; use radicle::git::{raw as git2, RefString}; +use radicle::node::routing as RoutingStore; use radicle::node::tracking::store as TrackingStore; use radicle::profile::Home; use radicle::storage::ReadStorage; @@ -85,7 +86,9 @@ pub fn contributor(dir: &Path) -> Context { fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G) -> Context { let tracking_db = dir.join("radicle").join("node").join("tracking.db"); + let routing_db = dir.join("radicle").join("node").join("routing.db"); TrackingStore::Config::open(tracking_db).unwrap(); + RoutingStore::Table::open(routing_db).unwrap(); let workdir = dir.join("hello-world"); From 6f20e93c3caa952f304dbaef862ceb60ffcf5253 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Sun, 28 May 2023 09:06:47 +0000 Subject: [PATCH 121/264] node: Introduce `AliasStore` trait httpd: use the `AliasStore` trait Signed-off-by: xphoniex --- radicle-httpd/src/api/json.rs | 8 ++++---- radicle-httpd/src/api/v1/projects.rs | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 011ad99f2..1a2dbe622 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -14,7 +14,7 @@ use radicle::cob::thread; use radicle::cob::thread::{CommentId, Thread}; use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::git::RefString; -use radicle::node::tracking::store as TrackingStore; +use radicle::node::AliasStore; use radicle::prelude::NodeId; use radicle::storage::{git, refs, ReadRepository}; use radicle_surf::blob::Blob; @@ -96,7 +96,7 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value { } /// Returns JSON for an `issue`. -pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &TrackingStore::Config) -> Value { +pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &impl AliasStore) -> Value { json!({ "id": id.to_string(), "author": author(&issue.author(), aliases.alias(issue.author().id())), @@ -116,7 +116,7 @@ pub(crate) fn patch( id: PatchId, patch: Patch, repo: &git::Repository, - aliases: &TrackingStore::Config, + aliases: &impl AliasStore, ) -> Value { json!({ "id": id.to_string(), @@ -249,7 +249,7 @@ impl<'a> Comment<'a> { id: &'a CommentId, comment: &'a thread::Comment, thread: &'a Thread, - aliases: &TrackingStore::Config, + aliases: &impl AliasStore, ) -> Self { let comment_author = Author::new(comment.author()); Self { diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 38653962d..41487f1e9 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -15,6 +15,7 @@ use tower_http::set_header::SetResponseHeaderLayer; use radicle::cob::{issue, patch, thread, ActorId, Tag}; use radicle::identity::Id; use radicle::node::routing::Store; +use radicle::node::AliasStore; use radicle::node::NodeId; use radicle::storage::git::paths; use radicle::storage::{ReadRepository, ReadStorage, WriteRepository}; @@ -456,7 +457,7 @@ async fn issues_handler( let tracking_store = &ctx.profile.tracking()?; let issues = issues .into_iter() - .map(|(id, issue, _)| api::json::issue(id, issue, tracking_store)) + .map(|(id, issue, _)| api::json::issue(id, issue, &tracking_store)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -578,7 +579,7 @@ async fn issue_handler( Ok::<_, Error>(Json(api::json::issue( issue_id.into(), issue, - tracking_store, + &tracking_store, ))) } @@ -740,7 +741,7 @@ async fn patches_handler( let tracking_store = &ctx.profile.tracking()?; let patches = patches .into_iter() - .map(|(id, patch, _)| api::json::patch(id, patch, &repo, tracking_store)) + .map(|(id, patch, _)| api::json::patch(id, patch, &repo, &tracking_store)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -765,7 +766,7 @@ async fn patch_handler( patch_id.into(), patch, &repo, - tracking_store, + &tracking_store, ))) } From b1169319c3413781074e299f8a47bb2e5ac08f8a Mon Sep 17 00:00:00 2001 From: xphoniex Date: Sat, 27 May 2023 13:46:39 +0000 Subject: [PATCH 122/264] radicle: Create `Aliases` and use it in `httpd` Signed-off-by: xphoniex --- radicle-httpd/src/api/v1/projects.rs | 24 ++++++++++-------------- radicle-httpd/src/test.rs | 4 ++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 41487f1e9..bb97dedca 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -324,7 +324,7 @@ async fn remotes_handler(State(ctx): State, Path(project): Path) -> let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let delegates = repo.delegates()?; - let tracking_store = &ctx.profile.tracking()?; + let aliases = &ctx.profile.aliases()?; let remotes = repo .remotes()? .filter_map(|r| r.map(|r| r.1).ok()) @@ -339,7 +339,7 @@ async fn remotes_handler(State(ctx): State, Path(project): Path) -> }) .collect::>(); - match tracking_store.alias(&remote.id) { + match aliases.alias(&remote.id) { Some(alias) => json!({ "id": remote.id, "alias": alias, @@ -454,10 +454,10 @@ async fn issues_handler( .collect::>(); issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); - let tracking_store = &ctx.profile.tracking()?; + let aliases = &ctx.profile.aliases()?; let issues = issues .into_iter() - .map(|(id, issue, _)| api::json::issue(id, issue, &tracking_store)) + .map(|(id, issue, _)| api::json::issue(id, issue, &aliases)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -574,13 +574,9 @@ async fn issue_handler( let issue = issue::Issues::open(&repo)? .get(&issue_id.into())? .ok_or(Error::NotFound)?; - let tracking_store = &ctx.profile.tracking()?; + let aliases = &ctx.profile.aliases()?; - Ok::<_, Error>(Json(api::json::issue( - issue_id.into(), - issue, - &tracking_store, - ))) + Ok::<_, Error>(Json(api::json::issue(issue_id.into(), issue, &aliases))) } #[derive(Deserialize, Serialize)] @@ -738,10 +734,10 @@ async fn patches_handler( }) .collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); - let tracking_store = &ctx.profile.tracking()?; + let aliases = &ctx.profile.aliases()?; let patches = patches .into_iter() - .map(|(id, patch, _)| api::json::patch(id, patch, &repo, &tracking_store)) + .map(|(id, patch, _)| api::json::patch(id, patch, &repo, &aliases)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -760,13 +756,13 @@ async fn patch_handler( let patch = patch::Patches::open(&repo)? .get(&patch_id.into())? .ok_or(Error::NotFound)?; - let tracking_store = &ctx.profile.tracking()?; + let aliases = &ctx.profile.aliases()?; Ok::<_, Error>(Json(api::json::patch( patch_id.into(), patch, &repo, - &tracking_store, + &aliases, ))) } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 14a922bc9..aad3054d0 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -16,6 +16,7 @@ use radicle::crypto::ssh::keystore::MemorySigner; use radicle::crypto::ssh::Keystore; use radicle::crypto::{KeyPair, Seed, Signer}; use radicle::git::{raw as git2, RefString}; +use radicle::node::address as AddressStore; use radicle::node::routing as RoutingStore; use radicle::node::tracking::store as TrackingStore; use radicle::profile::Home; @@ -87,8 +88,11 @@ pub fn contributor(dir: &Path) -> Context { fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G) -> Context { let tracking_db = dir.join("radicle").join("node").join("tracking.db"); let routing_db = dir.join("radicle").join("node").join("routing.db"); + let addresses_db = dir.join("radicle").join("node").join("addresses.db"); + TrackingStore::Config::open(tracking_db).unwrap(); RoutingStore::Table::open(routing_db).unwrap(); + AddressStore::Book::open(addresses_db).unwrap(); let workdir = dir.join("hello-world"); From 7e681e7d7c4e50fa47f30b1aa0c5844a6413015e Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 30 May 2023 11:54:07 +0200 Subject: [PATCH 123/264] httpd: Add `validate` fn to auth module Check for AuthState and expiry. Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/auth.rs | 19 ++++++++++++++++++- radicle-httpd/src/api/v1/projects.rs | 24 +++++------------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/radicle-httpd/src/api/auth.rs b/radicle-httpd/src/api/auth.rs index f4c0daba3..745802f86 100644 --- a/radicle-httpd/src/api/auth.rs +++ b/radicle-httpd/src/api/auth.rs @@ -1,8 +1,12 @@ -use radicle::crypto::PublicKey; use serde::{Deserialize, Serialize}; use time::serde::timestamp; use time::{Duration, OffsetDateTime}; +use radicle::crypto::PublicKey; + +use crate::api::error::Error; +use crate::api::Context; + pub const UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60); pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1); @@ -23,3 +27,16 @@ pub struct Session { #[serde(with = "timestamp")] pub expires_at: OffsetDateTime, } + +pub async fn validate(ctx: &Context, token: &str) -> Result<(), Error> { + let sessions_store = ctx.sessions.read().await; + let session = sessions_store + .get(token) + .ok_or(Error::Auth("Unauthorized"))?; + + if session.status != AuthState::Authorized || session.expires_at <= OffsetDateTime::now_utc() { + return Err(Error::Auth("Unauthorized")); + } + + Ok(()) +} diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index bb97dedca..3a4e32e2b 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -64,7 +64,7 @@ pub fn router(ctx: Context) -> Router { ) .route( "/projects/:project/patches/:id", - get(patch_handler).patch(patch_update_handler), + patch(patch_update_handler).get(patch_handler), ) .with_state(ctx) } @@ -481,8 +481,7 @@ async fn issue_create_handler( Path(project): Path, Json(issue): Json, ) -> impl IntoResponse { - let sessions = ctx.sessions.read().await; - sessions.get(&token).ok_or(Error::Auth("Unauthorized"))?; + api::auth::validate(&ctx, &token).await?; let storage = &ctx.profile.storage; let signer = ctx .profile @@ -514,12 +513,7 @@ async fn issue_update_handler( Path((project, issue_id)): Path<(Id, Oid)>, Json(action): Json, ) -> impl IntoResponse { - ctx.sessions - .write() - .await - .get(&token) - .ok_or(Error::Auth("Unauthorized"))?; - + api::auth::validate(&ctx, &token).await?; let storage = &ctx.profile.storage; let signer = ctx.profile.signer().unwrap(); let repo = storage.repository(project)?; @@ -596,11 +590,7 @@ async fn patch_create_handler( Path(project): Path, Json(patch): Json, ) -> impl IntoResponse { - ctx.sessions - .read() - .await - .get(&token) - .ok_or(Error::Auth("Unauthorized"))?; + api::auth::validate(&ctx, &token).await?; let storage = &ctx.profile.storage; let signer = ctx .profile @@ -635,11 +625,7 @@ async fn patch_update_handler( Path((project, patch_id)): Path<(Id, Oid)>, Json(action): Json, ) -> impl IntoResponse { - ctx.sessions - .write() - .await - .get(&token) - .ok_or(Error::Auth("Unauthorized"))?; + api::auth::validate(&ctx, &token).await?; let storage = &ctx.profile.storage; let signer = ctx .profile From 0eeaa7cbe999474a579f4888e34155efeecd949b Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 12 Jun 2023 12:02:09 +0200 Subject: [PATCH 124/264] Fixes and improvements to alias handling * Return the correct alias from a node announcement, stripping '\0' bytes. * Accept `AliasStore` types as references and as owned. * Store a `null` in the database if the alias received is empty. --- radicle-httpd/src/api/v1/projects.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 3a4e32e2b..556c37b5a 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -457,7 +457,7 @@ async fn issues_handler( let aliases = &ctx.profile.aliases()?; let issues = issues .into_iter() - .map(|(id, issue, _)| api::json::issue(id, issue, &aliases)) + .map(|(id, issue, _)| api::json::issue(id, issue, aliases)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -568,7 +568,7 @@ async fn issue_handler( let issue = issue::Issues::open(&repo)? .get(&issue_id.into())? .ok_or(Error::NotFound)?; - let aliases = &ctx.profile.aliases()?; + let aliases = ctx.profile.aliases()?; Ok::<_, Error>(Json(api::json::issue(issue_id.into(), issue, &aliases))) } @@ -720,7 +720,7 @@ async fn patches_handler( }) .collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); - let aliases = &ctx.profile.aliases()?; + let aliases = ctx.profile.aliases()?; let patches = patches .into_iter() .map(|(id, patch, _)| api::json::patch(id, patch, &repo, &aliases)) @@ -742,7 +742,7 @@ async fn patch_handler( let patch = patch::Patches::open(&repo)? .get(&patch_id.into())? .ok_or(Error::NotFound)?; - let aliases = &ctx.profile.aliases()?; + let aliases = ctx.profile.aliases()?; Ok::<_, Error>(Json(api::json::patch( patch_id.into(), From d5e262fd31f84311b5a3cc2136b523e7d6288aad Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 15 Jun 2023 15:58:15 +0200 Subject: [PATCH 125/264] Update `radicle-surf` --- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/api/v1/projects.rs | 102 +++++++++++++++++++-------- 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index f01db8dc6..a16b6f38d 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -25,7 +25,7 @@ flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } nonempty = { version = "0.8.1", features = ["serialize"] } -radicle-surf = { version = "0.11.0", default-features = false, features = ["serde"] } +radicle-surf = { version = "0.12.0", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 556c37b5a..9d864f00c 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -871,6 +871,10 @@ mod routes { ], "eof": "noneMissing", }, + "new": { + "oid": "980a0d5f19a64b4b30a87d4206aade58726b60e3", + "mode": "blob", + }, }, { "path": "dir1/README", @@ -889,7 +893,11 @@ mod routes { } ], "eof": "noneMissing", - } + }, + "new": { + "oid": "1dd5654ca2d2cf9f33b14c92b5ca9e1d21a91ae1", + "mode": "blob", + }, } ], "deleted": [ @@ -911,6 +919,10 @@ mod routes { ], "eof": "noneMissing", }, + "old": { + "oid": "82eb77880c693655bce074e3dbbd9fa711dc018b", + "mode": "blob", + }, }, ], "moved": [], @@ -961,6 +973,10 @@ mod routes { ], "eof": "noneMissing", }, + "new": { + "oid": "82eb77880c693655bce074e3dbbd9fa711dc018b", + "mode": "blob", + }, }, ], "deleted": [ @@ -982,6 +998,10 @@ mod routes { ], "eof": "noneMissing", }, + "old": { + "oid": "980a0d5f19a64b4b30a87d4206aade58726b60e3", + "mode": "blob", + }, }, ], "moved": [], @@ -1031,7 +1051,11 @@ mod routes { } ], "eof": "noneMissing", - } + }, + "new": { + "oid": "980a0d5f19a64b4b30a87d4206aade58726b60e3", + "mode": "blob", + }, } ], "deleted": [], @@ -1087,20 +1111,24 @@ mod routes { { "path": "README", "diff": { - "type": "plain", - "hunks": [ - { - "header": "@@ -0,0 +1 @@\n", - "lines": [ - { - "line": "Hello World!\n", - "lineNo": 1, - "type": "addition", - }, - ], - }, - ], - "eof": "noneMissing", + "type": "plain", + "hunks": [ + { + "header": "@@ -0,0 +1 @@\n", + "lines": [ + { + "line": "Hello World!\n", + "lineNo": 1, + "type": "addition", + }, + ], + }, + ], + "eof": "noneMissing", + }, + "new": { + "oid": "980a0d5f19a64b4b30a87d4206aade58726b60e3", + "mode": "blob", }, }, { @@ -1120,27 +1148,35 @@ mod routes { } ], "eof": "noneMissing", - } + }, + "new": { + "oid": "1dd5654ca2d2cf9f33b14c92b5ca9e1d21a91ae1", + "mode": "blob", + }, } ], "deleted": [ { "path": "CONTRIBUTING", "diff": { - "type": "plain", - "hunks": [ - { - "header": "@@ -1 +0,0 @@\n", - "lines": [ - { - "line": "Thank you very much!\n", - "lineNo": 1, - "type": "deletion", - }, - ], - }, - ], - "eof": "noneMissing", + "type": "plain", + "hunks": [ + { + "header": "@@ -1 +0,0 @@\n", + "lines": [ + { + "line": "Thank you very much!\n", + "lineNo": 1, + "type": "deletion", + }, + ], + }, + ], + "eof": "noneMissing", + }, + "old": { + "oid": "82eb77880c693655bce074e3dbbd9fa711dc018b", + "mode": "blob", }, }, ], @@ -1387,6 +1423,10 @@ mod routes { ], "eof": "noneMissing", }, + "new": { + "oid": "1dd5654ca2d2cf9f33b14c92b5ca9e1d21a91ae1", + "mode": "blob", + }, }, ], "deleted": [], From 6dcc2a31d76ce8b95c76bf05a72001d3ac73521f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 15 Jun 2023 15:36:09 +0200 Subject: [PATCH 126/264] radicle: Change review data in patch COB * `CodeLocation` had a `blob` and `commit` field. These are redundant, since the review lives under a revision. We also change the line range to be diff-compatible, with an old and new range. The ranges are optional to account for different scenarios like file deletion, move etc. * `CodeComment` had redundant getters which were removed in favor of public fields. We also remove the `timestamp` field, as it will be the same as the review timestamp. --- radicle-httpd/src/api/v1/projects.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 9d864f00c..9f97d5bbe 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -2309,16 +2309,11 @@ mod routes { "inline": [ { "location": { - "blob": "82eb77880c693655bce074e3dbbd9fa711dc018b", "path": "./README.md", - "commit": HEAD, - "lines": { - "start": 1, - "end": 3, - }, + "old": null, + "new": null, }, "comment": "This is a comment on line 1", - "timestamp": TIMESTAMP, } ], "timestamp": TIMESTAMP, From f3ae69fae830d42554ba8dd272636aa1f40563b7 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 21 Jun 2023 16:56:06 +0200 Subject: [PATCH 127/264] Improve unit files, add binary install to README After going through the process of getting this working using the unit files, I found a few issues that are fixed here. --- radicle-httpd/radicle-httpd.service | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 radicle-httpd/radicle-httpd.service diff --git a/radicle-httpd/radicle-httpd.service b/radicle-httpd/radicle-httpd.service new file mode 100644 index 000000000..f922e6822 --- /dev/null +++ b/radicle-httpd/radicle-httpd.service @@ -0,0 +1,28 @@ +# Example systemd unit file for `radicle-httpd`. +# +# When running radicle-httpd on a server, it should be run as a separate user. +# +# Copy this file into /etc/systemd/system and set the User/Group parameters +# under [Service] appropriately. +# +# For users wishing to run a configured version of the service, copy this +# service file into the user's systemd directory, and edit appropriately. +# +# For example: +# +# mkdir -p $HOME/.config/systemd/user/ +# cp radicle-node.service $HOME/.config/systemd/user/radicle-node.service +# +[Unit] +After=syslog.target network.target +Description=Radicle HTTPd + +[Service] +ExecStart=%h/.radicle/bin/radicle-httpd +Environment=RAD_HOME=%h/.radicle +KillMode=process +Restart=always +RestartSec=1 + +[Install] +WantedBy=default.target From 628f31caca21e2407ad080463afa708a897e08ab Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 22 Jun 2023 12:13:11 +0200 Subject: [PATCH 128/264] Move unit files to `systemd` directory --- radicle-httpd/radicle-httpd.service | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 radicle-httpd/radicle-httpd.service diff --git a/radicle-httpd/radicle-httpd.service b/radicle-httpd/radicle-httpd.service deleted file mode 100644 index f922e6822..000000000 --- a/radicle-httpd/radicle-httpd.service +++ /dev/null @@ -1,28 +0,0 @@ -# Example systemd unit file for `radicle-httpd`. -# -# When running radicle-httpd on a server, it should be run as a separate user. -# -# Copy this file into /etc/systemd/system and set the User/Group parameters -# under [Service] appropriately. -# -# For users wishing to run a configured version of the service, copy this -# service file into the user's systemd directory, and edit appropriately. -# -# For example: -# -# mkdir -p $HOME/.config/systemd/user/ -# cp radicle-node.service $HOME/.config/systemd/user/radicle-node.service -# -[Unit] -After=syslog.target network.target -Description=Radicle HTTPd - -[Service] -ExecStart=%h/.radicle/bin/radicle-httpd -Environment=RAD_HOME=%h/.radicle -KillMode=process -Restart=always -RestartSec=1 - -[Install] -WantedBy=default.target From bbe0ee26b3eeb48e223261c4d8a4ffb249e2f500 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 26 Jun 2023 23:56:44 +0200 Subject: [PATCH 129/264] cob: Move comment reactions to comment To simplify the code, we move comment reactions to the comment struct itself. --- radicle-httpd/src/api/json.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 1a2dbe622..4cb5c3957 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -11,7 +11,7 @@ use radicle::cob::patch::Merge; use radicle::cob::patch::Review; use radicle::cob::patch::{Patch, PatchId}; use radicle::cob::thread; -use radicle::cob::thread::{CommentId, Thread}; +use radicle::cob::thread::CommentId; use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::git::RefString; use radicle::node::AliasStore; @@ -105,7 +105,7 @@ pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &impl AliasStore) -> Val "assignees": issue.assigned().collect::>(), "discussion": issue .comments() - .map(|(id, comment)| Comment::new(id, comment, issue.thread(), aliases)) + .map(|(id, comment)| Comment::new(id, comment, aliases)) .collect::>(), "tags": issue.tags().collect::>(), }) @@ -136,7 +136,7 @@ pub(crate) fn patch( "oid": rev.head(), "refs": get_refs(repo, patch.author().id(), &rev.head()).unwrap_or(vec![]), "discussions": rev.discussion().comments() - .map(|(id, comment)| Comment::new(id, comment, rev.discussion(), aliases)) + .map(|(id, comment)| Comment::new(id, comment, aliases)) .collect::>(), "timestamp": rev.timestamp(), "reviews": rev.reviews().map(|(nid, _review)| review(nid, aliases.alias(nid), _review)).collect::>(), @@ -245,18 +245,13 @@ struct Comment<'a> { } impl<'a> Comment<'a> { - fn new( - id: &'a CommentId, - comment: &'a thread::Comment, - thread: &'a Thread, - aliases: &impl AliasStore, - ) -> Self { + fn new(id: &'a CommentId, comment: &'a thread::Comment, aliases: &impl AliasStore) -> Self { let comment_author = Author::new(comment.author()); Self { id: *id, author: author(&comment_author, aliases.alias(comment_author.id())), body: comment.body(), - reactions: thread.reactions(id).collect::>(), + reactions: comment.reactions().collect::>(), timestamp: comment.timestamp(), reply_to: comment.reply_to(), } From 622b2735f3d31143375db8e4b67eca8bb95ee3bd Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 28 Jun 2023 12:44:04 +0200 Subject: [PATCH 130/264] cob: Changes to patch code review CRDT This patch incorporates several important changes to code review: * Code review verdict is no longer editable * Made reviews redactable instead * Renamed review top-level comment to "summary" * Renamed inline comments to "comments" * Made inline comments addressable by `EntryId` * This is to allow for editing * Removed inline comments from review creation * Added a `CodeComment` action instead * Removed `Semilattice` instance from `Review` * Added a `EditReview` action instead to have more control * Made it valid for revisions to be redacted concurrently with a review/merge Most of these changes are needed to support proper inline code comments. --- radicle-httpd/src/api/json.rs | 8 ++--- radicle-httpd/src/api/v1/projects.rs | 45 ++++++++++------------------ 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 4cb5c3957..2b1d7dbae 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -188,8 +188,8 @@ fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { "alias": alias, }, "verdict": review.verdict(), - "comment": review.comment(), - "inline": review.inline().collect::>(), + "summary": review.summary(), + "comments": review.comments().collect::>(), "timestamp": review.timestamp(), }), None => json!({ @@ -197,8 +197,8 @@ fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { "id": nid, }, "verdict": review.verdict(), - "comment": review.comment(), - "inline": review.inline().collect::>(), + "summary": review.summary(), + "comments": review.comments().collect::>(), "timestamp": review.timestamp(), }), } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 9f97d5bbe..7c84c4be1 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -644,6 +644,13 @@ async fn patch_update_handler( } => { patch.edit_revision(revision, description, &signer)?; } + patch::Action::EditReview { + revision, + author, + summary, + } => { + patch.edit_review(revision, author, summary, &signer)?; + } patch::Action::Tag { add, remove } => { patch.tag(add, remove, &signer)?; } @@ -662,11 +669,13 @@ async fn patch_update_handler( } patch::Action::Review { revision, - comment, + summary, verdict, - inline, } => { - patch.review(revision, verdict, comment, inline, &signer)?; + patch.review(revision, verdict, summary, &signer)?; + } + patch::Action::CodeComment { .. } => { + todo!() } patch::Action::Merge { revision, commit } => { patch.merge(revision, commit, &signer)?; @@ -2237,23 +2246,8 @@ mod routes { let thread_body = serde_json::to_vec(&json!({ "type": "review", "revision": CONTRIBUTOR_PATCH_ID, - "comment": "A small review", + "summary": "A small review", "verdict": "accept", - "inline": [ - { - "location": { - "blob": "82eb77880c693655bce074e3dbbd9fa711dc018b", - "path": "./README.md", - "commit": HEAD, - "lines": { - "start": 1, - "end": 3, - }, - }, - "comment": "This is a comment on line 1", - "timestamp": TIMESTAMP, - } - ] })) .unwrap(); let response = patch( @@ -2305,17 +2299,8 @@ mod routes { "id": CONTRIBUTOR_NID, }, "verdict": "accept", - "comment": "A small review", - "inline": [ - { - "location": { - "path": "./README.md", - "old": null, - "new": null, - }, - "comment": "This is a comment on line 1", - } - ], + "summary": "A small review", + "comments": [], "timestamp": TIMESTAMP, }, ], From b23d15d59819dc3aef431fec80bbaaf7e86ef455 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 28 Jun 2023 13:31:08 +0200 Subject: [PATCH 131/264] cob: Implement review code commenting --- radicle-httpd/src/api/v1/projects.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 7c84c4be1..b80dc7df2 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -644,12 +644,11 @@ async fn patch_update_handler( } => { patch.edit_revision(revision, description, &signer)?; } - patch::Action::EditReview { - revision, - author, - summary, - } => { - patch.edit_review(revision, author, summary, &signer)?; + patch::Action::EditReview { review, summary } => { + patch.edit_review(review, summary, &signer)?; + } + patch::Action::EditCodeComment { .. } => { + todo!() } patch::Action::Tag { add, remove } => { patch.tag(add, remove, &signer)?; From 5103d9cedda4eae3b72fbfe987fee0e66642fc29 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 29 Jun 2023 15:39:52 +0200 Subject: [PATCH 132/264] httpd: Implement `EditCodeComment` in route --- radicle-httpd/src/api/v1/projects.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index b80dc7df2..2a4612b90 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -647,8 +647,12 @@ async fn patch_update_handler( patch::Action::EditReview { review, summary } => { patch.edit_review(review, summary, &signer)?; } - patch::Action::EditCodeComment { .. } => { - todo!() + patch::Action::EditCodeComment { + review, + comment, + body, + } => { + patch.edit_code_comment(review, comment, body, &signer)?; } patch::Action::Tag { add, remove } => { patch.tag(add, remove, &signer)?; From 2496a1d93d88a921c5c210fcd38c2d80ff9da66c Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 4 Jul 2023 00:26:10 +0200 Subject: [PATCH 133/264] node: Make node aliases required This is a fairly substantial change, which adds a new configuration file to the user's `$RAD_HOME` that includes node configuration, including the alias, and also makes node aliases required. When running `rad auth`, the user is now prompted for an alias, which defaults to `$USER`. When running `rad self`, the alias is now shown. If the user runs radicle without a config file, the defaults are loaded, and `$USER` is used as the alias. --- radicle-httpd/src/api/json.rs | 8 ++++---- radicle-httpd/src/test.rs | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 2b1d7dbae..3fa310ccb 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -14,7 +14,7 @@ use radicle::cob::thread; use radicle::cob::thread::CommentId; use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::git::RefString; -use radicle::node::AliasStore; +use radicle::node::{Alias, AliasStore}; use radicle::prelude::NodeId; use radicle::storage::{git, refs, ReadRepository}; use radicle_surf::blob::Blob; @@ -146,7 +146,7 @@ pub(crate) fn patch( } /// Returns JSON for an `author` and fills in `alias` when present. -fn author(author: &Author, alias: Option) -> Value { +fn author(author: &Author, alias: Option) -> Value { match alias { Some(alias) => json!({ "id": author.id, @@ -157,7 +157,7 @@ fn author(author: &Author, alias: Option) -> Value { } /// Returns JSON for a patch `Merge` and fills in `alias` when present. -fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> Value { +fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> Value { match alias { Some(alias) => json!({ "author": { @@ -180,7 +180,7 @@ fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> Value { } /// Returns JSON for a patch `Review` and fills in `alias` when present. -fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { +fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { match alias { Some(alias) => json!({ "author": { diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index aad3054d0..82967df09 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -16,9 +16,11 @@ use radicle::crypto::ssh::keystore::MemorySigner; use radicle::crypto::ssh::Keystore; use radicle::crypto::{KeyPair, Seed, Signer}; use radicle::git::{raw as git2, RefString}; +use radicle::node; use radicle::node::address as AddressStore; use radicle::node::routing as RoutingStore; use radicle::node::tracking::store as TrackingStore; +use radicle::profile; use radicle::profile::Home; use radicle::storage::ReadStorage; use radicle::Storage; @@ -63,6 +65,9 @@ pub fn profile(home: &Path, seed: [u8; 32]) -> radicle::Profile { storage, keystore, public_key: keypair.pk.into(), + config: profile::Config { + node: node::Config::new(node::Alias::new("seed")), + }, } } From ac8112a9a00bb485fc3318ff9a2eaff95782baaa Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 12 Jul 2023 11:33:43 +0200 Subject: [PATCH 134/264] Don't fail if we can't open the database We make sure that even if there is no database file, we're still able to return `None` for the alias lookup. --- radicle-httpd/src/api/v1/projects.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 2a4612b90..c2a4b7ded 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -324,7 +324,7 @@ async fn remotes_handler(State(ctx): State, Path(project): Path) -> let storage = &ctx.profile.storage; let repo = storage.repository(project)?; let delegates = repo.delegates()?; - let aliases = &ctx.profile.aliases()?; + let aliases = &ctx.profile.aliases(); let remotes = repo .remotes()? .filter_map(|r| r.map(|r| r.1).ok()) @@ -454,7 +454,7 @@ async fn issues_handler( .collect::>(); issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); - let aliases = &ctx.profile.aliases()?; + let aliases = &ctx.profile.aliases(); let issues = issues .into_iter() .map(|(id, issue, _)| api::json::issue(id, issue, aliases)) @@ -568,7 +568,7 @@ async fn issue_handler( let issue = issue::Issues::open(&repo)? .get(&issue_id.into())? .ok_or(Error::NotFound)?; - let aliases = ctx.profile.aliases()?; + let aliases = ctx.profile.aliases(); Ok::<_, Error>(Json(api::json::issue(issue_id.into(), issue, &aliases))) } @@ -732,7 +732,7 @@ async fn patches_handler( }) .collect::>(); patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); - let aliases = ctx.profile.aliases()?; + let aliases = ctx.profile.aliases(); let patches = patches .into_iter() .map(|(id, patch, _)| api::json::patch(id, patch, &repo, &aliases)) @@ -754,7 +754,7 @@ async fn patch_handler( let patch = patch::Patches::open(&repo)? .get(&patch_id.into())? .ok_or(Error::NotFound)?; - let aliases = ctx.profile.aliases()?; + let aliases = ctx.profile.aliases(); Ok::<_, Error>(Json(api::json::patch( patch_id.into(), From e41739187a29ceec64cdd6a96c9fb81f714c82e3 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 22 Jun 2023 13:01:18 +0200 Subject: [PATCH 135/264] Update `radicle-surf` and `git-ref-format` --- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/api/v1/projects.rs | 86 +++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index a16b6f38d..979401f18 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -25,7 +25,7 @@ flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } nonempty = { version = "0.8.1", features = ["serialize"] } -radicle-surf = { version = "0.12.0", default-features = false, features = ["serde"] } +radicle-surf = { version = "0.14.0", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index c2a4b7ded..b362300d2 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -879,6 +879,14 @@ mod routes { "type": "addition", }, ], + "old": { + "start": 0, + "end": 0, + }, + "new": { + "start": 1, + "end": 2, + }, }, ], "eof": "noneMissing", @@ -901,7 +909,15 @@ mod routes { "lineNo": 1, "type": "addition" } - ] + ], + "old": { + "start": 0, + "end": 0, + }, + "new": { + "start": 1, + "end": 2, + }, } ], "eof": "noneMissing", @@ -927,6 +943,14 @@ mod routes { "type": "deletion", }, ], + "old": { + "start": 1, + "end": 2, + }, + "new": { + "start": 0, + "end": 0, + }, }, ], "eof": "noneMissing", @@ -981,6 +1005,14 @@ mod routes { "type": "addition", }, ], + "old": { + "start": 0, + "end": 0, + }, + "new": { + "start": 1, + "end": 2, + }, }, ], "eof": "noneMissing", @@ -1006,6 +1038,14 @@ mod routes { "type": "deletion", }, ], + "old": { + "start": 1, + "end": 2, + }, + "new": { + "start": 0, + "end": 0, + }, }, ], "eof": "noneMissing", @@ -1059,7 +1099,15 @@ mod routes { "lineNo": 1, "type": "addition" } - ] + ], + "old": { + "start": 0, + "end": 0, + }, + "new": { + "start": 1, + "end": 2, + }, } ], "eof": "noneMissing", @@ -1134,6 +1182,14 @@ mod routes { "type": "addition", }, ], + "old": { + "start": 0, + "end": 0, + }, + "new": { + "start": 1, + "end": 2, + }, }, ], "eof": "noneMissing", @@ -1156,7 +1212,15 @@ mod routes { "lineNo": 1, "type": "addition" } - ] + ], + "old": { + "start": 0, + "end": 0, + }, + "new": { + "start": 1, + "end": 2, + }, } ], "eof": "noneMissing", @@ -1182,6 +1246,14 @@ mod routes { "type": "deletion", }, ], + "old": { + "start": 1, + "end": 2, + }, + "new": { + "start": 0, + "end": 0, + }, }, ], "eof": "noneMissing", @@ -1431,6 +1503,14 @@ mod routes { "type": "addition", }, ], + "old": { + "start": 0, + "end": 0, + }, + "new": { + "start": 1, + "end": 2, + }, }, ], "eof": "noneMissing", From 7e211ab1dfd15021905142fa4678670ad8805f3d Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Sat, 15 Jul 2023 14:18:52 +0200 Subject: [PATCH 136/264] cli: Make key encryption optional By leaving the passphrase prompt blank, users can create unencrypted keys. --- radicle-httpd/src/test.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 82967df09..7438dee26 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -46,8 +46,6 @@ pub const CONTRIBUTOR_PATCH_ID: &str = "044b577cc7551cd09d4b2f03566a553762180de4 pub const CONTRIBUTOR_COMMENT_1: &str = "92aab76516ae7f60a9b2952043ba578383de7d46"; pub const CONTRIBUTOR_COMMENT_2: &str = "cb360eee0ec70563d5c4c3613fdc076648523248"; -const PASSWORD: &str = "radicle"; - /// Create a new profile. pub fn profile(home: &Path, seed: [u8; 32]) -> radicle::Profile { let home = Home::new(home).unwrap(); @@ -56,9 +54,7 @@ pub fn profile(home: &Path, seed: [u8; 32]) -> radicle::Profile { let keypair = KeyPair::from_seed(Seed::from(seed)); radicle::storage::git::transport::local::register(storage.clone()); - keystore - .store(keypair.clone(), "radicle", PASSWORD.to_owned()) - .unwrap(); + keystore.store(keypair.clone(), "radicle", None).unwrap(); radicle::Profile { home, @@ -85,7 +81,7 @@ pub fn contributor(dir: &Path) -> Context { let home = dir.join("radicle"); let profile = profile(home.as_path(), seed); - let signer = MemorySigner::load(&profile.keystore, PASSWORD.to_owned().into()).unwrap(); + let signer = MemorySigner::load(&profile.keystore, None).unwrap(); seed_with_signer(dir, profile, &signer) } @@ -102,7 +98,6 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G let workdir = dir.join("hello-world"); env::set_var("RAD_COMMIT_TIME", TIMESTAMP.to_string()); - env::set_var("RAD_PASSPHRASE", PASSWORD); fs::create_dir_all(&workdir).unwrap(); From ed62ac4264514e9e5a8f1852af4982376a369e6e Mon Sep 17 00:00:00 2001 From: xphoniex Date: Thu, 20 Jul 2023 11:34:36 +0000 Subject: [PATCH 137/264] Add `-h` flag to binaries Signed-off-by: xphoniex --- radicle-httpd/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/main.rs b/radicle-httpd/src/main.rs index 2d4c07593..065a54bc4 100644 --- a/radicle-httpd/src/main.rs +++ b/radicle-httpd/src/main.rs @@ -64,7 +64,7 @@ fn parse_options() -> Result { aliases.insert(alias, id); } - Long("help") => { + Long("help") | Short('h') => { println!("usage: radicle-httpd [--listen ] [--alias ].."); process::exit(0); } From 3fcadc505875ef57f77260511184db283b8d651b Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Sat, 22 Jul 2023 07:44:52 +0200 Subject: [PATCH 138/264] httpd: Fix commit count in repo stats in history route Instead of using always the repo HEAD for the commit count with `repo::stats` we should use `repo::stats_from` and pass the `parent` commit. This way we allow e.g. a commit history for a peer with lesser commits to be counted correctly. Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/v1/projects.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index b362300d2..95574405c 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -206,7 +206,7 @@ async fn history_handler( let response = json!({ "commits": commits, - "stats": repo.stats()?, + "stats": repo.stats_from(&sha)?, }); Ok::<_, Error>((StatusCode::OK, Json(response))) From 2e25250be44a687e820c41e6adf64dab98d53e88 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Wed, 12 Jul 2023 17:43:11 +0000 Subject: [PATCH 139/264] httpd: Add a cache for `/tree` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This improves response time of: - GET /api/v1/projects//tree// from 205.71ms down to 376.4µs - GET /api/v1/projects//tree//.nix from 111.06ms down to 351.70µs Signed-off-by: xphoniex --- radicle-httpd/Cargo.toml | 1 + radicle-httpd/src/api.rs | 7 ++++++- radicle-httpd/src/api/v1/projects.rs | 12 ++++++++++++ radicle-httpd/src/cache.rs | 22 ++++++++++++++++++++++ radicle-httpd/src/lib.rs | 9 ++++++++- radicle-httpd/src/main.rs | 9 ++++++++- radicle-httpd/src/test.rs | 8 +++++++- 7 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 radicle-httpd/src/cache.rs diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 979401f18..72dbbbe9e 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -24,6 +24,7 @@ fastrand = { version = "1.9.0" } flate2 = { version = "1" } hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } +lru = { version = "0.10.0" } nonempty = { version = "0.8.1", features = ["serialize"] } radicle-surf = { version = "0.14.0", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index a4a71919c..e39d05857 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -25,6 +25,9 @@ mod error; mod json; mod v1; +use crate::cache::Cache; +use crate::Options; + pub const VERSION: &str = env!("CARGO_PKG_VERSION"); /// Identifier for sessions @@ -34,13 +37,15 @@ type SessionId = String; pub struct Context { profile: Arc, sessions: Arc>>, + cache: Option, } impl Context { - pub fn new(profile: Arc) -> Self { + pub fn new(profile: Arc, options: &Options) -> Self { Self { profile, sessions: Default::default(), + cache: options.cache.map(Cache::new), } } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 95574405c..67e7bfd00 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -309,12 +309,24 @@ async fn tree_handler( State(ctx): State, Path((project, sha, path)): Path<(Id, Oid, String)>, ) -> impl IntoResponse { + if let Some(ref cache) = ctx.cache { + let cache = &mut cache.tree.lock().await; + if let Some(response) = cache.get(&(project, sha, path.clone())) { + return Ok::<_, Error>(Json(response.clone())); + } + } + let storage = &ctx.profile.storage; let repo = Repository::open(paths::repository(storage, &project))?; let tree = repo.tree(sha, &path)?; let stats = repo.stats_from(&sha)?; let response = api::json::tree(&tree, &path, &stats); + if let Some(cache) = ctx.cache { + let cache = &mut cache.tree.lock().await; + cache.put((project, sha, path.clone()), response.clone()); + } + Ok::<_, Error>(Json(response)) } diff --git a/radicle-httpd/src/cache.rs b/radicle-httpd/src/cache.rs new file mode 100644 index 000000000..172b3e03c --- /dev/null +++ b/radicle-httpd/src/cache.rs @@ -0,0 +1,22 @@ +use std::num::NonZeroUsize; +use std::sync::Arc; + +use lru::LruCache; +use tokio::sync::Mutex; + +use radicle::prelude::Id; +use radicle_surf::Oid; + +#[derive(Clone)] +pub struct Cache { + pub tree: Arc>>, +} + +impl Cache { + /// Creates a new cache of the given size. + pub fn new(size: NonZeroUsize) -> Self { + Cache { + tree: Arc::new(Mutex::new(LruCache::new(size))), + } + } +} diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 888ecdb15..1271912f6 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -4,6 +4,7 @@ pub mod error; use std::collections::HashMap; use std::net::SocketAddr; +use std::num::NonZeroUsize; use std::process::Command; use std::str; use std::sync::Arc; @@ -24,16 +25,21 @@ use tracing_extra::{tracing_middleware, ColoredStatus, Paint, RequestId, Tracing mod api; mod axum_extra; +mod cache; mod git; mod raw; #[cfg(test)] mod test; mod tracing_extra; +/// Default cache HTTP size. +pub const DEFAULT_CACHE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(100) }; + #[derive(Debug, Clone)] pub struct Options { pub aliases: HashMap, pub listen: SocketAddr, + pub cache: Option, } /// Run the Server. @@ -101,7 +107,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> { /// Create a router consisting of other sub-routers. fn router(options: Options, profile: Profile) -> anyhow::Result { let profile = Arc::new(profile); - let ctx = api::Context::new(profile.clone()); + let ctx = api::Context::new(profile.clone(), &options); let api_router = api::router(ctx); let git_router = git::router(profile.clone(), options.aliases); @@ -132,6 +138,7 @@ mod routes { super::Options { aliases: HashMap::new(), listen: SocketAddr::from(([0, 0, 0, 0], 8080)), + cache: None, }, test::profile(tmp.path(), [0xff; 32]), ) diff --git a/radicle-httpd/src/main.rs b/radicle-httpd/src/main.rs index 065a54bc4..374fd088f 100644 --- a/radicle-httpd/src/main.rs +++ b/radicle-httpd/src/main.rs @@ -1,3 +1,4 @@ +use std::num::NonZeroUsize; use std::{collections::HashMap, process}; use radicle::prelude::Id; @@ -51,6 +52,7 @@ fn parse_options() -> Result { let mut parser = lexopt::Parser::from_env(); let mut listen = None; let mut aliases = HashMap::new(); + let mut cache = Some(httpd::DEFAULT_CACHE_SIZE); while let Some(arg) = parser.next()? { match arg { @@ -64,8 +66,12 @@ fn parse_options() -> Result { aliases.insert(alias, id); } + Long("cache") => { + let size = parser.value()?.parse()?; + cache = NonZeroUsize::new(size); + } Long("help") | Short('h') => { - println!("usage: radicle-httpd [--listen ] [--alias ].."); + println!("usage: radicle-httpd [--listen ] [--alias ] [--cache ].."); process::exit(0); } _ => return Err(arg.unexpected()), @@ -74,5 +80,6 @@ fn parse_options() -> Result { Ok(httpd::Options { aliases, listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8080).into()), + cache, }) } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 7438dee26..72b11e40f 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -203,7 +203,13 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G ) .unwrap(); - Context::new(Arc::new(profile)) + let options = crate::Options { + aliases: std::collections::HashMap::new(), + listen: std::net::SocketAddr::from(([0, 0, 0, 0], 8080)), + cache: Some(crate::DEFAULT_CACHE_SIZE), + }; + + Context::new(Arc::new(profile), &options) } /// Adds an authorized session to the Context::sessions HashMap. From 1dc799d679258663de2bd7d2dc31e9f708ab61f4 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 13 Jul 2023 13:21:15 +0200 Subject: [PATCH 140/264] cob: Remove CRDTs from COB state It turns out that the CRDT formed by the union of Git DAGs is enough to guarantee everything we need for COBs. This changes the following things: * COB operations no longer need to be commutative * COB histories are traversed in the same deterministic order on all replicas * It's now possible to implement RSMs on top of COBs, eg. scripting * Lamport clocks have been removed * `radicle-crdt` is no longer a dependency of `radicle` * COBs are no longer instances of `Semilattice` * The `Ops` type was removed in favor of having `Op` contain multiple actions --- radicle-httpd/src/api/json.rs | 11 ++++++----- radicle-httpd/src/api/v1/projects.rs | 16 ++++++++-------- radicle-httpd/src/test.rs | 4 ++-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 3fa310ccb..7408d7fa2 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -138,7 +138,7 @@ pub(crate) fn patch( "discussions": rev.discussion().comments() .map(|(id, comment)| Comment::new(id, comment, aliases)) .collect::>(), - "timestamp": rev.timestamp(), + "timestamp": rev.timestamp().as_secs().to_string(), "reviews": rev.reviews().map(|(nid, _review)| review(nid, aliases.alias(nid), _review)).collect::>(), }) }).collect::>(), @@ -165,7 +165,7 @@ fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> Value { "alias": alias, }, "commit": merge.commit, - "timestamp": merge.timestamp, + "timestamp": merge.timestamp.as_secs().to_string(), "revision": merge.revision, }), None => json!({ @@ -173,7 +173,7 @@ fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> Value { "id": nid, }, "commit": merge.commit, - "timestamp": merge.timestamp, + "timestamp": merge.timestamp.as_secs().to_string(), "revision": merge.revision, }), } @@ -190,7 +190,7 @@ fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { "verdict": review.verdict(), "summary": review.summary(), "comments": review.comments().collect::>(), - "timestamp": review.timestamp(), + "timestamp": review.timestamp().as_secs().to_string(), }), None => json!({ "author": { @@ -199,7 +199,7 @@ fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { "verdict": review.verdict(), "summary": review.summary(), "comments": review.comments().collect::>(), - "timestamp": review.timestamp(), + "timestamp": review.timestamp().as_secs().to_string(), }), } } @@ -240,6 +240,7 @@ struct Comment<'a> { author: Value, body: &'a str, reactions: Vec<(&'a ActorId, &'a Reaction)>, + #[serde(with = "radicle::serde_ext::localtime::time")] timestamp: Timestamp, reply_to: Option, } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 67e7bfd00..910d002ac 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -460,16 +460,16 @@ async fn issues_handler( let mut issues: Vec<_> = issues .all()? .filter_map(|r| { - let (id, issue, clock) = r.ok()?; - (state.matches(issue.state())).then_some((id, issue, clock)) + let (id, issue) = r.ok()?; + (state.matches(issue.state())).then_some((id, issue)) }) .collect::>(); - issues.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); + issues.sort_by(|(_, a), (_, b)| b.timestamp().cmp(&a.timestamp())); let aliases = &ctx.profile.aliases(); let issues = issues .into_iter() - .map(|(id, issue, _)| api::json::issue(id, issue, aliases)) + .map(|(id, issue)| api::json::issue(id, issue, aliases)) .skip(page * per_page) .take(per_page) .collect::>(); @@ -739,15 +739,15 @@ async fn patches_handler( let mut patches = patches .all()? .filter_map(|r| { - let (id, patch, clock) = r.ok()?; - (state.matches(patch.state())).then_some((id, patch, clock)) + let (id, patch) = r.ok()?; + (state.matches(patch.state())).then_some((id, patch)) }) .collect::>(); - patches.sort_by(|(_, a, _), (_, b, _)| b.timestamp().cmp(&a.timestamp())); + patches.sort_by(|(_, a), (_, b)| b.timestamp().cmp(&a.timestamp())); let aliases = ctx.profile.aliases(); let patches = patches .into_iter() - .map(|(id, patch, _)| api::json::patch(id, patch, &repo, &aliases)) + .map(|(id, patch)| api::json::patch(id, patch, &repo, &aliases)) .skip(page * per_page) .take(per_page) .collect::>(); diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 72b11e40f..5191520df 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -37,7 +37,7 @@ pub const ISSUE_ID: &str = "5ad77fa3f476beed9a26f49b2b3b844e61bef792"; pub const ISSUE_DISCUSSION_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; pub const ISSUE_COMMENT_ID: &str = "845218041bf9eb8155bfa4aaa8f0c91ce18e5c13"; pub const SESSION_ID: &str = "u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"; -pub const TIMESTAMP: u64 = 1671125284; +pub const TIMESTAMP: &str = "1671125284"; pub const CONTRIBUTOR_RID: &str = "rad:z4XaCmN3jLSeiMvW15YTDpNbDHFhG"; pub const CONTRIBUTOR_DID: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; pub const CONTRIBUTOR_NID: &str = "z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; @@ -97,7 +97,7 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G let workdir = dir.join("hello-world"); - env::set_var("RAD_COMMIT_TIME", TIMESTAMP.to_string()); + env::set_var("RAD_COMMIT_TIME", TIMESTAMP); fs::create_dir_all(&workdir).unwrap(); From a0c578604e1c94feee7bd2aa93a8de61ead27306 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 24 Jul 2023 10:42:46 +0200 Subject: [PATCH 141/264] httpd: Add commit parents vector to `/commits/:sha` endpoint So we can act upon the eventual parents of a commit, after fetching a single commit we need to have the parents of that commit as part of the json payload. Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api/json.rs | 1 + radicle-httpd/src/api/v1/projects.rs | 36 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index 7408d7fa2..b085e23e0 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -33,6 +33,7 @@ pub(crate) fn commit(commit: &Commit) -> Value { }, "summary": commit.summary, "description": commit.description(), + "parents": commit.parents, "committer": { "name": commit.committer.name, "email": commit.committer.email, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index 910d002ac..fd3d7c3ce 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -869,6 +869,9 @@ mod routes { }, "summary": "Add another folder", "description": "", + "parents": [ + "ee8d6a29304623a78ebfa5eeed5af674d0e58f83", + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -995,6 +998,9 @@ mod routes { }, "summary": "Add contributing file", "description": "", + "parents": [ + "f604ce9fd5b7cc77b7609beda45ea8760bee78f7", + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -1090,6 +1096,7 @@ mod routes { }, "summary": "Initial commit", "description": "", + "parents": [], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -1172,6 +1179,9 @@ mod routes { }, "summary": "Add another folder", "description": "", + "parents": [ + "ee8d6a29304623a78ebfa5eeed5af674d0e58f83", + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -1322,6 +1332,9 @@ mod routes { }, "summary": "Add another folder", "description": "", + "parents": [ + "ee8d6a29304623a78ebfa5eeed5af674d0e58f83", + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -1331,8 +1344,8 @@ mod routes { "name": "", "path": "", "stats": { - "commits": 3, "branches": 1, + "commits": 3, "contributors": 1 } } @@ -1360,6 +1373,9 @@ mod routes { }, "summary": "Add another folder", "description": "", + "parents": [ + "ee8d6a29304623a78ebfa5eeed5af674d0e58f83", + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -1369,8 +1385,8 @@ mod routes { "name": "dir1", "path": "dir1", "stats": { - "branches": 1, "commits": 3, + "branches": 1, "contributors": 1 } }) @@ -1442,6 +1458,9 @@ mod routes { }, "summary": "Add another folder", "description": "", + "parents": [ + "ee8d6a29304623a78ebfa5eeed5af674d0e58f83" + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -1464,7 +1483,8 @@ mod routes { response.json().await, json!({ "binary": false, - "content": "Hello World!\n", + "name": "README", + "path": "README", "lastCommit": { "id": INITIAL_COMMIT, "author": { @@ -1473,14 +1493,14 @@ mod routes { }, "summary": "Initial commit", "description": "", + "parents": [], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", "time": 1673001014 }, }, - "name": "README", - "path": "README" + "content": "Hello World!\n" }) ); } @@ -1552,6 +1572,9 @@ mod routes { }, "summary": "Add another folder", "description": "", + "parents": [ + "ee8d6a29304623a78ebfa5eeed5af674d0e58f83" + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", @@ -1566,6 +1589,9 @@ mod routes { }, "summary": "Add contributing file", "description": "", + "parents": [ + "f604ce9fd5b7cc77b7609beda45ea8760bee78f7", + ], "committer": { "name": "Alice Liddell", "email": "alice@radicle.xyz", From d45cce1df5521e6f8771d31e742c35c15b2c42df Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 27 Jul 2023 18:30:30 +0200 Subject: [PATCH 142/264] Use numbers for `LocalTime` timestamps Instead of using strings, use numbers. *This is a breaking change and affects `config.json` parsing.* To upgrade, simply unquote any number in your `config.json`. --- radicle-httpd/src/api/json.rs | 10 +++++----- radicle-httpd/src/test.rs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index b085e23e0..d3b9183da 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -139,7 +139,7 @@ pub(crate) fn patch( "discussions": rev.discussion().comments() .map(|(id, comment)| Comment::new(id, comment, aliases)) .collect::>(), - "timestamp": rev.timestamp().as_secs().to_string(), + "timestamp": rev.timestamp().as_secs(), "reviews": rev.reviews().map(|(nid, _review)| review(nid, aliases.alias(nid), _review)).collect::>(), }) }).collect::>(), @@ -166,7 +166,7 @@ fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> Value { "alias": alias, }, "commit": merge.commit, - "timestamp": merge.timestamp.as_secs().to_string(), + "timestamp": merge.timestamp.as_secs(), "revision": merge.revision, }), None => json!({ @@ -174,7 +174,7 @@ fn merge(merge: &Merge, nid: &NodeId, alias: Option) -> Value { "id": nid, }, "commit": merge.commit, - "timestamp": merge.timestamp.as_secs().to_string(), + "timestamp": merge.timestamp.as_secs(), "revision": merge.revision, }), } @@ -191,7 +191,7 @@ fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { "verdict": review.verdict(), "summary": review.summary(), "comments": review.comments().collect::>(), - "timestamp": review.timestamp().as_secs().to_string(), + "timestamp": review.timestamp().as_secs(), }), None => json!({ "author": { @@ -200,7 +200,7 @@ fn review(nid: &NodeId, alias: Option, review: &Review) -> Value { "verdict": review.verdict(), "summary": review.summary(), "comments": review.comments().collect::>(), - "timestamp": review.timestamp().as_secs().to_string(), + "timestamp": review.timestamp().as_secs(), }), } } diff --git a/radicle-httpd/src/test.rs b/radicle-httpd/src/test.rs index 5191520df..72b11e40f 100644 --- a/radicle-httpd/src/test.rs +++ b/radicle-httpd/src/test.rs @@ -37,7 +37,7 @@ pub const ISSUE_ID: &str = "5ad77fa3f476beed9a26f49b2b3b844e61bef792"; pub const ISSUE_DISCUSSION_ID: &str = "f1dff128a22e8183a23516dd9812e72e80914c92"; pub const ISSUE_COMMENT_ID: &str = "845218041bf9eb8155bfa4aaa8f0c91ce18e5c13"; pub const SESSION_ID: &str = "u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"; -pub const TIMESTAMP: &str = "1671125284"; +pub const TIMESTAMP: u64 = 1671125284; pub const CONTRIBUTOR_RID: &str = "rad:z4XaCmN3jLSeiMvW15YTDpNbDHFhG"; pub const CONTRIBUTOR_DID: &str = "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; pub const CONTRIBUTOR_NID: &str = "z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"; @@ -97,7 +97,7 @@ fn seed_with_signer(dir: &Path, profile: radicle::Profile, signer: &G let workdir = dir.join("hello-world"); - env::set_var("RAD_COMMIT_TIME", TIMESTAMP); + env::set_var("RAD_COMMIT_TIME", TIMESTAMP.to_string()); fs::create_dir_all(&workdir).unwrap(); From df2251f6ee44ac6b15d0500f4130b3c7aa7604b4 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 26 Jul 2023 23:01:50 +0200 Subject: [PATCH 143/264] httpd: Add socketAddress to `/node` endpoint This allows us in the web client to show and let user copy the node id together with the correct port number Signed-off-by: Sebastian Martinez --- radicle-httpd/src/api.rs | 3 +++ radicle-httpd/src/api/v1/node.rs | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index e39d05857..0d7bb5de1 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -1,6 +1,7 @@ pub mod auth; use std::collections::HashMap; +use std::net::SocketAddr; use std::sync::Arc; use std::time::Duration; @@ -38,6 +39,7 @@ pub struct Context { profile: Arc, sessions: Arc>>, cache: Option, + address: SocketAddr, } impl Context { @@ -46,6 +48,7 @@ impl Context { profile, sessions: Default::default(), cache: options.cache.map(Cache::new), + address: options.listen, } } diff --git a/radicle-httpd/src/api/v1/node.rs b/radicle-httpd/src/api/v1/node.rs index 1dfea6132..65a08a692 100644 --- a/radicle-httpd/src/api/v1/node.rs +++ b/radicle-httpd/src/api/v1/node.rs @@ -1,3 +1,5 @@ +use std::net::SocketAddr; + use axum::extract::State; use axum::response::IntoResponse; use axum::routing::get; @@ -10,17 +12,19 @@ use crate::api::Context; pub fn router(ctx: Context) -> Router { let node_id = ctx.profile.public_key; + let address = ctx.address; Router::new() .route("/node", get(node_handler)) - .with_state(node_id) + .with_state((node_id, address)) } /// Return the node id for the node identity. /// `GET /node` -async fn node_handler(State(node_id): State) -> impl IntoResponse { +async fn node_handler(State((node_id, address)): State<(NodeId, SocketAddr)>) -> impl IntoResponse { let response = json!({ "id": node_id.to_string(), + "address": address.to_string(), }); Json(response) From 1e4c35497239519c2cd2409e65b34e0dba96e503 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Thu, 27 Jul 2023 07:40:06 -0500 Subject: [PATCH 144/264] docker: Make Dockerfile easier to read Avoid long lines by separating commands and arguments with '\'. --- radicle-httpd/Dockerfile | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/radicle-httpd/Dockerfile b/radicle-httpd/Dockerfile index e87728cbd..51c9aeebe 100644 --- a/radicle-httpd/Dockerfile +++ b/radicle-httpd/Dockerfile @@ -6,15 +6,25 @@ COPY . /workdir RUN rustup target add x86_64-unknown-linux-musl WORKDIR /workdir/radicle-httpd -RUN RUSTFLAGS=-Cforce-frame-pointers=yes cargo build --profile=container --target=x86_64-unknown-linux-musl -RUN objcopy --compress-debug-sections /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd.compressed +RUN RUSTFLAGS=-Cforce-frame-pointers=yes \ + cargo build \ + --profile=container \ + --target=x86_64-unknown-linux-musl +RUN objcopy \ + --compress-debug-sections \ + /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd \ + /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd.compressed # Run FROM debian:bullseye-slim@sha256:25f10b4f1ded5341a3ca0a30290ff3cd5639415f0c5a2222d5e7d5dd72952aa1 RUN echo deb http://deb.debian.org/debian bullseye-backports main contrib non-free >/etc/apt/sources.list.d/backports.list -RUN apt-get update && apt -t bullseye-backports install --yes git && rm -rf /var/lib/apt/lists/* -COPY --from=build /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd.compressed /usr/local/bin/radicle-httpd +RUN apt-get update && \ + apt -t bullseye-backports install --yes git && \ + rm -rf /var/lib/apt/lists/* +COPY --from=build \ + /workdir/target/x86_64-unknown-linux-musl/container/radicle-httpd.compressed \ + /usr/local/bin/radicle-httpd WORKDIR /app From 3ddec3dd7dc3dcf74ef826024f54c3023ad1e0b9 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 26 Jul 2023 12:32:20 +0200 Subject: [PATCH 145/264] cob: Stabilize formats > I'm going to be making a set of breaking changes to COBs in order to stabilize the data formats. This is hopefully a one-time change that bundles various breaking changes. All COBs have been reworked: issue, patch, id. The changes included are: * Revise the assign and tag actions to take a single list of assignees/tags to set, instead of an "add" and a "remove" list. This makes API usage simpler when editing issues, and simplifies the apply function * Rename "tags" to "labels", and the tag action to label. This is because tag is confusing in the context of git, as it could mean a git tag. Using label removes that confusion. * Use DIDs instead of PublicKeys for assignees -- this is more future-proof * Modify the manifest file format in the COB tree. Mainly, remove the `history_type` key which is redundant, and use camelCase for keys * Flatten the `Thread` actions into the parent action type * Ensure that operations on redacted objects do not fail, since redactions could have happened concurrently * Use a consistent naming scheme for actions, using `.` as separator * Consolidate comment types and remove `CodeComment`, by adding an optional `location` field to `Comment` * Add many placeholder actions that are not yet implemented To preserve backwards compatibility, a `legacy` module is created with the old `apply` function. When loading the manifest, we check whether it is a legacy COB or a "stable" COB, and in the legacy case, use the legacy code to materialize the state and then convert the object into the stable type. Eventually, we'll delete the legacy code. --- radicle-httpd/src/api/error.rs | 13 +- radicle-httpd/src/api/json.rs | 6 +- radicle-httpd/src/api/v1/projects.rs | 214 ++++++++++++--------------- radicle-httpd/src/test.rs | 15 +- 4 files changed, 115 insertions(+), 133 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index f1763e266..6ecfacaf8 100644 --- a/radicle-httpd/src/api/error.rs +++ b/radicle-httpd/src/api/error.rs @@ -77,20 +77,29 @@ pub enum Error { /// Routing store error. #[error(transparent)] RoutingStore(#[from] radicle::node::routing::Error), + + /// Invalid update to issue or patch. + #[error("{0}")] + BadRequest(String), } impl IntoResponse for Error { fn into_response(self) -> Response { - let (status, msg) = match &self { + let message = self.to_string(); + let (status, msg) = match self { Error::NotFound => (StatusCode::NOT_FOUND, None), + Error::CobStore(radicle::cob::store::Error::NotFound(_, _)) => { + (StatusCode::NOT_FOUND, None) + } Error::Auth(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), Error::Crypto(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())), Error::Git2(e) => ( StatusCode::INTERNAL_SERVER_ERROR, Some(e.message().to_owned()), ), + Error::BadRequest(msg) => (StatusCode::BAD_REQUEST, Some(msg)), other => { - tracing::error!("Error: {:?}", &self); + tracing::error!("Error: {message}"); if cfg!(debug_assertions) { ( diff --git a/radicle-httpd/src/api/json.rs b/radicle-httpd/src/api/json.rs index d3b9183da..88507adfb 100644 --- a/radicle-httpd/src/api/json.rs +++ b/radicle-httpd/src/api/json.rs @@ -108,7 +108,7 @@ pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &impl AliasStore) -> Val .comments() .map(|(id, comment)| Comment::new(id, comment, aliases)) .collect::>(), - "tags": issue.tags().collect::>(), + "labels": issue.labels().collect::>(), }) } @@ -125,9 +125,9 @@ pub(crate) fn patch( "title": patch.title(), "state": patch.state(), "target": patch.target(), - "tags": patch.tags().collect::>(), + "labels": patch.labels().collect::>(), "merges": patch.merges().map(|(nid, m)| merge(m, nid, aliases.alias(nid))).collect::>(), - "reviewers": patch.reviewers().collect::>(), + "assignees": patch.assignees().collect::>(), "revisions": patch.revisions().map(|(id, rev)| { json!({ "id": id, diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index fd3d7c3ce..e2212ccd0 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -12,8 +12,8 @@ use serde::{Deserialize, Serialize}; use serde_json::json; use tower_http::set_header::SetResponseHeaderLayer; -use radicle::cob::{issue, patch, thread, ActorId, Tag}; -use radicle::identity::Id; +use radicle::cob::{issue, patch, Label}; +use radicle::identity::{Did, Id}; use radicle::node::routing::Store; use radicle::node::AliasStore; use radicle::node::NodeId; @@ -481,8 +481,8 @@ async fn issues_handler( pub struct IssueCreate { pub title: String, pub description: String, - pub tags: Vec, - pub assignees: Vec, + pub labels: Vec