From 90db61a6f4a62d29afd4e8a04112ad9021a6919c Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 4 May 2021 17:25:52 +0200 Subject: feat(web/atward): Support depot paths (to cgit for now) Sends depot paths (such as //web/atward or //nix/readTree/README.md) to cgit. If Markdown files are detected the user is sent to the about page to get the rendered view. Future work will make cgit vs. SourceGraph configurable. Change-Id: I48dea2dc8994644fb5a6f4bfbb846c771996cfc3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3095 Tested-by: BuildkiteCI Reviewed-by: tazjin --- web/atward/src/main.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'web/atward/src') diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index aef41a29b4..30b672129b 100644 --- a/web/atward/src/main.rs +++ b/web/atward/src/main.rs @@ -22,6 +22,15 @@ struct Query { target: for<'s> fn(&'s str, regex::Captures<'s>) -> Option, } +/// Create a URL to a file (and, optionally, specific line) in cgit. +fn cgit_url(path: &str) -> String { + if path.ends_with(".md") { + format!("https://code.tvl.fyi/about/{}", path) + } else { + format!("https://code.tvl.fyi/tree/{}", path) + } +} + /// Definition of all supported queries in atward. fn queries() -> Vec { vec![ @@ -35,6 +44,12 @@ fn queries() -> Vec { pattern: Regex::new("^cl/(?P\\d+)$").unwrap(), target: |_, captures| Some(format!("https://cl.tvl.fyi/{}", &captures["cl"])), }, + // Depot paths (e.g. //web/atward or //ops/nixos/whitby/default.nix) + // TODO(tazjin): Add support for specifying lines in a query parameter + Query { + pattern: Regex::new("^//(?P[a-zA-Z].*)$").unwrap(), + target: |_, captures| Some(cgit_url(&captures["path"])), + }, ] } @@ -104,4 +119,19 @@ mod tests { ); assert_eq!(dispatch(&queries(), "cl/invalid"), None,); } + + #[test] + fn depot_path_query() { + assert_eq!( + dispatch(&queries(), "//web/atward/default.nix"), + Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()), + ); + + assert_eq!( + dispatch(&queries(), "//nix/readTree/README.md"), + Some("https://code.tvl.fyi/about/nix/readTree/README.md".to_string()), + ); + + assert_eq!(dispatch(&queries(), "/not/a/depot/path"), None); + } } -- cgit 1.4.1