about summary refs log tree commit diff
path: root/web/atward/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-08-26T18·54+0300
committertazjin <mail@tazj.in>2021-08-26T19·02+0000
commit72ebd3411bf79c16b5f3be99a3f6aacaf5fb9be5 (patch)
tree3435070e20b9ae11254d167ae1ffb6892407a4c4 /web/atward/src
parentda0b3307565bdec0d67014b0de8be71d90789762 (diff)
fix(atward): Redirect `//` queries to depot root r/2789
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 <sternenseemann@systemli.org>
Diffstat (limited to 'web/atward/src')
-rw-r--r--web/atward/src/main.rs26
1 files changed, 23 insertions, 3 deletions
diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs
index 38d0e6939d4e..94e9fff9bc71 100644
--- a/web/atward/src/main.rs
+++ b/web/atward/src/main.rs
@@ -113,12 +113,18 @@ fn handlers() -> Vec<Handler> {
         // 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<path>[a-zA-Z].*)$").unwrap(),
+            pattern: Regex::new("^//(?P<path>[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))
                 }
             },
         },
@@ -256,6 +262,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!(
             dispatch(&handlers(), &"cs".into()),