feat(web/atward): Support depot paths (to cgit for now)

Sends depot paths (such as //web/atward or //nix/readTree/README.md)
to cgit. If Markdown files are detected the user is sent to the about
page to get the rendered view.

Future work will make cgit vs. SourceGraph configurable.

Change-Id: I48dea2dc8994644fb5a6f4bfbb846c771996cfc3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/3095
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
Vincent Ambo 2021-05-04 17:25:52 +02:00 committed by tazjin
parent 67389b6b0b
commit 90db61a6f4

View file

@ -22,6 +22,15 @@ struct Query {
target: for<'s> fn(&'s str, regex::Captures<'s>) -> Option<String>,
}
/// Create a URL to a file (and, optionally, specific line) in cgit.
fn cgit_url(path: &str) -> String {
if path.ends_with(".md") {
format!("https://code.tvl.fyi/about/{}", path)
} else {
format!("https://code.tvl.fyi/tree/{}", path)
}
}
/// Definition of all supported queries in atward.
fn queries() -> Vec<Query> {
vec![
@ -35,6 +44,12 @@ fn queries() -> Vec<Query> {
pattern: Regex::new("^cl/(?P<cl>\\d+)$").unwrap(),
target: |_, captures| Some(format!("https://cl.tvl.fyi/{}", &captures["cl"])),
},
// Depot paths (e.g. //web/atward or //ops/nixos/whitby/default.nix)
// TODO(tazjin): Add support for specifying lines in a query parameter
Query {
pattern: Regex::new("^//(?P<path>[a-zA-Z].*)$").unwrap(),
target: |_, captures| Some(cgit_url(&captures["path"])),
},
]
}
@ -104,4 +119,19 @@ mod tests {
);
assert_eq!(dispatch(&queries(), "cl/invalid"), None,);
}
#[test]
fn depot_path_query() {
assert_eq!(
dispatch(&queries(), "//web/atward/default.nix"),
Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()),
);
assert_eq!(
dispatch(&queries(), "//nix/readTree/README.md"),
Some("https://code.tvl.fyi/about/nix/readTree/README.md".to_string()),
);
assert_eq!(dispatch(&queries(), "/not/a/depot/path"), None);
}
}