From 10e279ac13f3fe98cfd301dc38d54507740f9b86 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 5 Nov 2021 12:29:08 +0100 Subject: feat(atward): Support depot revision queries (r/...) Redirects these to the cgit commit view. Only supports cgit because we don't have a good way to coax Sourcegraph into fetching these refs. Change-Id: I8c28ed015ba37c04eb4b7a667bde70ff6a92bf4c Reviewed-on: https://cl.tvl.fyi/c/depot/+/3772 Tested-by: BuildkiteCI Reviewed-by: sterni --- web/atward/src/main.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'web/atward') diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index 94e9fff9bc..26d79cde1a 100644 --- a/web/atward/src/main.rs +++ b/web/atward/src/main.rs @@ -91,7 +91,6 @@ fn cgit_url(path: &str) -> String { 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![ @@ -110,6 +109,16 @@ fn handlers() -> Vec { pattern: Regex::new("^(?Pb|cl|cs|code|at|todo)$").unwrap(), target: |_, captures| Some(format!("https://{}.tvl.fyi/", &captures["host"])), }, + // Depot revisions (e.g. r/3002) + Handler { + pattern: Regex::new("^r/(?P\\d+)$").unwrap(), + target: |_, captures| { + Some(format!( + "https://code.tvl.fyi/commit/?id=refs/r/{}", + &captures["rev"] + )) + }, + }, // Depot paths (e.g. //web/atward or //ops/nixos/whitby/default.nix) // TODO(tazjin): Add support for specifying lines in a query parameter Handler { @@ -117,9 +126,7 @@ fn handlers() -> Vec { 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(""); + let path = captures.name("path").map(|m| m.as_str()).unwrap_or(""); if query.cs { Some(sourcegraph_path_url(path)) @@ -363,4 +370,19 @@ mod tests { }, ); } + + #[test] + fn depot_revision_query() { + assert_eq!( + dispatch(&handlers(), &"r/3002".into()), + Some("https://code.tvl.fyi/commit/?id=refs/r/3002".to_string()) + ); + + assert_eq!( + dispatch(&handlers(), &"something only mentioning r/3002".into()), + None, + ); + + assert_eq!(dispatch(&handlers(), &"r/invalid".into()), None,); + } } -- cgit 1.4.1