From 72ebd3411bf79c16b5f3be99a3f6aacaf5fb9be5 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 26 Aug 2021 21:54:03 +0300 Subject: fix(atward): Redirect `//` queries to depot root Makes it possible to open the default code viewer for the user at the depot root by searching for `//`. Fixes b/134. Change-Id: I409ad36cea28de27cd1789a84eda71f8979d3133 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3437 Tested-by: BuildkiteCI Reviewed-by: sterni --- web/atward/src/main.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'web/atward') diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index 38d0e6939d..94e9fff9bc 100644 --- a/web/atward/src/main.rs +++ b/web/atward/src/main.rs @@ -113,12 +113,18 @@ fn handlers() -> Vec { // Depot paths (e.g. //web/atward or //ops/nixos/whitby/default.nix) // TODO(tazjin): Add support for specifying lines in a query parameter Handler { - pattern: Regex::new("^//(?P[a-zA-Z].*)$").unwrap(), + pattern: Regex::new("^//(?P[a-zA-Z].*)?$").unwrap(), target: |query, captures| { + // Pass an empty string if the path is missing, to + // redirect to the depot root. + let path = captures.name("path") + .map(|m| m.as_str()) + .unwrap_or(""); + if query.cs { - Some(sourcegraph_path_url(&captures["path"])) + Some(sourcegraph_path_url(path)) } else { - Some(cgit_url(&captures["path"])) + Some(cgit_url(path)) } }, }, @@ -255,6 +261,20 @@ mod tests { ); } + #[test] + fn depot_root_cgit_query() { + assert_eq!( + dispatch( + &handlers(), + &Query { + query: "//".to_string(), + cs: false, + } + ), + Some("https://code.tvl.fyi/tree/".to_string()), + ); + } + #[test] fn plain_host_queries() { assert_eq!( -- cgit 1.4.1