diff options
author | Vincent Ambo <mail@tazj.in> | 2021-05-04T15·25+0200 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-05-06T13·39+0000 |
commit | 90db61a6f4a62d29afd4e8a04112ad9021a6919c (patch) | |
tree | 72ab51734547cabfb7aff9793681dcda1f3891d0 | |
parent | 67389b6b0b70a5eab821b6c7c9eb23072858f960 (diff) |
feat(web/atward): Support depot paths (to cgit for now) r/2570
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 <mail@tazj.in>
-rw-r--r-- | web/atward/src/main.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index aef41a29b4e7..30b672129b01 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<String>, } +/// 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<Query> { vec![ @@ -35,6 +44,12 @@ fn queries() -> Vec<Query> { pattern: Regex::new("^cl/(?P<cl>\\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<path>[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); + } } |