From 13336c60570bef5e6a303a5c1fb76595dda07da8 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 6 May 2021 16:36:50 +0200 Subject: feat(atward): Add 'cs' query parameter to toggle Sourcegraph support 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 --- web/atward/src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'web/atward') diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index 400e773416..49e23dde7b 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 { vec![ @@ -74,7 +88,13 @@ fn handlers() -> Vec { // TODO(tazjin): Add support for specifying lines in a query parameter Handler { pattern: Regex::new("^//(?P[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 + ); + } } -- cgit 1.4.1