From 238a6b8a2ad997ce75b6635cc4d8c2becddbaf2e Mon Sep 17 00:00:00 2001 From: Vladislav Ivanov Date: Thu, 13 Jun 2024 19:31:19 +0200 Subject: [PATCH] Split josh-graphql and josh-templates out of josh-core This reduces number of dependencies of josh-core --- .dockerignore | 2 + Cargo.lock | 41 ++++++++++++++++--- Cargo.toml | 2 + josh-core/Cargo.toml | 10 ----- josh-core/src/filter/parse.rs | 2 +- josh-core/src/lib.rs | 17 ++------ josh-filter/Cargo.toml | 2 + josh-filter/src/bin/josh-filter.rs | 6 +-- josh-graphql/Cargo.toml | 22 ++++++++++ {josh-core => josh-graphql}/src/graphql.rs | 15 +++---- josh-graphql/src/lib.rs | 5 +++ josh-proxy/Cargo.toml | 2 + josh-proxy/src/bin/josh-proxy.rs | 6 +-- josh-templates/Cargo.toml | 13 ++++++ josh-templates/src/lib.rs | 2 + .../src/templates.rs | 32 ++++++++------- 16 files changed, 118 insertions(+), 61 deletions(-) create mode 100644 josh-graphql/Cargo.toml rename {josh-core => josh-graphql}/src/graphql.rs (99%) create mode 100644 josh-graphql/src/lib.rs create mode 100644 josh-templates/Cargo.toml create mode 100644 josh-templates/src/lib.rs rename josh-core/src/query.rs => josh-templates/src/templates.rs (91%) diff --git a/.dockerignore b/.dockerignore index 38a7a2e9c..c0fd65fa5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,6 +8,8 @@ !josh-proxy !josh-rpc !josh-ssh-shell +!josh-templates +!josh-graphql !josh-ui/*.json !josh-ui/*.rs !josh-ui/*.toml diff --git a/Cargo.lock b/Cargo.lock index f87197ada..72f1dfaab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1563,16 +1563,12 @@ version = "22.4.15" dependencies = [ "backtrace", "bitvec", - "chrono", - "form_urlencoded", "git-version", "git2", "glob", - "handlebars", "hex", "indoc", "itertools 0.13.0", - "juniper", "lazy_static", "log", "percent-encoding", @@ -1585,8 +1581,6 @@ dependencies = [ "serde_json", "serde_yaml", "sled", - "strfmt", - "toml", "tracing", ] @@ -1599,6 +1593,8 @@ dependencies = [ "env_logger", "git2", "josh", + "josh-graphql", + "josh-templates", "juniper", "rs_tracing", "serde", @@ -1606,6 +1602,24 @@ dependencies = [ "serde_yaml", ] +[[package]] +name = "josh-graphql" +version = "0.1.0" +dependencies = [ + "chrono", + "git2", + "josh", + "juniper", + "lazy_static", + "regex", + "rs_tracing", + "serde_json", + "serde_yaml", + "strfmt", + "toml", + "tracing", +] + [[package]] name = "josh-proxy" version = "22.4.15" @@ -1622,7 +1636,9 @@ dependencies = [ "hyper_cgi", "indoc", "josh", + "josh-graphql", "josh-rpc", + "josh-templates", "juniper", "lazy_static", "opentelemetry", @@ -1676,6 +1692,19 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "josh-templates" +version = "0.1.0" +dependencies = [ + "form_urlencoded", + "git2", + "handlebars", + "josh", + "josh-graphql", + "juniper", + "serde_json", +] + [[package]] name = "josh-ui" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ff4922f71..49a93b33d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,9 +4,11 @@ members = [ "hyper_cgi", "josh-core", "josh-filter", + "josh-graphql", "josh-proxy", "josh-rpc", "josh-ssh-shell", + "josh-templates", "josh-ui", ] diff --git a/josh-core/Cargo.toml b/josh-core/Cargo.toml index f61bb2e8a..805e6b954 100644 --- a/josh-core/Cargo.toml +++ b/josh-core/Cargo.toml @@ -15,7 +15,6 @@ bitvec = "1.0.1" git-version = "0.3.9" git2 = { workspace = true } glob = "0.3.1" -handlebars = "5.1.2" hex = "0.4.3" indoc = "2.0.5" itertools = "0.13.0" @@ -31,13 +30,4 @@ serde = { workspace = true } serde_json = { workspace = true } serde_yaml = { workspace = true } sled = "0.34.7" -strfmt = "0.2.4" -toml = { workspace = true } tracing = { workspace = true } -juniper = { workspace = true } -form_urlencoded = "1.2.1" - -[dependencies.chrono] -default-features = false -features = ["alloc", "std"] -version = "0.4.38" diff --git a/josh-core/src/filter/parse.rs b/josh-core/src/filter/parse.rs index 401ac9f40..24080172f 100644 --- a/josh-core/src/filter/parse.rs +++ b/josh-core/src/filter/parse.rs @@ -293,6 +293,6 @@ pub fn get_comments(filter_spec: &str) -> JoshResult { ))); } -#[derive(Parser)] +#[derive(pest_derive::Parser)] #[grammar = "filter/grammar.pest"] struct Grammar; diff --git a/josh-core/src/lib.rs b/josh-core/src/lib.rs index 4536465eb..e79bd504a 100644 --- a/josh-core/src/lib.rs +++ b/josh-core/src/lib.rs @@ -25,21 +25,10 @@ macro_rules! ok_or { #[macro_use] extern crate rs_tracing; -#[macro_use] -extern crate handlebars; - -#[macro_use] -extern crate pest_derive; - -#[macro_use] -extern crate serde_json; - pub mod cache; pub mod filter; -pub mod graphql; pub mod history; pub mod housekeeping; -pub mod query; pub mod shell; pub struct Change { @@ -190,7 +179,7 @@ macro_rules! regex_parsed { impl $name { fn from_str(path: &str) -> Option<$name> { -lazy_static! { +lazy_static::lazy_static! { static ref REGEX: regex::Regex = regex::Regex::new($re) .expect("can't compile regex"); @@ -368,7 +357,7 @@ type Users = std::collections::HashMap; #[derive(Debug, serde::Deserialize)] struct User { - pub groups: toml::value::Array, + pub groups: Vec, } type Groups = std::collections::HashMap>; @@ -400,7 +389,7 @@ pub fn get_acl( let mut blacklist = filter::empty(); for g in &u.groups { let lists = groups.get(repo).and_then(|repo| { - repo.get(g.as_str()?).map(|group| { + repo.get(g.as_str()).map(|group| { let w = filter::parse(&group.whitelist); let b = filter::parse(&group.blacklist); (w, b) diff --git a/josh-filter/Cargo.toml b/josh-filter/Cargo.toml index c7d2b606a..0dc46fbc8 100644 --- a/josh-filter/Cargo.toml +++ b/josh-filter/Cargo.toml @@ -11,6 +11,8 @@ version = "22.4.15" [dependencies] josh = { path = "../josh-core" } +josh-graphql = { path = "../josh-graphql" } +josh-templates = { path = "../josh-templates" } env_logger = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/josh-filter/src/bin/josh-filter.rs b/josh-filter/src/bin/josh-filter.rs index 69e76c551..e916a75b6 100644 --- a/josh-filter/src/bin/josh-filter.rs +++ b/josh-filter/src/bin/josh-filter.rs @@ -399,12 +399,12 @@ fn run_filter(args: Vec) -> josh::JoshResult { } if let Some(gql_query) = args.get_one::("graphql") { - let context = josh::graphql::context(transaction.try_clone()?, transaction.try_clone()?); + let context = josh_graphql::context(transaction.try_clone()?, transaction.try_clone()?); *context.allow_refs.lock()? = true; let (res, _errors) = juniper::execute_sync( gql_query, None, - &josh::graphql::repo_schema(".".to_string(), true), + &josh_graphql::repo_schema(".".to_string(), true), &std::collections::HashMap::new(), &context, )?; @@ -420,7 +420,7 @@ fn run_filter(args: Vec) -> josh::JoshResult { let commit_id = transaction.repo().refname_to_id(update_target)?; print!( "{}", - josh::query::render(&transaction, "", commit_id, query, false)? + josh_templates::render(&transaction, "", commit_id, query, false)? .map(|x| x.0) .unwrap_or("File not found".to_string()) ); diff --git a/josh-graphql/Cargo.toml b/josh-graphql/Cargo.toml new file mode 100644 index 000000000..0eb41c777 --- /dev/null +++ b/josh-graphql/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "josh-graphql" +version = "0.1.0" +edition = "2021" + +[dependencies] +josh = { path = "../josh-core" } +juniper = { workspace = true } +git2 = { workspace = true } +rs_tracing = { workspace = true } +regex = { workspace = true } +serde_json = { workspace = true } +serde_yaml = { workspace = true } +strfmt = "0.2.4" +toml = { workspace = true } +tracing = { workspace = true } +lazy_static = { workspace = true } + +[dependencies.chrono] +default-features = false +features = ["alloc", "std"] +version = "0.4.38" diff --git a/josh-core/src/graphql.rs b/josh-graphql/src/graphql.rs similarity index 99% rename from josh-core/src/graphql.rs rename to josh-graphql/src/graphql.rs index 669f43634..7ffd01dd2 100644 --- a/josh-core/src/graphql.rs +++ b/josh-graphql/src/graphql.rs @@ -1,6 +1,6 @@ #![allow(unused_variables)] -use super::*; +use josh::{cache, filter, history, josh_error, JoshResult}; use juniper::{graphql_object, EmptyMutation, EmptySubscription, FieldResult}; pub struct Revision { @@ -674,7 +674,7 @@ impl Path { fn dir(&self, relative: String) -> FieldResult { Ok(Path { - path: normalize_path(&self.path.join(relative)), + path: josh::normalize_path(&self.path.join(relative)), commit_id: self.commit_id, filter: self.filter, tree: self.tree, @@ -955,10 +955,7 @@ impl RepositoryMut { filter::nop() }; - Ok(RevMut { - at: at, - filter: filter, - }) + Ok(RevMut { at, filter }) } } @@ -983,7 +980,7 @@ impl Repository { pattern.unwrap_or_else(|| "refs/heads/*".to_string()) ); - log::debug!("refname: {:?}", refname); + tracing::debug!(refname = refname, "refs"); let mut refs = vec![]; @@ -1029,7 +1026,7 @@ impl Repository { } } -regex_parsed!( +josh::regex_parsed!( UpstreamRef, r"refs/josh/upstream/.*[.]git/(?Prefs/heads/.*)", [reference] @@ -1066,7 +1063,7 @@ pub fn repo_schema(name: String, local: bool) -> RepoSchema { let ns = if local { "".to_string() } else { - format!("refs/josh/upstream/{}.git/", to_ns(&name)) + format!("refs/josh/upstream/{}.git/", josh::to_ns(&name)) }; RepoSchema::new( Repository { name, ns }, diff --git a/josh-graphql/src/lib.rs b/josh-graphql/src/lib.rs new file mode 100644 index 000000000..1aeedf8f4 --- /dev/null +++ b/josh-graphql/src/lib.rs @@ -0,0 +1,5 @@ +pub mod graphql; +pub use graphql::{commit_schema, context, repo_schema}; + +#[macro_use] +extern crate rs_tracing; diff --git a/josh-proxy/Cargo.toml b/josh-proxy/Cargo.toml index d95734a43..9d94e6e92 100644 --- a/josh-proxy/Cargo.toml +++ b/josh-proxy/Cargo.toml @@ -20,6 +20,8 @@ hyper-tls = "0.5.0" hyper_cgi = { path = "../hyper_cgi" } indoc = "2.0.5" josh = {path = "../josh-core" } +josh-templates = { path = "../josh-templates" } +josh-graphql = { path = "../josh-graphql" } lazy_static = { workspace = true } opentelemetry = "0.23.0" opentelemetry-jaeger = "0.22.0" diff --git a/josh-proxy/src/bin/josh-proxy.rs b/josh-proxy/src/bin/josh-proxy.rs index ce602be06..b677bf006 100644 --- a/josh-proxy/src/bin/josh-proxy.rs +++ b/josh-proxy/src/bin/josh-proxy.rs @@ -1446,7 +1446,7 @@ async fn serve_query( let commit_id = josh::filter_commit(&transaction, filter, commit_id, josh::filter::empty())?; - josh::query::render(&transaction, "", commit_id, &q, true) + josh_templates::render(&transaction, "", commit_id, &q, true) }) .in_current_span() .await?; @@ -1807,7 +1807,7 @@ async fn serve_graphql( .to_string(), )?; - Ok(Arc::new(josh::graphql::context( + Ok(Arc::new(josh_graphql::graphql::context( transaction, transaction_mirror, ))) @@ -1815,7 +1815,7 @@ async fn serve_graphql( .await?? }; - let root_node = Arc::new(josh::graphql::repo_schema( + let root_node = Arc::new(josh_graphql::graphql::repo_schema( upstream_repo .strip_suffix(".git") .unwrap_or(&upstream_repo) diff --git a/josh-templates/Cargo.toml b/josh-templates/Cargo.toml new file mode 100644 index 000000000..5b8990d67 --- /dev/null +++ b/josh-templates/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "josh-templates" +version = "0.1.0" +edition = "2021" + +[dependencies] +git2 = { workspace = true } +josh = { path = "../josh-core" } +josh-graphql = { path = "../josh-graphql" } +juniper = { workspace = true } +serde_json = { workspace = true } +handlebars = "5.1.2" +form_urlencoded = "1.2.1" diff --git a/josh-templates/src/lib.rs b/josh-templates/src/lib.rs new file mode 100644 index 000000000..48ae1691d --- /dev/null +++ b/josh-templates/src/lib.rs @@ -0,0 +1,2 @@ +pub mod templates; +pub use templates::render; diff --git a/josh-core/src/query.rs b/josh-templates/src/templates.rs similarity index 91% rename from josh-core/src/query.rs rename to josh-templates/src/templates.rs index e8c79d11d..e5453a68a 100644 --- a/josh-core/src/query.rs +++ b/josh-templates/src/templates.rs @@ -1,4 +1,5 @@ -use super::*; +use josh::{cache, josh_error, JoshResult}; +use serde_json::json; struct GraphQLHelper { repo_path: std::path::PathBuf, @@ -21,7 +22,7 @@ impl GraphQLHelper { let path = std::path::PathBuf::from(template_name) .join("..") .join(path); - let path = normalize_path(&path); + let path = josh::normalize_path(&path); let transaction = if let Ok(to) = cache::Transaction::open(&self.repo_path.join("mirror"), Some(&self.ref_prefix)) @@ -77,9 +78,9 @@ impl GraphQLHelper { let (res, _errors) = juniper::execute_sync( &query, None, - &graphql::commit_schema(self.commit_id), + &josh_graphql::graphql::commit_schema(self.commit_id), &variables, - &graphql::context(transaction, transaction_mirror), + &josh_graphql::context(transaction, transaction_mirror), )?; let j = serde_json::to_string(&res)?; @@ -114,7 +115,7 @@ impl handlebars::HelperDef for GraphQLHelper { } mod helpers { - handlebars_helper!(concat_helper: |x: str, y: str| format!("{}{}", x, y) ); + handlebars::handlebars_helper!(concat_helper: |x: str, y: str| format!("{}{}", x, y) ); } pub fn render( @@ -138,14 +139,15 @@ pub fn render( }; let tree = transaction.repo().find_commit(commit_id)?.tree()?; + let obj = tree + .get_path(&std::path::PathBuf::from(path))? + .to_object(transaction.repo()); - let obj = ok_or!( - tree.get_path(&std::path::PathBuf::from(path))? - .to_object(transaction.repo()), - { - return Ok(None); - } - ); + let obj = if let Ok(obj) = obj { + obj + } else { + return Ok(None); + }; let template = if let Ok(blob) = obj.peel_to_blob() { let file = std::str::from_utf8(blob.content())?; @@ -191,9 +193,9 @@ pub fn render( let (res, _errors) = juniper::execute_sync( file, None, - &graphql::commit_schema(commit_id), + &josh_graphql::commit_schema(commit_id), &variables, - &graphql::context(transaction, transaction_mirror), + &josh_graphql::context(transaction, transaction_mirror), )?; let j = serde_json::to_string_pretty(&res)?; @@ -228,7 +230,7 @@ pub fn render( handlebars.register_helper( "graphql", Box::new(GraphQLHelper { - repo_path: repo_path, + repo_path, ref_prefix: ref_prefix.to_owned(), commit_id, }),