about summary refs log tree commit diff
path: root/web
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-05-06T14·36+0200
committertazjin <mail@tazj.in>2021-05-11T14·20+0000
commit13336c60570bef5e6a303a5c1fb76595dda07da8 (patch)
tree95bc70e91bd566b81939d02f2061a6e1c77a20af /web
parentd4bdfe8127f9a54bb9a15b6c278267bb725e3457 (diff)
feat(atward): Add 'cs' query parameter to toggle Sourcegraph support r/2576
Users can set `?cs=true` to be sent to cs.tvl.fyi instead of
code.tvl.fyi for things that look like code paths.

Change-Id: I7c8f9b71cde25d35787c941e5308330c6f16f8d7
Reviewed-on: https://cl.tvl.fyi/c/depot/+/3102
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'web')
-rw-r--r--web/atward/src/main.rs51
1 files changed, 48 insertions, 3 deletions
diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs
index 400e77341650..49e23dde7bc9 100644
--- a/web/atward/src/main.rs
+++ b/web/atward/src/main.rs
@@ -26,6 +26,9 @@ struct Handler {
 struct Query {
     /// Query string itself.
     query: String,
+
+    /// Should Sourcegraph be used instead of cgit?
+    cs: bool,
 }
 
 impl Query {
@@ -35,7 +38,12 @@ impl Query {
             None => return None,
         };
 
-        Some(Query { query })
+        let cs = match req.get_param("cs") {
+            Some(s) if s == "true" => true,
+            _ => false,
+        };
+
+        Some(Query { query, cs })
     }
 }
 
@@ -44,6 +52,7 @@ impl From<&str> for Query {
     fn from(query: &str) -> Query {
         Query {
             query: query.to_string(),
+            cs: false,
         }
     }
 }
@@ -57,6 +66,11 @@ fn cgit_url(path: &str) -> String {
     }
 }
 
+/// Create a URL to a path in Sourcegraph.
+fn sourcegraph_path_url(path: &str) -> String {
+    format!("https://cs.tvl.fyi/depot/-/tree/{}", path)
+}
+
 /// Definition of all supported query handlers in atward.
 fn handlers() -> Vec<Handler> {
     vec![
@@ -74,7 +88,13 @@ fn handlers() -> Vec<Handler> {
         // TODO(tazjin): Add support for specifying lines in a query parameter
         Handler {
             pattern: Regex::new("^//(?P<path>[a-zA-Z].*)$").unwrap(),
-            target: |_, captures| Some(cgit_url(&captures["path"])),
+            target: |query, captures| {
+                if query.cs {
+                    Some(sourcegraph_path_url(&captures["path"]))
+                } else {
+                    Some(cgit_url(&captures["path"]))
+                }
+            },
         },
     ]
 }
@@ -151,7 +171,7 @@ mod tests {
     }
 
     #[test]
-    fn depot_path_query() {
+    fn depot_path_cgit_query() {
         assert_eq!(
             dispatch(&handlers(), &"//web/atward/default.nix".into()),
             Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()),
@@ -164,4 +184,29 @@ mod tests {
 
         assert_eq!(dispatch(&handlers(), &"/not/a/depot/path".into()), None);
     }
+
+    #[test]
+    fn depot_path_sourcegraph_query() {
+        assert_eq!(
+            dispatch(
+                &handlers(),
+                &Query {
+                    query: "//web/atward/default.nix".to_string(),
+                    cs: true,
+                }
+            ),
+            Some("https://cs.tvl.fyi/depot/-/tree/web/atward/default.nix".to_string()),
+        );
+
+        assert_eq!(
+            dispatch(
+                &handlers(),
+                &Query {
+                    query: "/not/a/depot/path".to_string(),
+                    cs: true,
+                }
+            ),
+            None
+        );
+    }
 }