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 <mail@tazj.in>
This commit is contained in:
Vincent Ambo 2021-05-06 16:36:50 +02:00 committed by tazjin
parent d4bdfe8127
commit 13336c6057

View file

@ -26,6 +26,9 @@ struct Handler {
struct Query { struct Query {
/// Query string itself. /// Query string itself.
query: String, query: String,
/// Should Sourcegraph be used instead of cgit?
cs: bool,
} }
impl Query { impl Query {
@ -35,7 +38,12 @@ impl Query {
None => return None, 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 { fn from(query: &str) -> Query {
Query { Query {
query: query.to_string(), 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. /// Definition of all supported query handlers in atward.
fn handlers() -> Vec<Handler> { fn handlers() -> Vec<Handler> {
vec![ vec![
@ -74,7 +88,13 @@ fn handlers() -> Vec<Handler> {
// TODO(tazjin): Add support for specifying lines in a query parameter // TODO(tazjin): Add support for specifying lines in a query parameter
Handler { Handler {
pattern: Regex::new("^//(?P<path>[a-zA-Z].*)$").unwrap(), 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] #[test]
fn depot_path_query() { fn depot_path_cgit_query() {
assert_eq!( assert_eq!(
dispatch(&handlers(), &"//web/atward/default.nix".into()), dispatch(&handlers(), &"//web/atward/default.nix".into()),
Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()), 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); 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
);
}
} }